import { Boon } from "./boon" ; import { Card } from "./card" ; import { DominionSet } from "./dominion-set" ; import { Event } from "./event" ; import { Landmark } from "./landmark" ; import { Project } from "./project" ; import { SetId } from "./set-id" ; import { SupplyCard } from "./supply-card" ; import { Way } from "./way" ; declare global { interface Window { DominionSets : any ; } } export class DominionSets { static readonly sets : { [ key in SetId ] ? : DominionSet ; } = DominionSets . createSets ( ) ; static readonly cards : { [ index ] : Card ; } = DominionSets . createCardMap ( ) ; public static convertToSetId ( stringSetId ) { const setId = DominionSets . convertToSetIdSafe ( stringSetId ) ; if ( ! setId ) { throw new Error ( `Unknown set ID: ${ setId }` ) ; } return setId ; } public static convertToSetIdSafe ( stringSetId ) { const setIds = Object . keys ( SetId ) ; for ( let setId of setIds ) { if ( SetId [ setId as keyof typeof SetId ] == stringSetId ) { return stringSetId as SetId ; } } return null ; } public static getAllSets ( ) { const sets = [ ] ; const setIds = Object . keys ( DominionSets . sets ) ; for ( let setId of setIds ) { sets . push ( DominionSets . sets [ setId as SetId ] as DominionSet ) ; } return sets ; } public static getAllCards ( ) { const cards = [ ] ; const cardIds = Object . keys ( DominionSets . cards ) ; for ( let cardId of cardIds ) { cards . push ( DominionSets . cards [ cardId ] ) ; } return cards ; } public static getSetById ( setId ) { return DominionSets . sets [ setId ] as DominionSet ; } public static getCardById ( cardId ) { const card = DominionSets . cards [ cardId ] ; if ( ! card ) { throw new Error ( `Unknown card id: ${ cardId }` ) ; } return card ; } public static getSupplyCardById ( cardId ) { const card = DominionSets . getCardById ( cardId ) ; if ( ! ( card instanceof SupplyCard ) ) { throw new Error ( `Card id (${ cardId }) does not refer to a supply card` ) ; } return card ; } public static getEventById ( cardId ) { const card = DominionSets . getCardById ( cardId ) ; if ( ! ( card instanceof Event ) ) { throw new Error ( `Card id (${ cardId }) does not refer to an event` ) ; } return card ; } public static getLandmarkById ( cardId ) { const card = DominionSets . getCardById ( cardId ) ; if ( ! ( card instanceof Landmark ) ) { throw new Error ( `Card id (${ cardId }) does not refer to a landmark` ) ; } return card ; } public static getProjectById ( cardId ) { const card = DominionSets . getCardById ( cardId ) ; if ( ! ( card instanceof Project ) ) { throw new Error ( `Card id (${ cardId }) does not refer to a project` ) ; } return card ; } public static getBoonById ( cardId ) { const card = DominionSets . getCardById ( cardId ) ; if ( ! ( card instanceof Boon ) ) { throw new Error ( `Card id (${ cardId }) does not refer to a boon` ) ; } return card ; } public static getWayById ( cardId ) { const card = DominionSets . getCardById ( cardId ) ; if ( ! ( card instanceof Way ) ) { throw new Error ( `Card id (${ cardId }) does not refer to a way` ) ; } return card ; } private static createSets ( ) { const setIds = Object . keys ( window . DominionSets ) as SetId [ ] ; const sets = { } ; for ( let setId of setIds ) { sets [ setId ] = DominionSet . fromJson ( window . DominionSets [ setId ] ) ; } return sets ; } private static createCardMap ( ) { const cards = { } ; const setIds = Object . keys ( DominionSets . sets ) ; for ( let setId of setIds ) { const set = DominionSets . sets [ setId as SetId ] as DominionSet ; const cardsFromSet = ( set . supplyCards as Card [ ] ) . concat ( set . events , set . landmarks , set . projects , set . ways , set . boons ) ; for ( let card of cardsFromSet ) { cards [ card . id ] = card ; if ( ! cards [ card . shortId ] ) { cards [ card . shortId ] = card ; } } } return cards ; } }