From d3d8295f1be86253d294942cdfcd3ebadd24bff6 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 21 Jun 2022 12:00:38 +0200 Subject: Don't use JSON for undo. --- rules.js | 58 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/rules.js b/rules.js index c65aae8..a331a05 100644 --- a/rules.js +++ b/rules.js @@ -332,28 +332,54 @@ function remove_from_array(array, item) { array.splice(i, 1); } -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 undo = game.undo; - let save_log = game.log; - game = JSON.parse(undo.pop()); - game.undo = 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) { -- cgit v1.2.3