From d49e8bb10aa25807274acf535c40dfd310d94868 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Feb 2018 20:19:03 -0800 Subject: [PATCH] Used ItemsHoldr with item generics (#609) * Used ItemsHoldr with item generics * itemsholdr@0.7.6 --- package.json | 3 +- src/FullScreenPokemon.ts | 4 +-- src/components/Inputs.ts | 4 +-- src/components/Saves.ts | 32 +++++++++-------- src/components/Storage.ts | 39 ++++++++++++++++++++- src/components/battles/animations/Ending.ts | 3 +- src/components/menus/Pause.ts | 2 +- src/components/storage/ItemNames.ts | 2 +- src/creators/createMenuGrapher.ts | 14 ++++---- src/creators/createModAttacher.ts | 3 +- src/creators/createStateHolder.ts | 3 +- 11 files changed, 75 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index bed7a7e4..56d40e43 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "changelinr": "^0.7.2", "flagswappr": "^0.7.2", "gamestartr": "^0.7.13", + "itemsholdr": "^0.7.6", "menugraphr": "^0.7.4", "stateholdr": "^0.7.3", "stringfilr": "^0.7.2", @@ -144,4 +145,4 @@ }, "types": "./src/index.d.ts", "version": "0.7.1" -} \ No newline at end of file +} diff --git a/src/FullScreenPokemon.ts b/src/FullScreenPokemon.ts index 1c6bfdf4..73691125 100644 --- a/src/FullScreenPokemon.ts +++ b/src/FullScreenPokemon.ts @@ -43,7 +43,7 @@ import { Mods } from "./components/Mods"; import { Physics } from "./components/Physics"; import { Saves } from "./components/Saves"; import { Scrolling } from "./components/Scrolling"; -import { Storage } from "./components/Storage"; +import { IStorageItems, Storage } from "./components/Storage"; import { IPlayer, IThing, Things } from "./components/Things"; import { Utilities } from "./components/Utilities"; import { createAreaSpawner } from "./creators/createAreaSpawner"; @@ -152,7 +152,7 @@ export class FullScreenPokemon extends GameStartr { * Cache-based wrapper around localStorage. */ @component(createItemsHolder) - public readonly itemsHolder: ItemsHoldr; + public readonly itemsHolder: ItemsHoldr; /** * Storage container and lazy loader for GameStartr maps. diff --git a/src/components/Inputs.ts b/src/components/Inputs.ts index 5f43e2c7..17c09b70 100644 --- a/src/components/Inputs.ts +++ b/src/components/Inputs.ts @@ -275,12 +275,12 @@ export class Inputs extends GeneralCompon this.gameStarter.modAttacher.fireEvent(this.gameStarter.mods.eventNames.onKeyDownSelect); - const selectItem: string = this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.selectItem); + const selectItem = this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.selectItem); if (!selectItem) { return; } - const itemSchema: IItemSchema = this.gameStarter.constants.items.byName[selectItem]; + const itemSchema: IItemSchema = this.gameStarter.constants.items.byName[selectItem.join("")]; if (!itemSchema.bagActivate) { throw new Error("Currently selected item does not have a .bagActivate."); } diff --git a/src/components/Saves.ts b/src/components/Saves.ts index 74fad2a8..0a580acb 100644 --- a/src/components/Saves.ts +++ b/src/components/Saves.ts @@ -1,9 +1,9 @@ import { GeneralComponent } from "gamestartr"; -import { IExportedItems } from "itemsholdr"; import { FullScreenPokemon } from "../FullScreenPokemon"; import { PokedexListingStatus } from "./Constants"; import { IPokedex, IPokedexInformation, IPokemonListing } from "./constants/Pokemon"; +import { IStorageItems } from "./Storage"; import { ICharacter } from "./Things"; /** @@ -39,17 +39,17 @@ export class Saves extends GeneralCompone * upon a new game being started. */ public clearSavedData(): void { - const oldLocalStorage: IExportedItems = this.gameStarter.itemsHolder.exportItems(); + const oldLocalStorage: IStorageItems & { [i: string]: any } = this.gameStarter.itemsHolder.exportItems(); const collectionKeys: string[] = this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.stateCollectionKeys); if (collectionKeys) { for (const collection of collectionKeys) { - oldLocalStorage[collection] = this.gameStarter.itemsHolder.getItem(collection); + oldLocalStorage[collection] = this.gameStarter.itemsHolder.getItem(collection as keyof IStorageItems); } } for (const key of Object.keys(oldLocalStorage)) { - this.gameStarter.itemsHolder.removeItem(key); + this.gameStarter.itemsHolder.removeItem(key as keyof IStorageItems); } this.gameStarter.itemsHolder.clear(); @@ -68,18 +68,20 @@ export class Saves extends GeneralCompone return; } - const oldLocalStorage: IExportedItems = this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.oldLocalStorage); - for (const key in oldLocalStorage) { - if (!oldLocalStorage.hasOwnProperty(key)) { - continue; - } + const oldLocalStorage = this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.oldLocalStorage); + if (oldLocalStorage !== undefined) { + for (const key in oldLocalStorage) { + if (!oldLocalStorage.hasOwnProperty(key)) { + continue; + } - const prefix = this.gameStarter.stateHolder.getPrefix(); + const prefix = this.gameStarter.stateHolder.getPrefix(); - if (key.slice(0, prefix.length) === prefix) { - this.gameStarter.stateHolder.setCollection(key.slice(prefix.length), oldLocalStorage[key]); - } else { - this.gameStarter.itemsHolder.setItem(key, oldLocalStorage[key]); + if (key.slice(0, prefix.length) === prefix) { + this.gameStarter.stateHolder.setCollection(key.slice(prefix.length), oldLocalStorage[key as keyof IStorageItems]); + } else { + this.gameStarter.itemsHolder.setItem(key as keyof IStorageItems, oldLocalStorage[key as keyof IStorageItems]); + } } } @@ -173,7 +175,7 @@ export class Saves extends GeneralCompone const split: string[] = key.split("::"); this.gameStarter.stateHolder.setCollection(split[1] + "::" + split[2], data[key]); } else { - this.gameStarter.itemsHolder.setItem(key, data[key]); + this.gameStarter.itemsHolder.setItem(key as keyof IStorageItems, data[key]); } } diff --git a/src/components/Storage.ts b/src/components/Storage.ts index 710cdafc..30526ae5 100644 --- a/src/components/Storage.ts +++ b/src/components/Storage.ts @@ -1,9 +1,46 @@ +import { component } from "babyioc"; import { GeneralComponent } from "gamestartr"; -import { component } from "babyioc"; import { FullScreenPokemon } from "../FullScreenPokemon"; +import { IPokemon } from "./Battles"; +import { IPokedex, IPokedexListing } from "./constants/Pokemon"; +import { IInventoryListing } from "./menus/Items"; import { ItemNames } from "./storage/ItemNames"; +export interface ILastPokecenter { + location: string | undefined; + map: string; +} + +/** + * Items to be held in storage. + */ +export interface IStorageItems { + area: string; + autoSave: boolean; + badges: { + [i: string]: boolean; + }; + gameStarted: boolean; + hasPokedex: boolean; + items: IInventoryListing[]; + lastPokecenter: ILastPokecenter; + location: string | undefined; + map: string; + money: number; + name: string[]; + nameRival: string[]; + oldLocalStorage?: IStorageItems; + pokedex: IPokedex; + pokemonInParty: IPokemon[]; + pokemonInPC: IPokemon[]; + selectItem: string[] | undefined; + stateCollectionKeys: string[]; + starter: string[]; + starterRival: string[]; + time: number; +} + /** * Settings for storing items in ItemsHoldrs. */ diff --git a/src/components/battles/animations/Ending.ts b/src/components/battles/animations/Ending.ts index 0a171d99..66cd454d 100644 --- a/src/components/battles/animations/Ending.ts +++ b/src/components/battles/animations/Ending.ts @@ -56,8 +56,9 @@ export class Ending extends GeneralCompon queue.run((): void => this.finalize(battleInfo, outcome, onBattleComplete)); if (this.gameStarter.battles.isPartyWiped()) { + const { map, location } = this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.lastPokecenter); this.gameStarter.battles.healParty(); - this.gameStarter.maps.setMap(this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.lastPokecenter)); + this.gameStarter.maps.setMap(map, location); this.gameStarter.mapScreener.blockInputs = false; } } diff --git a/src/components/menus/Pause.ts b/src/components/menus/Pause.ts index 01a7a96d..0520388a 100644 --- a/src/components/menus/Pause.ts +++ b/src/components/menus/Pause.ts @@ -43,7 +43,7 @@ export class Pause extends GeneralCompone }, ]; - if (this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.hasPokedex) === true) { + if (this.gameStarter.itemsHolder.getItem(this.gameStarter.storage.names.hasPokedex)) { options.unshift({ text: "%%%%%%%POKEDEX%%%%%%%", callback: this.open, diff --git a/src/components/storage/ItemNames.ts b/src/components/storage/ItemNames.ts index 90c41e2d..c8a36df5 100644 --- a/src/components/storage/ItemNames.ts +++ b/src/components/storage/ItemNames.ts @@ -84,7 +84,7 @@ export class ItemNames extends GeneralCom /** * Pokemon in the player's PC storage. */ - public readonly pokemonInPC = "pokemonInPc"; + public readonly pokemonInPC = "pokemonInPC"; /** * Item name the player has listed under select. diff --git a/src/creators/createMenuGrapher.ts b/src/creators/createMenuGrapher.ts index 036fef37..41dabd56 100644 --- a/src/creators/createMenuGrapher.ts +++ b/src/creators/createMenuGrapher.ts @@ -3,7 +3,7 @@ import { MenuGraphr, } from "menugraphr"; -import { IPokedexInformation, IPokedexListing } from "../components/constants/Pokemon"; +import { IPokedex, IPokedexInformation, IPokedexListing } from "../components/constants/Pokemon"; import { IDialog, IMenuBase, IMenuSchema } from "../components/Menus"; import { FullScreenPokemon } from "../FullScreenPokemon"; @@ -66,16 +66,14 @@ export const createMenuGrapher = (fsp: FullScreenPokemon): MenuGraphr => return total.toString().split(""); }, "POKEDEX.LENGTH": (): string[] => { - const pokedex: IPokedexListing[] = fsp.itemsHolder.getItem(fsp.storage.names.pokedex); - if (!pokedex || !pokedex.length) { + const pokedex: IPokedex = fsp.itemsHolder.getItem(fsp.storage.names.pokedex); + if (!pokedex) { return ["0"]; } - return pokedex - .map((listing: IPokedexListing): number => - Number(listing.seen)) - .reduce((a: number, b: number): number => - a + b) + return Object.keys(pokedex) + .map((listing): number => Number(pokedex[listing].seen)) + .reduce((a: number, b: number): number => a + b) .toString() .split(""); }, diff --git a/src/creators/createModAttacher.ts b/src/creators/createModAttacher.ts index 818da640..571367e8 100644 --- a/src/creators/createModAttacher.ts +++ b/src/creators/createModAttacher.ts @@ -1,10 +1,11 @@ +import { IItemsHoldr } from "itemsholdr"; import { ModAttachr } from "modattachr"; import { FullScreenPokemon } from "../FullScreenPokemon"; export const createModAttacher = (fsp: FullScreenPokemon): ModAttachr => new ModAttachr({ - itemsHolder: fsp.itemsHolder, + itemsHolder: fsp.itemsHolder as IItemsHoldr, transformModName: (name: string): string => `Mods::${name}`, mods: fsp.mods.mods, }); diff --git a/src/creators/createStateHolder.ts b/src/creators/createStateHolder.ts index 11234afd..9614f10b 100644 --- a/src/creators/createStateHolder.ts +++ b/src/creators/createStateHolder.ts @@ -1,9 +1,10 @@ +import { IItemsHoldr } from "itemsholdr"; import { StateHoldr } from "stateholdr"; import { FullScreenPokemon } from "../FullScreenPokemon"; export const createStateHolder = (fsp: FullScreenPokemon): StateHoldr => new StateHoldr({ - itemsHolder: fsp.itemsHolder, + itemsHolder: fsp.itemsHolder as IItemsHoldr, prefix: "StateHolder::", });