summaryrefslogtreecommitdiff
path: root/rules.js
diff options
context:
space:
mode:
Diffstat (limited to 'rules.js')
-rw-r--r--rules.js108
1 files changed, 108 insertions, 0 deletions
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
+ },
+}