From 8c8dc473b00c3ca65d525b8f5066d82f3977b05b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 5 Dec 2023 19:25:03 +0100 Subject: Add timeout so as not to hang other games when supply calculations overrun. --- rules.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rules.js b/rules.js index 3a4b767..fcb889a 100644 --- a/rules.js +++ b/rules.js @@ -11,6 +11,10 @@ const max = Math.max const min = Math.min const abs = Math.abs +const TIMEOUT = 500 + +var timeout = 0 + var states = {} var game = null var view = null @@ -1165,10 +1169,17 @@ function is_supply_line_blocked(here, next, side) { return false } +function check_timeout() { + if (Date.now() > timeout) + throw new Error("timeout (supply lines are too complicated)") +} + function trace_supply_highway(here, v) { if (supply_src[here]) return true + check_timeout() + let has_supply = false let save_v = supply_visited[here] @@ -1216,6 +1227,8 @@ function trace_supply_chain(here, n, range, v) { if (supply_src[here]) return true + check_timeout() + let has_supply = false let save_v = supply_visited[here] @@ -7376,6 +7389,7 @@ exports.setup = function (seed, scenario, options) { } exports.view = function(state, current) { + timeout = Date.now() + TIMEOUT // don't think too long! load_state(state) let scenario = current_scenario() @@ -7439,6 +7453,7 @@ exports.view = function(state, current) { } exports.query = function (state, current, q) { + timeout = Date.now() + TIMEOUT // don't think too long! if (q === 'supply') { load_state(state) if (game.buildup) { @@ -7561,6 +7576,7 @@ exports.resign = function (state, current) { } exports.action = function (state, current, action, arg) { + timeout = Date.now() + TIMEOUT // don't think too long! load_state(state) // Object.seal(game) // XXX: don't allow adding properties let S = states[game.state] -- cgit v1.2.3