From 0fae284f79b1c934e58214cf5f88cad4cab6b03b Mon Sep 17 00:00:00 2001 From: Frans Bongers Date: Wed, 19 Mar 2025 21:55:43 +0100 Subject: refactor: FrontId to number --- data.js | 20 ++++++++++---------- data.ts | 20 ++++++++++---------- rules.js | 16 ++++++++-------- rules.ts | 27 +++++++++++++++------------ types.d.ts | 12 ++++-------- 5 files changed, 47 insertions(+), 48 deletions(-) diff --git a/data.js b/data.js index b90990f..58bb00c 100644 --- a/data.js +++ b/data.js @@ -28,13 +28,13 @@ const INITIATIVE_PLAYER = 'i'; exports.INITIATIVE_PLAYER = INITIATIVE_PLAYER; const ALL_PLAYERS = 'all'; exports.ALL_PLAYERS = ALL_PLAYERS; -const ARAGON = 'a'; +const ARAGON = 0; exports.ARAGON = ARAGON; -const MADRID = 'm'; +const MADRID = 1; exports.MADRID = MADRID; -const NORTHERN = 'n'; +const NORTHERN = 2; exports.NORTHERN = NORTHERN; -const SOUTHERN = 's'; +const SOUTHERN = 3; exports.SOUTHERN = SOUTHERN; const CLOSEST_TO_DEFEAT = 'd'; exports.CLOSEST_TO_DEFEAT = CLOSEST_TO_DEFEAT; @@ -1731,12 +1731,6 @@ const data = { }, ], fronts: [ - { - id: NORTHERN, - name: 'Northern', - left: 89, - top: 96, - }, { id: ARAGON, name: 'Aragon', @@ -1749,6 +1743,12 @@ const data = { left: 115, top: 262, }, + { + id: NORTHERN, + name: 'Northern', + left: 89, + top: 96, + }, { id: SOUTHERN, name: 'Southern', diff --git a/data.ts b/data.ts index f36f9c3..424fcc7 100644 --- a/data.ts +++ b/data.ts @@ -17,10 +17,10 @@ const PLAYER_WITH_MOST_HERO_POINTS = 0; const INITIATIVE_PLAYER = 'i'; const ALL_PLAYERS = 'all'; -const ARAGON = 'a'; -const MADRID = 'm'; -const NORTHERN = 'n'; -const SOUTHERN = 's'; +const ARAGON = 0; +const MADRID = 1; +const NORTHERN = 2; +const SOUTHERN = 3; const CLOSEST_TO_DEFEAT = 'd'; const CLOSEST_TO_VICTORY = 'v'; const FRONTS: FrontId[] = [ARAGON, MADRID, NORTHERN, SOUTHERN]; @@ -1803,12 +1803,6 @@ const data: StaticData = { }, ], fronts: [ - { - id: NORTHERN, - name: 'Northern', - left: 89, - top: 96, - }, { id: ARAGON, name: 'Aragon', @@ -1821,6 +1815,12 @@ const data: StaticData = { left: 115, top: 262, }, + { + id: NORTHERN, + name: 'Northern', + left: 89, + top: 96, + }, { id: SOUTHERN, name: 'Southern', diff --git a/rules.js b/rules.js index 9dedb5b..40cde75 100644 --- a/rules.js +++ b/rules.js @@ -410,28 +410,28 @@ function setup(seed, _scenario, options) { }, engine: [], faction_turn: null, - fronts: { - a: { + fronts: [ + { value: -2, contributions: [], status: null, }, - m: { + { value: -2, contributions: [], status: null, }, - n: { + { value: -2, contributions: [], status: null, }, - s: { + { value: -2, contributions: [], status: null, }, - }, + ], glory: [], first_player: null, hands: { @@ -2551,7 +2551,7 @@ function defeat_on_a_front(front_id) { log_br(); log('Defeat on ' + get_front_name(front_id) + '!'); log_br(); - if (front_id === 'm' || get_defeated_front_count() == 2) { + if (front_id === data_1.MADRID || get_defeated_front_count() == 2) { game_over('Fascist', 'All players lose the game!'); return; } @@ -2745,7 +2745,7 @@ function get_fronts_closest_to(target) { return []; } const targetValue = target === 'd' ? Math.min(...values) : Math.max(...values); - return Object.keys(game.fronts).filter((frontId) => game.fronts[frontId].value === targetValue); + return game.fronts.findIndex((front) => front.value === targetValue); } function log_br() { if (game.log.length > 0 && game.log[game.log.length - 1] !== '') diff --git a/rules.ts b/rules.ts index 8299d1b..1e2ff94 100644 --- a/rules.ts +++ b/rules.ts @@ -145,7 +145,7 @@ function gen_action_card(card_id: CardId) { gen_action('card', card_id); } -function gen_action_front(front_id: string) { +function gen_action_front(front_id: FrontId) { gen_action('front', front_id); } @@ -593,28 +593,28 @@ export function setup(seed: number, _scenario: string, options: Record game.fronts[frontId].value === targetValue - ) as FrontId[]; + return game.fronts.findIndex( + (front) => front.value === targetValue + ) as unknown as FrontId[]; } // #endregion diff --git a/types.d.ts b/types.d.ts index 2387420..16cae59 100644 --- a/types.d.ts +++ b/types.d.ts @@ -8,7 +8,8 @@ export type Brand = T & { export type Player = Brand; export type CardId = Brand; export type FactionId = 'a' | 'c' | 'm'; -export type FrontId = 'a' | 'm' | 'n' | 's'; +export type FrontId = 0 | 1 | 2 | 3; +// export type FrontId = 'a' | 'm' | 'n' | 's'; export interface Front { value: number; @@ -46,12 +47,7 @@ export interface Game { * First player of current game turn */ first_player: FactionId | null; - fronts: { - a: Front; - m: Front; - n: Front; - s: Front; - }; + fronts: Front[]; glory: FactionId[]; hands: Record; hero_points: Record; @@ -202,7 +198,7 @@ export interface Effect { export interface StaticData { cards: Card[]; fronts: Array<{ - id: string; + id: number; name: string; left: number; top: number; -- cgit v1.2.3