From a07c8e521a06ec7228edba8ca65c6ec9b81a1f7e Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 29 Jun 2023 20:51:44 +0200 Subject: Turn "undo" functions into no-ops if undo stack is not present. Allow replaying game states quickly without undo handling. --- public/common/util.js | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'public') 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) { -- cgit v1.2.3