Repository navigation
Expand file tree
/
Copy pathphase.js
More file actions
1934 lines (1861 loc) · 66.2 KB
/
Copy pathphase.js
File metadata and controls
1934 lines (1861 loc) · 66.2 KB
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//PHASES and subphases
/**
* Defines and responds to commands and effects
*
* @class Phase
* @param {Player} player which turn this is in, corp or runner
* @param {String} title formal phase as per rulebook
* @param {String} identifier subphase e.g. as identified p32
* @param {function} Init code executed at start of subphase
* @param {function[]} checks checks for commands defined as functions
* @param {function[]} commands commands defined as functions (in addition to defaults)
* @param {Phase} next default subphase to follow this
*/
var phases = {};
var currentPhase = null;
function TurnPhaseStr() {
return currentPhase.title; //+" ("+PlayerName(activePlayer)+")";//" > ";
}
//**Phase templates
var phaseTemplates = {};
/**
* Creates a new game phase based on a previously defined phase object
*
* @class CreatePhaseFromTemplate
* @param {Phase} template phase to copy
* @param {Player} player corp or runner
* @param {String} title formal phase as per rulebook
* @param {String} identifier subphase e.g. as identified p32
* @param {Phase} next default subphase to follow this
* @returns {Phase} newly created phase object
*/
function CreatePhaseFromTemplate(template, player, title, identifier, next) {
var ret = jQuery.extend(true, {}, template);
ret.player = player;
ret.title = title;
ret.identifier = identifier;
ret.next = next;
return ret;
}
//mulligan template
phaseTemplates.mulligan = {
player: null,
Resolve: {
m: function () {
SetHistoryThumbnail("", "Mulligan");
Log(PlayerName(activePlayer) + " took a mulligan");
Mulligan();
},
n: function () {
SetHistoryThumbnail("", "Keep");
Log(PlayerName(activePlayer) + " chose to keep");
IncrementPhase();
},
},
};
//response template (rez/trigger/score)
//This handles the respond/decline loop because the player turn is indicated in phase.player, and activePlayer is set to this during ChangePhase
// - http://ancur.wikia.com/wiki/Paid_Abilities_Primer
//If a player Resolves n after acting, activePlayer is toggled (no phase change)
//This template also contains some phase-specific checks to make things easier
phaseTemplates.standardResponse = {
Init: function () {
if (!currentPhase.lessOpportunities) opportunitiesGiven = false;
else opportunitiesGiven = true;
actedThisPhase = false;
if (currentPhase.identifier == "Corp 1.1") {
ResetClicks(corp);
}
else if (currentPhase.identifier == "Runner 1.1") {
ResetClicks(runner);
}
else if (currentPhase.identifier == "Run 2.1") {
//Run: Approach Ice (Nisei 2021 2.1)
//if outermost ice, do not show thumbnail (there will already be one for run events, triggers, etc)
var needsThumbnail = true;
if (approachIce == attackedServer.ice.length - 1) {
needsThumbnail = false;
//alternative rule: only show if there is not already a thumbnail
//if ($("#history").children().first().children().first().children().length > 1) needsThumbnail = false;
}
if (needsThumbnail) {
if (attackedServer.ice[approachIce].rezzed)
SetHistoryThumbnail(attackedServer.ice[approachIce].imageFile, "");
else
SetHistoryThumbnail(
"Corp_back.png",
"Approach",
"transform: rotate(90deg);"
);
}
UnbreakAll(attackedServer.ice[approachIce]); //subroutines start unbroken
if (approachIce == attackedServer.ice.length - 1)
Log(
"Approaching outermost piece of ice protecting " +
attackedServer.serverName
);
else
Log(
"Approaching next piece of ice protecting " +
attackedServer.serverName
);
//TODO cardApproached would trigger here but not implemented yet
} else if (currentPhase.identifier == "Run 3.1") {
//Run: Encounter Ice (Nisei 2021 3.1)
SetHistoryThumbnail(
attackedServer.ice[approachIce].imageFile,
"",
"transform: rotate(90deg);"
);
Log("Encountering " + GetTitle(attackedServer.ice[approachIce], true));
encountering = true;
//first the automatic triggers
AutomaticTriggers("automaticOnEncounter", [attackedServer.ice[approachIce]]);
//then the Enumerate ones
//currently giving whoever's turn it is priority...not sure this is always going to be right
TriggeredResponsePhase(playerTurn, "responseOnEncounter", [attackedServer.ice[approachIce]], function () {}, "Encounter");
} else if (currentPhase.identifier == "Run 4.3") {
//Run: Movement (Nisei 2021 4.3)
movement = true;
}
},
Enumerate: {
rez: function () {
if (activePlayer == corp) {
if (currentPhase.corpSkip) return [];
var ret = ChoicesInstalledCards(corp, function (card) {
//corp can always rez upgrades and assets (except for usability)
var validRezTypes = ["upgrade", "asset"];
//but only the approached (not encountered) ice
if (CheckApproach() && approachIce < attackedServer.ice.length) {
if (card == attackedServer.ice[approachIce]) validRezTypes.push("ice");
}
//check rezzability inc. costs
return FullCheckRez(card, validRezTypes);
});
return ret;
} else return [];
},
trigger: function () {
if (activePlayer == corp && currentPhase.corpSkip) return [];
return ChoicesTriggerableAbilities(activePlayer);
},
},
Resolve: {
n: function () {
if (activePlayer == corp && currentPhase.corpSkip)
currentPhase.corpSkip = false;
if (activePlayer != currentPhase.player) opportunitiesGiven = true; //each player gets at least one chance to act
if (opportunitiesGiven && !actedThisPhase) {
//checkpoint for remote attackingServer destroyed (run ends after paid ability window closes)
//see Nisei CR1.5 4.6.8e
//note that paid ability windows during run are: 2b, 3b, 4b, 4e
if (attackedServer && typeof attackedServer.cards == 'undefined') {
var runPaidAbilityWindows = ["Run 2.1", "Run 3.1", "Run 4.3", "Run 4.5"];
if (runPaidAbilityWindows.includes(currentPhase.identifier)) {
if (attackedServer.ice.length == 0 && attackedServer.root.length == 0) {
//end the run now, the server is gone
attackedServer=null;
ChangePhase(phases.runEnds);
return;
}
}
}
//player declined to act, phase ends
if (currentPhase.identifier == "Run 2.1") {
//Run: Approach paid ability window (Nisei 2021 2.2)
if (attackedServer.ice[approachIce].rezzed) {
ChangePhase(phases.runEncounterIce); //ice at this position is rezzed, encounter it (Nisei 2021 2.3.1)
return;
}
Log("Corp did not rez ice");
ChangePhase(phases.runPassesIce);
return;
} else if (currentPhase.identifier == "Run 3.1") {
//Run: Encounter triggers fire (Nisei 2021 3.1)
subroutine = 0;
ChangePhase(phases.runSubroutines); //3.1 included Nisei 3.2 runner can use paid abilities (e.g. break subroutines) so now time to fire them
return;
} else if (currentPhase.identifier == "Run 4.5") {
//Run: Movement phase ends (Nisei 2021 4.6)
movement = false;
if (forceNextIce !== null) {
approachIce = forceNextIce;
forceNextIce = null;
} else approachIce--;
if (approachIce > -1) ChangePhase(phases.runApproachIce);
//runners position has moved but approach triggers fire after paid ability window
else ChangePhase(phases.runApproachServer); //by default approach server
return;
}
IncrementPhase();
} else {
//player acted, give other player a chance to respond
if (activePlayer == corp) activePlayer = runner;
else activePlayer = corp;
actedThisPhase = false;
}
},
rez: function (params) {
//the false here means don't ignore costs
Rez(params.card, false, function() {
actedThisPhase = true;
}, this);
},
trigger: function (params) {
/* old code
Trigger(params.card, params.ability, params.choice);
*/
TriggerAbility(params.card, params.ability);
actedThisPhase = true;
},
},
};
//Some response phases don't allow the corp to rez anything (these are only ever in runner turn)
phaseTemplates.noRezResponse = CreatePhaseFromTemplate(
phaseTemplates.standardResponse,
runner,
"",
"",
null
);
phaseTemplates.noRezResponse.Enumerate.rez = function () {
return [];
};
//corp could-score response template (rez/trigger/score)
phaseTemplates.corpScorableResponse = CreatePhaseFromTemplate(
phaseTemplates.standardResponse,
corp,
"",
"",
null
);
phaseTemplates.corpScorableResponse.Enumerate.score = function () {
if (activePlayer != corp) return [];
else if (currentPhase.corpSkip) return [];
var ret = [];
//for each remote server...
for (var i = 0; i < corp.remoteServers.length; i++) {
//for each installed agenda...
for (var j = 0; j < corp.remoteServers[i].root.length; j++) {
//if can score (advancement requirement met, not forbidden, etc), add to ret
var thecard = corp.remoteServers[i].root[j];
if (CheckScore(thecard))
ret.push({ card: thecard, label: GetTitle(thecard, true) });
}
}
return ret;
};
phaseTemplates.corpScorableResponse.Resolve.score = function (params) {
Score(params.card);
};
//discard template
phaseTemplates.discardStart = {
Enumerate: {
n: function () {
var discardDownTo = MaxHandSize(activePlayer);
if (discardDownTo < 0) discardDownTo = 0;
if (ActivePlayerHand().length > discardDownTo) return [];
else return [{}];
},
discard: function () {
var discardDownTo = MaxHandSize(activePlayer);
if (discardDownTo < 0) discardDownTo = 0;
if (ActivePlayerHand().length > discardDownTo) {
var ret = ChoicesHandCards(activePlayer);
return ret;
} else return [];
},
},
Resolve: {
n: function () {
if (activePlayer == runner) {
if (MaxHandSize(runner) < 0) {
//if runner has a maximum hand size of less than zero at the end of his turn, then he is flatlined and the Corporation wins the game.
PlayerWin(corp, "Runner flatlined");
return;
}
}
IncrementPhase();
},
discard: function (params) {
Discard(params.card);
},
},
};
//helper function for BuildGlobalTriggerList
function AddTriggersToTriggerList(triggerName) {
var initialList = ChoicesActiveTriggers(triggerName, activePlayer);
//immediately activate automatic ones and remove from list
for (var i = 0; i < initialList.length; i++) {
if (initialList[i].card[triggerName].automatic) {
if (typeof initialList[i].card[triggerName].Enumerate == "function")
LogError(
"." +
triggerName +
".Enumerate on " +
GetTitle(initialList[i].card) +
" will be ignored because it is set to automatic (consider moving the checks into .Resolve)"
);
var oldPhase = currentPhase;
initialList[i].card[triggerName].Resolve.call(initialList[i].card);
//phase shouldn't change during automatic triggers (except for game end)
if (oldPhase !== currentPhase && currentPhase.instruction !== "Game Over") {
LogError(
"." +
triggerName +
" on " +
GetTitle(initialList[i].card) +
" should not be automatic (it fires " +
currentPhase.triggerCallbackName +
" which might require a decision)"
);
//and avoid crash by moving the trigger to the proper list (this is not expected to behave properly)
currentPhase = oldPhase;
currentPhase.triggerList.push(initialList[i]);
}
} else {
currentPhase.triggerList.push(initialList[i]);
}
}
}
//subutility for GlobalTriggersPhaseCommonResolveN
function BuildGlobalTriggerList() {
currentPhase.triggerList = [];
if (!currentPhase.buildTriggerList) return;
AddTriggersToTriggerList(currentPhase.triggerCallbackName);
AddTriggersToTriggerList(currentPhase.triggerSecondCallbackName);
}
//phase utility for phaseTemplates.globalTriggers
function GlobalTriggersPhaseCommonResolveN(
skipInit,
afterOpportunity,
context
) {
//afterOpportunity is called after BOTH players are done
//if only the first player's triggers have resolved, switch active player and resolve theirs
if (currentPhase.player == playerTurn) {
//swap player
if (currentPhase.player == corp) currentPhase.player = runner;
else currentPhase.player = corp;
activePlayer = currentPhase.player;
//rebuild trigger list
BuildGlobalTriggerList();
}
//both players triggers have been resolved, finish this pseudophase
else {
//now move to next phase (or return to previous)
IncrementPhase(skipInit);
if (typeof afterOpportunity === "function") {
afterOpportunity.call(context);
}
}
}
//template for phases which trigger simultaneously on any number of cards
//first the player whose turn it is, then the other player
//NOTE: triggerCallbackName must be set
phaseTemplates.globalTriggers = {
triggerList: [],
triggerEnumerateParams: [],
triggerCallbackName: "NOT SET",
triggerSecondEnumerateParams: [],
triggerSecondCallbackName: "NOT SET",
buildTriggerList: true,
Init: function () {
currentPhase.buildTriggerList = true;
//follow priority rules
//https://ancur.fandom.com/wiki/Timing_Priority
activePlayer = currentPhase.player = playerTurn;
//if start of turn, replenish recurring credits
//as far as I can tell, this is correctly implemented as per Nisei CR 1.5
//(recurring credits are replenished after gaining allotted clicks and paid ability window, but before turn begins triggers fire)
if (
currentPhase.identifier == "Corp 1.2" ||
currentPhase.identifier == "Runner 1.2"
) {
ApplyToAllActiveCards(function (card) {
if (typeof card.recurringCredits !== "undefined")
card.credits = card.recurringCredits;
}, currentPhase.player);
//AI keeps track of how many turns (each player's turn counts as 1) a card (agenda/asset/upgrade) has been installed a server
for (var i=0; i<corp.remoteServers.length; i++) {
for (var j=0; j<corp.remoteServers[i].root.length; j++) {
if (typeof corp.remoteServers[i].root[j].AITurnsInstalled !== "undefined")
corp.remoteServers[i].root[j].AITurnsInstalled++;
else corp.remoteServers[i].root[j].AITurnsInstalled = 1;
}
}
//log some stuff for debugging purposes (console/captured only, not to history bar)
console.log("SPOILER: At start of "+currentPhase.identifier.substring(0,currentPhase.identifier.length-4)+" turn:");
console.log("SPOILER: Corp has "+corp.creditPool+" credit(s) and "+corp.HQ.cards.length+" card(s) in hand: "+JSON.stringify(corp.HQ.cards));
console.log("SPOILER: Runner has "+runner.creditPool+" credit(s), "+runner.tags+" tag(s) and "+runner.grip.length+" card(s) in hand: "+JSON.stringify(runner.grip));
}
//end of encounter needs to...end the encounter
if (currentPhase.identifier == "Run EncounterEnd") encountering = false;
//log approach to server
if (currentPhase.identifier == "Run 4.6.2") {
Log("Approaching " + attackedServer.serverName);
approachIce = -1;
}
//log successful run
if (currentPhase.identifier == "Run 5.1") {
//first check if run can be "declared successful"
var modifySuccess = ModifyingTriggers("modifyDeclareSuccess", null, 0); //null means no parameter is sent, lower limit of 0 means the total will not be any lower than zero
//in this case a result of 0 means no prevention (i.e. not modified)
if (modifySuccess == 0) {
//fire any pre-success triggers
AutomaticTriggers("automaticOnWouldDeclareSuccess", []);
}
//check modify again in case the pre-success changed it
var modifySuccess = ModifyingTriggers("modifyDeclareSuccess", null, 0); //null means no parameter is sent, lower limit of 0 means the total will not be any lower than zero
//still can be declared successful? ok!
if (modifySuccess == 0) {
//now declare successful
Log("Run successful");
//and fire the pre-phase succesful automatics
AutomaticTriggers("automaticOnRunSuccessful", [attackedServer]);
}
else currentPhase.buildTriggerList = false;
//store a little extra info to help AIs with decisionmaking
//note that AI currently includes even successful runs that are not "declared" successful
if (
typeof attackedServer.cards !== "undefined" ||
attackedServer.root.length > 0
) {
//ignore random runs on empty remotes
if (typeof attackedServer.AISuccessfulRuns !== "undefined")
attackedServer.AISuccessfulRuns++;
else attackedServer.AISuccessfulRuns = 1;
}
}
//build trigger list (will just empty the list if currentPhase.buildTriggerList is false)
BuildGlobalTriggerList();
},
PreEnumerate: function () {
//only do this once
if (currentPhase.preEnumerated) return;
//log run end
if (currentPhase.identifier == "Run 6.4") {
Log("Run ends");
accessedCards = {root: [], cards: []};
}
},
Enumerate: {
trigger: function () {
return ValidateTriggerList(
currentPhase.triggerList,
currentPhase.triggerCallbackName,
currentPhase.triggerEnumerateParams,
currentPhase.triggerSecondCallbackName,
currentPhase.triggerSecondEnumerateParams
);
},
n: function () {
if (
ValidateTriggerList(
currentPhase.triggerList,
currentPhase.triggerCallbackName,
currentPhase.triggerEnumerateParams,
currentPhase.triggerSecondCallbackName,
currentPhase.triggerSecondEnumerateParams
).length < 1
) {
//special case: instead of breaching (Runner only)
if (currentPhase.triggerCallbackName == "responseOnRunSuccessful" && currentPhase.player == runner) {
var iobChoices = ChoicesActiveTriggers("specialOnInsteadOfBreaching", runner);
//that gives a list of cards but we still need to use Enumerate to remove impossibles and force required
var iobRequired = false;
for (var i=iobChoices.length-1; i>-1; i--) {
var thisCardChoices = [{}]; //assume valid nonrequired choice if no Enumerate
if (typeof iobChoices[i].card.specialOnInsteadOfBreaching.Enumerate == 'function') {
thisCardChoices = iobChoices[i].card.specialOnInsteadOfBreaching.Enumerate.call(iobChoices[i].card);
}
//if not valid, remove from list
if (thisCardChoices.length < 1) {
iobChoices.splice(i,1);
}
else {
//just check first option for required
if (thisCardChoices[0].required) iobRequired = true;
}
}
var replaceBreach = false;
if (iobChoices.length > 0) replaceBreach = true;
if (!iobRequired) {
iobChoices.push({label:"Breach", button:"Breach"});
}
if (replaceBreach) return iobChoices;
}
return [{}]; //breach
}
return []; //runSuccessful triggers remain unresolved, cannot breach yet
},
},
Resolve: {
trigger: function (params) {
var card = params.card;
var callbackName = currentPhase.triggerCallbackName;
//override name if defined by params
if (typeof params.callbackName != 'undefined') {
callbackName = params.callbackName;
}
var triggerCallback = card[callbackName];
currentPhase.triggerList.splice(params.id, 1); //remove from triggers list (only trigger once)
var instruction = GetTitle(card, true);
if (typeof triggerCallback.text !== "undefined")
instruction = triggerCallback.text;
else instruction = GetTitle(card, true);
var choices = [{}]; //assume valid by default
if (typeof triggerCallback.Enumerate === "function") {
var enumerateParams = currentPhase.triggerEnumerateParams;
//override enumerate params if defined
if (typeof params.enumerateParams != 'undefined') {
enumerateParams = params.enumerateParams;
}
choices = triggerCallback.Enumerate.apply(card,enumerateParams);
}
var decisionPhase = DecisionPhase(
card.player,
choices,
triggerCallback.Resolve,
instruction,
instruction,
card
);
if (typeof triggerCallback.footerText !== "undefined")
decisionPhase.footerText = triggerCallback.footerText;
},
n: function (params) {
//if a card was specified, at the moment the only reason a breach replacement
if (typeof params.card == 'undefined') GlobalTriggersPhaseCommonResolveN();
else {
ChangePhase(phases.runEnds); //skip breach
//Enumerate the card and do a decision phase to replace breach
var card = params.card;
var instruction = GetTitle(card);
var choices = [{}]; //assume valid by default
if (typeof card.specialOnInsteadOfBreaching.Enumerate === "function")
choices = card.specialOnInsteadOfBreaching.Enumerate.call(card);
DecisionPhase(
card.player,
choices,
card.specialOnInsteadOfBreaching.Resolve,
instruction,
instruction,
card
);
}
},
},
text: {},
chosenString: "triggered",
};
var forcePreventCombinePhase = false;
function CombineResponseWithPhase(
responsePhase,
combineWith,
nextIfException,
nextIfCombine,
exceptionCondition,
disallowCombinedN = true
) {
responsePhase.backup = {};
responsePhase.backupStored = false;
responsePhase.combineWith = combineWith;
responsePhase.exceptionCondition = exceptionCondition;
responsePhase.PreEnumerate = function () {
//since this is post-action, this is also where we clean up any resolving cards that still remain
while (corp.resolvingCards.length > 0) {
MoveCard(corp.resolvingCards[0], corp.archives.cards);
}
//check whether we can use a combined phase (see below)
var combineResponse = false;
//console.log(ChoicesTriggerableAbilities(runner));
if (corp.AI == null && !forcePreventCombinePhase)
combineResponse = ChoicesTriggerableAbilities(runner).length == 0; //i.e. whether runner has potential actions - corp needs to decide whether to respond
if (exceptionCondition()) {
//e.g. if out of clicks, time to move on, or need to discard, can't skip that phase
currentPhase.next = nextIfException;
combineResponse = false;
} else {
if (combineResponse) currentPhase.next = nextIfCombine;
else currentPhase.next = combineWith;
}
//special things to simplify UI when playing as Corp
//specifically, we add the properties of currentPhase.combineWith into currentPhase
//a more simple approach would be to use a pre-created phase for this - but then anything that modified either phase wouldn't affect it
currentPhase.phaseNotCombined=true;
if (corp.AI == null && !forcePreventCombinePhase) {
if (combineResponse) {
currentPhase.phaseNotCombined=false;
//if the current phase has not yet been backed up, store any properties that will be overwritten
//do this by iterating through currentPhase.combineWith and storing currentPhases's property, if defined
if (!currentPhase.backupStored) {
for (prop in currentPhase.combineWith) {
if (typeof currentPhase[prop] !== "undefined") {
if (prop == "Enumerate" || prop == "Resolve" || prop == "text") {
//iterate these at sub-level
currentPhase.backup[prop] = {}; //we need a fresh copy so we don't modify them in the original phase
for (subprop in currentPhase.combineWith[prop]) {
if (subprop != "trigger") {
//for trigger, keep original function i.e. don't limit to click abilities
if (typeof currentPhase[prop][subprop] !== "undefined") {
//needs to be backed up
currentPhase.backup[prop][subprop] =
currentPhase[prop][subprop];
}
}
}
} else if (prop != "next") {
//don't modify this, allow it to be set above
if (typeof currentPhase[prop] !== "undefined") {
//needs to be backed up
currentPhase.backup[prop] = currentPhase[prop];
}
}
}
}
}
//now copy properties from currentPhase.combineWith into currentPhase
for (prop in currentPhase.combineWith) {
if (prop == "Enumerate" || prop == "Resolve" || prop == "text") {
//iterate these at sub-level
if (typeof currentPhase[prop] == "undefined")
currentPhase[prop] = {};
for (subprop in currentPhase.combineWith[prop]) {
if (subprop != "trigger") {
//for trigger, keep original function i.e. don't limit to click abilities
currentPhase[prop][subprop] =
currentPhase.combineWith[prop][subprop];
}
}
} else if (prop != "next") {
//don't modify this, allow it to be set above
currentPhase[prop] = currentPhase.combineWith[prop];
}
}
if (disallowCombinedN)
currentPhase.Enumerate.n = function () {
return [];
};
currentPhase.backupStored = true;
executingCommand = "n"; //we are not mid-action
} //not combineResponse
else {
//if the current phase has a stored backup, restore any properties
//and delete properties that are not present in the backup
if (currentPhase.backupStored) {
//if a backup has been stored, restore from it
if (currentPhase.backupStored) {
for (prop in currentPhase.combineWith) {
if (typeof currentPhase.backup[prop] !== "undefined") {
//exists in backup, we will copy from it
if (
prop == "Enumerate" ||
prop == "Resolve" ||
prop == "text"
) {
//iterate these at sub-level
for (subprop in currentPhase.combineWith[prop]) {
if (subprop != "trigger") {
//for trigger, keep original function i.e. don't limit to click abilities
if (
typeof currentPhase.backup[prop][subprop] !==
"undefined"
)
currentPhase[prop][subprop] =
currentPhase.backup[prop][subprop];
else delete currentPhase[prop][subprop]; //wasn't backed up so it should be removed
}
}
}
} else if (prop != "next") {
//next wasn't backed up but should be left alone
delete currentPhase[prop]; //wasn't backed up so it should be removed
}
}
}
}
if (disallowCombinedN)
currentPhase.Enumerate.n = function () {
return [{}];
};
}
}
};
}
//ACTUAL PHASES IMPLEMENTATION BELOW (templates above)
//Corp Mulligan
phases.corpMulligan = CreatePhaseFromTemplate(
phaseTemplates.mulligan,
corp,
"Corporation's Mulligan",
"Corp Mulligan",
null
);
phases.corpMulligan.historyBreak = {
title: "Corporation's Mulligan",
style: "small",
};
//Runner Mulligan
phases.runnerMulligan = CreatePhaseFromTemplate(
phaseTemplates.mulligan,
runner,
"Runner's Mulligan",
"Runner Mulligan",
null
);
phases.runnerMulligan.historyBreak = {
title: "Runner's Mulligan",
style: "small",
};
//Start of Draw Phase
phases.corpStartDraw = CreatePhaseFromTemplate(
phaseTemplates.corpScorableResponse,
corp,
"Corporation's Draw Phase",
"Corp 1.1",
null
);
phases.corpStartDraw.historyBreak = {
title: "Corporation's Draw Phase",
style: "small",
};
//"When your turn begins" conditionals meet their trigger conditions
phases.corpTurnBegin = CreatePhaseFromTemplate(
phaseTemplates.globalTriggers,
corp,
"Corporation's Turn Begins",
"Corp 1.2",
null
);
phases.corpTurnBegin.triggerCallbackName = "responseOnCorpTurnBegins";
//End of Draw Phase
phases.corpEndDraw = {
player: corp,
title: "Corporation's Draw Phase",
identifier: "Corp 1.3",
Init: function () {
SetHistoryThumbnail("Corp_back.png", "Draw");
}, //auto-draw
Resolve: {
n: function () {
Draw(corp, 1, function(){
IncrementPhase();
},this);
},
},
};
//Start of Action Phase
phases.corpActionStart = CreatePhaseFromTemplate(
phaseTemplates.corpScorableResponse,
corp,
"Corporation's Action Phase",
"Corp 2.1",
null
);
//Take action
phases.corpActionMain = {
player: corp,
title: "Corporation's Action Phase",
identifier: "Corp 2.2",
Enumerate: {
gain: function () {
if (CheckActionClicks(corp, 1)) {
return [{}];
} else return [];
},
draw: function () {
if (CheckActionClicks(corp, 1)) {
return [{}];
} else return [];
},
install: function () {
if (CheckActionClicks(corp, 1)) {
var ret = ChoicesHandInstall(corp);
return ret;
} else return [];
},
play: function () {
var ret = [];
var hand = PlayerHand(corp);
for (var i = 0; i < hand.length; i++) {
var card = hand[i];
if (FullCheckPlay(card))
ret.push({ card: card, label: GetTitle(card, true) });
}
return ret;
},
advance: function () {
var ret = [];
if (CheckActionClicks(corp, 1)) {
var possibleAdvance = ChoicesInstalledCards(corp, CheckAdvance);
for (var i = 0; i < possibleAdvance.length; i++) {
if (CheckCredits(corp, 1, "advancing", possibleAdvance[i].card)) {
//for usability, don't overadvance
var allowAdvance = true;
if (
typeof possibleAdvance[i].card.advancementRequirement !==
"undefined"
) {
if (
!possibleAdvance[i].card.AIOverAdvance &&
Counters(possibleAdvance[i].card, "advancement") >=
AdvancementRequirement(possibleAdvance[i].card)
)
allowAdvance = false;
}
if (allowAdvance) ret.push(possibleAdvance[i]);
}
}
}
return ret;
},
trash: function () {
var ret = [];
if (CheckTags(1)) {
if (CheckActionClicks(corp, 1)) {
for (var i = 0; i < runner.rig.resources.length; i++) {
var card = runner.rig.resources[i];
if (CheckTrash(card)) {
if (CheckCredits(corp, 2, "trashing", card))
ret.push({ card: card, label: GetTitle(card, true) });
}
}
}
}
return ret;
},
trigger: function () {
return ChoicesTriggerableAbilities(corp, "click"); //click abilities only
},
purge: function () {
//the agenda point check is so purge isn't available for the tutorial decks
if (CheckActionClicks(corp, 3) && globalProperties.agendaPointsToWin == 7) {
if (CheckPurge()) return [{}];
}
return [];
},
n: function () {
if (!CheckActionClicks(corp, 1)) {
return [{}];
} else return [];
},
},
Resolve: {
draw: function () {
SetHistoryThumbnail("Corp_back.png", "Draw");
BasicActionDraw(corp, function() {
//next phase will be undefined if the Corp lost by attempting to draw from empty R&D
if (typeof currentPhase.next != 'undefined') IncrementPhase();
});
},
gain: function () {
SetHistoryThumbnail("credit.png", "Gain");
SpendClicks(corp, 1);
GainCredits(corp, 1);
IncrementPhase();
},
install: function (params) {
Install(
params.card,
params.server,
false,
null,
false,
function () {
if (CheckCardType(params.card,["ice"])) SetHistoryThumbnail(
"Corp_back.png",
"Install",
"transform: rotate(90deg);"
);
else SetHistoryThumbnail("Corp_back.png", "Install");
SpendClicks(corp, 1);
},
this
);
},
play: function (params) {
Play(
params.card,
function (finishResolve) {
SetHistoryThumbnail(params.card.imageFile, "Play");
if (CheckSubType(params.card, "Double")) SpendClicks(corp, 2);
else SpendClicks(corp, 1);
SpendCredits(
corp,
PlayCost(params.card),
"playing",
params.card,
function () {
IncrementPhase(); //set new phase first in case card play changes phase
finishResolve();
},
this
);
},
this
);
},
advance: function (params) {
SetHistoryThumbnail("Corp_back.png", "Advance");
SpendClicks(corp, 1);
SpendCredits(
corp,
1,
"advancing",
params.card,
function () {
IncrementPhase();
Advance(params.card);
},
this
);
},
trash: function (params) {
SetHistoryThumbnail(params.card.imageFile, "Trash");
SpendClicks(corp, 1);
SpendCredits(
corp,
2,
"trashing",
params.card,
function () {
IncrementPhase(); //set new phase first since trash uses a pseudophase
Trash(params.card, true);
},
this
);
},
trigger: function (params) {
TriggerAbility(
params.card,
params.ability,
function () {
SetHistoryThumbnail(params.card.imageFile, "Use");
IncrementPhase(); //set new phase first in case ability trigger changes phase
},
this
);
},
purge: function () {
SetHistoryThumbnail("generic_red.png", "Purge");
SpendClicks(corp, 3);
IncrementPhase();
Purge();
},
n: function() {
IncrementPhase();
},
},
text: {
draw: "[click]: Draw a card",
gain: "[click]: Gain one credit",
install: "[click]: Install an agenda, asset, upgrade or ice",
play: "[click]: Play an operation",
advance: "[click], 1[c]: Advance a card",
trash: "[click], 2[c]: Trash a resource",
trigger: "Trigger a [click] ability",
purge: "[click],[click],[click]: Purge virus counters",
},
};
phases.corpActionMain.historyBreak = {
title: "Corporation's Action Phase",
style: "large",
};
//After each action
phases.corpPostAction = CreatePhaseFromTemplate(
phaseTemplates.corpScorableResponse,
corp,
"Corporation's Action Phase",
"Corp 2.2*",
null
);