summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2022-06-21 12:00:20 +0200
committerTor Andersson <tor@ccxvii.net>2023-02-18 12:42:59 +0100
commit69431f8710e8a556ec733d069ddef399a97b19c1 (patch)
tree58a82871dbfde43d858c415078fa6cf9e194d8f2
parent26bdd11e03f58e60f5582a691a022fe6b6c9a072 (diff)
download300-earth-and-water-69431f8710e8a556ec733d069ddef399a97b19c1.tar.gz
Don't use JSON for undo.
-rw-r--r--rules.js63
1 files changed, 44 insertions, 19 deletions
diff --git a/rules.js b/rules.js
index 31bc076..182ad3e 100644
--- a/rules.js
+++ b/rules.js
@@ -264,28 +264,54 @@ function log(s) {
game.log.push($(s));
}
-function clear_undo() {
- if (game.undo)
- game.undo.length = 0;
- else
- 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;
- save_log.length = game.log;
- game.log = save_log;
+ 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 clear_undo() {
+ game.undo = []
}
function gen_action_undo(view) {
@@ -3303,6 +3329,8 @@ states.game_over = {
exports.setup = function (seed, scenario, options) {
game = {
seed: seed,
+ undo: [],
+ log: [],
// game board state
campaign: 1,
@@ -3357,9 +3385,6 @@ exports.setup = function (seed, scenario, options) {
attrition: 0,
from: null,
where: null,
-
- undo: [],
- log: [],
};
if (options.cheap_fleets) {