From b979bb16c6cfe49550c3cbceda36f61436c4c7fb Mon Sep 17 00:00:00 2001 From: Davis Ford Date: Wed, 8 Feb 2023 12:50:44 -0700 Subject: [PATCH 1/3] add check in factionClass --- src/factions/factionClass.ts | 25 +++++++++++++++++++++++-- src/factions/skaven/prayers.ts | 2 ++ src/types/data.ts | 21 ++++++++++++++++++++- src/utils/getArmy/getArmy.ts | 4 ++++ src/utils/getArmy/getCollection.ts | 2 ++ src/utils/getArmy/modify.ts | 1 + 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/factions/factionClass.ts b/src/factions/factionClass.ts index f66f67c13..2437479b7 100644 --- a/src/factions/factionClass.ts +++ b/src/factions/factionClass.ts @@ -1,9 +1,11 @@ +import { uniq } from 'lodash' import { TGrandAlliances } from 'meta/alliances' import { TSupportedFaction } from 'meta/factions' import { TRuleSource } from 'meta/rule_sources' import { TSubfactionArmy } from 'types/army' -import { TEffects } from 'types/data' -import { TItemDescriptions } from './factionTypes' +import { SELECTION_TYPES, TEffects } from 'types/data' +import { TSelectionTypes } from 'types/selections' +import { TItemDescriptions, TItemKey } from './factionTypes' import { getAggregateArmy, temporaryAdapter } from './temporaryAdapter' /** @@ -54,6 +56,25 @@ export class Faction< a[subFactionName] = temporaryAdapter(this.SubFactions[subFactionName], subFactionName, this.flavorLabel) return a }, {} as Record) + + // throw error if we've got bad data (this will discover bad data entry) + const validSelectionKeys: TItemKey[] = [...SELECTION_TYPES, 'allied_units'] + + this.subFactionKeys.forEach(k => { + const _subfaction = SubFactions[k] + const availableKeys = _subfaction?.available ? Object.keys(_subfaction.available) : [] + const mandatoryKeys = _subfaction?.mandatory ? Object.keys(_subfaction.mandatory) : [] + + const allKeys = uniq([...availableKeys, ...mandatoryKeys]) + + allKeys.forEach(selection => { + if (!validSelectionKeys.includes(selection as TSelectionTypes)) { + throw new Error( + `${factionName} subfaction ${k} has an invalid/unknown key: ${selection}. Please check your data in this faction's subfactions.ts file` + ) + } + }) + }) } } diff --git a/src/factions/skaven/prayers.ts b/src/factions/skaven/prayers.ts index deb21660a..0126775fa 100644 --- a/src/factions/skaven/prayers.ts +++ b/src/factions/skaven/prayers.ts @@ -41,4 +41,6 @@ const Prayers = { }, } +console.log(tagAs(Prayers, 'prayer')) + export default tagAs(Prayers, 'prayer') diff --git a/src/types/data.ts b/src/types/data.ts index 28763577c..62c399926 100644 --- a/src/types/data.ts +++ b/src/types/data.ts @@ -41,6 +41,25 @@ export const ENTRY_PROPERTIES: TEntryProperties[] = [ 'unit', ] +export const SELECTION_TYPES: TSelectionTypes[] = [ + 'artifacts', + 'battalions', + 'command_abilities', + 'command_traits', + 'core_rules', + 'endless_spells', + 'flavors', + 'grand_strategies', + 'incarnates', + 'monstrous_rampages', + 'mount_traits', + 'prayers', + 'scenery', + 'spells', + 'triumphs', + 'units', +] + export const lowerToUpperLookup: Record = { artifacts: 'Artifacts', battalions: 'Battalions', @@ -58,7 +77,7 @@ export const lowerToUpperLookup: Record = { spells: 'Spells', triumphs: 'Triumphs', units: 'Units', -} +} as const export const upperToLowerLookup: Record = { Artifacts: 'artifacts', diff --git a/src/utils/getArmy/getArmy.ts b/src/utils/getArmy/getArmy.ts index eb22aa2f5..634353423 100644 --- a/src/utils/getArmy/getArmy.ts +++ b/src/utils/getArmy/getArmy.ts @@ -36,6 +36,8 @@ export const getArmy = ( const Army: TSubfactionArmy = (subFactionName && subFactionArmies?.[subFactionName]) || AggregateArmy + debugger + const Collection = getCollection(Army) const army = modifyArmy(Army, { @@ -46,6 +48,8 @@ export const getArmy = ( realmscape, }) + debugger + return army as unknown as IArmy } diff --git a/src/utils/getArmy/getCollection.ts b/src/utils/getArmy/getCollection.ts index 19ee11754..f09f1c140 100644 --- a/src/utils/getArmy/getCollection.ts +++ b/src/utils/getArmy/getCollection.ts @@ -90,6 +90,8 @@ export const getCollection = (army: TInitialArmy): TCollection => { checkForMandatoryItems(army.SubFaction) types.forEach(x => x?.forEach(checkForMandatoryItems)) + debugger + return { Artifacts: sortBy(Collection.Artifacts, 'name'), Battalions: sortBy(Collection.Battalions, 'name'), diff --git a/src/utils/getArmy/modify.ts b/src/utils/getArmy/modify.ts index e6da1fc15..779f59108 100644 --- a/src/utils/getArmy/modify.ts +++ b/src/utils/getArmy/modify.ts @@ -149,6 +149,7 @@ const modifySpells = (spells: TEntry[], Collection: TCollection): TEntry[] => { const modifyPrayers = (prayers: TEntry[], Collection: TCollection): TEntry[] => { const Prayers = prayers.concat(Collection.Prayers) + debugger return uniqBy( sortBy(Prayers, 'name') .concat(sortBy(GenericPrayers, 'name')) From c8d19f25738b474f7c828303eacbb6a416281056 Mon Sep 17 00:00:00 2001 From: Davis Ford Date: Wed, 8 Feb 2023 14:04:12 -0700 Subject: [PATCH 2/3] Fix missing Skaven prayers (fixes #1595) --- src/factions/factionClass.ts | 8 ++++++-- src/factions/skaven/prayers.ts | 2 -- src/factions/skaven/subfactions.ts | 2 +- src/tests/getArmy.test.ts | 7 +++++++ src/types/data.ts | 2 +- src/utils/getArmy/getArmy.ts | 4 ---- src/utils/getArmy/getCollection.ts | 2 -- src/utils/getArmy/modify.ts | 1 - 8 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/factions/factionClass.ts b/src/factions/factionClass.ts index 2437479b7..26097d8e4 100644 --- a/src/factions/factionClass.ts +++ b/src/factions/factionClass.ts @@ -57,11 +57,15 @@ export class Faction< return a }, {} as Record) + this.checkForDataEntryErrors() + } + + private checkForDataEntryErrors = () => { // throw error if we've got bad data (this will discover bad data entry) const validSelectionKeys: TItemKey[] = [...SELECTION_TYPES, 'allied_units'] this.subFactionKeys.forEach(k => { - const _subfaction = SubFactions[k] + const _subfaction = this.SubFactions[k] const availableKeys = _subfaction?.available ? Object.keys(_subfaction.available) : [] const mandatoryKeys = _subfaction?.mandatory ? Object.keys(_subfaction.mandatory) : [] @@ -70,7 +74,7 @@ export class Faction< allKeys.forEach(selection => { if (!validSelectionKeys.includes(selection as TSelectionTypes)) { throw new Error( - `${factionName} subfaction ${k} has an invalid/unknown key: ${selection}. Please check your data in this faction's subfactions.ts file` + `${this.factionName} subfaction ${k} has an invalid/unknown key: ${selection}. Please check your data in this faction's subfactions.ts file` ) } }) diff --git a/src/factions/skaven/prayers.ts b/src/factions/skaven/prayers.ts index 0126775fa..deb21660a 100644 --- a/src/factions/skaven/prayers.ts +++ b/src/factions/skaven/prayers.ts @@ -41,6 +41,4 @@ const Prayers = { }, } -console.log(tagAs(Prayers, 'prayer')) - export default tagAs(Prayers, 'prayer') diff --git a/src/factions/skaven/subfactions.ts b/src/factions/skaven/subfactions.ts index 0936b2e1f..18fbb9784 100644 --- a/src/factions/skaven/subfactions.ts +++ b/src/factions/skaven/subfactions.ts @@ -23,7 +23,7 @@ const subFactions = { endless_spells: [EndlessSpells], flavors: [Flavors], grand_strategies: [GrandStrategies], - Prayers: [Prayers], + prayers: [Prayers], scenery: [Scenery], spells: [Spells], units: [Units], diff --git a/src/tests/getArmy.test.ts b/src/tests/getArmy.test.ts index 9a6ee4b5e..cfc34847b 100644 --- a/src/tests/getArmy.test.ts +++ b/src/tests/getArmy.test.ts @@ -2,6 +2,7 @@ import { GenericEndlessSpells, GenericTriumphs } from 'generic_rules' import { sortBy } from 'lodash' import { ORDER } from 'meta/alliances' import { BEASTS_OF_CHAOS, SERAPHON, SYLVANETH } from 'meta/factions' +import { getFactionList } from 'meta/faction_list' import { IArmy } from 'types/army' import { getAllianceItems } from 'utils/getArmy/getAllianceItems' import { getArmy } from 'utils/getArmy/getArmy' @@ -49,3 +50,9 @@ describe('getArmy', () => { expect(shouldNotHaveThisUnit).toBeUndefined() }) }) + +describe('getFactionList', () => { + it('should not throw errors when using getFactionList', () => { + expect(getFactionList).not.toThrow() + }) +}) diff --git a/src/types/data.ts b/src/types/data.ts index 62c399926..faf453779 100644 --- a/src/types/data.ts +++ b/src/types/data.ts @@ -77,7 +77,7 @@ export const lowerToUpperLookup: Record = { spells: 'Spells', triumphs: 'Triumphs', units: 'Units', -} as const +} export const upperToLowerLookup: Record = { Artifacts: 'artifacts', diff --git a/src/utils/getArmy/getArmy.ts b/src/utils/getArmy/getArmy.ts index 634353423..eb22aa2f5 100644 --- a/src/utils/getArmy/getArmy.ts +++ b/src/utils/getArmy/getArmy.ts @@ -36,8 +36,6 @@ export const getArmy = ( const Army: TSubfactionArmy = (subFactionName && subFactionArmies?.[subFactionName]) || AggregateArmy - debugger - const Collection = getCollection(Army) const army = modifyArmy(Army, { @@ -48,8 +46,6 @@ export const getArmy = ( realmscape, }) - debugger - return army as unknown as IArmy } diff --git a/src/utils/getArmy/getCollection.ts b/src/utils/getArmy/getCollection.ts index f09f1c140..19ee11754 100644 --- a/src/utils/getArmy/getCollection.ts +++ b/src/utils/getArmy/getCollection.ts @@ -90,8 +90,6 @@ export const getCollection = (army: TInitialArmy): TCollection => { checkForMandatoryItems(army.SubFaction) types.forEach(x => x?.forEach(checkForMandatoryItems)) - debugger - return { Artifacts: sortBy(Collection.Artifacts, 'name'), Battalions: sortBy(Collection.Battalions, 'name'), diff --git a/src/utils/getArmy/modify.ts b/src/utils/getArmy/modify.ts index 779f59108..e6da1fc15 100644 --- a/src/utils/getArmy/modify.ts +++ b/src/utils/getArmy/modify.ts @@ -149,7 +149,6 @@ const modifySpells = (spells: TEntry[], Collection: TCollection): TEntry[] => { const modifyPrayers = (prayers: TEntry[], Collection: TCollection): TEntry[] => { const Prayers = prayers.concat(Collection.Prayers) - debugger return uniqBy( sortBy(Prayers, 'name') .concat(sortBy(GenericPrayers, 'name')) From 799b4f0862194e8cd9aec619a0434f8d76328940 Mon Sep 17 00:00:00 2001 From: Davis Ford Date: Wed, 8 Feb 2023 14:07:57 -0700 Subject: [PATCH 3/3] Update factionClass.ts --- src/factions/factionClass.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/factions/factionClass.ts b/src/factions/factionClass.ts index 26097d8e4..5b1e44d41 100644 --- a/src/factions/factionClass.ts +++ b/src/factions/factionClass.ts @@ -60,19 +60,18 @@ export class Faction< this.checkForDataEntryErrors() } + /** Throw an error if we've got bad data (this will discover bad data entry) */ private checkForDataEntryErrors = () => { - // throw error if we've got bad data (this will discover bad data entry) const validSelectionKeys: TItemKey[] = [...SELECTION_TYPES, 'allied_units'] this.subFactionKeys.forEach(k => { - const _subfaction = this.SubFactions[k] - const availableKeys = _subfaction?.available ? Object.keys(_subfaction.available) : [] - const mandatoryKeys = _subfaction?.mandatory ? Object.keys(_subfaction.mandatory) : [] - - const allKeys = uniq([...availableKeys, ...mandatoryKeys]) + const allKeys = uniq([ + ...Object.keys(this.SubFactions[k]?.available ?? {}), + ...Object.keys(this.SubFactions[k]?.mandatory ?? {}), + ]) as TSelectionTypes[] allKeys.forEach(selection => { - if (!validSelectionKeys.includes(selection as TSelectionTypes)) { + if (!validSelectionKeys.includes(selection)) { throw new Error( `${this.factionName} subfaction ${k} has an invalid/unknown key: ${selection}. Please check your data in this faction's subfactions.ts file` )