From 61dcf972cecd41e6bfe459725414f5b2c15c4bdc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 29 Oct 2024 17:48:51 +0100 Subject: Initial commit. --- rules.js | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 rules.js (limited to 'rules.js') diff --git a/rules.js b/rules.js new file mode 100644 index 0000000..08edea8 --- /dev/null +++ b/rules.js @@ -0,0 +1,108 @@ +"use strict" + +var states = {} +var game = null +var view = null + +exports.scenarios = [ "Standard" ] + +exports.roles = [ "Anarchist", "Moderate", "Fascist" ] + +function gen_action(action, argument) { + if (!(action in view.actions)) + view.actions[action] = [] + view.actions[action].push(argument) +} + +function gen_action_space(space) { + gen_action("space", space) +} + +function gen_action_piece(piece) { + gen_action("piece", piece) +} + +function gen_action_card(card) { + gen_action("card", card) +} + +exports.action = function (state, player, action, arg) { + game = state + let S = states[game.state] + if (action in S) + S[action](arg, player) + else if (action === "undo" && game.undo && game.undo.length > 0) + pop_undo() + else + throw new Error("Invalid action: " + action) + return game +} + +exports.view = function (state, player) { + game = state + + view = { + log: game.log, + prompt: null, + location: game.location, + selected: game.selected, + } + + if (player !== game.active) { + let inactive = states[game.state].inactive || game.state + view.prompt = `Waiting for ${game.active} to ${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 +} + +exports.setup = function (seed, scenario, options) { + game = { + seed: seed, + state: null, + active: "Anarchist", + log: [], + undo: [], + } + + game.state = "move" + + return game +} + +states.move = { + inactive: "move", + prompt() { + view.prompt = "Move a piece." + for (let p = 0; p < 32; ++p) + gen_action_piece(p) + }, + piece(p) { + game.selected = p + game.state = "move_to" + }, +} + +states.move_to = { + inactive: "move", + prompt() { + view.prompt = "Move the piece to a space." + for (let s = 0; s < 64; ++s) + gen_action_space(s) + }, + space(to) { + game.location[game.selected] = to + game.state = "move" + if (game.active === PLAYER1) + game.active = PLAYER2 + else + game.active = PLAYER1 + }, +} -- cgit v1.2.3