diff options
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; } |