diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-10-19 02:14:04 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-10-21 18:32:29 +0200 |
commit | d2ff4e66e48d78bef4970c584bdcacedbcc05aef (patch) | |
tree | be8a3cf023f89e246cc7aee3517b86f21e04598e /rules.js | |
parent | f6c6cfa47e922907aa80bd892308e1aec10e9954 (diff) | |
download | washingtons-war-d2ff4e66e48d78bef4970c584bdcacedbcc05aef.tar.gz |
Mobile layout.
Diffstat (limited to 'rules.js')
-rw-r--r-- | rules.js | 70 |
1 files changed, 52 insertions, 18 deletions
@@ -71,7 +71,7 @@ let game; let view; function random(n) { - return ((game.seed = game.seed * 69621 % 0x7fffffff) / 0x7fffffff) * n | 0; + return (game.seed = game.seed * 200105 % 34359738337) % n } function logbr() { @@ -87,25 +87,63 @@ function logp(s) { game.log.push(game.active[0] + " " + s); } +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 clear_undo() { - game.undo = []; + if (game.undo) { + game.undo.length = 0 + } } function push_undo() { - game.undo.push(JSON.stringify(game, (k,v) => { - if (k === 'undo') return 0; - if (k === 'log') return v.length; - return v; - })); + 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) + } } function pop_undo() { - let save_undo = game.undo; - let save_log = game.log; - game = JSON.parse(save_undo.pop()); - game.undo = save_undo; - save_log.length = game.log; - game.log = save_log; + 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 + } } function remove_from_array(array, item) { @@ -3444,11 +3482,7 @@ states.game_over = { /* CLIENT/SERVER COMMS */ -exports.ready = function (scenario, options, players) { - return players.length === 2; -} - -exports.setup = function (seed, scenario, players) { +exports.setup = function (seed, scenario, options) { setup_game(seed); return game; } |