diff options
author | Tor Andersson <tor@ccxvii.net> | 2021-06-20 12:37:26 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2022-11-16 19:19:38 +0100 |
commit | 9ecbee1517966c892c60ddc2310e03d57b0de413 (patch) | |
tree | ed5f1ff4c1ae68113291936b201c336c7a50441d | |
parent | 64186a7f490167a04e5dde149079091d9b2d9928 (diff) | |
download | crusader-rex-9ecbee1517966c892c60ddc2310e03d57b0de413.tar.gz |
crusader: Simplify block layout by sending all info to client.
Trust the user not to cheat...
-rw-r--r-- | play.html | 3 | ||||
-rw-r--r-- | rules.js | 38 | ||||
-rw-r--r-- | ui.js | 171 |
3 files changed, 40 insertions, 172 deletions
@@ -133,7 +133,8 @@ body.shift .block.known:hover { .map .block { position: absolute; z-index: 2; } .map .block.highlight { z-index: 3; } .map .block.selected { z-index: 4; } -.map .block.known:hover { z-index: 5; } +.map .block.known { z-index: 5; } +.map .block.known:hover { z-index: 6; } .block.highlight { cursor: pointer; box-shadow: 0px 0px 4px 1px white; } @@ -1719,9 +1719,10 @@ exports.view = function(state, current) { s_card: (game.show_cards || current == SARACEN) ? game.s_card : 0, hand: (current == FRANK) ? game.f_hand : (current == SARACEN) ? game.s_hand : [], who: (game.active == current) ? game.who : null, - where: game.where, - known: {}, - secret: { Frank: {}, Saracen: {}, Assassins: {} }, + location: game.location, + steps: game.steps, + reserves: game.reserves1.concat(game.reserves2), + moved: game.moved, battle: null, prompt: null, actions: null, @@ -1732,36 +1733,5 @@ exports.view = function(state, current) { if (states[game.state].show_battle) view.battle = make_battle_view(); - for (let b in BLOCKS) { - let a = game.location[b]; - if (!a) - continue; - if (a == DEAD) - continue; - if (a == F_POOL) // && current != FRANK) - continue; - if (a == S_POOL) // && current != SARACEN) - continue; - if (a == F_POOL || a == S_POOL) - a = "Pool"; - - let is_known = false; - if (current == block_owner(b)) - is_known = true; - if (b == ASSASSINS) - is_known = true; - - if (is_known) { - view.known[b] = [a, game.steps[b], game.moved[b] ? 1 : 0]; - } else { - let list = view.secret[BLOCKS[b].owner]; - if (!(a in list)) - list[a] = [0, 0]; - list[a][0]++; - if (game.moved[b]) - list[a][1]++; - } - } - return view; } @@ -50,8 +50,7 @@ function wide_map() { let ui = { cards: {}, towns: {}, - known: {}, - secret: { Frank: {}, Saracen: {}, Assassins: {} }, + blocks: {}, battle_menu: {}, battle_block: {}, present: new Set(), @@ -79,27 +78,17 @@ function block_name(who) { return who; } function block_home(who) { return BLOCKS[who].home; } function block_owner(who) { return BLOCKS[who].owner; } -function on_focus_secret_block(evt) { - let owner = evt.target.owner; - let text = owner; - document.getElementById("status").textContent = text; -} - -function on_blur_secret_block(evt) { - document.getElementById("status").textContent = ""; -} - -function on_click_secret_block(evt) { -} - function on_focus_map_block(evt) { - let b = evt.target.block; - let s = game.known[b][1]; - let text = block_name(b) + " "; - if (BLOCKS[b].move) - text += BLOCKS[b].move + "-"; - text += STEP_TEXT[s] + "-" + BLOCKS[b].combat; - document.getElementById("status").textContent = text; + let info = BLOCKS[evt.target.block]; + if (info.owner == player || info.owner == ASSASSINS) { + let text = info.name + " "; + if (info.move) + text += info.move + "-"; + text += STEP_TEXT[info.steps] + "-" + info.combat; + document.getElementById("status").textContent = text; + } else { + document.getElementById("status").textContent = info.owner; + } } function on_blur_map_block(evt) { @@ -248,7 +237,7 @@ function build_battle_block(b, block) { ui.battle_menu[b] = menu; } -function build_known_block(b, block) { +function build_map_block(b, block) { let element = document.createElement("div"); element.classList.add("block"); element.classList.add("known"); @@ -261,19 +250,6 @@ function build_known_block(b, block) { return element; } -function build_secret_block(b, block, secret_index) { - let element = document.createElement("div"); - element.secret_index = secret_index; - element.classList.add("block"); - element.classList.add("secret"); - element.classList.add(BLOCKS[b].owner); - element.addEventListener("mouseenter", on_focus_secret_block); - element.addEventListener("mouseleave", on_blur_secret_block); - element.addEventListener("click", on_click_secret_block); - element.owner = BLOCKS[b].owner; - return element; -} - function town_x(t) { if (map_orientation == 'tall') return TOWNS[t].x; @@ -336,20 +312,12 @@ function build_map() { for (let name in TOWNS) { let town = TOWNS[name]; ui.towns[name] = build_town(name, town); - ui.secret.Frank[name] = []; - ui.secret.Saracen[name] = []; - ui.secret.Assassins[name] = []; } - ui.secret.Frank.offmap = []; - ui.secret.Saracen.offmap = []; - ui.secret.Assassins.offmap = []; for (let b in BLOCKS) { let block = BLOCKS[b]; + ui.blocks[b] = build_map_block(b, block); build_battle_block(b, block); - ui.known[b] = build_known_block(b, block); - let e = build_secret_block(b, block, ui.secret[BLOCKS[b].owner].offmap.length); - ui.secret[BLOCKS[b].owner].offmap.push(e); } update_map_layout(); @@ -484,103 +452,36 @@ function hide_block(element) { } function update_map() { - let overflow = { Frank: [], Saracen: [], Assassins: [] }; let layout = {}; document.getElementById("turn").textContent = "Year " + game.year + " (" + (game.year-1186) + "/6)" ; for (let town in TOWNS) - layout[town] = { secret: [], known: [] }; - - // Move secret blocks to overflow queue if there are too many in a town - for (let town in TOWNS) { - for (let color of [FRANK, SARACEN, ASSASSINS]) { - if (game.secret[color]) { - let max = game.secret[color][town] ? game.secret[color][town][0] : 0; - while (ui.secret[color][town].length > max) { - overflow[color].push(ui.secret[color][town].pop()); - } - } - } - } - - // Add secret blocks if there are too few in a location - for (let town in TOWNS) { - for (let color of [FRANK, SARACEN, ASSASSINS]) { - if (game.secret[color]) { - let max = game.secret[color][town] ? game.secret[color][town][0] : 0; - while (ui.secret[color][town].length < max) { - if (overflow[color].length > 0) { - ui.secret[color][town].push(overflow[color].pop()); - } else { - let element = ui.secret[color].offmap.pop(); - show_block(element); - ui.secret[color][town].push(element); - } - } - } - } - } - - // Remove any blocks left in the overflow queue - for (let color of [FRANK, SARACEN, ASSASSINS]) { - while (overflow[color].length > 0) { - let element = overflow[color].pop(); - hide_block(element); - ui.secret[color].offmap.push(element); - } - } - - // Hide formerly known blocks - for (let b in BLOCKS) { - if (!(b in game.known)) { - hide_block(ui.known[b]); - } - } - - // Add secret blocks to layout - for (let town in TOWNS) { - for (let color of [FRANK, SARACEN, ASSASSINS]) { - let i = 0, n = 0, m = 0; - if (game.secret[color] && game.secret[color][town]) { - n = game.secret[color][town][0]; - m = game.secret[color][town][1]; - } - // Preserve block stacking order, but lets the user track block identities... - if (label_layout == 'stack') - ui.secret[color][town].sort((a,b) => b.secret_index - a.secret_index); - for (let element of ui.secret[color][town]) { - if (i++ < n - m) - element.classList.remove("moved"); - else - element.classList.add("moved"); - layout[town].secret.push(element); + layout[town] = { north: [], south: [] }; + + for (let b in game.location) { + let info = BLOCKS[b]; + let element = ui.blocks[b]; + let town = game.location[b]; + if (town in TOWNS) { + let moved = game.moved[b] ? " moved" : ""; + if (info.owner == player || info.owner == ASSASSINS) { + let image = " known block_" + info.image; + let steps = " r" + (info.steps - game.steps[b]); + element.classList = info.owner + " block" + image + steps + moved; + layout[town].south.push(element); + } else { + element.classList = info.owner + " block" + moved; + layout[town].north.push(element); } - } - } - - // Add known blocks to layout - for (let b in game.known) { - let town = game.known[b][0]; - if (town) { - let steps = game.known[b][1]; - let moved = game.known[b][2]; - let element = ui.known[b]; - show_block(element); - layout[town].known.push(element); - update_steps(b, steps, element); - - if (moved) - element.classList.add("moved"); - else - element.classList.remove("moved"); + } else { + hide_block(element); } } - // Layout blocks on map for (let town in TOWNS) - layout_blocks(town, layout[town].secret, layout[town].known); + layout_blocks(town, layout[town].north, layout[town].south); for (let where in TOWNS) { if (ui.towns[where]) { @@ -594,16 +495,12 @@ function update_map() { if (game.muster) ui.towns[game.where].classList.add('muster'); - for (let b in BLOCKS) { - ui.known[b].classList.remove('highlight'); - ui.known[b].classList.remove('selected'); - } if (!game.battle) { if (game.actions && game.actions.block) for (let b of game.actions.block) - ui.known[b].classList.add('highlight'); + ui.blocks[b].classList.add('highlight'); if (game.who) - ui.known[game.who].classList.add('selected'); + ui.blocks[game.who].classList.add('selected'); } } |