summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2022-06-20 22:10:18 +0200
committerTor Andersson <tor@ccxvii.net>2022-11-16 19:08:56 +0100
commitdd6ad74898427e5fb907519ed3859519aa71a809 (patch)
tree04815ab65b9d0bff324215c57ea4e83b03f2c992
parentfdbe7f2b30ed3d88be9ef0b4cbc17e66a8c07eeb (diff)
downloadjulius-caesar-dd6ad74898427e5fb907519ed3859519aa71a809.tar.gz
Don't use JSON for undo state.
Simple deep copies are much faster.
-rw-r--r--rules.js56
1 files changed, 42 insertions, 14 deletions
diff --git a/rules.js b/rules.js
index a0175cc..f950531 100644
--- a/rules.js
+++ b/rules.js
@@ -132,28 +132,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
+ 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) {
@@ -2497,6 +2523,9 @@ states.game_over = {
exports.setup = function (seed, scenario, options) {
game = {
seed: seed,
+ log: [],
+ undo: [],
+
c_hand: [],
p_hand: [],
c_card: 0,
@@ -2515,7 +2544,6 @@ exports.setup = function (seed, scenario, options) {
attacker: {},
main_road: {},
reserves: [],
- log: [],
}
if (options.tournament) {