summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-12-05 19:25:03 +0100
committerTor Andersson <tor@ccxvii.net>2023-12-06 11:40:43 +0100
commit8c8dc473b00c3ca65d525b8f5066d82f3977b05b (patch)
treec7ee6069439d1344c1cd56b7ea4da821f1776e27
parent47026c54cd34f9c015a5c066a7279e389b7aad01 (diff)
downloadrommel-in-the-desert-8c8dc473b00c3ca65d525b8f5066d82f3977b05b.tar.gz
Add timeout so as not to hang other games when supply calculations overrun.
-rw-r--r--rules.js16
1 files changed, 16 insertions, 0 deletions
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]