'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.roles = exports.scenarios = void 0; exports.action = action; exports.view = game_view; exports.setup = setup; const data_1 = require("./data"); const states = {}; let game = {}; var view = {}; const role_ids = [data_1.ANARCHISTS_ID, data_1.COMMUNISTS_ID, data_1.MODERATES_ID]; const faction_player_map = { a: data_1.ANARCHIST, c: data_1.COMMUNIST, m: data_1.MODERATE, }; const player_faction_map = { [data_1.ANARCHIST]: data_1.ANARCHISTS_ID, [data_1.COMMUNIST]: data_1.COMMUNISTS_ID, [data_1.MODERATE]: data_1.MODERATES_ID, }; const front_names = { a: 'Aragon Front', m: 'Madrid Front', n: 'Northern Front', s: 'Southern Front', d: 'the Front closest to Defeat', v: 'the Front closest to Victory', }; const bonus_names = ['Morale Bonus', 'Teamwork Bonus']; const { cards, medallions, tracks, } = data_1.default; const bonuses = [data_1.MORALE_BONUS, data_1.TEAMWORK_BONUS]; const faction_cards = { [data_1.ANARCHISTS_ID]: make_list(37, 54), [data_1.COMMUNISTS_ID]: make_list(19, 36), [data_1.MODERATES_ID]: make_list(1, 18), }; const fascist_decks = { 1: make_list(55, 72), 2: make_list(73, 90), 3: make_list(91, 108), }; exports.scenarios = ['Standard']; exports.roles = [data_1.ANARCHIST, data_1.COMMUNIST, data_1.MODERATE]; function gen_action(action, argument) { if (argument === undefined) { view.actions[action] = 1; } else { if (!(action in view.actions)) view.actions[action] = []; view.actions[action].push(argument); } } function gen_action_blank_marker(marker_id) { gen_action('blank_marker', marker_id); } function gen_action_bonus(bonus_id) { gen_action('bonus', bonus_id); } function gen_action_card(card_id) { gen_action('card', card_id); } function gen_action_front(front_id) { gen_action('front', front_id); } function gen_action_medallion(medallion_id) { gen_action('medallion', medallion_id); } function gen_action_standee(track_id) { gen_action('standee', track_id); } function action(state, player, action, arg) { console.log('action', player, action, arg); if (action !== 'undo') { state.undo = push_undo(); } game = state; let S = states[game.state]; if (action in S) S[action](arg, player); else if (action === 'undo' && game.undo && game.undo.length > 0) pop_undo(); else throw new Error('Invalid action: ' + action); return game; } const leaf_node = 'l'; const seq_node = 's'; const function_node = 'f'; const resolved = 1; function create_leaf_node(state, faction, args) { return { t: leaf_node, s: state, p: faction, a: args, r: 0, }; } function create_function_node(func_name, args) { return { t: function_node, f: func_name, a: args, r: 0, }; } function create_seq_node(children) { return { t: seq_node, c: children, }; } function setup_bag_of_glory() { game.engine = [ create_leaf_node('add_glory', game.initiative), create_function_node('end_of_turn'), ]; next(); } function setup_choose_card() { const player_order = get_player_order(); game.engine = player_order.map((faction_id) => create_leaf_node('choose_card', faction_id)); game.engine.push(create_function_node('setup_player_turn')); next(); } function setup_final_bid() { log_h1('Final Bid'); const player_order = get_player_order(); game.engine = player_order.map((faction_id) => create_leaf_node('choose_final_bid', faction_id)); game.engine.push(create_function_node('resolve_final_bid')); game.engine.push(create_function_node('setup_choose_card')); next(); } function setup_player_turn() { const player_order = get_player_order(); game.engine = player_order.map((faction_id) => create_seq_node([ create_function_node('start_of_player_turn', { f: faction_id }), create_leaf_node('player_turn', faction_id), ])); game.engine.push(create_function_node('resolve_fascist_test')); game.engine.push(create_function_node('setup_bag_of_glory')); next(); } function start_of_player_turn() { const args = get_active_node_args(); const player = faction_player_map[args.f]; log_h2(player, player); game.faction_turn = args.f; resolve_active_and_proceed(); } const engine_functions = { check_activate_icon, end_of_turn, setup_bag_of_glory, setup_choose_card, setup_final_bid, setup_player_turn, start_of_player_turn, start_year, resolve_fascist_test, resolve_final_bid, card1_event2, card3_event2, card10_event2, card16_event2, card17_event3, card20_event3, card22_event3, card23_event1, card26_event1, card29_event2, card35_event2, card42_event3, card45_event2, card46_event3, card50_event2, card53_event2, card54_event1, }; function get_active(engine) { for (let i of engine) { if ((i.t === leaf_node || i.t === function_node) && i.r !== resolved) { return { parent: engine, node: i }; } if (i.t === seq_node) { const next_child = get_active(i.c); if (next_child !== null) { return next_child; } } } return null; } function get_active_node(engine = game.engine) { const a = get_active(engine); return a === null ? null : a.node; } function get_active_node_args() { const node = get_active_node(game.engine); if (node.t === leaf_node || node.t === function_node) { return node.a; } return null; } function insert_before_or_after_active_node(node, position, engine = game.engine) { const a = get_active(engine); if (a === null) { return; } const i = a.parent.indexOf(a.node); if (i >= 0) { array_insert(a.parent, i + (position == 'after' ? 1 : 0), node); } } function insert_after_active_node(node, engine = game.engine) { insert_before_or_after_active_node(node, 'after', engine); } function insert_before_active_node(node, engine = game.engine) { insert_before_or_after_active_node(node, 'before', engine); } function next() { const node = get_active_node(game.engine); if (node.t === function_node && engine_functions[node.f]) { const args = node.a; if (args) { engine_functions[node.f](args); } else { engine_functions[node.f](); } } else if (node.t === 'l') { game.state = node.s; const current_active = game.active; const next_active = faction_player_map[node.p]; if (next_active !== current_active && game.undo.length > 0) { insert_before_active_node(create_leaf_node('confirm_turn', get_active_faction())); game.state = 'confirm_turn'; return; } game.active = next_active; } } function resolve_active_node(checkpoint = false) { const next_node = get_active_node(game.engine); if (next_node !== null) { next_node.r = resolved; } if (checkpoint) { clear_undo(); } } function resolve_active_and_proceed(checkpoint = false) { resolve_active_node(checkpoint); next(); } function game_view(state, player) { game = state; const faction_id = player_faction_map[player]; view = { engine: game.engine, log: game.log, prompt: null, bag_of_glory: game.bag_of_glory, bonuses: game.bonuses, current_events: game.current_events, fronts: game.fronts, glory: game.glory, hand: game.hands[faction_id], hero_points: game.hero_points, initiative: game.initiative, medallions: game.medallions, played_card: game.played_card, selectable_cards: game.selectable_cards, selected_cards: game.selected_cards[faction_id], tableaus: game.tableaus, tracks: game.tracks, triggered_track_effects: game.triggered_track_effects, undo: game.undo, used_medallions: game.used_medallions, year: game.year, }; if (game.state === 'game_over') { view.prompt = game.victory; } else if (player !== game.active) { let inactive = states[game.state].inactive || game.state; view.prompt = `Waiting for ${game.active} to ${inactive}.`; } else { view.actions = {}; states[game.state].prompt(); if (game.undo && game.undo.length > 0) view.actions.undo = 1; else view.actions.undo = 0; } return view; } function setup(seed, _scenario, _options) { game = { seed: seed, state: null, active: data_1.ANARCHIST, active_abilities: [], bag_of_glory: [data_1.ANARCHISTS_ID, data_1.COMMUNISTS_ID, data_1.MODERATES_ID], blank_markers: [[], [], [], [], []], bonuses: [data_1.ON, data_1.ON], current_events: [], discard: { a: [], c: [], m: [], f: [], }, engine: [], faction_turn: null, fronts: { a: { value: -2, contributions: [], status: null, }, m: { value: -2, contributions: [], status: null, }, n: { value: -2, contributions: [], status: null, }, s: { value: -2, contributions: [], status: null, }, }, glory: [], hands: { a: [], c: [], m: [], }, hero_points: { a: 2, c: 2, m: 0, pool: 14, }, initiative: data_1.MODERATES_ID, medallions: { a: [], c: [], m: [], pool: [], }, played_card: null, selectable_cards: [], selected_cards: { a: [], c: [], m: [], }, tableaus: { a: [], c: [], m: [], }, tracks: [5, 5, 6, 3, 3], trash: { a: [], c: [], m: [], }, triggered_track_effects: [], log: [], undo: [], used_medallions: [], top_of_events_deck: null, turn: 0, year: 0, }; draw_medallions(); start_year(); return game; } function draw_hand_cards(faction_id, count) { const deck = list_deck(faction_id); if (game.medallions[faction_id].includes(data_1.INTELLIGENCE_MEDALLION_ID)) { count++; } const drawn_cards = count; if (deck.length < count) { count = count - deck.length; game.hands[faction_id] = game.hands[faction_id].concat(deck); game.discard[faction_id] = []; } const log = drawn_cards === 1 ? `${get_player(faction_id)} draws 1 card` : `${get_player(faction_id)} draws ${drawn_cards} cards`; logi(log); for (let i = 0; i < count; i++) { const deck = list_deck(faction_id); game.hands[faction_id].push(draw_card(deck)); } } function start_year() { game.year++; game.turn = 1; game.current_events = []; role_ids.forEach((role) => { draw_hand_cards(role, 5); }); start_turn(); } function start_turn() { log_h1(`Year ${game.year} - Turn ${game.turn}`); const cardId = draw_fascist_card(); game.current_events.push(cardId); const card = cards[cardId]; log_h2('Fascist Event', 'fascist'); log(card.title); game.engine = card.effects.map((effect) => resolve_effect(effect)); if (game.year === 3 && game.turn === 4) { game.engine.push(create_function_node('setup_final_bid')); } else { game.engine.push(create_function_node('setup_choose_card')); } next(); } states.activate_icon = { inactive: 'activate an icon', prompt() { view.prompt = 'Choose an icon to activate'; const c = cards[game.played_card]; for (const i of c.icons) { gen_action(i); } }, add_to_front() { insert_after_active_node(create_leaf_node('add_to_front', get_active_faction(), { t: data_1.ANY, v: get_icon_count_in_tableau('add_to_front'), })); resolve_active_and_proceed(); }, collectivization() { const count = get_icon_count_in_tableau('collectivization'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.COLLECTIVIZATION, count); } else { move_track(data_1.COLLECTIVIZATION, count); } resolve_active_and_proceed(); }, d_collectivization() { const count = -1 * get_icon_count_in_tableau('d_collectivization'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.COLLECTIVIZATION, count); } else { move_track(data_1.COLLECTIVIZATION, count); } resolve_active_and_proceed(); }, d_foreign_aid() { const count = -1 * get_icon_count_in_tableau('d_foreign_aid'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.FOREIGN_AID, count); } else { move_track(data_1.FOREIGN_AID, count); } resolve_active_and_proceed(); }, d_government() { const count = -1 * get_icon_count_in_tableau('d_government'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.GOVERNMENT, count); } else { move_track(data_1.GOVERNMENT, count); } resolve_active_and_proceed(); }, d_liberty() { const count = -1 * get_icon_count_in_tableau('d_liberty'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.LIBERTY, count); } else { move_track(data_1.LIBERTY, count); } resolve_active_and_proceed(); }, d_soviet_support() { const count = -1 * get_icon_count_in_tableau('d_soviet_support'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.SOVIET_SUPPORT, count); } else { move_track(data_1.SOVIET_SUPPORT, count); } resolve_active_and_proceed(); }, draw_card() { draw_hand_cards(get_active_faction(), get_icon_count_in_tableau('draw_card')); resolve_active_and_proceed(); }, foreign_aid() { const count = get_icon_count_in_tableau('foreign_aid'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.FOREIGN_AID, count); } else { move_track(data_1.FOREIGN_AID, count); } resolve_active_and_proceed(); }, government() { const direction = game.active === data_1.COMMUNIST ? -1 : 1; const count = direction * get_icon_count_in_tableau('government'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.GOVERNMENT, count); } else { move_track(data_1.GOVERNMENT, count); } resolve_active_and_proceed(); }, government_to_center() { const direction = game.tracks[data_1.GOVERNMENT] >= 6 ? -1 : 1; move_track(data_1.GOVERNMENT, direction * get_icon_count_in_tableau('government_to_center')); resolve_active_and_proceed(); }, liberty() { const count = get_icon_count_in_tableau('liberty'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.LIBERTY, count); } else { move_track(data_1.LIBERTY, count); } resolve_active_and_proceed(); }, soviet_support() { const count = get_icon_count_in_tableau('soviet_support'); if (can_use_medallion(data_1.ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(data_1.SOVIET_SUPPORT, count); } else { move_track(data_1.SOVIET_SUPPORT, count); } resolve_active_and_proceed(); }, teamwork_on() { game.bonuses[data_1.TEAMWORK_BONUS] = data_1.ON; resolve_active_and_proceed(); }, }; states.add_card_to_tableau = { inactive: 'add a card to their tableau', prompt() { view.prompt = 'Choose a card to add to your tableau'; for (const c of game.hands[get_active_faction()]) { gen_action_card(c); } }, card(c) { const faction_id = get_active_faction(); const card = cards[c]; array_remove(game.hands[faction_id], game.hands[faction_id].indexOf(c)); game.tableaus[faction_id].push(c); logi(`${faction_player_map[faction_id]} adds ${card.title} to their tableau`); resolve_active_and_proceed(); }, }; states.add_glory = { inactive: 'add tokens to the Bag of Glory', prompt() { view.prompt = 'Add tokens to the Bag of Glory'; gen_action('add_glory'); }, add_glory() { let number = 1; if (game.turn === 4) { number++; } add_glory(get_active_faction(), number); resolve_active_and_proceed(); }, }; states.add_to_front = { inactive: 'add strength to a Front', prompt() { const args = get_active_node_args(); const possible_fronts = get_fronts_to_add_to(args.t); view.prompt = possible_fronts.length === 1 ? `Add strength to ${front_names[possible_fronts[0]]}` : 'Add strength to a Front'; for (let f of possible_fronts) { gen_action_front(f); } }, front(f) { const value = get_active_node_args().v; update_front(f, value, get_active_faction()); resolve_active_and_proceed(); }, }; states.attack_front = { inactive: 'attack a Front', prompt() { const node = get_active_node(); const front = node.a.t; let targets = []; if (front === 'd' || front === 'v') { targets = get_fronts_closest_to(front); } else if (game.fronts[front].status === data_1.DEFEAT) { targets = get_fronts_closest_to('d'); } else if (game.fronts[front].status === data_1.VICTORY) { targets = get_fronts_to_add_to(data_1.ANY); } else { targets.push(front); } view.prompt = targets.length === 1 ? `Attack ${front_names[targets[0]]}` : 'Attack a front'; targets.forEach((id) => gen_action('front', id)); }, front(f) { const node = get_active_node(); const value = node.a.v; update_front(f, value); resolve_active_and_proceed(); }, }; states.break_tie_final_bid = { inactive: 'break tie for Final Bid', prompt() { view.prompt = 'Choose the winner of the Final Bid'; const { winners } = get_active_node_args(); for (const f of winners) { gen_action(faction_player_map[f]); } }, Anarchist() { win_final_bid(data_1.ANARCHISTS_ID); resolve_active_and_proceed(); }, Communist() { win_final_bid(data_1.COMMUNISTS_ID); resolve_active_and_proceed(); }, Moderate() { win_final_bid(data_1.MODERATES_ID); resolve_active_and_proceed(); }, }; states.break_tie_winner = { inactive: 'break tie for winner of the game', prompt() { view.prompt = 'Choose the winner of the game'; const { winners } = get_active_node_args(); for (const f of winners) { gen_action(faction_player_map[f]); } }, Anarchist() { const { glory } = get_active_node_args(); win_game(data_1.ANARCHIST, glory); resolve_active_and_proceed(); }, Communist() { const { glory } = get_active_node_args(); win_game(data_1.COMMUNIST, glory); resolve_active_and_proceed(); }, Moderate() { const { glory } = get_active_node_args(); win_game(data_1.MODERATE, glory); resolve_active_and_proceed(); }, }; states.choose_area_ap = { inactive: 'choose area to use Action Points', prompt() { view.prompt = 'Choose area of the board to affect'; for (const track of tracks) { gen_action_standee(track.id); } const fronts = get_fronts_to_add_to(data_1.ANY); for (const front of fronts) { gen_action_front(front); } for (const bonus of bonuses) { if (game.bonuses[bonus] === data_1.OFF) { gen_action_bonus(bonus); } } }, bonus(b) { update_bonus(b, data_1.ON); resolve_active_and_proceed(); }, front(f) { const s = get_active_node_args().strength; update_front(f, s, get_active_faction_id()); resolve_active_and_proceed(); }, standee(track_id) { insert_after_active_node({ t: leaf_node, p: get_active_faction_id(), s: 'move_track_up_or_down', a: { track_id, strength: get_active_node()?.a.strength, }, }); resolve_active_and_proceed(); }, }; states.change_bonus = { inactive: 'gain select Bonus', prompt() { const args = get_active_node_args(); if ((args.v === data_1.ON && game.bonuses[data_1.TEAMWORK_BONUS] === data_1.ON && game.bonuses[data_1.MORALE_BONUS] === data_1.ON) || (args.v === data_1.OFF && game.bonuses[args.t] === data_1.OFF)) { gen_action('skip'); } if (args.t === data_1.ANY && args.v === data_1.ON) { view.prompt = 'Turn on a Bonus'; for (const bonus of bonuses) { if (game.bonuses[bonus] === data_1.OFF) { gen_action_bonus(bonus); } } } else if (args.v === data_1.OFF) { view.prompt = `Turn off ${bonus_names[args.t]}`; gen_action_bonus(args.t); } }, bonus(b) { const value = get_active_node_args().v; update_bonus(b, value); resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.choose_card = { inactive: 'choose a card', prompt() { view.prompt = 'Choose a card to play this turn'; const faction = get_active_faction(); const hand = game.hands[faction]; for (let c of hand) { if (!game.selected_cards[faction].includes(c)) { gen_action_card(c); } } }, card(c) { const faction = get_active_faction(); game.selected_cards[faction].push(c); resolve_active_and_proceed(); }, }; states.choose_final_bid = { inactive: 'choose Final Bid', prompt() { view.prompt = 'Choose a card to add to the Final Bid'; const faction = get_active_faction(); for (let c of game.hands[faction]) { if (!game.selected_cards[faction].includes(c)) { gen_action_card(c); } } gen_action('done'); }, card(c) { const faction = get_active_faction(); game.selected_cards[faction].push(c); if (game.selected_cards[faction].length < 3) { next(); } else { resolve_active_and_proceed(); } }, done() { resolve_active_and_proceed(); }, }; states.choose_medallion = { inactive: 'choose a medallion', prompt() { view.prompt = 'Choose a medallion'; for (let m of game.medallions.pool) { gen_action_medallion(m); } if (!game.medallions.pool.some((m) => m !== null)) { gen_action('skip'); } }, medallion(m) { const faction = get_active_faction(); const medallion = medallions[m]; logi(`${faction_player_map[faction]} earns ${medallion.name}`); const index = game.medallions.pool.indexOf(m); game.medallions.pool[index] = null; switch (m) { case 0: add_glory(faction, 1, true); break; case 1: gain_hero_points(faction, 7); break; case 2: insert_after_active_node(create_leaf_node('choose_card', faction)); break; default: game.medallions[faction].push(m); } resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.confirm_turn = { inactive: 'confirm their turn', prompt() { view.prompt = 'Confirm your actions or undo'; gen_action('confirm'); }, confirm() { resolve_active_and_proceed(true); }, }; states.draw_card = { inactive: 'draw a card', prompt() { const { v } = get_active_node_args(); view.prompt = v === 1 ? 'Draw a card' : `Draw ${v} cards`; gen_action(v === 1 ? 'draw_card' : 'draw_cards'); }, draw_card() { const { v } = get_active_node_args(); draw_hand_cards(get_active_faction(), v); resolve_active_and_proceed(); }, draw_cards() { const { v } = get_active_node_args(); draw_hand_cards(get_active_faction(), v); resolve_active_and_proceed(); }, }; states.end_of_year_discard = { inactive: 'discard cards from hand and tableau', prompt() { view.prompt = 'Discard a card'; const faction_id = get_active_faction(); const hand = game.hands[faction_id]; if (hand.length > get_hand_limit(faction_id)) { for (let c of hand) gen_action_card(c); } const tableau = game.tableaus[faction_id]; if (tableau.length > game.year) { for (let c of tableau) gen_action_card(c); } }, card(c) { const faction_id = get_active_faction(); if (game.hands[faction_id].includes(c)) { game.hands[faction_id] = game.hands[faction_id].filter((id) => id !== c); } else if (game.tableaus[faction_id].includes(c)) { game.tableaus[faction_id] = game.tableaus[faction_id].filter((id) => id !== c); } game.discard[faction_id].push(c); if (game.hands[faction_id].length > get_hand_limit(faction_id) || game.tableaus[faction_id].length > game.year) { next(); } else { log(`${faction_player_map[faction_id]} discards cards`); resolve_active_and_proceed(); } }, }; states.gain_hero_points = { inactive: 'gain Hero Points', prompt() { const value = get_active_node_args().v; if (game.hero_points.pool > 0) { view.prompt = value > 1 ? `Gain ${value} Hero Points` : 'Gain 1 Hero Point'; gen_action('gain_hp'); } else { view.prompt = 'No Hero Points available in pool. You must skip'; gen_action('skip'); } }, gain_hp() { const value = get_active_node_args().v; gain_hero_points(get_active_faction(), value); resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.game_over = { get inactive() { return game.victory; }, prompt() { view.prompt = game.victory; }, }; function resolve_player_with_most_hero_points(faction) { const value = get_active_node_args().v; if (value < 0) { lose_hero_point(faction, value); } else { gain_hero_points(faction, value); } resolve_active_and_proceed(); } states.select_player_with_most_hero_points = { inactive: 'choose a Player', prompt() { const { v } = get_active_node_args(); view.prompt = v < 0 ? 'Choose player to lose Hero Points' : 'Choose player to gain Hero Points'; const factions = get_factions_with_most_hero_poins(); for (let faction_id of factions) { gen_action(faction_player_map[faction_id]); } }, Anarchist() { resolve_player_with_most_hero_points(data_1.ANARCHISTS_ID); }, Communist() { resolve_player_with_most_hero_points(data_1.COMMUNISTS_ID); }, Moderate() { resolve_player_with_most_hero_points(data_1.MODERATES_ID); }, }; states.move_attacks = { inactive: 'move attacks', prompt() { view.prompt = 'Choose a Front'; }, }; states.move_track = { inactive: 'move a Track', prompt() { const node = get_active_node(); const track = node.a.t; const value = node.a.v; const name = track === data_1.LIBERTY_OR_COLLECTIVIZATION ? 'Liberty OR Collectivization' : tracks[track].name; view.prompt = `Move ${name} ${value > 0 ? 'up' : 'down'}`; if (track === data_1.GOVERNMENT && value === data_1.TOWARDS_CENTER) { view.prompt = `Move ${name} towards center`; } else if (track === data_1.GOVERNMENT && value === data_1.AWAY_FROM_CENTER) { view.prompt = `Move ${name} away from center`; } if (track === data_1.LIBERTY_OR_COLLECTIVIZATION) { gen_action_standee(data_1.LIBERTY); gen_action_standee(data_1.COLLECTIVIZATION); } else { gen_action_standee(track); } }, standee(s) { const node = get_active_node(); let value = node.a.v; if (s === data_1.GOVERNMENT && (value === data_1.TOWARDS_CENTER || value === data_1.AWAY_FROM_CENTER)) { const direction = get_government_track_direction(value); value = direction; } move_track(s, value); resolve_active_and_proceed(); }, }; states.move_track_up_or_down = { inactive: 'move a track', prompt() { const node = get_active_node(); view.prompt = `Move ${get_track_name(node.a.track_id)} up or down`; gen_action('up'); gen_action('down'); }, down() { const node = get_active_node(); move_track(node.a.track_id, -1 * node.a.strength); resolve_active_and_proceed(); }, up() { const node = get_active_node(); move_track(node.a.track_id, node.a.strength); resolve_active_and_proceed(); }, }; states.peek_fascist_cards = { inactive: 'peek at Fascist cards', prompt() { view.prompt = 'Choose one card to return to the top of the deck'; for (const c of game.selectable_cards) { gen_action_card(c); } }, card(c) { game.top_of_events_deck = c; for (const ec of game.selectable_cards) { if (ec !== c) { game.discard.f.push(ec); } } game.selectable_cards = []; resolve_active_and_proceed(); }, }; states.player_turn = { inactive: 'play their turn', prompt() { const faction_id = get_faction_id(game.active); const can_spend_hp = game.faction_turn === faction_id && game.hero_points[faction_id] > 0; const can_play_card = game.selected_cards[faction_id].length > 0; view.prompt = 'Play a card or spend Hero points'; if (!(can_play_card || can_spend_hp)) { view.prompt = 'End turn'; } else if (!can_play_card && can_spend_hp) { view.prompt = 'Spend Hero Points or end turn'; } else if (can_play_card && !can_spend_hp) { view.prompt = 'Play a card'; } if (can_play_card) { gen_action('play_for_ap'); gen_action('play_for_event'); } else { gen_action('done'); } if (can_spend_hp) { gen_action('spend_hp'); } }, done() { game.faction_turn = null; game.played_card = null; resolve_active_and_proceed(true); }, play_for_ap() { const faction = get_active_faction(); const { strength } = play_card(faction, 'play_for_ap'); insert_before_active_node(create_seq_node([ create_leaf_node('choose_area_ap', faction, { strength, }), create_function_node('check_activate_icon'), ])); next(); }, play_for_event() { const faction = get_active_faction(); const { effects } = play_card(faction, 'play_for_ap'); insert_before_active_node(create_effects_node(effects)); next(); }, spend_hp() { insert_before_active_node(create_leaf_node('spend_hero_points', get_active_faction())); log('Spends Hero Points'); next(); }, }; states.remove_blank_marker = { inactive: 'remove a Blank marker', prompt() { view.prompt = 'Remove a Blank marker'; for (const b of game.triggered_track_effects) { gen_action_blank_marker(b); } }, blank_marker(b) { const faction = get_active_faction(); pay_hero_points(faction, 1); const track_id = Math.floor(b / 11); const space_id = b % 11; logi(`${faction_player_map[faction]} removes a Blank marker from space ${space_id} of ${get_track_name(track_id)}`); game.triggered_track_effects = game.triggered_track_effects.filter((id) => id !== b); game.used_medallions.push(data_1.ARCHIVES_MEDALLION_ID); resolve_active_and_proceed(); }, }; states.remove_attack_from_fronts = { inactive: 'remove attacks', prompt() { view.prompt = 'Choose a front to remove an attack'; }, }; states.return_card = { inactive: 'return a card', prompt() { view.prompt = 'Choose a card to return'; }, }; states.spend_hero_points = { inactive: 'spend Hero points', prompt() { view.prompt = 'Spend your Hero points'; gen_action('done'); const faction = get_active_faction(); const hero_points = game.hero_points[get_active_faction()]; if (hero_points === 0) { return; } gen_action('draw_card'); if (can_use_medallion(data_1.ARCHIVES_MEDALLION_ID, faction)) { gen_action('remove_blank_marker'); } if (can_use_medallion(data_1.VOLUNTEERS_MEDALLION_ID, faction)) { gen_action('add_to_front'); } if (hero_points < 2) { return; } gen_action_standee(data_1.FOREIGN_AID); gen_action_standee(data_1.SOVIET_SUPPORT); for (const bonus of bonuses) { if (game.bonuses[bonus] === data_1.OFF) { gen_action_bonus(bonus); } } if (hero_points < 3) { return; } gen_action_standee(data_1.COLLECTIVIZATION); gen_action_standee(data_1.LIBERTY); if (hero_points < 4) { return; } gen_action_standee(data_1.GOVERNMENT); }, add_to_front() { const faction = get_active_faction(); pay_hero_points(faction, 1); insert_after_active_node(create_seq_node([ create_leaf_node('add_to_front', faction, { t: data_1.ANY, v: 1, }), create_leaf_node('spend_hero_points', faction), ])); resolve_active_and_proceed(); }, done() { resolve_active_and_proceed(); }, bonus(b) { update_bonus(b, data_1.ON); pay_hero_points(get_active_faction(), 2); }, draw_card() { const faction = get_active_faction(); pay_hero_points(faction, 1); draw_hand_cards(faction, 1); }, remove_blank_marker() { const faction = get_active_faction(); if (game.used_medallions) { game.used_medallions.push(data_1.ARCHIVES_MEDALLION_ID); } else { game.used_medallions = [data_1.ARCHIVES_MEDALLION_ID]; } insert_after_active_node(create_seq_node([ create_leaf_node('remove_blank_marker', faction), create_leaf_node('spend_hero_points', faction), ])); resolve_active_and_proceed(); }, standee(track_id) { let amount = 2; if (track_id === data_1.LIBERTY || track_id === data_1.COLLECTIVIZATION) { amount = 3; } else if (track_id === data_1.GOVERNMENT) { amount = 4; } const faction = get_active_faction(); pay_hero_points(faction, amount); insert_after_active_node(create_seq_node([ create_leaf_node('move_track_up_or_down', faction, { track_id, strength: 1, }), create_leaf_node('spend_hero_points', faction), ])); resolve_active_and_proceed(); }, }; states.swap_card_tableau_hand = { inactive: 'swap cards', prompt() { view.prompt = 'Choose cards'; const faction = get_active_faction(); for (const c of game.hands[faction]) { gen_action_card(c); } }, }; function resolve_take_hero_points(faction) { const { v } = get_active_node_args(); const amount = Math.min(v, game.hero_points[faction]); lose_hero_point(faction, amount); gain_hero_points(get_active_faction(), amount); resolve_active_and_proceed(); } states.take_hero_points = { inactive: 'take Hero Points', prompt() { const { v } = get_active_node_args(); view.prompt = v === 1 ? 'Choose a player to take a Hero Point from' : `Choose a player to take ${v} Hero Points from`; const active_faction = get_active_faction(); for (const faction of role_ids) { if (faction !== active_faction) { gen_action(faction_player_map[faction]); } } }, Anarchist() { resolve_take_hero_points(data_1.ANARCHISTS_ID); }, Communist() { resolve_take_hero_points(data_1.COMMUNISTS_ID); }, Moderate() { resolve_take_hero_points(data_1.MODERATES_ID); }, }; states.use_organization_medallion = { inactive: 'use Organization Medallion', prompt() { view.prompt = 'Use Organization Medallion?'; gen_action('yes'); gen_action('no'); }, yes() { const faction = get_active_faction(); pay_hero_points(faction, 1); game.used_medallions.push(data_1.ORGANIZATION_MEDALLION_ID); let { t, v } = get_active_node_args(); if (v > 0) { v++; } else { v--; } move_track(t, v); resolve_active_and_proceed(); }, no() { const { t, v } = get_active_node_args(); move_track(t, v); resolve_active_and_proceed(); }, }; states.use_strategy_medallion = { inactive: 'use Strategy Medallion', prompt() { view.prompt = 'Use Strategy Medallion?'; gen_action('yes'); gen_action('no'); }, yes() { game.used_medallions.push(data_1.STRATEGY_MEDALLION_ID); const { f } = get_active_node_args(); const faction = get_active_faction(); update_front(f, 1, faction); resolve_active_and_proceed(); }, no() { resolve_active_and_proceed(); }, }; function card1_event2() { const value = game.tracks[data_1.FOREIGN_AID] >= 6 ? 3 : 2; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.FOREIGN_AID, value))); resolve_active_and_proceed(); } function card3_event2() { const value = game.tracks[data_1.FOREIGN_AID] >= 8 ? 2 : 1; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.GOVERNMENT, value))); resolve_active_and_proceed(); } function card10_event2() { if (game.tracks[data_1.FOREIGN_AID] >= 6) { resolve_effect((0, data_1.create_effect)('draw_card', data_1.SELF, 2)); } resolve_active_and_proceed(); } function card16_event2() { const value = game.tracks[data_1.GOVERNMENT] >= 6 ? 4 : 3; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.FOREIGN_AID, value))); resolve_active_and_proceed(); } function card17_event3() { const value = game.tracks[data_1.GOVERNMENT] >= 6 ? -4 : -3; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.COLLECTIVIZATION, value))); resolve_active_and_proceed(); } function card20_event3() { const value = game.tracks[data_1.SOVIET_SUPPORT] >= 6 ? 2 : 1; insert_after_active_node(create_seq_node([ resolve_effect((0, data_1.create_effect)('front', data_1.MADRID, value)), resolve_effect((0, data_1.create_effect)('front', data_1.SOUTHERN, value)), ])); resolve_active_and_proceed(); } function card22_event3() { const value = game.tracks[data_1.SOVIET_SUPPORT] >= 8 ? -3 : -3; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.GOVERNMENT, value))); resolve_active_and_proceed(); } function card23_event1() { const value = game.tracks[data_1.SOVIET_SUPPORT] >= 6 ? 4 : 3; insert_after_active_node(resolve_effect((0, data_1.create_effect)('front', data_1.ANY, value))); resolve_active_and_proceed(); } function card26_event1() { game.active_abilities.push(data_1.COMMUNIST_EXTRA_HERO_POINT); } function card29_event2() { const value = game.tracks[data_1.GOVERNMENT] <= 5 ? -3 : -2; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.LIBERTY, value))); resolve_active_and_proceed(); } function card35_event2() { const value = game.tracks[data_1.GOVERNMENT] <= 5 ? 2 : 1; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.SOVIET_SUPPORT, value))); resolve_active_and_proceed(); } function card42_event3() { game.active_abilities.push(data_1.ANARCHIST_EXTRA_HERO_POINT); } function card45_event2() { if (game.tracks[data_1.LIBERTY] >= 6) { insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.COLLECTIVIZATION, 1))); } resolve_active_and_proceed(); } function card46_event3() { for (let i = 0; i < 3; ++i) { game.selectable_cards.push(draw_fascist_card()); } resolve_active_and_proceed(); } function card50_event2() { const value = game.tracks[data_1.COLLECTIVIZATION] >= 8 ? 3 : 2; insert_after_active_node(resolve_effect((0, data_1.create_effect)('front', data_1.ARAGON, value))); resolve_active_and_proceed(); } function card53_event2() { const value = game.tracks[data_1.LIBERTY] >= 8 ? 3 : 2; insert_after_active_node(resolve_effect((0, data_1.create_effect)('front', data_1.ANY, value))); resolve_active_and_proceed(); } function card54_event1() { const value = game.tracks[data_1.COLLECTIVIZATION] >= 8 ? 3 : 2; insert_after_active_node(resolve_effect((0, data_1.create_effect)('track', data_1.LIBERTY, value))); resolve_active_and_proceed(); } function add_glory(faction, amount, indent = false) { for (let i = 0; i < amount; ++i) { game.bag_of_glory.push(get_active_faction()); } let text = amount === 1 ? `${faction_player_map[faction]} adds 1 token to the Bag of Glory` : `${faction_player_map[faction]} adds ${amount} tokens to the Bag of Glory`; if (indent) { logi(text); } else { log_h3(text); } } function check_activate_icon() { if (game.bonuses[data_1.MORALE_BONUS] === data_1.ON) { insert_after_active_node(create_leaf_node('activate_icon', get_active_faction_id())); } resolve_active_and_proceed(); } function check_initiative() { let initiative; if (game.tracks[data_1.LIBERTY] >= 6 && game.tracks[data_1.COLLECTIVIZATION] >= 6) { initiative = data_1.ANARCHISTS_ID; } else if (game.tracks[data_1.GOVERNMENT] <= 5) { initiative = data_1.COMMUNISTS_ID; } else { initiative = data_1.MODERATES_ID; } if (game.initiative === initiative) { return; } game.initiative = initiative; logi(`${faction_player_map[initiative]} claims the Initiative`); } function war_is_won() { let won_fronts = 0; for (const f of data_1.FRONTS) { if (game.fronts[f].value >= 1) { won_fronts++; } } return won_fronts >= 3; } function determine_winner() { const glory = { [data_1.ANARCHISTS_ID]: 0, [data_1.COMMUNISTS_ID]: 0, [data_1.MODERATES_ID]: 0, }; for (const g of game.glory) { glory[g]++; } let highest_glory = 0; let winners = []; for (let f of role_ids) { if (glory[f] === highest_glory) { winners.push(f); } else if (glory[f] > highest_glory) { highest_glory = glory[f]; winners = [f]; } } if (winners.length === 1) { win_game(faction_player_map[winners[0]], highest_glory); } else { insert_after_active_node(create_leaf_node('break_tie_winner', game.initiative, { winners, glory: highest_glory, })); } resolve_active_and_proceed(); } function end_of_turn() { Object.keys(game.fronts).forEach((front_id) => { game.fronts[front_id].contributions = []; }); game.active_abilities = []; game.used_medallions = []; if (game.turn === 4) { end_of_year(); } else { game.turn++; start_turn(); } } function end_of_year() { if (game.year === 3) { log_h1('End of the game'); const is_won = war_is_won(); if (is_won) { log('The war is won!'); } else { game_over('None', 'The war is lost. All Players lose the game!'); return; } } const glory_to_draw = [0, 1, 2, 5]; const glory_this_year = { a: false, c: false, m: false, }; for (let i = 0; i < glory_to_draw[game.year]; ++i) { const index = random(game.bag_of_glory.length); const faction = game.bag_of_glory[index]; game.glory.push(faction); glory_this_year[faction] = true; array_remove(game.bag_of_glory, index); } if (game.year === 3) { determine_winner(); return; } const players_to_gain_hero_points = role_ids.filter((f) => !glory_this_year[f]); gain_hero_points_in_player_order(players_to_gain_hero_points, game.year); game.engine = get_player_order().map((f) => create_leaf_node('end_of_year_discard', f)); game.engine.push(create_function_node('start_year')); game.top_of_events_deck = null; next(); } function gain_hero_points_in_player_order(factions, value) { for (const f of get_player_order()) { if (factions.includes(f)) { gain_hero_points(f, value); } } } function gain_hero_points(faction_id, value, skip_abilities = false) { if (game.hero_points.pool === 0) { return; } if (!skip_abilities && ((faction_id === data_1.ANARCHISTS_ID && (game.active_abilities || []).includes(data_1.ANARCHIST_EXTRA_HERO_POINT)) || (faction_id === data_1.COMMUNISTS_ID && (game.active_abilities || []).includes(data_1.COMMUNIST_EXTRA_HERO_POINT)))) { value++; } const gain = Math.min(game.hero_points.pool, value); game.hero_points.pool -= gain; game.hero_points[faction_id] += gain; logi(`${get_player(faction_id)} +${gain} ${gain === 1 ? 'Hero Point' : 'Hero Points'}`); } function game_over(result, victory) { insert_after_active_node(create_leaf_node('game_over', 'None')); game.result = result; game.victory = victory; log_br(); log(game.victory); } function get_hand_limit(faction) { let hand_limit = game.year; if (game.medallions[faction].includes(data_1.INTELLIGENCE_MEDALLION_ID)) { hand_limit++; } return hand_limit; } function play_card(faction, type) { const index = game.selected_cards[faction].length - 1; const card_id = game.selected_cards[faction][index]; const card = cards[card_id]; game.played_card = card_id; log_h3(`${game.active} plays ${card.title} for the ${type === 'play_for_event' ? 'Event' : 'Action Points'}`); array_remove(game.hands[faction], game.hands[faction].indexOf(card_id)); array_remove(game.selected_cards[faction], index); if (type === 'play_for_event') { game.trash[faction].push(card_id); } else { game.tableaus[faction].push(card_id); } return card; } function resolve_fascist_test() { log_h2('Fascist Test', 'fascist'); const test = get_current_event().test; const status = game.fronts[test.front].status; const test_passed = status === data_1.VICTORY || (status !== data_1.DEFEAT && game.fronts[test.front].value >= test.value); if (test_passed) { log('The Test is passed'); for (const faction of get_player_order()) { let hero_points_gain = game.fronts[test.front].contributions.includes(faction) ? 2 : 0; if (can_use_medallion(data_1.PROPAGANDA_MEDALLION_ID, faction)) { hero_points_gain += 2; } if (hero_points_gain > 0) { gain_hero_points(faction, hero_points_gain); } } } else { log('The Test is failed'); } const effect = test_passed ? test.pass : test.fail; const node = resolve_effect(effect); if (node !== null) { insert_after_active_node(node); } resolve_active_and_proceed(); } function resolve_final_bid() { let highest_bid = 0; let winners = []; for (const f of get_player_order()) { let player_bid = 0; for (const c of game.selected_cards[f]) { player_bid += cards[c].strength; } log(`${faction_player_map[f]} bids ${player_bid}`); if (player_bid === highest_bid) { winners.push(f); } else if (player_bid > highest_bid) { highest_bid = player_bid; winners = [f]; } game.hands[f] = game.hands[f].filter((c) => !game.selected_cards[f].includes(c)); game.discard[f].concat(game.selected_cards[f]); game.selected_cards[f] = []; } if (winners.length === 1) { win_final_bid(winners[0]); } else { insert_after_active_node(create_leaf_node('break_tie_final_bid', game.initiative, { winners })); } resolve_active_and_proceed(); } function get_fronts_to_add_to(target) { if (target === data_1.CLOSEST_TO_DEFEAT || target === data_1.CLOSEST_TO_VICTORY) { return get_fronts_closest_to(target); } else if (target === data_1.ANY) { return data_1.FRONTS.filter((id) => game.fronts[id].status === null); } else { return [target]; } } function get_max_value_for_track(track_id) { switch (track_id) { case data_1.LIBERTY: const max_lib = game.tracks[data_1.COLLECTIVIZATION] >= 8 ? 10 : 7; return Math.max(max_lib, game.tracks[data_1.LIBERTY]); case data_1.GOVERNMENT: const max_gov = game.tracks[data_1.FOREIGN_AID] >= 8 ? 10 : 7; return Math.max(max_gov, game.tracks[data_1.GOVERNMENT]); case data_1.COLLECTIVIZATION: case data_1.SOVIET_SUPPORT: case data_1.FOREIGN_AID: default: return 10; } } function get_min_value_for_track(track_id) { switch (track_id) { case data_1.GOVERNMENT: const min_gov = game.tracks[data_1.SOVIET_SUPPORT] >= 8 ? 4 : 1; return Math.min(min_gov, game.tracks[data_1.GOVERNMENT]); case data_1.LIBERTY: case data_1.COLLECTIVIZATION: case data_1.SOVIET_SUPPORT: case data_1.FOREIGN_AID: default: return 0; } } function get_government_track_direction(direction) { const value = game.tracks[data_1.GOVERNMENT]; if ((direction === data_1.TOWARDS_CENTER && value >= 6) || (direction === data_1.AWAY_FROM_CENTER && value <= 5)) { return -1; } else { return 1; } } function move_track(track_id, change) { const current_value = game.tracks[track_id]; let new_value = current_value + change; new_value = Math.max(new_value, get_min_value_for_track(track_id)); new_value = Math.min(new_value, get_max_value_for_track(track_id)); game.tracks[track_id] = new_value; logi(`${get_track_name(track_id)} to ${new_value}`); check_initiative(); const triggered_spaces = change > 0 ? make_list(current_value + 1, new_value).reverse() : make_list(new_value, current_value - 1); triggered_spaces.forEach((space_id) => { const trigger = tracks[track_id].triggers[space_id]; if (trigger !== null && !game.triggered_track_effects.includes(get_blank_marker_id(track_id, space_id))) { if (space_id !== 0) { game.triggered_track_effects.push(get_blank_marker_id(track_id, space_id)); } const node = resolve_effect(trigger); if (node !== null) { insert_after_active_node(node); } } }); } function pay_hero_points(faction, amount) { game.hero_points[faction] -= amount; game.hero_points.pool += amount; } function can_use_medallion(medallion_id, faction) { faction = faction === undefined ? get_active_faction() : faction; const can_use = game.medallions[faction].includes(medallion_id) && !game.used_medallions.includes(medallion_id); if (medallion_id === data_1.ORGANIZATION_MEDALLION_ID) { return can_use && game.hero_points[faction] > 0; } else { return can_use; } } function insert_use_organization_medallion_node(track_id, value) { const faction = get_active_faction(); insert_after_active_node(create_leaf_node('use_organization_medallion', faction, { t: track_id, v: value, })); } function update_bonus(bonus_id, status) { if (game.bonuses[bonus_id] === status) { return; } game.bonuses[bonus_id] = status; logi(`${bonus_names[bonus_id]} ${status === data_1.ON ? 'on' : 'off'}`); } function update_front(front_id, change, faction_id = null) { const player_token_on_front = faction_id !== null && game.fronts[front_id].contributions.includes(faction_id); if (game.bonuses[data_1.TEAMWORK_BONUS] === data_1.ON && change > 0 && faction_id !== null && !player_token_on_front && game.fronts[front_id].contributions.length > 0) { change += 1; } const value_before = game.fronts[front_id].value; game.fronts[front_id].value += change; logi(`${front_names[front_id]}: ${change > 0 ? '+' : ''}${change}`); if (faction_id !== null && value_before <= 0 && game.fronts[front_id].value > 0) { gain_hero_points(faction_id, 1); } if (faction_id !== null && !game.fronts[front_id].contributions.includes(faction_id)) { game.fronts[front_id].contributions.push(faction_id); } if (change > 0 && faction_id !== undefined && game.fronts[front_id].value < 10 && can_use_medallion(data_1.STRATEGY_MEDALLION_ID)) { insert_after_active_node(create_leaf_node('use_strategy_medallion', get_active_faction(), { f: front_id, })); } if (game.fronts[front_id].value >= 10) { victory_on_a_front(front_id); } else if (game.fronts[front_id].value <= -10) { defeat_on_a_front(front_id); } } function defeat_on_a_front(front_id) { game.fronts[front_id].status = data_1.DEFEAT; log('Defeat on ' + get_front_name(front_id)); if (front_id === 'm' || get_defeated_front_count() == 2) { game_over('None', 'All players lose the game!'); return; } insert_after_active_node(create_effects_node([ (0, data_1.create_effect)('bonus', data_1.MORALE_BONUS, data_1.OFF), (0, data_1.create_effect)('track', data_1.COLLECTIVIZATION, -1), (0, data_1.create_effect)('track', data_1.SOVIET_SUPPORT, -1), (0, data_1.create_effect)('track', data_1.FOREIGN_AID, -1), ])); } function victory_on_a_front(front_id) { game.fronts[front_id].status = data_1.VICTORY; log('Victory on ' + get_front_name(front_id)); gain_hero_points_in_player_order(game.fronts[front_id].contributions, 3); } function create_effects_node(effects) { const nodes = effects.reduce((accrued, current) => { const node = resolve_effect(current); if (node !== null) { accrued.push(node); } return accrued; }, []); return create_seq_node(nodes); } function get_faction_to_resolve_effect(effect) { if (!effect.faction) { return get_active_faction(); } if (effect.faction === data_1.INITIATIVE_PLAYER) { return game.initiative; } return effect.faction; } const effect_type_state_map = { add_card_to_tableau: 'add_card_to_tableau', attack: 'attack_front', bonus: 'change_bonus', front: 'add_to_front', medallion: 'choose_medallion', remove_blank_marker: 'remove_blank_marker', return_card: 'return_card', swap_card_tableau_hand: 'swap_card_tableau_hand', take_hero_points: 'take_hero_points', track: 'move_track', }; function resolve_effect(effect) { const args = { t: effect.target, v: effect.value, }; const faction = get_faction_to_resolve_effect(effect); if (effect.type === 'function') { return create_function_node(effect.target); } if (effect.type === 'state') { return create_leaf_node(effect.target, faction, { v: effect.value, }); } let state = effect_type_state_map[effect.type]; if (state !== undefined) { return create_leaf_node(state, faction, args); } const strategies = [ { condition: effect.type === 'hero_points' && effect.target === data_1.PLAYER_WITH_MOST_HERO_POINTS, resolve: () => { return create_leaf_node('select_player_with_most_hero_points', faction, args); }, }, { condition: effect.type === 'hero_points' && effect.target === data_1.ALL_PLAYERS, resolve: () => { return create_seq_node(get_player_order().map((faction) => create_leaf_node('gain_hero_points', faction, { v: effect.value, }))); }, }, { condition: effect.type === 'hero_points' && effect.target === data_1.SELF, resolve: () => { return create_leaf_node('gain_hero_points', faction, args); }, }, { condition: effect.type === 'hero_points' && role_ids.includes(effect.target), resolve: () => { return create_leaf_node('gain_hero_points', effect.target, args); }, }, { condition: effect.type === 'hero_points' && effect.target === data_1.INITIATIVE_PLAYER, resolve: () => { return create_leaf_node('gain_hero_points', game.initiative, args); }, }, { condition: effect.type === 'draw_card' && effect.target === data_1.SELF, resolve: () => { return create_leaf_node('draw_card', faction, args); }, }, { condition: effect.type === 'draw_card' && effect.target === data_1.INITIATIVE_PLAYER, resolve: () => { return create_leaf_node('draw_card', game.initiative, args); }, }, { condition: effect.type === 'draw_card' && role_ids.includes(effect.target), resolve: () => { return create_leaf_node('draw_card', effect.target, args); }, }, { condition: effect.type === 'draw_card' && effect.target === data_1.ALL_PLAYERS, resolve: () => { return create_seq_node(get_player_order(get_active_faction()).map((faction) => create_leaf_node('draw_card', faction, { v: effect.value, }))); }, }, { condition: effect.type === 'draw_card' && effect.target === data_1.OTHER_PLAYERS, resolve: () => { const leaf_nodes = get_player_order(get_active_faction()).map((faction) => create_leaf_node('draw_card', faction, { v: effect.value, })); array_remove(leaf_nodes, 0); return create_seq_node(leaf_nodes); }, }, { condition: effect.type === 'play_card', resolve: () => { return create_seq_node([ create_leaf_node('choose_card', faction), create_leaf_node('player_turn', faction), ]); }, }, ]; const strategy = strategies.find((strategy) => strategy.condition); if (strategy) { return strategy.resolve(); } else { console.log('----UNRESOLVED EFFECT----', effect); throw new Error('Unresolved effect'); } } function win_final_bid(faction_id) { log_br(); log(`${faction_player_map[faction_id]} wins the Final Bid`); game.glory.push(faction_id); } function win_game(player, glory) { game_over(player, `${player} wins the game with a total of ${glory} Glory!`); } function draw_card(deck) { clear_undo(); let i = random(deck.length); let c = deck[i]; set_delete(deck, c); return c; } function draw_fascist_card() { if (game.top_of_events_deck !== null) { const card_id = game.top_of_events_deck; game.top_of_events_deck = null; return card_id; } return draw_card(list_deck(data_1.FASCIST_ID)); } function lose_hero_point(faction, value) { const points_lost = Math.min(game.hero_points[faction], Math.abs(value)); game.hero_points.pool += points_lost; game.hero_points[faction] -= points_lost; if (points_lost === 0) { return; } if (points_lost === 1) { log(`${get_player(faction)}: -1 Hero Point`); } else { log(`${get_player(faction)}: -${points_lost} Hero Points`); } } function get_fronts_closest_to(target) { const values = Object.values(game.fronts).reduce((accrued, current) => { if (current.status === null) { accrued.push(current.value); } return accrued; }, []); const targetValue = target === 'd' ? Math.min(...values) : Math.max(...values); return Object.keys(game.fronts).filter((frontId) => game.fronts[frontId].value === targetValue); } function log_br() { if (game.log.length > 0 && game.log[game.log.length - 1] !== '') game.log.push(''); } function log(msg) { game.log.push(msg); } function logi(msg) { log('>' + msg); } function log_h1(msg) { log_br(); log('.h1 ' + msg); log_br(); } function log_h2(msg, player) { log_br(); log(`.h2${player ? `.${player}` : ''} ${msg}`); log_br(); } function log_h3(msg) { log_br(); log('.h3 ' + msg); } function get_active_faction() { return player_faction_map[game.active]; } function get_active_faction_id() { return player_faction_map[game.active]; } function get_blank_marker_id(track_id, space_id) { return track_id * 11 + space_id; } function get_faction_id(player) { return player_faction_map[player]; } function get_front_name(id) { return front_names[id]; } function get_current_event() { return cards[game.current_events[game.current_events.length - 1]]; } function get_defeated_front_count() { let count = 0; for (const front_id of data_1.FRONTS) { if (game.fronts[front_id].status === data_1.DEFEAT) { count++; } } return count; } function get_icon_count_in_tableau(icon, faction = get_active_faction_id()) { let count = 0; for (const c of game.tableaus[faction]) { const card = cards[c]; for (const i of card.icons) { if (i === icon) { ++count; } } } return count; } function get_player(faction_id) { return faction_player_map[faction_id]; } function get_player_order(first_player = game.initiative) { const order = []; let faction = first_player; for (let i = 0; i < 3; ++i) { order.push(faction); faction = game.year === 2 ? get_previous_faction(faction) : get_next_faction(faction); } return order; } function get_previous_faction(faction_id) { const index = role_ids.indexOf(faction_id); if (index === 0) { return role_ids[2]; } return role_ids[index - 1]; } function get_next_faction(faction_id) { const index = role_ids.indexOf(faction_id); if (index === 2) { return role_ids[0]; } return role_ids[index + 1]; } function get_factions_with_most_hero_poins() { let most_hero_points = null; let faction_ids = []; Object.entries(game.hero_points).forEach(([id, value]) => { if (id === 'pool') { return; } if (most_hero_points === null || value > most_hero_points) { most_hero_points = value; faction_ids = [id]; } else if (most_hero_points === value) { faction_ids.push(id); } }); return faction_ids; } function get_track_name(track_id) { return tracks[track_id].name; } function make_list(first, last) { let list = []; for (let i = first; i <= last; i++) list.push(i); return list; } function list_deck(id) { const deck = []; const card_list = id === data_1.FASCIST_ID ? fascist_decks[game.year] : faction_cards[id]; card_list.forEach((card) => { if (game.discard[id].includes(card) || game.selectable_cards.includes(card)) { return; } if (id === data_1.FASCIST_ID && game.current_events.includes(card)) { return; } else if (id !== data_1.FASCIST_ID && (game.hands[id].includes(card) || game.discard[id].includes(card) || game.tableaus[id].includes(card) || game.trash[id].includes(card))) { return; } deck.push(card); }); return deck; } function draw_medallions() { const medallion_ids = make_list(0, 8); for (let m = 0; m < 5; ++m) { let i = random(medallion_ids.length); let r = medallion_ids[i]; set_delete(medallion_ids, r); game.medallions.pool.push(r); } } function clear_undo() { if (game.undo) { game.undo.length = 0; } } function push_undo() { if (game.undo) { let copy = {}; for (let k in game) { let v = game[k]; if (k === 'undo') continue; else if (k === 'log') v = v.length; else if (typeof v === 'object' && v !== null) v = object_copy(v); copy[k] = v; } game.undo.push(copy); } return game.undo; } function pop_undo() { if (game.undo) { let save_log = game.log; let save_undo = game.undo; game = save_undo.pop(); save_log.length = game.log; game.log = save_log; game.undo = save_undo; } next(); } function random(range) { return (game.seed = (game.seed * 200105) % 34359738337) % range; } function object_copy(original) { if (Array.isArray(original)) { let n = original.length; let copy = new Array(n); for (let i = 0; i < n; ++i) { let v = original[i]; if (typeof v === 'object' && v !== null) copy[i] = object_copy(v); else copy[i] = v; } return copy; } else { let copy = {}; for (let i in original) { let v = original[i]; if (typeof v === 'object' && v !== null) copy[i] = object_copy(v); else copy[i] = v; } return copy; } } function array_remove(array, index) { let n = array.length; for (let i = index + 1; i < n; ++i) array[i - 1] = array[i]; array.length = n - 1; } function array_insert(array, index, item) { for (let i = array.length; i > index; --i) array[i] = array[i - 1]; array[index] = item; } function set_delete(set, item) { let a = 0; let b = set.length - 1; while (a <= b) { const m = (a + b) >> 1; const x = set[m]; if (item < x) b = m - 1; else if (item > x) a = m + 1; else return array_remove(set, m); } return set; }