From 9dd700c33bea438152e9a232189d88947aaf3523 Mon Sep 17 00:00:00 2001 From: Mischa Untaga <99098079+MischaU8@users.noreply.github.com> Date: Thu, 23 Nov 2023 10:34:48 +0100 Subject: combine campaigners --- play.js | 43 +++++++++++-------------------------------- rules.js | 60 +++++++++++++++++++----------------------------------------- 2 files changed, 30 insertions(+), 73 deletions(-) diff --git a/play.js b/play.js index e499787..0d45669 100644 --- a/play.js +++ b/play.js @@ -40,8 +40,7 @@ let ui = { document.getElementById("role_Opposition"), ], pieces: document.getElementById("pieces"), - support_campaigner: [], - opposition_campaigner: [], + campaigners: [], cubes: [], green_checks: [], red_xs: [], @@ -342,7 +341,6 @@ function on_click_cube(evt) { function on_click_green_check(evt) { if (evt.button === 0) { - console.log("green_check", evt.target.my_us_state) if (send_action('green_check', evt.target.my_us_state)) evt.stopPropagation() } @@ -351,7 +349,6 @@ function on_click_green_check(evt) { function on_click_red_x(evt) { if (evt.button === 0) { - console.log("red_x", evt.target.my_us_state) if (send_action('red_x', evt.target.my_us_state)) evt.stopPropagation() } @@ -421,13 +418,11 @@ function build_user_interface() { elt.addEventListener("mouseleave", on_blur) } - ui.support_campaigner = [ + ui.campaigners = [ create_campaigner('purple1', 1), create_campaigner('purple2', 2), create_campaigner('yellow1', 3), - create_campaigner('yellow2', 4) - ] - ui.opposition_campaigner = [ + create_campaigner('yellow2', 4), create_campaigner('red1', 5), create_campaigner('red2', 6), ] @@ -658,32 +653,17 @@ function on_update() { // eslint-disable-line no-unused-vars ui.us_states[i].classList.toggle("action", is_us_state_action(i)) } - for (let i = 0; i < ui.support_campaigner.length; ++i) { - // TODO Cleanup - let campaigner_region = view.support_campaigner[i] - if (campaigner_region) { - ui.pieces.appendChild(ui.support_campaigner[i]) - let [x, y] = REGIONS_LAYOUT[campaigner_region] - ui.support_campaigner[i].style.left = x - 30 + (15 * i) + "px" - ui.support_campaigner[i].style.top = y - 40 + "px" - ui.support_campaigner[i].classList.toggle("action", is_campaigner_action(1 + i)) - ui.support_campaigner[i].classList.toggle("selected", 1 + i === view.selected_campaigner) - } else { - ui.support_campaigner[i].remove() - } - } - for (let i = 0; i < ui.opposition_campaigner.length; ++i) { - // TODO Cleanup - let campaigner_region = view.opposition_campaigner[i] + for (let i = 0; i < ui.campaigners.length; ++i) { + let campaigner_region = view.campaigners[i] if (campaigner_region) { - ui.pieces.appendChild(ui.opposition_campaigner[i]) + ui.pieces.appendChild(ui.campaigners[i]) let [x, y] = REGIONS_LAYOUT[campaigner_region] - ui.opposition_campaigner[i].style.left = x - 30 + (15 * i) + "px" - ui.opposition_campaigner[i].style.top = y - 10 + "px" - ui.opposition_campaigner[i].classList.toggle("action", is_campaigner_action(5 + i)) - ui.opposition_campaigner[i].classList.toggle("selected", 5 + i === view.selected_campaigner) + ui.campaigners[i].style.left = x - 30 + (15 * (i % 4)) + "px" + ui.campaigners[i].style.top = y - 40 + (30 * Math.floor(i / 4)) + "px" + ui.campaigners[i].classList.toggle("action", is_campaigner_action(1 + i)) + ui.campaigners[i].classList.toggle("selected", 1 + i === view.selected_campaigner) } else { - ui.opposition_campaigner[i].remove() + ui.campaigners[i].remove() } } @@ -695,7 +675,6 @@ function on_update() { // eslint-disable-line no-unused-vars // TODO Cleanup if (view.us_states[i]) { let state_cubes = [] - // console.log("US_STATE", i, purple_cubes(i), yellow_cubes(i), red_cubes(i), is_green_check(i), is_red_x(i)) for (let c = 0; c < purple_cubes(i); ++c) { e = ui.cubes[cube_idx++] // TODO track both state and color diff --git a/rules.js b/rules.js index c047902..7da6a2b 100644 --- a/rules.js +++ b/rules.js @@ -188,63 +188,44 @@ function us_state_region(s) { } function free_campaigner(campaigners, color) { - const start = color === YELLOW ? 2 : 0 + const start = color === YELLOW ? 2 : color === RED ? 4 : 0 const index = campaigners.indexOf(0, start) - return color !== YELLOW && index > 1 ? -1 : index + return index > start + 1 ? -1 : index } function add_campaigner(color, region) { - const campaigners = player_campaigners() - const index = free_campaigner(campaigners, color) + const index = free_campaigner(game.campaigners, color) if (index !== -1) { - campaigners[index] = region + game.campaigners[index] = region } else { throw Error("No free campaigners") } log(`Placed ${COLOR_CODE[color]}R in R${region}`) } -// TODO unify campaigners from both players into one array - function player_campaigners() { if (game.active === SUF) { - return game.support_campaigner + return game.campaigners.slice(0, 4) } else { - return game.opposition_campaigner + return game.campaigners.slice(4) } } function for_each_player_campaigner(fn) { - if (game.active === SUF) { - for (let c = 0; c <= 3; c++) { - let r = game.support_campaigner[c] - if (r) - fn(c + 1) - } - } else { - for (let c = 0; c <= 1; c++) { - let r = game.opposition_campaigner[c] - if (r) - fn(c + 5) - } - } + let i = 1 + for (let r of player_campaigners()) + if (r) + fn(i) + i++ } function campaigner_region(c) { - if (game.active === SUF) { - return game.support_campaigner[c - 1] - } else { - return game.opposition_campaigner[c - 5] - } + return game.campaigners[c - 1] } function move_campaigner(c, region) { log(`${COLOR_CODE[campaigner_color(c)]}R moved to R${region}.`) - if (game.active === SUF) { - game.support_campaigner[c - 1] = region - } else { - game.opposition_campaigner[c - 5] = region - } + game.campaigners[c - 1] = region } // RED cubes (6 bits), YELLOW cubes (7 bits), PURPLE cubes (7 bits), RED_X (1 bit), GREEN_CHECK (1 bit), @@ -518,6 +499,7 @@ exports.setup = function (seed, _scenario, _options) { congress: 0, us_states: new Array(us_states_count + 1).fill(0), nineteenth_amendment: 0, + campaigners: [0, 0, 0, 0, 0, 0], // purple, purple, yellow, yellow, red, red strategy_deck: [], strategy_draw: [], @@ -531,14 +513,12 @@ exports.setup = function (seed, _scenario, _options) { support_discard: [], support_hand: [], support_claimed: [], - support_campaigner: [0, 0, 0, 0], // purple, purple, yellow, yellow support_buttons: 0, opposition_deck: [], opposition_discard: [], opposition_hand: [], opposition_claimed: [], - opposition_campaigner: [0, 0], opposition_buttons: 0, out_of_play: [] @@ -617,6 +597,7 @@ exports.VIEW_SCHEMA = { congress: {type: "integer", minimum: 0, maximum: 6}, us_states: {type: "array", minItems: us_states_count + 1, maxItems: us_states_count + 1, items: {type: "integer", minimum: 0}}, nineteenth_amendment: {type: "integer", minimum: 0, maximum: 1}, + campaigners: {type: "array", minItems: 6, maxItems: 6, items: {type: "integer", minimum: 0, maximum: region_count}}, strategy_deck: {type: "integer", minimum: 0, maximum: 12}, strategy_draw: {type: "array", maxItems: 3, items: {type: "integer", minimum: 1, maximum: 128}}, @@ -630,14 +611,12 @@ exports.VIEW_SCHEMA = { support_discard: {type: "array", items: {type: "integer", minimum: 1, maximum: 128}}, support_hand: {type: "integer", minimum: 1, maximum: 7}, support_claimed: {type: "array", items: {type: "integer", minimum: 1, maximum: 128}}, - support_campaigner: {type: "array", minItems: 4, maxItems: 4, items: {type: "integer", minimum: 0, maximum: region_count}}, support_buttons: {type: "integer", minimum: 0, maximum: MAX_SUPPORT_BUTTONS}, opposition_deck: {type: "integer", minimum: 0, maximum: 52}, opposition_discard: {type: "array", items: {type: "integer", minimum: 1, maximum: 128}}, opposition_hand: {type: "integer", minimum: 1, maximum: 7}, opposition_claimed: {type: "array", items: {type: "integer", minimum: 1, maximum: 128}}, - opposition_campaigner: {type: "array", minItems: 2, maxItems: 2, items: {type: "integer", minimum: 0, maximum: region_count}}, opposition_buttons: {type: "integer", minimum: 0, maximum: MAX_OPPOSITION_BUTTONS}, out_of_play: {type: "array", items: {type: "integer", minimum: 1, maximum: 128}}, @@ -646,10 +625,10 @@ exports.VIEW_SCHEMA = { }, required: [ - "log", "active", "prompt", "turn", "congress", "us_states", + "log", "active", "prompt", "turn", "congress", "us_states", "campaigners", "strategy_deck", "strategy_draw", "states_draw", - "support_deck", "support_discard", "support_hand", "support_claimed", "support_campaigner", "support_buttons", - "opposition_deck", "opposition_discard", "opposition_hand", "opposition_claimed", "opposition_campaigner", "opposition_buttons", + "support_deck", "support_discard", "support_hand", "support_claimed", "support_buttons", + "opposition_deck", "opposition_discard", "opposition_hand", "opposition_claimed", "opposition_buttons", ], additionalProperties: false @@ -673,6 +652,7 @@ exports.view = function(state, player) { congress: game.congress, us_states: game.us_states, nineteenth_amendment: game.nineteenth_amendment, + campaigners: game.campaigners, strategy_deck: game.strategy_deck.length, strategy_draw: game.strategy_draw, @@ -686,14 +666,12 @@ exports.view = function(state, player) { support_discard: game.support_discard, // top_discard? support_hand: game.support_hand.length, support_claimed: game.support_claimed, - support_campaigner: game.support_campaigner, support_buttons: game.support_buttons, opposition_deck: game.opposition_deck.length, opposition_discard: game.opposition_discard, // top_discard? opposition_hand: game.opposition_hand.length, opposition_claimed: game.opposition_claimed, - opposition_campaigner: game.opposition_campaigner, opposition_buttons: game.opposition_buttons, out_of_play: game.out_of_play, -- cgit v1.2.3