diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-06-05 01:23:20 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-06-05 01:23:20 +0200 |
commit | 7eadd297b6c8b04d9636d61ce3742965ce8965de (patch) | |
tree | 02f874fcbf3193b2a20be56e0559d450db428484 /rules.js | |
parent | a711fd68eab919f422bca2a89767151c408d19da (diff) | |
download | field-cloth-gold-7eadd297b6c8b04d9636d61ce3742965ce8965de.tar.gz |
Skeleton.
Diffstat (limited to 'rules.js')
-rw-r--r-- | rules.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/rules.js b/rules.js new file mode 100644 index 0000000..6229427 --- /dev/null +++ b/rules.js @@ -0,0 +1,66 @@ +"use strict" + +const RED = "Red" +const BLUE = "Blue" + +var game, view, states + +exports.scenarios = [ "Standard" ] + +exports.roles = [ RED, BLUE ] + +exports.setup = function (seed, scenario, options) { + game = { + seed: seed, + state: null, + log: [], + undo: [], + } + return game +} + +exports.action = function (state, player, action, arg) { + game = state + let S = states[game.state] + if (action in S) + S[action](arg, player) + else + throw new Error("Invalid action: " + action) + return game +} + +exports.resign = function (state, player) { + game = state + if (game.state !== 'game_over') { + if (player === RED) + goto_game_over(BLUE, "Red resigned.") + if (player === BLUE) + goto_game_over(RED, "Blue resigned.") + } + return game +} + +exports.view = function(state, player) { + game = state + + let view = { + log: game.log, + prompt: null, + } + + if (game.state === "game_over") { + view.prompt = game.victory + } else if (player !== game.active) { + let inactive = states[game.state].inactive || game.state + view.prompt = `Waiting for ${game.active} \u2014 ${inactive}...` + } else { + view.actions = {} + states[game.state].prompt() + if (game.undo && game.undo.length > 0) + view.actions.undo = 1 + else + view.actions.undo = 0 + } + + return view; +} |