summaryrefslogtreecommitdiff
path: root/rules.js
blob: 08edea84bf17a7afc55fde750a1896dbe9a148ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
	},
}