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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.roles = exports.scenarios = exports.MODERATE = exports.COMMUNIST = exports.ANARCHIST = void 0;
exports.action = action;
exports.view = game_view;
exports.setup = setup;
const data_1 = require("./data");
const states = {};
let game = {};
var view = {};
exports.ANARCHIST = 'Anarchist';
exports.COMMUNIST = 'Communist';
exports.MODERATE = 'Moderate';
const ANARCHISTS_ID = 'a';
const COMMUNISTS_ID = 'c';
const MODERATES_ID = 'm';
const role_ids = [ANARCHISTS_ID, COMMUNISTS_ID, MODERATES_ID];
const faction_player_map = {
[ANARCHISTS_ID]: exports.ANARCHIST,
[COMMUNISTS_ID]: exports.COMMUNIST,
[MODERATES_ID]: exports.MODERATE,
};
const player_faction_map = {
[exports.ANARCHIST]: ANARCHISTS_ID,
[exports.COMMUNIST]: COMMUNISTS_ID,
[exports.MODERATE]: MODERATES_ID,
};
const front_names = {
a: 'the Aragon Front',
m: 'the Madrid Front',
n: 'the Nothern Front',
s: 'the Southern Front',
d: 'the Front closest to Defeat',
v: 'the Front closest to Victory',
};
const tracks = [
data_1.LIBERTY,
data_1.COLLECTIVIZATION,
data_1.GOVERNMENT,
data_1.SOVIET_SUPPORT,
data_1.FOREIGN_AID,
];
const track_names = {
[data_1.LIBERTY]: 'Liberty',
[data_1.COLLECTIVIZATION]: 'Collectivization',
[data_1.GOVERNMENT]: 'Government',
[data_1.SOVIET_SUPPORT]: 'Soviet Support',
[data_1.FOREIGN_AID]: 'Foreign Aid',
};
const { cards, } = data_1.default;
const faction_cards = {
[ANARCHISTS_ID]: make_list(37, 54),
[COMMUNISTS_ID]: make_list(19, 36),
[MODERATES_ID]: make_list(1, 18),
};
const medaillons = make_list(0, 8);
console.log('medaillons', medaillons);
const fascist_decks = {
1: make_list(55, 62),
};
exports.scenarios = ['Standard'];
exports.roles = [exports.ANARCHIST, exports.COMMUNIST, exports.MODERATE];
function gen_action(action, argument) {
if (argument === undefined) {
view.actions[action] = 1;
}
else {
if (!(action in view.actions))
view.actions[action] = [];
view.actions[action].push(argument);
}
}
function gen_action_card(card_id) {
gen_action('card', card_id);
}
function gen_action_standee(track_id) {
gen_action('standee', track_id);
}
function action(state, player, action, arg) {
console.log('action', 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;
}
const leaf_node = 'l';
const seq_node = 's';
const function_node = 'f';
const resolved = 1;
function setup_bag_of_glory() {
console.log('setup_bag_of_glory');
game.engine = [
{
t: leaf_node,
p: game.initiative,
s: 'add_glory',
},
{
t: function_node,
f: 'end_of_turn',
},
];
next();
}
function setup_choose_card() {
console.log('setup_choose_card');
const player_order = get_player_order();
game.engine = player_order.map((faction_id) => ({
t: leaf_node,
p: faction_id,
s: 'choose_card',
}));
game.engine.push({
t: function_node,
f: 'setup_player_turn',
});
next();
}
function setup_player_turn() {
console.log('setup_player_turn');
const player_order = get_player_order();
game.engine = player_order.map((faction_id) => ({
t: seq_node,
c: [
{
t: leaf_node,
s: 'player_turn',
p: faction_id,
},
],
}));
game.engine.push({
t: function_node,
f: 'resolve_fascist_test',
});
game.engine.push({
t: function_node,
f: 'setup_bag_of_glory',
});
next();
}
const engine_functions = {
end_of_turn,
end_of_year,
setup_bag_of_glory,
setup_choose_card,
setup_player_turn,
resolve_fascist_test,
};
function get_active(engine) {
for (let i of engine) {
if ((i.t === leaf_node || i.t === function_node) && i.r !== resolved) {
return { parent: engine, node: i };
}
if (i.t === seq_node) {
const next_child = get_active(i.c);
if (next_child !== null) {
return next_child;
}
}
}
return null;
}
function get_active_node(engine = game.engine) {
const a = get_active(engine);
return a === null ? null : a.node;
}
function get_active_node_args() {
const node = get_active_node(game.engine);
if (node.t === leaf_node) {
return node.a;
}
return null;
}
function insert_before_or_after_active_node(node, position, engine = game.engine) {
const a = get_active(engine);
if (a === null) {
return;
}
const i = a.parent.indexOf(a.node);
console.log('insert_before_active_node', i);
if (i >= 0) {
array_insert(a.parent, i + (position == 'after' ? 1 : 0), node);
}
}
function insert_after_active_node(node, engine = game.engine) {
insert_before_or_after_active_node(node, 'after', engine);
}
function insert_before_active_node(node, engine = game.engine) {
insert_before_or_after_active_node(node, 'before', engine);
}
function next() {
console.log('next');
const node = get_active_node(game.engine);
console.log('node', node);
if (node.t === function_node && engine_functions[node.f]) {
resolve_active_node();
const args = node.a;
if (args) {
engine_functions[node.f](args);
}
else {
engine_functions[node.f]();
}
}
else if (node.t === 'l') {
game.state = node.s;
game.active = faction_player_map[node.p];
}
}
function resolve_active_node() {
const next_node = get_active_node(game.engine);
console.log('resolve_active_node', next_node);
if (next_node !== null) {
next_node.r = resolved;
}
}
function resolve_active_and_proceed() {
resolve_active_node();
next();
}
function game_view(state, player) {
game = state;
const faction_id = player_faction_map[player];
view = {
engine: game.engine,
log: game.log,
prompt: null,
location: game.location,
selected: game.selected,
bonuses: game.bonuses,
current_events: game.current_events,
fronts: game.fronts,
hand: game.hands[faction_id],
medaillons: game.medaillons,
selected_card: game.cards_in_play[faction_id],
tracks: game.tracks,
};
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;
}
function setup(seed, _scenario, _options) {
game = {
seed: seed,
state: null,
active: exports.ANARCHIST,
bag_of_glory: {
[ANARCHISTS_ID]: 1,
[COMMUNISTS_ID]: 1,
[MODERATES_ID]: 1,
},
blank_markers: [[], [], [], [], []],
bonuses: [data_1.ON, data_1.ON],
current_events: [],
discard: {
[ANARCHISTS_ID]: [],
[COMMUNISTS_ID]: [],
[MODERATES_ID]: [],
f: [],
},
engine: [],
fronts: {
a: -2,
m: -2,
n: -2,
s: -2,
},
hands: {
[ANARCHISTS_ID]: [],
[COMMUNISTS_ID]: [],
[MODERATES_ID]: [],
},
hero_points: {
[ANARCHISTS_ID]: 2,
[COMMUNISTS_ID]: 2,
[MODERATES_ID]: 0,
pool: 14,
},
cards_in_play: {
[ANARCHISTS_ID]: null,
[COMMUNISTS_ID]: null,
[MODERATES_ID]: null,
},
initiative: MODERATES_ID,
medaillons: [
draw_item(medaillons),
draw_item(medaillons),
draw_item(medaillons),
draw_item(medaillons),
draw_item(medaillons),
],
tableaus: {
[ANARCHISTS_ID]: [],
[COMMUNISTS_ID]: [],
[MODERATES_ID]: [],
},
tracks: [5, 5, 6, 3, 3],
log: [],
undo: [],
turn: 1,
year: 1,
state_data: null,
};
start_year();
return game;
}
function draw_hand_cards() {
role_ids.forEach((role) => {
const deck = faction_cards[role];
for (let i = 0; i < 5; i++) {
game.hands[role];
game.hands[role].push(draw_card(deck));
}
});
}
function start_year() {
log_h1('Year ' + game.year);
draw_hand_cards();
start_turn();
}
function start_turn() {
log_h2('Turn ' + game.turn);
const deck = fascist_decks[game.year];
const cardId = draw_card(deck);
game.current_events.push(cardId);
const card = cards[cardId];
log_h3('Fascist Event: ' + card.title);
game.engine = card.effects.map((_effect, index) => ({
t: leaf_node,
s: 'resolve_event',
p: game.initiative,
a: index,
}));
game.engine.push({
t: 'f',
f: 'setup_choose_card',
});
next();
}
states.add_glory = {
inactive: 'add tokens to the Bag of Glory',
prompt() {
view.prompt = 'Add tokens to the Bag of Glory';
gen_action('add_glory');
},
add_glory() {
console.log('add_glory');
let number = 1;
if (game.turn === 4) {
number++;
}
game.bag_of_glory[get_active_faction()] += number;
if (number === 1) {
log_h3(`${game.active} adds 1 token to the Bag of Glory`);
}
else {
log_h3(`${game.active} adds ${number} tokens to the Bag of Glory`);
}
resolve_active_and_proceed();
},
};
states.choose_area_ap = {
inactive: 'choose area to use Action Points',
prompt() {
view.prompt = 'Choose area of the board to affect';
for (let track_id of tracks) {
gen_action_standee(track_id);
}
},
standee(track_id) {
console.log('standee', track_id);
insert_after_active_node({
t: leaf_node,
p: get_active_faction_id(),
s: 'move_track_up_or_down',
a: {
track_id,
strength: get_active_node()?.a.strength,
},
});
resolve_active_and_proceed();
},
};
states.move_track_up_or_down = {
inactive: 'move a track',
prompt() {
const node = get_active_node();
view.prompt = `Move ${get_track_name(node.a.track_id)} up or down`;
gen_action('up');
gen_action('down');
},
down() {
const node = get_active_node();
move_track(node.a.track_id, -1 * node.a.strength);
resolve_active_and_proceed();
},
up() {
const node = get_active_node();
move_track(node.a.track_id, node.a.strength);
resolve_active_and_proceed();
}
};
states.choose_card = {
inactive: 'choose a card',
prompt() {
view.prompt = 'Choose a card to play this turn';
const hand = game.hands[player_faction_map[game.active]];
for (let c of hand)
gen_action_card(c);
},
card(c) {
log_h3(`${game.active} chooses a card`);
if (!game.cards_in_play) {
game.cards_in_play = {};
}
game.cards_in_play[player_faction_map[game.active]] = c;
resolve_active_and_proceed();
},
};
states.player_turn = {
inactive: 'play their turn',
prompt() {
const faction_id = get_faction_id(game.active);
const hero_points = game.hero_points[faction_id];
view.prompt =
hero_points === 0
? 'Play your card'
: 'Play your card or spend Hero points';
if (game.cards_in_play[faction_id] !== null) {
gen_action('play_for_ap');
gen_action('play_for_event');
}
else {
gen_action('done');
}
if (hero_points > 0) {
gen_action('spend_hp');
}
},
done() {
resolve_active_and_proceed();
},
play_for_ap() {
const faction_id = get_faction_id(game.active);
const card = game.cards_in_play[faction_id];
log_h3(`${game.active} plays ${cards[card].title} for the Action Points`);
if (!game.discard) {
game.discard = {
[ANARCHISTS_ID]: [],
[COMMUNISTS_ID]: [],
[MODERATES_ID]: [],
f: [],
};
}
array_remove(game.hands[faction_id], game.hands[faction_id].indexOf(card));
game.discard[faction_id].push(card);
insert_before_active_node({
t: leaf_node,
p: faction_id,
s: 'choose_area_ap',
a: {
strength: cards[card].strength,
},
});
next();
},
play_for_event() {
const faction_id = get_faction_id(game.active);
const card = game.cards_in_play[faction_id];
log_h3(`${game.active} plays ${cards[card].title} for the Event`);
game.cards_in_play[faction_id] = null;
game.tableaus[faction_id].push(card);
resolve_active_and_proceed();
},
spend_hp() {
insert_before_active_node({
t: leaf_node,
p: get_faction_id(game.active),
s: 'spend_hero_points',
});
next();
},
card(c) {
const faction = get_active_faction();
log_h3(`${game.active} plays ${cards[c].title} to their tableau`);
if (!game.tableaus) {
game.tableaus = {
[ANARCHISTS_ID]: [],
[COMMUNISTS_ID]: [],
[MODERATES_ID]: [],
};
}
game.cards_in_play[faction] = null;
game.tableaus[faction].push(c);
array_remove(game.hands[faction], game.hands[faction].indexOf(c));
resolve_active_and_proceed();
},
};
states.resolve_event = {
inactive: 'resolve Fascist Event',
prompt() {
const card = get_current_event();
const node = get_active_node(game.engine);
const effect = card.effects[node.a];
view.prompt = get_event_prompt(effect);
if (effect.type === 'attack' &&
(effect.target === 'd' || effect.target === 'v')) {
const fronts = get_fronts_closest_to(effect.target);
fronts.forEach((id) => gen_action('front', id));
}
else if (effect.type === 'attack') {
gen_action('front', effect.target);
}
else if (effect.type === 'track') {
gen_action('standee', effect.target);
}
else if (effect.type === 'hero_points' &&
effect.target === data_1.PLAYER_WITH_MOST_HERO_POINTS) {
const factions = get_factions_with_most_hero_poins();
for (let faction_id of factions) {
gen_action(get_player(faction_id));
}
}
},
front(f) {
const card = get_current_event();
const value = card.effects[get_active_node_args()].value;
game.fronts[f] -= value;
log_h3(`${value} attacks added to ${front_names[f]}`);
resolve_active_and_proceed();
},
standee(s) {
const effect = get_current_event().effects[get_active_node_args()];
const value = effect.value;
game.tracks[s] += value;
log_h3(`${track_names[effect.target]} decreased by ${Math.abs(value)}`);
resolve_active_and_proceed();
},
Anarchist() {
lose_hero_point(ANARCHISTS_ID, 1);
resolve_active_and_proceed();
},
Communist() {
lose_hero_point(ANARCHISTS_ID, 1);
resolve_active_and_proceed();
},
Moderate() {
lose_hero_point(ANARCHISTS_ID, 1);
resolve_active_and_proceed();
},
};
states.spend_hero_points = {
inactive: 'spend Hero points',
prompt() {
view.prompt = 'Spend your Hero points';
gen_action('done');
gen_action('draw_card');
},
done() {
resolve_active_and_proceed();
},
draw_card() {
game.hero_points[get_active_faction_id()]--;
log(`${game.active} draws a card`);
if (game.hero_points[get_active_faction_id()] === 0) {
resolve_active_and_proceed();
}
},
};
function pop_undo() {
const save_log = game.log;
const save_undo = game.undo;
game = save_undo.pop();
save_log.length = game.log;
game.log = save_log;
game.undo = save_undo;
}
function end_of_turn() {
log_h2('End of turn');
if (game.turn === 4) {
end_of_year();
}
else {
game.turn++;
start_turn();
}
}
function end_of_year() { }
function resolve_fascist_test() {
console.log('resolve fascist test');
log_h2('Fascist test is resolved');
next();
}
function move_track(track, change) {
}
function draw_card(deck) {
clear_undo();
let i = random(deck.length);
let c = deck[i];
set_delete(deck, c);
return c;
}
function draw_item(ids) {
let i = random(ids.length);
let r = ids[i];
set_delete(ids, r);
return r;
}
function lose_hero_point(faction, value) {
const points_lost = Math.min(game.hero_points[faction], value);
game.hero_points.pool += points_lost;
game.hero_points[faction] -= points_lost;
if (points_lost === 1) {
log(`${get_player(faction)} loses 1 Hero Point`);
}
else {
log(`${get_player(faction)} loses ${points_lost} Hero Points`);
}
}
function get_current_event() {
return cards[game.current_events[game.current_events.length - 1]];
}
function get_event_prompt(effect) {
let prompt = '';
switch (effect.type) {
case 'attack':
return 'Attack ' + front_names[effect.target];
case 'bonus':
break;
case 'hero_points':
return 'Select player with most Hero points';
case 'track':
return 'Decrease ' + track_names[effect.target];
}
return prompt;
}
function get_fronts_closest_to(target) {
const values = Object.values(game.fronts);
const targetValue = target === 'd' ? Math.min(...values) : Math.max(...values);
return Object.keys(game.fronts).filter((frontId) => game.fronts[frontId] === targetValue);
}
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 log_h1(msg) {
log_br();
log('.h1 ' + msg);
log_br();
}
function log_h2(msg) {
log_br();
log('.h2 ' + msg);
log_br();
}
function log_h3(msg) {
log_br();
log('.h3 ' + msg);
}
function clear_undo() {
console.log('game clear undo', game?.undo);
if (game?.undo && game.undo.length > 0)
game.undo = [];
}
function get_active_faction() {
return player_faction_map[game.active];
}
function get_active_faction_id() {
return player_faction_map[game.active];
}
function get_faction_id(player) {
return player_faction_map[player];
}
function get_player(faction_id) {
return faction_player_map[faction_id];
}
function get_player_order(first_player = game.initiative) {
const order = [];
let faction = first_player;
for (let i = 0; i < 3; ++i) {
order.push(faction);
faction =
game.year === 2
? get_previous_faction(faction)
: get_next_faction(faction);
}
return order;
}
function get_previous_faction(faction_id) {
const index = role_ids.indexOf(faction_id);
if (index === 0) {
return role_ids[2];
}
return role_ids[index - 1];
}
function get_next_faction(faction_id) {
const index = role_ids.indexOf(faction_id);
if (index === 2) {
return role_ids[0];
}
return role_ids[index + 1];
}
function get_factions_with_most_hero_poins() {
const most_hero_points = Math.max(...Object.values(game.hero_points));
const faction_ids = [];
Object.entries(game.hero_points).forEach(([faction, hero_points]) => {
if (hero_points === most_hero_points) {
faction_ids.push(faction);
}
});
return faction_ids;
}
function get_track_name(track_id) {
return track_names[track_id];
}
function make_list(first, last) {
let list = [];
for (let i = first; i <= last; i++)
list.push(i);
return list;
}
function random(range) {
return (game.seed = (game.seed * 200105) % 34359738337) % range;
}
function set_delete(set, item) {
let a = 0;
let b = set.length - 1;
while (a <= b) {
let m = (a + b) >> 1;
let x = set[m];
if (item < x)
b = m - 1;
else if (item > x)
a = m + 1;
else {
array_remove(set, m);
return;
}
}
}
function array_remove(array, index) {
let n = array.length;
for (let i = index + 1; i < n; ++i)
array[i - 1] = array[i];
array.length = n - 1;
}
function array_insert(array, index, item) {
for (let i = array.length; i > index; --i)
array[i] = array[i - 1];
array[index] = item;
}
|