From 9643ad342f7ea0e877c64be7952cddee6fb7cb12 Mon Sep 17 00:00:00 2001 From: iainp5 Date: Mon, 28 Oct 2024 23:50:59 +0000 Subject: Tidy up code --- rules.js | 237 +++++++++++++++++++-------------------------------------------- 1 file changed, 70 insertions(+), 167 deletions(-) (limited to 'rules.js') diff --git a/rules.js b/rules.js index 55f064c..f77eb6e 100644 --- a/rules.js +++ b/rules.js @@ -1945,9 +1945,7 @@ states.general_strike = { clear_undo() let roll = roll_d6() log(`Roll: D${roll}`) - logi(`+${game.available_ops} ops`) - let total = roll + game.available_ops log(`Total: ${total}`) @@ -1958,7 +1956,6 @@ states.general_strike = { } else { log('The strike continues. Required 6 or more') } - game.state = 'end_round' }, } @@ -2059,7 +2056,7 @@ states.new_years_eve_party = { } else if (game.vp < 0) { goto_game_over(COM, `New Year's Eve Party: ${COM} wins on Victory Point Track!`) } else if (game.vp === 0) { - goto_game_over('', `CNew Year's Eve Party: The game is tied!`) /*Not sure what to pass for result */ + goto_game_over('', `New Year's Eve Party: The game is tied!`) /*Not sure what to pass for result */ } } } @@ -2067,15 +2064,14 @@ states.new_years_eve_party = { states.stasi_end_round = { inactive: 'choose next card due to Stasi.', prompt() { - let available_cards = game.democrat_hand - if (available_cards.length === 0) { + if (game.democrat_hand.length === 0) { view.prompt = 'Stasi: no cards remaining.' gen_action('pass') return } view.prompt = 'Stasi: you must select your next card to play.' - for (let card of available_cards) { + for (let card of game.democrat_hand) { gen_action_card(card) } }, @@ -2090,14 +2086,6 @@ states.stasi_end_round = { game.stasi_card = 0 end_stasi_choose_card() }, - end_round() { - push_undo() - if (game.stasi_card === C_COMMON_EUROPEAN_HOME) { - game.state = 'stasi_confirm' - } else { - end_stasi_choose_card() - } - } } states.stasi_finish = { @@ -2119,10 +2107,8 @@ states.stasi_finish = { states.stasi_confirm = { inactive: 'choose next card due to Stasi.', prompt() { - if (game.stasi_card === C_COMMON_EUROPEAN_HOME ) { - view.prompt = `If Common European Home selected, it must be played for Operations. Otherwise select the opponent's card instead.` - gen_action('done') - } + view.prompt = `If Common European Home selected, it must be played for Operations. Otherwise select the opponent's card instead.` + gen_action('done') }, done() { game.playable_cards = game.playable_cards.filter( n => n !== C_COMMON_EUROPEAN_HOME) @@ -2138,10 +2124,7 @@ states.stasi_play_card = { gen_action('pass') } else { view.prompt = `Stasi: you must play ${clean_name(cards[game.stasi_card].name)}.` - let available_cards = [game.stasi_card] - for (let card of available_cards) { - gen_action_card(card) - } + gen_action_card(game.stasi_card) } }, card(card) { @@ -2157,7 +2140,6 @@ states.stasi_play_card = { } else { game.state = 'play_card' } - }, pass () { log('No cards remaining. Passed') @@ -2208,14 +2190,14 @@ function add_infl(space) { } // Check Genscher - if (game.persistent_events.includes(63) && game.active === DEM && spaces[space].country === 'East_Germany' && check_com_control(space)) { + if (game.persistent_events.includes(C_GENSCHER) && game.active === DEM && spaces[space].country === 'East_Germany' && check_com_control(space)) { game.available_ops-- - log_summary(`(-1 op due to C63)`) + log_summary(`(-1 op due to C${C_GENSCHER})`) } else if (check_opp_control(space)) { game.available_ops -= 2 //Check if Austria Hungary Border Reopened was used to place last SP in a controlled space in East Germany. If so, game.available_op will be negative if (game.available_ops < 0) { - log_summary(`(Used +1 op from C58)`) + log_summary(`(Used +1 op from C${C_AUSTRIA_HUNGARY_BORDER_REOPENED})`) } } else { game.available_ops-- @@ -2332,7 +2314,6 @@ function do_sc(space) { } else { let card_ops = get_card_ops(this_card()) - roll += card_ops logi(`+${card_ops} from card ops`) } @@ -2343,37 +2324,31 @@ function do_sc(space) { } // Events which modify SC rolls - //Tear Gas - if (game.active === COM && game.persistent_events.includes(30) && spaces[space].socio === 6) { + if (game.active === COM && game.persistent_events.includes(C_TEAR_GAS) && spaces[space].socio === 6) { roll ++ - logi('+1 from C30') - permanently_remove(30) - game.persistent_events = game.persistent_events.filter(n => n !== 30) + logi(`+1 from C${C_TEAR_GAS}`) + permanently_remove(C_TEAR_GAS) + game.persistent_events = game.persistent_events.filter(n => n !== C_TEAR_GAS) } - //FRG Embassies - if (game.active === DEM && spaces[space].region === 'Eastern Europe' && game.persistent_events.includes(74)) { + if (game.active === DEM && spaces[space].region === 'Eastern Europe' && game.persistent_events.includes(C_FRG_EMBASSIES)) { roll++ - logi('+1 from C74') + logi(`+1 from C${C_FRG_EMBASSIES}`) } - //GrenzTruppen - if (game.active === DEM && spaces[space].country === 'East_Germany' && game.persistent_events.includes(59)) { + if (game.active === DEM && spaces[space].country === 'East_Germany' && game.persistent_events.includes(C_GRENZTRUPPEN)) { roll-- - logi('-1 from C59') + logi(`-1 from C${C_GRENZTRUPPEN}`) } - //Stand Fast if ((game.active === COM && game.stand_fast === DEM && check_dem_control(space)) || (game.active === DEM && game.stand_fast === COM && check_com_control(space))){ roll-- - logi('-1 from C100') + logi(`-1 from C${C_STAND_FAST}`) } - //Elena - if (game.active === DEM && game.persistent_events.includes(101) && spaces[space].country === 'Romania') { + if (game.active === DEM && game.persistent_events.includes(C_ELENA) && spaces[space].country === 'Romania') { roll-- - logi('-1 from C101') + logi(`-1 from C${C_ELENA}`) } - //Austria Hungary Border Reopened - if (game.active === DEM && game.persistent_events.includes(58) && game.austria_hungary_border_reopened_tracker) { + if (game.active === DEM && game.persistent_events.includes(C_AUSTRIA_HUNGARY_BORDER_REOPENED) && game.austria_hungary_border_reopened_tracker) { roll++ - logi(`+1 from C58`) + logi(`+1 from C${C_AUSTRIA_HUNGARY_BORDER_REOPENED}`) } // Continue with logic - check for adjacency @@ -2381,14 +2356,14 @@ function do_sc(space) { // Events which affect adjacency - The Wall const adj = count_adj(space) - if (game.active === COM && game.persistent_events.includes(9) && spaces[space].country === 'East_Germany') { - logi('No adjacency for Democrats due to C9') - logi('C9 no longer in effect') + if (game.active === COM && game.persistent_events.includes(C_THE_WALL) && spaces[space].country === 'East_Germany') { + logi(`No adjacency for Democrats due to C${C_THE_WALL}`) + logi(`C${C_THE_WALL} no longer in effect`) roll += adj.com_adj if (adj.com_adj > 0) { logi(`+${adj.com_adj} adjacency`) } - game.persistent_events = game.persistent_events.filter(n => n !== 9) + game.persistent_events = game.persistent_events.filter(n => n !== C_THE_WALL) // Standard adjacency } else { @@ -2413,7 +2388,6 @@ function do_sc(space) { } } } - } // Support check calcs @@ -2466,11 +2440,9 @@ function do_sc(space) { } } game.selected_space = 0 - } function valid_spaces_setup() { - game.valid_spaces = [] let valid_spaces_set = new Set(); for (let i =0 ; i < spaces.length ; i++) { let space = spaces[i] @@ -2507,31 +2479,20 @@ function valid_spaces_sc() { } else { let infl = game.demInfl[i] if (infl !== 0 ) { - // Check Solidarity Legalised + // Check Events that block support checks in a given space if (game.persistent_events.includes(C_SOLIDARITY_LEGALIZED) && space.space_id === S_GDANSK) {continue} - - // Check Civic Forum if (game.persistent_events.includes(C_CIVIC_FORUM) && space.space_id === S_CHARLES_UNIVERSITY) {continue} - - // Check We Are the People if (game.persistent_events.includes(C_WE_ARE_THE_PEOPLE) && space.space_id === S_LEIPZIG) {continue} - - //Check for Foreign Currency Debt Burden - if (game.persistent_events.includes(C_FOREIGN_CURRENCY_DEBT_BURDEN) && space.country === game.foreign_currency_debt_burden) {continue} { + if (game.persistent_events.includes(C_FOREIGN_CURRENCY_DEBT_BURDEN) && space.country === game.foreign_currency_debt_burden) {continue} valid_spaces_set.add(space.space_id); - - } } } } - - - // Convert the set to an array before returning game.valid_spaces = Array.from(valid_spaces_set); //Check for the Crown Turns Against Ceausescu - if (game.is_pwr_struggle && game.pwr_struggle_in === 'Romania' && game.persistent_events.includes(54)) { + if (game.is_pwr_struggle && game.pwr_struggle_in === 'Romania' && game.persistent_events.includes(C_THE_CROWD_TURNS_AGAINST_CEAUSESCU)) { game.valid_spaces = game.valid_spaces.filter(n => spaces[n].country === 'Romania') } return game.valid_spaces; @@ -2553,42 +2514,33 @@ function valid_spaces_support_loss() { } } } - - // Convert the set to an array before returning game.valid_spaces = Array.from(valid_spaces_set); return game.valid_spaces; } function valid_spaces_infl() { - // Check if function is called from the VM or not, take relevant ops variable let ops = game.state.startsWith('vm') ? game.vm_available_ops : game.available_ops; let valid_spaces_set = new Set(); - // Iterate over all spaces to find the ones with the player's influence for (let i = 0; i < game.demInfl.length; i++) { - //piece = game.pieces[i] let space = spaces[i] - let player_influence = game.active === COM ? game.comInfl[i] : game.demInfl[i]; // If the piece has the player's influence, add it and its adjacent spaces to the set if (player_influence > 0) { valid_spaces_set.add(space.space_id); - - // Check adjacency information let adjacent_spaces = get_adjusted_adjacency(space.space_id) for (let adj_space_id of adjacent_spaces) { if (adj_space_id) { - const adj_piece = spaces[adj_space_id]; // Check if the adjacent space is controlled by the opponent const opponent_control = check_opp_control(adj_piece.space_id) //Check for Genscher. Can always place in East Germany even with 1 op - if (game.active === DEM && adj_piece.country === 'East_Germany' && game.persistent_events.includes(63)){ + if (game.active === DEM && adj_piece.country === 'East_Germany' && game.persistent_events.includes(C_GENSCHER)){ valid_spaces_set.add(adj_piece.space_id) } @@ -2600,7 +2552,6 @@ function valid_spaces_infl() { } } } - // Convert the set to an array before returning game.valid_spaces = Array.from(valid_spaces_set); return game.valid_spaces; } @@ -2610,14 +2561,15 @@ function valid_cards(player_hand, presence) { if (game.phase === 0) { for (let c of player_hand) { let card = power_cards[c] + // Never add tactics fails if (c === PC_TACTIC_FAILS) { continue } - // Never add tactics fails + // Cannot play the suit of Tactics Fails if (card.name === game.tactics_fails) { continue } - // Cannot play the suit of Tactics Fails + if (card.socio === 0) { valid_cards_set.add(c) } else if (leaders.includes(card.socio) && presence[card.socio]) { @@ -2627,7 +2579,6 @@ function valid_cards(player_hand, presence) { } else if (game.phase === 1) { for (let c of player_hand) { let card = power_cards[c] - console.log("card", card) if (card.name === power_cards[game.played_power_card].name) { valid_cards_set.add(c) } else if (card.name === game.proxy_power_card) { @@ -2647,7 +2598,6 @@ function do_valid_cards() { let presence = check_presence(game.pwr_struggle_in) if (game.active === DEM) { valid_cards(game.dem_pwr_hand, presence.dem_leaders) - } else { valid_cards(game.com_pwr_hand, presence.com_leaders)} } @@ -2883,11 +2833,9 @@ function battlegrounds(country) { } function take_power(country) { - log(`Democrat takes power in ${game.pwr_struggle_in}`) game.revolutions[find_country_index(country)] = true game.times_held[find_country_index(country)] = 1 - } function retain_power(country){ @@ -2910,7 +2858,6 @@ function score_country(country) { } else { value_control = 4 } -//Log for scoring let dem_vp = 0 let com_vp = 0 @@ -2944,7 +2891,6 @@ function score_country(country) { } else { log('No presence: 0 VP') } - } else if (presence.com_control || presence.com_domination) { log('Communist:') @@ -2972,7 +2918,6 @@ function score_country(country) { } else { log('No presence: 0 VP') } - } //Otherwise, presence and battlegrounds @@ -3007,7 +2952,6 @@ function score_country(country) { log('No presence: 0 VP') } } - //Calculate change VP let change_vp = dem_vp + com_vp @@ -3103,7 +3047,6 @@ function goto_game_over(result, victory) { log_h1("Game Over") log(game.victory) return - } function goto_struggle(){ @@ -3147,7 +3090,7 @@ function end_goddess() { } } -function check_reformer() { +function check_reformer() { /*Is this function still used?*/ if (game.dem_tst_position !== game.com_tst_position) { if (!game.playable_cards.includes(67)) { game.playable_cards.push(67) @@ -3155,7 +3098,6 @@ function check_reformer() { } else { game.playable_cards = game.playable_cards.filter(n => n !== 67) } - } function count_scoring_cards() { @@ -3193,23 +3135,18 @@ function select_card(card){ game.state = 'play_card' } -function find_event(card) { - return variable_events.indexOf(card) -} - function is_auto_resolve(card) { - let ceausecu_events = [10, 41, 101, 107] - if (card === 97) { - if (!game.persistent_events.includes(54)) { + if (card === C_THE_TYRANT_IS_GONE) { + if (!game.persistent_events.includes(C_THE_CROWD_TURNS_AGAINST_CEAUSESCU)) { return true } } - else if (ceausecu_events.includes(card) && game.persistent_events.includes(97)) { + else if (ceausecu_events.includes(card) && game.persistent_events.includes(C_THE_TYRANT_IS_GONE)) { return true } - else if (card === 87 && !game.persistent_events.includes(C_THE_WALL_MUST_GO)) { + else if (card === C_KOHL_PROPOSES_REUNIFICATION && !game.persistent_events.includes(C_THE_WALL_MUST_GO)) { return true } else if (auto_resolve_events.includes(card)) { @@ -3245,7 +3182,7 @@ function event_is_playable(card) { return false } //Check for The Chinese Solution - else if (game.com_tst_position === 8 && card === 96) { + else if (game.com_tst_position === 8 && card === C_THE_CHINESE_SOLUTION) { return true } //Then check normally @@ -3264,15 +3201,15 @@ function get_card_ops(card) { if (card) { ops = cards[card].ops } - if (game.persistent_events.includes(25) && game.active === COM) { + if (game.persistent_events.includes(C_PERESTROIKA) && game.active === COM) { if(game.state === 'choose_card' || game.state === 'stasi_play_card') { - log('+1 op from C25') + log(`+1 op from C${C_PERESTROIKA}`) } ops ++ } - if (game.persistent_events.includes(50) && game.active === DEM) { + if (game.persistent_events.includes(C_THE_SINATRA_DOCTRINE) && game.active === DEM) { if(game.state === 'choose_card' || game.state === 'stasi_play_card' || game.state === 'vm_laszlo_tokes') { - log('+1 op from C50') + log(`+1 op from C${C_THE_SINATRA_DOCTRINE}`) } ops ++ } @@ -3287,10 +3224,10 @@ function get_card_ops(card) { if ((game.active === DEM && game.prudence && game.prudence.DEM !== 0)) { if(game.state === 'choose_card' || game.state === 'stasi_play_card' || game.state === 'vm_laszlo_tokes') { if (ops > 2) { - log(`${pluralize(game.prudence.DEM,'op')} from C8`) + log(`${pluralize(game.prudence.DEM,'op')} from C${C_PRUDENCE}`) } else { if (ops > 1) { - log(`-1 op from C8`) + log(`-1 op from C${C_PRUDENCE}`) } } } @@ -3304,9 +3241,9 @@ function get_card_ops(card) { if (game.active === COM && game.prudence && game.prudence.COM < 0) { if(game.state === 'choose_card') { if (ops > 2) { - log(`${pluralize(game.prudence.COM,'op')} from C8`) + log(`${pluralize(game.prudence.COM,'op')} from C${C_PRUDENCE}`) } else if (ops > 1) { - log(`-1 op from C8`) + log(`-1 op from C${C_PRUDENCE}`) } } ops += game.prudence.COM @@ -3318,38 +3255,32 @@ function get_card_ops(card) { function get_tst_6_ops() { let ops = 0 - - if (game.persistent_events.includes(25) && game.active === COM) { - log('+1 op from C25') + if (game.persistent_events.includes(C_PERESTROIKA) && game.active === COM) { + log(`+1 op from C${C_PERESTROIKA}`) ops ++ } - if (game.persistent_events.includes(50) && game.active === DEM) { - log('+1 op from C50') + if (game.persistent_events.includes(C_THE_SINATRA_DOCTRINE) && game.active === DEM) { + log(`+1 op from C${C_THE_SINATRA_DOCTRINE}`) ops ++ } - if ((game.active === DEM && game.prudence && game.prudence.DEM !== 0)) { if (ops > 0) { - log(`${pluralize(game.prudence.DEM,'op')} from C8`) + log(`${pluralize(game.prudence.DEM,'op')} from C${C_PRUDENCE}`) } else { - log(`-1 op from C8`) - } - + log(`-1 op from C${C_PRUDENCE}`) + } ops += game.prudence.DEM - if (ops < -1) { ops = -1 } } if (game.active === COM && game.prudence && game.prudence.COM < 0) { - if (ops > 0) { - log(`${pluralize(game.prudence.COM,'op')} from C8`) + log(`${pluralize(game.prudence.COM,'op')} from C${C_PRUDENCE}`) } else { - log(`-1 op from C8`) + log(`-1 op from C${C_PRUDENCE}`) } - ops += game.prudence.COM if (ops < -1) { ops = -1 @@ -3388,11 +3319,9 @@ function finish_the_wall() { } } - // =========== MOVING THROUGH TURNS ============ function end_round() { - //Check if the game is over! if (game.state === 'game_over') { return} @@ -3420,7 +3349,6 @@ function end_round() { check_common_european_home() reset_austria_hungary_border_reopened() /*This should be redundant! */ - // Check for duplicate card entries let card_check if (game.samizdat_card > 0) { @@ -3503,7 +3431,7 @@ function end_round() { // Resolve end action round //Stasi check - if(game.round_player === COM && game.persistent_events.includes(13)) { + if(game.round_player === COM && game.persistent_events.includes(C_STASI)) { //If in Honecker, turn ends if (game.round === 8) { clear_undo() @@ -3614,12 +3542,9 @@ function new_turn() { log_h1("Turn " + game.turn) - if (game.turn === 4) { - add_midyear() - } - if (game.turn === 8) { - add_lateyear() - } + if (game.turn === 4) { add_midyear() } + if (game.turn === 8) { add_lateyear() } + if (game.turn > 1) { if (game.persistent_events.includes(C_PRESIDENTIAL_VISIT)) { game.com_hand_limit = 7 @@ -3631,24 +3556,18 @@ function new_turn() { } //Check if TST effects need to be resolved - if (game.dem_tst_position >=5 && game.com_tst_position <= 4) { + if ((game.dem_tst_position >=5 && game.com_tst_position <= 4) || (game.com_tst_position >= 5 && game.dem_tst_position <= 4)) { log_h2('Tiananmen Square Track award') - if(game.active !== DEM) { next_player() } - for (let card of game.democrat_hand) { + + if ((game.dem_tst_position >= 5 && game.com_tst_position <= 4 && game.active !== DEM) || (game.com_tst_position >= 5 && game.dem_tst_position <= 4 && game.active !== COM)) { + next_player() + } + let hand = game.dem_tst_position >=5 ? game.democrat_hand : game.communist_hand + for (let card of hand) { if (scoring_cards.includes(card)) continue game.valid_cards.push(card) } - game.state = 'tst_goddess' /* Goddess only name of Democrat bonus, not Communist*/ - return - } - else if (game.com_tst_position >=5 && game.dem_tst_position <= 4) { - log_h2('Tiananmen Square Track award') - if(game.active !== COM) { next_player() } - for (let card of game.communist_hand) { - if (scoring_cards.includes(card)) continue - game.valid_cards.push(card) - } - game.state = 'tst_goddess' + game.state = 'tst_goddess' } else { log_h2("Action Round " + game.round) log_side() @@ -3674,7 +3593,6 @@ function next_player() { game.active = COM else game.active = DEM - log_side() } @@ -3687,9 +3605,6 @@ function change_player() { } function random(range) { - // An MLCG using integer arithmetic with doubles. - // https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf - // m = 2**35 − 31 return (game.seed = game.seed * 200105 % 34359738337) % range } @@ -3697,7 +3612,6 @@ function roll_d6() { return random(6) + 1 } - function find_space_index(name_unique) { return spaces.findIndex(space => space && space.name_unique === name_unique) } @@ -3720,12 +3634,9 @@ function draw_cards(deck, democrat_hand, communist_hand, dem_hand_limit, com_han while (democrat_hand.length < dem_hand_limit || communist_hand.length < com_hand_limit) { if (deck.length === 0) { log_h3('--- Reshuffle ---') - deck.push(...game.strategy_discard) game.strategy_discard = [] - } - - else if (turn === 'communist' && communist_hand.length < com_hand_limit) { + } else if (turn === 'communist' && communist_hand.length < com_hand_limit) { communist_hand.push(draw_card(deck)); turn = 'democrat'; } else if(turn === 'communist' && communist_hand.length === com_hand_limit) { @@ -3739,14 +3650,12 @@ function draw_cards(deck, democrat_hand, communist_hand, dem_hand_limit, com_han turn = 'communist'; } } - clear_undo() } function draw_card(deck) { if (deck.length === 0) { log_h3('--- Reshuffle ---') - deck.push(...game.strategy_discard) game.strategy_discard = [] } @@ -3757,7 +3666,6 @@ function draw_card(deck) { function discard(card) { let find_card if (!game.is_pwr_struggle) { - if (game.active === COM) { find_card = game.communist_hand.indexOf(card) game.communist_hand.splice(find_card, 1) @@ -3777,7 +3685,6 @@ function discard(card) { find_card = game.dem_pwr_hand.indexOf(card); game.dem_pwr_hand.splice(find_card, 1); } - game.power_struggle_discard.push(card) } } @@ -3839,7 +3746,6 @@ function discard_from_table(card) { } } - function add_midyear() { for (let c = first_strategy_card; c <= last_strategy_card; ++c) if (cards[c].period === 2) @@ -3847,7 +3753,6 @@ function add_midyear() { log_h3('Mid-year cards added to draw deck') } - function add_lateyear() { for (let c = first_strategy_card; c <= last_strategy_card; ++c) if (cards[c].period === 3) @@ -3878,9 +3783,7 @@ function reset_power() { game.persistent_events = game.persistent_events.filter(n => n !== 62) } if (game.persistent_events.includes(54) && game.pwr_struggle_in === 'Romania'){ - permanently_remove(54) - - + permanently_remove(54) } if (game.pwr_struggle_in === 'Romania' && game.persistent_events.includes(C_SECURITATE)){ game.persistent_events = game.persistent_events.filter(n => n !== C_SECURITATE) -- cgit v1.2.3