summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2022-06-20 23:24:54 +0200
committerTor Andersson <tor@ccxvii.net>2023-02-18 12:31:29 +0100
commit75f6b58dae1101606078bf18155bcc80dac0a688 (patch)
tree39b43f36feb42a12771f6722106fefb008944b1d
parentab0e69f51f9904f918c88953218aa3546638a697 (diff)
downloadpax-pamir-75f6b58dae1101606078bf18155bcc80dac0a688.tar.gz
Don't use JSON for undo.
-rw-r--r--rules.js55
1 files changed, 42 insertions, 13 deletions
diff --git a/rules.js b/rules.js
index 6c0aec0..44ad947 100644
--- a/rules.js
+++ b/rules.js
@@ -209,25 +209,54 @@ function logi(msg) {
game.log.push(">" + msg)
}
-function clear_undo() {
- game.undo = []
+function deep_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] = deep_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] = deep_copy(v)
+ else
+ copy[i] = v
+ }
+ return copy
+ }
}
function push_undo() {
- game.undo.push(JSON.stringify(game, (k,v) => {
- if (k === 'undo') return 0
- if (k === 'log') return v.length
- return v
- }))
+ 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 = deep_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
+ let save_undo = game.undo
+ game = save_undo.pop()
save_log.length = game.log
game.log = save_log
+ game.undo = save_undo
+}
+
+function clear_undo() {
+ game.undo = []
}
function gen_action(action, argument=undefined) {
@@ -3045,6 +3074,9 @@ exports.setup = function (seed, scenario, options) {
game = {
seed: seed,
+ log: [],
+ undo: [],
+
open: options.open_hands ? 1 : 0,
active: 0,
@@ -3074,9 +3106,6 @@ exports.setup = function (seed, scenario, options) {
[0,0,0,0,0,0],
],
players: [],
-
- log: [],
- undo: [],
}
for (let i = 0; i < player_count; ++i) {