diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-06-29 20:51:44 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-06-30 17:24:26 +0200 |
commit | a07c8e521a06ec7228edba8ca65c6ec9b81a1f7e (patch) | |
tree | 5fe711216e2be9396ecb1445fd1deee3fc66719c | |
parent | 57e984b43119aadc13618abd8cc413345d3ccb9e (diff) | |
download | server-a07c8e521a06ec7228edba8ca65c6ec9b81a1f7e.tar.gz |
Turn "undo" functions into no-ops if undo stack is not present.
Allow replaying game states quickly without undo handling.
-rw-r--r-- | public/common/util.js | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/public/common/util.js b/public/common/util.js index 0f6fc44..18a78e8 100644 --- a/public/common/util.js +++ b/public/common/util.js @@ -1,32 +1,37 @@ // === COMMON LIBRARY === function clear_undo() { - if (game.undo.length > 0) - game.undo = [] + if (game.undo) { + game.undo.length = 0 + } } function push_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 + 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) } - game.undo.push(copy) } function pop_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 + 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 random(range) { |