From 64f1ea0be65f4643f9535154aace1833803dd06c Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 16 Nov 2021 02:16:37 +0100 Subject: [PATCH] added fromObject method --- src/components/App.tsx | 4 ++-- src/types/Card.ts | 9 +++++++++ src/types/CardGroup.ts | 7 +++++++ src/types/Deck.ts | 7 +++++++ src/types/RetakesConfig.ts | 20 ++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 6d8de3c..df8a460 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -15,8 +15,8 @@ const App: React.FC = () => { const retakesJSON = window.localStorage.getItem("retakesJSON") if (retakesJSON){ try{ - const parsedConfig: RetakesConfig = JSON.parse(retakesJSON) - setRetakesConfig(parsedConfig) + const parsedConfig: Record = JSON.parse(retakesJSON) + setRetakesConfig(RetakesConfig.fromObject(parsedConfig)) }catch(err){ window.localStorage.removeItem("retakesJSON") } diff --git a/src/types/Card.ts b/src/types/Card.ts index 07aac5e..fb539ce 100644 --- a/src/types/Card.ts +++ b/src/types/Card.ts @@ -16,6 +16,15 @@ class Card { public toCvar(): string { return `${this.title},${bToS(this.armor)},${bToS(this.helmet)},${this.items.join(",")}` } + + static fromObject(input: Record): Card { + return new Card( + input.title as string, + input.armor as boolean, + input.helmet as boolean, + ...input.items as Item[] + ) + } } /** diff --git a/src/types/CardGroup.ts b/src/types/CardGroup.ts index 6fc80f4..e71bc45 100644 --- a/src/types/CardGroup.ts +++ b/src/types/CardGroup.ts @@ -12,6 +12,13 @@ class CardGroup { public toCvar(): string { return `${this.numInDeck};${this.cards.map(e => e.toCvar()).join(";")}` } + + static fromObject(input: Record): CardGroup{ + return new CardGroup( + input.numInDeck as number, + ...(input.cards as Record[]).map((e)=>Card.fromObject(e)) + ) + } } export default CardGroup diff --git a/src/types/Deck.ts b/src/types/Deck.ts index 538b967..e9e7960 100644 --- a/src/types/Deck.ts +++ b/src/types/Deck.ts @@ -12,6 +12,13 @@ class Deck { public toCvar(): string { return `${this.numDefusers}|${this.cardGroups.map(e => e.toCvar()).join("|")}` } + + static fromObject(input: Record): Deck{ + return new Deck( + input.numDefusers as number, + ...(input.cardGroups as Record[]).map((e)=>CardGroup.fromObject(e)) + ) + } } export default Deck diff --git a/src/types/RetakesConfig.ts b/src/types/RetakesConfig.ts index 9b3a929..a08f29d 100644 --- a/src/types/RetakesConfig.ts +++ b/src/types/RetakesConfig.ts @@ -60,6 +60,7 @@ class RetakesConfig { } public toCvar(): string{ + console.debug("THIS:") return `mp_retake_ct_loadout_default_pistol_round "${this.ctPistol.toCvar()}" mp_retake_t_loadout_default_pistol_round "${this.tPistol.toCvar()}" mp_retake_ct_loadout_upgraded_pistol_round "${this.ctUpgradedPistol.toCvar()}" @@ -75,6 +76,25 @@ mp_retake_t_loadout_bonus_card_availability "${this.tBonusAvailability.join(",") ` } + static fromObject(input: Record): RetakesConfig { + return new RetakesConfig({ + ctPistol: Deck.fromObject(input.ctPistol as Record), + tPistol: Deck.fromObject(input.tPistol as Record), + ctUpgradedPistol: Deck.fromObject(input.ctUpgradedPistol as Record), + tUpgradedPistol: Deck.fromObject(input.tUpgradedPistol as Record), + ctLight: Deck.fromObject(input.ctLight as Record), + tLight: Deck.fromObject(input.tLight as Record), + ctFull: Deck.fromObject(input.ctFull as Record), + tFull: Deck.fromObject(input.tFull as Record), + ctEnemy: Card.fromObject(input.ctEnemy as Record), + tEnemy: Card.fromObject(input.tEnemy as Record), + ctBonus: Card.fromObject(input.ctBonus as Record), + tBonus: Card.fromObject(input.tBonus as Record), + ctBonusAvailability: input.ctBonusAvailability as number[], + tBonusAvailability: input.tBonusAvailability as number[], + }) + } + } export default RetakesConfig