summaryrefslogtreecommitdiff
path: root/rules.js
blob: bbde529fc030cf5b8d02fb8ccefd67d745557933 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//"use strict"

const { finished } = require("nodemailer/lib/xoauth2/index.js")
const data = require("./data.js")

console.log('hello')

var game, view, states = {}

const DEM = "Democrat"
const COM = "Communist"

const first_strategy_card = 1
const last_strategy_card = 110 /* to check! */
const hand_limit = 8

const spaces = [    
	{name: 'Schwerin', name_unique: 'Schwerin', socio: 4, stability: 3, battleground: 0, country: 1, box: {x: 111, y: 61, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Rostock', name_unique: 'Rostock', socio: 4, stability: 3, battleground: 0, country: 1, box: {x: 228, y: 41, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Berlin', name_unique: 'Berlin', socio: 2, stability: 3, battleground: 1, country: 1, box: {x: 251, y: 113, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'German Writers', name_unique: 'German Writers', socio: 5, stability: 2, battleground: 0, country: 1, box: {x: 58, y: 185, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Walter Ulbricht Academy', name_unique: 'Walter Ulbricht Academy', socio: 6, stability: 1, battleground: 0, country: 1, box: {x: 175, y: 187, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Lutherian Church', name_unique: 'Lutherian Church', socio: 7, stability: 5, battleground: 1, country: 1, box: {x: 300, y: 212, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Magdeburg', name_unique: 'Magdeburg', socio: 4, stability: 3, battleground: 1, country: 1, box: {x: 58, y: 275, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Halle', name_unique: 'Halle', socio: 4, stability: 3, battleground: 0, country: 1, box: {x: 175, y: 278, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Leipzig', name_unique: 'Leipzig', socio: 4, stability: 3, battleground: 1, country: 1, box: {x: 297, y: 297, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Erfurt', name_unique: 'Erfurt', socio: 3, stability: 4, battleground: 0, country: 1, box: {x: 23, y: 356, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Karl-Marx-Stadt', name_unique: 'Karl-Marx-Stadt', socio: 4, stability: 3, battleground: 1, country: 1, box: {x: 138, y: 385, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Dresden', name_unique: 'Dresden', socio: 1, stability: 4, battleground: 1, country: 1, box: {x: 262, y: 375, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'Szczecin', name_unique: 'Szczecin', socio: 4, stability: 3, battleground: 0, country: 2, box: {x: 434, y: 204, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Gdansk', name_unique: 'Gdansk', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 699, y: 215, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Bydgoszcz', name_unique: 'Bydgoszcz', socio: 1, stability: 4, battleground: 0, country: 2, box: {x: 679, y: 304, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0},
	{name: 'Poznan', name_unique: 'Poznan', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 521, y: 355, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Warszawa', name_unique: 'Warszawa', socio: 2, stability: 3, battleground: 1, country: 2, box: {x: 806, y: 383, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0},
	{name: 'Bialystok', name_unique: 'Bialystok', socio: 3, stability: 4, battleground: 0, country: 2, box: {x: 940, y: 342, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Wroclaw', name_unique: 'Wroclaw', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 462, y: 443, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Catholic Church', name_unique: 'Catholic Church Poland', socio: 7, stability: 5, battleground: 0, country: 2, box: {x: 625, y: 437, h: 65, w: 105}, demInfl: 5, comInfl: 0, demCtrl: 1, comCtrl: 0},
	{name: 'Lodz', name_unique: 'Lodz', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 749, y: 486, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Katowice', name_unique: 'Katowice', socio: 4, stability: 3, battleground: 0, country: 2, box: {x: 570, y: 569, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Krakow', name_unique: 'Krakow', socio: 4, stability: 3, battleground: 1, country: 2, box: {x: 711, y: 598, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Lublin', name_unique: 'Lublin', socio: 3, stability: 4, battleground: 0, country: 2, box: {x: 879, y: 603, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0},
	{name: 'Jagiellian University', name_unique: 'Jagiellian University', socio: 6, stability: 1, battleground: 0, country: 2, box: {x: 679, y: 681, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Polish Writers', name_unique: 'Polish Writers', socio: 5, stability: 2, battleground: 0, country: 2, box: {x: 832, y: 694, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Plzen', name_unique: 'Plzen', socio: 1, stability: 4, battleground: 0, country: 3, box: {x: 159, y: 483, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'Ceske Budejovice', name_unique: 'Ceske Budejovice', socio: 3, stability: 3, battleground: 0, country: 3, box: {x: 196, y: 601, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Praha', name_unique: 'Praha', socio: 2, stability: 3, battleground: 1, country: 3, box: {x: 317, y: 614, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'Charles University', name_unique: 'Charles University', socio: 6, stability: 1, battleground: 1, country: 3, box: {x: 380, y: 532, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Czech Writers', name_unique: 'Czech Writers', socio: 5, stability: 2, battleground: 1, country: 3, box: {x: 444, y: 607, h: 65, w: 105}, demInfl: 2, comInfl: 0, demCtrl: 1, comCtrl: 0},
	{name: 'Brno', name_unique: 'Brno', socio: 4, stability: 3, battleground: 1, country: 3, box: {x: 403, y: 711, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0},
	{name: 'Ostrava', name_unique: 'Ostrava', socio: 4, stability: 3, battleground: 1, country: 3, box: {x: 523, y: 681, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Bratislava', name_unique: 'Bratislava', socio: 4, stability: 3, battleground: 1, country: 3, box: {x: 417, y: 797, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Catholic Church', name_unique: 'Catholic Church Czech', socio: 7, stability: 5, battleground: 0, country: 3, box: {x: 538, y: 794, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Presov', name_unique: 'Presov', socio: 3, stability: 4, battleground: 0, country: 3, box: {x: 658, y: 794, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Kosice', name_unique: 'Kosice', socio: 3, stability: 4, battleground: 0, country: 3, box: {x: 777, y: 815, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Catholic Church', name_unique: 'Catholic Church Hungary', socio: 7, stability: 5, battleground: 0, country: 4, box: {x: 314, y: 886, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Gyor', name_unique: 'Gyor', socio: 4, stability: 3, battleground: 0, country: 4, box: {x: 434, y: 887, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Tatabanya', name_unique: 'Tatabanya', socio: 4, stability: 3, battleground: 0, country: 4, box: {x: 549, y: 886, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Miskolc', name_unique: 'Miskolc', socio: 4, stability: 3, battleground: 1, country: 4, box: {x: 664, y: 901, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Debrecen', name_unique: 'Debrecen', socio: 4, stability: 3, battleground: 1, country: 4, box: {x: 781, y: 938, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Szombathely', name_unique: 'Szombathely', socio: 1, stability: 4, battleground: 0, country: 4, box: {x: 316, y: 963, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0},
	{name: 'Szekesfehervar', name_unique: 'Szekesfehervar', socio: 4, stability: 3, battleground: 0, country: 4, box: {x: 442, y: 962, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Budapest', name_unique: 'Budapest', socio: 2, stability: 3, battleground: 1, country: 4, box: {x: 630, y: 983, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Hungarian Writers', name_unique: 'Hungarian Writers', socio: 5, stability: 2, battleground: 0, country: 4, box: {x: 348, y: 1040, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Eotvos Lorand University', name_unique: 'Eotvos Lorand University', socio: 6, stability: 1, battleground: 0, country: 4, box: {x: 512, y: 1034, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Szeged', name_unique: 'Szeged', socio: 3, stability: 4, battleground: 1, country: 4, box: {x: 632, y: 1073, h: 65, w: 105}, demInfl: 1, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Pecs', name_unique: 'Pecs', socio: 3, stability: 4, battleground: 1, country: 4, box: {x: 486, y: 1105, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Timisoara', name_unique: 'Timisoara', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 597, y: 1204, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Cluj-Napoca', name_unique: 'Cluj-Napoca', socio: 1, stability: 4, battleground: 1, country: 5, box: {x: 756, y: 1125, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'Targu Mures', name_unique: 'Targu Mures', socio: 3, stability: 4, battleground: 0, country: 5, box: {x: 915, y: 1136, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Iasi', name_unique: 'Iasi', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 1072, y: 1097, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Babes-Bolyai University', name_unique: 'Babes-Bolyai University', socio: 6, stability: 1, battleground: 0, country: 5, box: {x: 746, y: 1203, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Romanian Writers', name_unique: 'Romanian Writers', socio: 5, stability: 2, battleground: 0, country: 5, box: {x: 739, y: 1278, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Hargita/Covasna', name_unique: 'Hargita/Covasna', socio: 8, stability: 4, battleground: 0, country: 5, box: {x: 928, y: 1227, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Brasov', name_unique: 'Brasov', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 1049, y: 1225, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Orthodox Church', name_unique: 'Orthodox Church Romania', socio: 7, stability: 3, battleground: 0, country: 5, box: {x: 855, y: 1338, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Ploiesti', name_unique: 'Ploiesti', socio: 4, stability: 3, battleground: 0, country: 5, box: {x: 1061, y: 1316, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Craiova', name_unique: 'Craiova', socio: 3, stability: 4, battleground: 0, country: 5, box: {x: 763, y: 1411, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Bucuresti', name_unique: 'Bucuresti', socio: 2, stability: 3, battleground: 1, country: 5, box: {x: 929, y: 1445, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'Galatii', name_unique: 'Galatii', socio: 4, stability: 3, battleground: 1, country: 5, box: {x: 1104, y: 1399, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Constanta', name_unique: 'Constanta', socio: 4, stability: 3, battleground: 0, country: 5, box: {x: 1130, y: 1517, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Pleven', name_unique: 'Pleven', socio: 3, stability: 4, battleground: 0, country: 6, box: {x: 764, y: 1534, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Orthodox Church', name_unique: 'Orthodox Church Bulgaria', socio: 7, stability: 3, battleground: 0, country: 6, box: {x: 882, y: 1540, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Ruse', name_unique: 'Ruse', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 998, y: 1540, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Sofia University', name_unique: 'Sofia University', socio: 6, stability: 1, battleground: 0, country: 6, box: {x: 645, y: 1650, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Sofia', name_unique: 'Sofia', socio: 2, stability: 3, battleground: 1, country: 6, box: {x: 768, y: 1653, h: 65, w: 105}, demInfl: 0, comInfl: 2, demCtrl: 0, comCtrl: 0},
	{name: 'Stara Zagora', name_unique: 'Stara Zagora', socio: 1, stability: 4, battleground: 0, country: 6, box: {x: 886, y: 1694, h: 65, w: 105}, demInfl: 0, comInfl: 1, demCtrl: 0, comCtrl: 0},
	{name: 'Razgrad', name_unique: 'Razgrad', socio: 8, stability: 4, battleground: 0, country: 6, box: {x: 954, y: 1620, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Burgas', name_unique: 'Burgas', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 1004, y: 1695, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Varna', name_unique: 'Varna', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 1086, y: 1613, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Bulgarian Writers', name_unique: 'Bulgarian Writers', socio: 5, stability: 2, battleground: 0, country: 6, box: {x: 652, y: 1726, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Plovdiv', name_unique: 'Plovdiv', socio: 4, stability: 3, battleground: 1, country: 6, box: {x: 771, y: 1739, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	{name: 'Sliven', name_unique: 'Sliven', socio: 3, stability: 4, battleground: 0, country: 6, box: {x: 894, y: 1768, h: 65, w: 105}, demInfl: 0, comInfl: 0, demCtrl: 0, comCtrl: 0},
	
	] 

exports.scenarios = [ "Standard" ]

exports.roles = [ DEM, COM ]



// --- SET UP ---

exports.setup = function (seed, scenario, options) {
	console.log('setup ran')

	game = {
		seed: seed,
		log: [],
		undo: [],
		active: null,
		state: "com_init_1",

		played_card: [],
		available_ops: 0,

		turn: 0,
		round: 0,

		pieces: [],

		strategy_deck: [],
		strategy_discard: [],

		democrat_hand: [],
        communist_hand: [],
		democrat_set_aside: [],

	}

	log_h1("1989 Dawn of Freedom")

	game.active = COM
	start_game()

	return game
}

function start_game() {
	//starting influence
	console.log('start_game called')

	spaces.forEach(space => {
		game.pieces.push({
			name_unique: space.name_unique,
			demInfl: space.demInfl,
			comInfl: space.comInfl
		});
	});
	//Communist starts by placing 2 influence
	game.available_ops = 2
}


exports.view = function(state, player) {
	game = state

	view = {
		log: game.log,
		active: game.active,
		prompt: null,
		actions: null,

		played_card: game.played_card,
		selected_space: game.selected_space,

		pieces: game.pieces,
		turn: game.turn,
		round: game.round,
		strategy_deck: game.strategy_deck.length,

		democrat_hand: game.democrat_hand.length,
		communist_hand: game.communist_hand.length,

		hand: [],
		set_aside: [],


	}

	if (player === game.active && game.vm && game.vm.draw)
		view.drawn = game.vm.draw

	if (player === DEM) {
		view.hand = game.democrat_hand
		view.set_aside = game.democrat_set_aside
	} else if (player === COM) {
		view.hand = game.communist_hand
	}

	if (game.state === "game_over") {
		view.prompt = game.victory
	} else if (player === "Observer" || (game.active !== player && game.active !== "Both")) {
		if (states[game.state]) {
			let inactive = states[game.state].inactive
			if (typeof inactive === "function")
				view.prompt = `Waiting for ${game.active} ${inactive()}`
			else
				view.prompt = `Waiting for ${game.active} to ${inactive}`
		} else {
			view.prompt = "Unknown state: " + game.state
		}
	} else {
		view.actions = {}

		if (states[game.state])
			states[game.state].prompt(player)
		else
			view.prompt = "Unknown state: " + game.state
		if (view.actions.undo === undefined) {
			if (game.undo && game.undo.length > 0)
				view.actions.undo = 1
			else
				view.actions.undo = 0
		}
	}

	return view
}


// === ACTIONS

function gen_action(action, argument) {
//console.log('gen_action called with ', action, ' and ', argument)
	if (argument === undefined) {
		view.actions[action] = 1
	} else {
		if (!(action in view.actions))
			view.actions[action] = []
		view.actions[action].push(argument)
	}
	//console.log('view.actions: ', view.actions, 'view.actions[action]: ', view.actions[action])
}

function gen_action_infl(space){
	console.log('gen_action_infl called')
	gen_action("infl", space)
}

exports.action = function (state, player, action, arg) {
	//console.log('exports.action called with state:' , state, 'player:', player, 'action: ', action, 'arg: ', arg)
	game = state
	if (states[game.state] && action in states[game.state]) {
		states[game.state][action](arg, player)
	} else {
		if (action === "undo" && game.undo && game.undo.length > 0)
			pop_undo()
		else
			throw new Error("Invalid action: " + action)
	}
	return game
}



function init_player_cards(first_card) {
	let c = first_card
	let deck = []

	for (let n = 0; n < era_cards_count; ++n)
		deck.push(++c)

	shuffle(deck)

	return deck
}



states.com_init_1 = {
	inactive: 'place starting influence',
	prompt() {
		view.prompt = `Place ${game.available_ops} starting influence`
	
		let available_spaces = spaces.slice()
        for (let space of available_spaces) {
            gen_action_infl(space.name_unique);
			//console.log('state: com_init_1')
			//console.log('space: ', space)
		}
	},

	infl(space) {
		push_undo()
		console.log('space clicked was: ', space)
		const clicked_space = find_space_index(space)
		console.log('clicked_space: ', clicked_space)
		console.log('game.pieces.space', game.pieces[clicked_space])
		console.log('initial influence: ', game.pieces[clicked_space] )
		game.pieces[clicked_space].comInfl++
		game.available_ops--
		console.log('new influence: ',  game.pieces[clicked_space])
		
		if (game.available_ops == 0) {
		game.state = "next_step"
		} 
	}

}

states.next_step = {
	inactive: "place starting influence",
	prompt() {
		view.prompt = "Next step"
	}
}

function find_space_index(name_unique) {
    return game.pieces.findIndex(piece => piece && piece.name_unique === name_unique);
}

// === COMMON LIBRARY ===

function shuffle(list) {
	// Fisher-Yates shuffle
	for (let i = list.length - 1; i > 0; --i) {
		let j = random(i + 1)
		let tmp = list[j]
		list[j] = list[i]
		list[i] = tmp
	}
	return list
}

function log_h1(msg) {
	log_br()
	log(".h1 " + msg)
	log_br()
}

function log_br() {
	if (game.log.length > 0 && game.log[game.log.length - 1] !== "")
		game.log.push("")
}

function log(msg) {
	game.log.push(msg)
}

function push_undo() {
	let copy = {}
	for (let k in game) {
		let v = game[k]
		if (k === "undo")
			continue
		else if (k === "log")
			v = v.length
		else if (typeof v === "object" && v !== null)
			v = object_copy(v)
		copy[k] = v
	}
	game.undo.push(copy)
}

function pop_undo() {
	let save_log = game.log
	let save_undo = game.undo
	game = save_undo.pop()
	save_log.length = game.log
	game.log = save_log
	game.undo = save_undo
}

// Fast deep copy for objects without cycles
function object_copy(original) {
	if (Array.isArray(original)) {
		let n = original.length
		let copy = new Array(n)
		for (let i = 0; i < n; ++i) {
			let v = original[i]
			if (typeof v === "object" && v !== null)
				copy[i] = object_copy(v)
			else
				copy[i] = v
		}
		return copy
	} else {
		let copy = {}
		for (let i in original) {
			let v = original[i]
			if (typeof v === "object" && v !== null)
				copy[i] = object_copy(v)
			else
				copy[i] = v
		}
		return copy
	}
}