-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardEvaluator.js
1257 lines (1098 loc) · 48.9 KB
/
CardEvaluator.js
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
import _ from 'lodash';
import loglevel from 'loglevel-decorator';
import uniqueId from 'unique-id';
import EvalError from './EvalError.js';
import Statuses from '../models/Statuses.js';
import PieceAction from '../actions/PieceAction.js';
import DrawCard from '../actions/DrawCard.js';
import DiscardCard from '../actions/DiscardCard.js';
import GiveCard from '../actions/GiveCard.js';
import ShuffleToDeck from '../actions/ShuffleToDeck.js';
import Message from '../actions/Message.js';
import ActionTimer from '../actions/ActionTimer.js';
import CharmPiece from '../actions/CharmPiece.js';
import SetPlayerResource from '../actions/SetPlayerResource.js';
import PieceHealthChange from '../actions/PieceHealthChange.js';
import PieceDestroyed from '../actions/PieceDestroyed.js';
import PieceStatusChange from '../actions/PieceStatusChange.js';
import PieceAttributeChange from '../actions/PieceAttributeChange.js';
import SpawnPiece from '../actions/SpawnPiece.js';
import PieceBuff from '../actions/PieceBuff.js';
import PieceAura from '../actions/PieceAura.js';
import CardAura from '../actions/CardAura.js';
import MovePiece from '../actions/MovePiece.js';
import TransformPiece from '../actions/TransformPiece.js';
import ChargeUp from '../actions/ChargeUp.js';
import PieceArmorChange from '../actions/PieceArmorChange.js';
import AttachCode from '../actions/AttachCode.js';
import UnsummonPiece from '../actions/UnsummonPiece.js';
import Choose from '../actions/Choose.js';
import TilesCleared from '../actions/TilesCleared.js';
/**
* Evaluate the scripts on cards
*/
@loglevel
export default class CardEvaluator{
constructor(queue, selector, pieceState, mapState){
this.queue = queue;
this.selector = selector;
this.pieceState = pieceState;
this.mapState = mapState;
this.startTurnTimers = [];
this.endTurnTimers = [];
this.eventDefaultSelectors = {
playMinion: {left: 'SELF'},
death: {left: 'SELF'},
damaged: {left: 'SELF'},
healed: {left: 'SELF'},
attacks: {left: 'SELF'},
ability: {left: 'SELF'},
cardDrawn: {left: 'PLAYER'},
playSpell: {left: 'PLAYER'},
chargeChange: {left: 'PLAYER'}
};
this.targetableActions = [
'Hit',
'Heal',
'SetAttribute',
'Buff',
'GiveStatus',
'RemoveStatus',
'Charm',
'Destroy',
'Move',
'Transform',
'GiveCard',
'ShuffleToDeck',
'Spawn',
'Unsummon',
'startTurnTimer',
'endTurnTimer'
];
this.targetableEvents = ['playMinion', 'playSpell'];
}
//evaluate an event that directly relates to a piece, i.e. the piece dies
evaluatePieceEvent(event, activatingPiece, pieceEventParams){
this.log.info('Eval piece event %s activating piece: %j', event, activatingPiece);
let evalActions = [];
let {targetPieceId, position, pivotPosition, chooseCardTemplateId} = pieceEventParams || {};
if(event === 'death'){
this.cleanupTimers(activatingPiece);
}
//in the pieces case, if this is a spawn piece event the evaluator has the chance to return false
//and scrub the spawn of the piece, so the activating piece isn't in the piece state yet.
//However, include it in the loop so its events will be evaluated
let pieces = new Set([...this.pieceState.pieces, activatingPiece]);
//first look through all the pieces on the board to see if any have actions on this event
for(let piece of pieces){
if(!piece.events || piece.events.length === 0) continue;
//find all actions for this event, there could be more than one
for(let cardEvent of piece.events){
if(cardEvent.event !== event) continue;
let pieceSelectorParams = {selfPiece: piece, activatingPiece, controllingPlayerId: piece.playerId};
if(cardEvent.condition){
let compareResult = this.selector.compareExpression(cardEvent.condition, this.pieceState, pieceSelectorParams, this.selector.selectPieces);
if(compareResult.length === 0){
continue;
}
}
//see if the selector matches up for this piece
let eventSelector = this.eventSelector(cardEvent);
if(!eventSelector){
eventSelector = this.eventDefaultSelectors[event];
if(!eventSelector){
throw 'Need default selector for ' + event;
}
}
//now find all pieces that match the selector given the context of the piece that the event is for
let piecesSelected = this.selector.selectPieces(eventSelector, pieceSelectorParams);
let selectorMatched = piecesSelected.indexOf(activatingPiece) > -1;
//if the activating piece is part of the selected pieces, add it to the list of actions
if(selectorMatched){
for(let cardEventAction of cardEvent.actions){
evalActions.push({
piece: piece,
playerId: activatingPiece.playerId,
position,
pivotPosition,
targetPieceId,
action: cardEventAction,
chooseCardTemplateId
});
}
}
}
}
return this.processActions(evalActions, activatingPiece, activatingPiece.playerId);
}
//evaluate an event that doesn't correspond to a piece directly
evaluatePlayerEvent(event, playerId){
this.log.info('Eval player event %s player: %s', event, playerId);
let evalActions = [];
//look through all the pieces on the board to see if any have actions on this event
for(let piece of this.pieceState.pieces){
if(!piece.events || piece.events.length === 0) continue;
//find all actions for this event, there could be more than one
for(let cardEvent of piece.events){
if(cardEvent.event !== event) continue;
if(cardEvent.condition){
let pieceSelectorParams = {selfPiece: piece, activatingPiece: piece, controllingPlayerId: piece.playerId};
let compareResult = this.selector.compareExpression(cardEvent.condition, this.pieceState, pieceSelectorParams, this.selector.selectPieces);
if(compareResult.length === 0){
continue;
}
}
//see if the selector matches up for this piece
let eventSelector = this.eventSelector(cardEvent);
if(!eventSelector){
eventSelector = this.eventDefaultSelectors[event];
if(!eventSelector){
throw 'Need default selector for ' + event;
}
}
//now find the player that matches the selector given the context of the piece that the event is for
let playerSelected = this.selector.selectPlayer(piece.playerId, eventSelector);
//if the activating piece is part of the selected pieces, add it to the list of actions
if(playerSelected === playerId){
for(let cardEventAction of cardEvent.actions){
evalActions.push({
piece: piece,
playerId: piece.playerId,
activatingPiece: piece,
action: cardEventAction
});
}
}
}
}
return this.processActions(evalActions, null, playerId);
}
//when a spell is played
evaluateSpellEvent(event,
{spellCard, playerId, targetPieceId, position, pivotPosition, chooseCardTemplateId, activatingPiece, selfPiece}
){
this.log.info('Eval spell event %s with spell %s player: %s', event, spellCard.name, playerId);
let evalActions = [];
//always add the actions from this spell card
let spellActions = spellCard.events.filter(e => e.event === event);
if(spellActions.length > 0){
for(let cardEventAction of spellActions[0].actions){
evalActions.push({
piece: selfPiece || null,
card: spellCard,
playerId,
position,
pivotPosition,
targetPieceId,
action: cardEventAction,
chooseCardTemplateId
});
}
}
//then look through all the pieces on the board to see if any have actions on this event
for(let piece of this.pieceState.pieces){
if(!piece.events || piece.events.length === 0) continue;
//find all actions for this event, there could be more than one
for(let cardEvent of piece.events){
if(cardEvent.event !== event) continue;
if(cardEvent.condition){
let pieceSelectorParams = {selfPiece: piece, activatingPiece: piece, controllingPlayerId: piece.playerId};
let compareResult = this.selector.compareExpression(cardEvent.condition, this.pieceState, pieceSelectorParams, this.selector.selectPieces);
if(compareResult.length === 0){
continue;
}
}
//see if the selector matches up for this card
let eventSelector = this.eventSelector(cardEvent);
if(!eventSelector){
eventSelector = this.eventDefaultSelectors[event];
if(!eventSelector){
throw 'Need default selector for ' + event;
}
}
//now find the player that matches the selector given the context of the piece that the event is for
let playerSelected = this.selector.selectPlayer(piece.playerId, eventSelector);
//if the activating piece is part of the selected pieces, add it to the list of actions
if(playerSelected === playerId){
for(let cardEventAction of cardEvent.actions){
evalActions.push({
piece: piece,
playerId: piece.playerId,
action: cardEventAction,
position,
pivotPosition,
targetPieceId,
chooseCardTemplateId
});
}
}
}
}
return this.processActions(evalActions, activatingPiece || null, playerId);
}
//Process all actions that have been selected in the evaluation phase into actual queue actions
// evalActions -> array of actions to be eval'd, with playerId's of the controlling player (current turn player)
// activatingPiece -> optional piece that will be used for SELF selections
// activatingPlayerId -> id of the player that fired all this off (generally the current turn player)
processActions(evalActions, activatingPiece, activatingPlayerId){
//actions to be added to the real queue
let queue = [];
let queueIndex = 0;
try{
for(let pieceAction of evalActions){
let action = pieceAction.action;
let piece = pieceAction.piece;
let card = pieceAction.card;
let pieceSelectorParams = {
selfPiece: piece,
controllingPlayerId: pieceAction.playerId,
activatingPiece: activatingPiece || pieceAction.activatingPiece || null,
targetPieceId: pieceAction.targetPieceId,
savedPieces: pieceAction.savedPieces,
position: pieceAction.position,
pivotPosition: pieceAction.pivotPosition,
isSpell: !pieceAction.piece,
isTimer: pieceAction.isTimer || false
};
//check to see if we should even do this action
if(action.condition){
let compareResult = this.selector.compareExpression(action.condition, this.pieceState, pieceSelectorParams, this.selector.selectPieces)
if(compareResult.length === 0){
continue;
}
}
let times = 1;
if(action.times){
times = this.selector.eventualNumber(action.times, pieceSelectorParams);
}
let totalSpellDamage = this.pieceState.totalSpellDamage(pieceSelectorParams.controllingPlayerId);
//for multi cast spells use spell damage as additional times instead of additional damage
if(pieceSelectorParams.isSpell && times > 1 && totalSpellDamage > 0){
times += totalSpellDamage;
}
let actionTriggerer = piece ? `piece ${piece.name}` : `spell ${pieceAction.card.name}`;
this.log.info('Evaluating action %s for %s %s %s'
, action.action, actionTriggerer, times, times > 1 ? 'times' : 'time');
//despite not using the selected pieces for each action we need to run them to catch any exceptions now
//for things like needing a target without one provided
for (var time = 0; time < times; time++) {
switch(action.action){
//Charm(pieceSelector)
case 'Charm':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, CharmPiece));
break;
}
//Destroy(pieceSelector)
case 'Destroy':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceDestroyed));
break;
}
//Discard(playerSelector)
//Only chooses a random card for now
case 'Discard':
{
let playerToDiscard = this.selector.selectPlayer(pieceAction.playerId, action.args[0]);
queue.push(new DiscardCard(playerToDiscard));
break;
}
//DrawCard(playerSelector)
case 'DrawCard':
{
let playerToDraw = this.selector.selectPlayer(pieceAction.playerId, action.args[0]);
queue.push(new DrawCard(playerToDraw));
break;
}
//ChangeEnergy(playerSelector, amount, permanent, full)
//Permanent meaning for every turn going forward, non permanent is just this turn
//Full meaning whether or not to fill the adjusted amount or if they come empty
case 'ChangeEnergy':
{
let playerSelector = this.selector.selectPlayer(pieceAction.playerId, action.args[0]);
queue.push(new SetPlayerResource(
playerSelector,
this.selector.eventualNumber(action.args[1], pieceSelectorParams),
action.args[2],
action.args[3]
));
break;
}
//ChargeUp(playerSelector, amount)
//Change a players charge by amount
case 'ChargeUp':
{
let playerSelector = this.selector.selectPlayer(pieceAction.playerId, action.args[0]);
queue.push(new ChargeUp(
playerSelector,
this.selector.eventualNumber(action.args[1], pieceSelectorParams)
));
break;
}
//Hit(pieceSelector, damageAmount)
case 'Hit':
{
let spellDamageBonus = 0;
let pieceSelector = action.args[0];
let hitDamage = action.args[1];
//apply spell damage only for one time cast spells
if(pieceSelectorParams.isSpell && times === 1){
spellDamageBonus = totalSpellDamage;
}
this.selector.selectPieces(pieceSelector, pieceSelectorParams);
queue.push(new PieceAction(pieceSelector, pieceSelectorParams, PieceHealthChange, {changeENumber: hitDamage, pieceSelectorParams, isHit: true, spellDamageBonus}));
//Find any tiles that might be made passable by an AOE
let clearedTiles = this.selector.selectClearableTiles(pieceSelector, pieceSelectorParams);
if(clearedTiles.length){
queue.push(new TilesCleared(clearedTiles.map(t => t.position)));
}
break;
}
//Heal(pieceSelector, healAmount)
case 'Heal':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let spellDamageBonus = 0;
if(pieceSelectorParams.isSpell && times === 1){
spellDamageBonus = totalSpellDamage;
}
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceHealthChange, {changeENumber: action.args[1], pieceSelectorParams, isHit: false, spellDamageBonus}));
break;
}
//SetAttribute(pieceSelector, attribute, value)
case 'SetAttribute':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let attributeParams = {};
//set up the appropriate attribute change from args, i.e. attack = 1
attributeParams[action.args[1]] = this.selector.eventualNumber(action.args[2], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceAttributeChange, attributeParams));
break;
}
//Buff(pieceSelector, buffName, [optional condition expression], attribute(amount), ...moreAttributes)
//Note that the attributes can also be Statuses with a true or false to add (or remove) the status
case 'Buff':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let buffName = action.args[1];
let buffParams = {}
let attributeIndex = 2;
let condition = null;
buffParams.addStatus = buffParams.removeStatus = 0;
if(action.args[2] && action.args[2].compareExpression){
attributeIndex = 3;
condition = action.args[2];
}
let buffAttributes = action.args.slice(attributeIndex);
for(let buffAttribute of buffAttributes){
if(buffAttribute.attribute){
buffParams[buffAttribute.attribute] = this.selector.eventualNumber(buffAttribute.amount, pieceSelectorParams);
}else if(buffAttribute.status){
if(buffAttribute.adding){
buffParams.addStatus |= Statuses[buffAttribute.status];
}else{
buffParams.removeStatus |= Statuses[buffAttribute.status];
}
}else{
this.log.error('Unrecognized buff attribute %j', buffAttribute);
}
}
buffParams.buffId = uniqueId();
buffParams.condition = condition;
buffParams.name = buffName;
buffParams.removed = false;
buffParams.buffAttributes = buffAttributes;
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceBuff, buffParams));
break;
}
//Aura(auraPieceSelector, pieceSelector, auraName, attribute(amount), ...moreAttributes)
//to clarify further: first piece selector is who to attach the aura to,
//second one is who is affected by the aura
case 'Aura':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let auraParams = {
pieceSelector: action.args[1],
name: action.args[2],
};
let auraAttributes = action.args.slice(3);
for(let auraAttribute of auraAttributes){
//specifically don't use eventual number here because it will be evaluated in the aura update
auraParams[auraAttribute.attribute] = auraAttribute.amount;
}
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceAura, auraParams));
break;
}
//CardAura(auraPieceSelector, cardSelector, auraName, attribute(amount), ...moreAttributes)
//to clarify further: first piece selector is who to attach the aura to,
//second one is which cards are affected by the aura.
case 'CardAura':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let auraParams = {
cardSelector: action.args[1],
name: action.args[2],
};
let auraAttributes = action.args.slice(3);
for(let auraAttribute of auraAttributes){
//specifically don't use eventual number here because it will be evaluated in the aura update
auraParams[auraAttribute.attribute] = auraAttribute.amount;
}
queue.push(new PieceAction(action.args[0], pieceSelectorParams, CardAura, auraParams));
break;
}
//RemoveBuff(pieceSelector, buffName)
case 'RemoveBuff':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let buffParams = {name: action.args[1], removed: true};
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceBuff, buffParams));
break;
}
//Spawn(pieceId, kingsRadiusToSpawnIn)
//Spawn a unit based on where the activating piece is located.
//So kingsRadius 0 = right where the piece was (after it died presumably).
//If it's 1, pick a random position from any of the surrounding tiles
//This requires an activating piece at the moment to act as the center position
//ALTERNATE:
//Spawn(pieceId, areaSelector)
//This means to spawn a piece in each of the tiles returned by the area selector
case 'Spawn':
{
let cardTemplateId = this.selector.eventualNumber(action.args[0], pieceSelectorParams);
let placeArg = action.args[1];
if(placeArg.left && placeArg.left.area){
//if it's an area, eval the tiles now rather than at exec time since they shouldn't be changing
//exec time will determine if a piece actually spawns or not in the case of an occupied tile
let areaToSpawn = this.selector.selectArea(placeArg.left, pieceSelectorParams);
if(areaToSpawn && areaToSpawn.areaTiles && areaToSpawn.areaTiles.length > 0){
for(let tile of areaToSpawn.areaTiles ){
queue.push(
new SpawnPiece({
playerId: pieceAction.playerId,
cardTemplateId,
position: tile,
})
);
}
}
}else if(piece){
queue.push(
new SpawnPiece({
playerId: piece.playerId,
cardTemplateId,
position: piece.position,
spawnKingRadius: placeArg
})
);
}
break;
}
//GiveStatus(pieceSelector, Status)
case 'GiveStatus':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceStatusChange, {add: Statuses[action.args[1]]}));
break;
}
//RemoveStatus(pieceSelector, Status)
case 'RemoveStatus':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceStatusChange, {remove: Statuses[action.args[1]]}));
break;
}
//startTurnTimer(initialTurnDelay, repeatingInterval, ...Actions)
//turns is a counter that gets decremented each start of the turn and fires when 0
//so a turns value of 1 will activate on the start of the following (probably enemy) turn
//repeating interval, if truth, determines what the turns counter is reset to every time
//the timer activates
case 'startTurnTimer':
{
let timerActions = action.args.slice(2);
queue.push(new ActionTimer({
isStartTimer: true,
pieceSelectorParams,
piece,
card,
playerId: pieceAction.playerId,
interval: action.args[1],
timer: action.args[0],
timerActions
}));
break;
}
//endTurnTimer(initialTurnDelay, repeatingInterval, ...Actions)
//see startTurnTimer above ^
case 'endTurnTimer':
{
let timerActions = action.args.slice(2);
queue.push(new ActionTimer({
isStartTimer: false,
pieceSelectorParams,
piece,
card,
playerId: pieceAction.playerId,
interval: action.args[1],
timer: action.args[0],
timerActions
}));
break;
}
//Move(pieceSelector, tile in areaSelector, isTeleport, ignoreCollisionCheck)
case 'Move':
{
let lastSelected = this.selector.selectPieces(action.args[0], pieceSelectorParams);
if(lastSelected.length > 1){
this.log.warn('Move selected more than one piece to move %j', lastSelected);
break;
}
let moveTo = this.selector.selectArea(action.args[1], pieceSelectorParams);
if(!moveTo || !moveTo.resolvedPosition){
this.log.warn("Move didn't resolve to a position %j", moveTo);
break;
}
//preemptively check for colliding piece so the action can be scrubbed early
let ignoreCollisionCheck = action.args[3] || false;
if(!ignoreCollisionCheck && this.pieceState.pieceAt(moveTo.resolvedPosition)){
throw new EvalError("You can't move on top of another piece!");
}
let movingPiece = lastSelected[0];
let destinationTile = this.mapState.getTile(moveTo.resolvedPosition)
//other checks for scrubbing early. Really need to dedupe w/move piece processor
if(!destinationTile || destinationTile.unpassable){
this.log.warn('Cannot move piece %s to unpassable tile %s'
, movingPiece.id, destinationTile ? destinationTile.position : 'Missing dest: ' + moveTo.resolvedPosition);
throw new EvalError("That tile doesn't look safe!");
return;
}
if(lastSelected && lastSelected.length === 1){
queue.push(new MovePiece({
pieceId: movingPiece.id,
to: moveTo.resolvedPosition,
isJump: true,
isTeleport: action.args[2],
ignoreCollisionCheck
}));
}
break;
}
//Transform(pieceSelector, cardTemplateId, targetPieceSelector)
//Transforms the piece selector pieces into either the cardTemplateId if it's a valid minion ID,
//otherwise, use the targetPieceSelector to find the first minion to copy props from
case 'Transform':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let cardTemplateId = action.args[1];
let targetPieceSelector = action.args[2];
//TODO: should be moved to processor to select
let transformPieceId = null;
if(targetPieceSelector){
let transformPieces = this.selector.selectPieces(targetPieceSelector, pieceSelectorParams);
if(transformPieces && transformPieces.length > 0){
transformPieceId = transformPieces[0].id;
}
}
queue.push(new PieceAction(action.args[0], pieceSelectorParams, TransformPiece, {cardTemplateId, transformPieceId}));
break;
}
//GiveCard(PlayerSelector, cardTemplateId)
case 'GiveCard':
{
let playerSelector = this.selector.selectPlayer(pieceAction.playerId, action.args[0]);
let cardId = this.selector.eventualNumber(action.args[1], pieceSelectorParams);
queue.push(new GiveCard(playerSelector, cardId));
break;
}
//ShuffleToDeck(PlayerSelector, cardTemplateId)
case 'ShuffleToDeck':
{
let playerSelector = this.selector.selectPlayer(pieceAction.playerId, action.args[0]);
let cardId = this.selector.eventualNumber(action.args[1], pieceSelectorParams);
queue.push(new ShuffleToDeck(playerSelector, cardId));
break;
}
//GiveArmor(pieceSelector, amount)
case 'GiveArmor':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
let change = this.selector.eventualNumber(action.args[1], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, PieceArmorChange, {change}));
break;
}
//AttachCode(pieceSelector, eventList)
//Second arg is like a top level event list node that will get merged into the selected pieces events
case 'AttachCode':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, AttachCode, {eventList: action.args[1]}));
break;
}
//Unsummon(pieceSelector)
case 'Unsummon':
{
this.selector.selectPieces(action.args[0], pieceSelectorParams);
queue.push(new PieceAction(action.args[0], pieceSelectorParams, UnsummonPiece));
break;
}
//Choose(cardTemplateId1, cardTemplateId2)
case 'Choose':
{
queue.push(new Choose(
action.args[0],
action.args[1],
pieceAction.chooseCardTemplateId,
{
playerId: pieceAction.playerId,
activatingPiece: pieceSelectorParams.activatingPiece,
selfPiece: pieceSelectorParams.selfPiece,
targetPieceId: pieceSelectorParams.targetPieceId,
position: pieceSelectorParams.position,
pivotPosition: pieceSelectorParams.pivotPosition
}
));
break;
}
}
}
//blast through all the items we need to add to the queue and associate the activating piece with them
//mainly for the client. Done each loop so that the activating piece can be included in the action we're on
//the queue index is persisted across the piece action loop so we only go over each item once with the appropriate
//pieceSelectorParams
for(queueIndex; queueIndex < queue.length; queueIndex++){
let action = queue[queueIndex];
action.activatingPieceId = pieceSelectorParams.activatingPiece ? pieceSelectorParams.activatingPiece.id : null;
}
}
}catch(e){
if(e instanceof EvalError){
this.log.info('EvalError player %s : %s', activatingPlayerId, e.message);
if(activatingPlayerId){
this.queue.push(new Message(e.message, activatingPlayerId));
}
return false;
}
throw e;
}
for(let action of queue){
this.queue.push(action);
}
return true;
}
//When a piece is transformed there's some state maintenance to do
updateTransformedPiece(piece, copiedPiece){
//First, cleanup any timers associated with the old piece
this.cleanupTimers(piece);
//if we're copying an active piece, duplicate any existing timers but maintain the new piece Id
if(copiedPiece){
let copiedEndTurnTimers = this.endTurnTimers.filter(t => t.piece && t.piece.id === copiedPiece.id);
let copiedStartTurnTimers = this.startTurnTimers.filter(t => t.piece && t.piece.id === copiedPiece.id);
for(let copiedEndTurnTimer of copiedEndTurnTimers){
let clonedTimer = _.cloneDeep(copiedEndTurnTimer);
clonedTimer.piece = piece;
clonedTimer.playerId = piece.playerId; //Not sure if this is really needed or might cause an issue
this.endTurnTimers.push(clonedTimer);
}
for(let copiedStartTurnTimer of copiedStartTurnTimers){
let clonedTimer = _.cloneDeep(copiedStartTurnTimer);
clonedTimer.piece = piece;
clonedTimer.playerId = piece.playerId; //see above
this.startTurnTimers.push(clonedTimer);
}
}
}
evaluateTurnEvent(isStart){
let evalActions = this.updateTimers(isStart);
this.log.info('Eval %s %s turn events', evalActions.length, isStart ? 'start' : 'end');
return this.processActions(evalActions, null, null);
}
//Tick down all the timers in the array. When the metaphorical bomb ticks down remove it from
//the saved timers and return its actions to be evaluated
//have to use a bool instead of passing the array in so we can reassign it to remove items :(
updateTimers(isStartTimers){
let timerArray = isStartTimers ? this.startTurnTimers : this.endTurnTimers;
if(timerArray.length === 0) return [];
for(let timer of timerArray){
timer.timer--;
}
let activatedTimers = timerArray.filter(t => t.timer === 0);
//reactivate those that are repeating
for(let activated of activatedTimers){
if(activated.interval){
activated.timer = activated.interval;
}
}
//refilter for expired
let expiredTimers = timerArray.filter(t => t.timer === 0);
if(isStartTimers){
this.startTurnTimers = _.without(timerArray, ...expiredTimers);
}else{
this.endTurnTimers = _.without(timerArray, ...expiredTimers);
}
let activatedEvents = [];
for(let activatedTimer of activatedTimers){
activatedEvents.push({
piece: activatedTimer.piece,
activatingPiece: activatedTimer.piece,
card: activatedTimer.card,
savedPieces: activatedTimer.saved,
playerId: activatedTimer.playerId,
action: activatedTimer.timerAction,
isTimer: true
});
}
return activatedEvents;
}
//Whenever a piece dies that had a repeating turn timer on it we need to remove it
cleanupTimers(killedPiece){
let abandonedFilter = (t) => (t.interval && t.piece && t.piece.id === killedPiece.id);
let abandonedStartTimers = this.startTurnTimers.filter(abandonedFilter);
if(abandonedStartTimers.length > 0){
this.log.info('Cleaning up %s start timers', abandonedStartTimers.length);
}
this.startTurnTimers = _.without(this.startTurnTimers, ...abandonedStartTimers);
let abandonedEndTimers = this.endTurnTimers.filter(abandonedFilter);
if(abandonedEndTimers.length > 0){
this.log.info('Cleaning up %s end timers', abandonedEndTimers.length);
}
this.endTurnTimers = _.without(this.endTurnTimers, ...abandonedEndTimers);
}
//look through the cards for any cards needing a TARGET
//on one of the targetableEvents
//and then find what the possible targets are
// [
// {cardId: 2, event: 'x', targetPieceIds: [4,5,6]}
// ]
findPossibleTargets(cards, playerId){
let targets = [];
for(let card of cards){
if(!card.events) continue;
for(let targetEvent of this.targetableEvents){
let event = card.events.find(e => e.event === targetEvent);
if(!event) continue;
let targetPieceIds = this.findActionTargets(event.actions, playerId, card.isSpell);
if(targetPieceIds){
targets.push({
cardId: card.id,
event: event.event,
targetPieceIds
});
}
}
}
return targets;
}
findActionTargets(eventActions, playerId, isSpell){
//try to find TARGETS in any of the actions
for(let cardEventAction of eventActions){
if(this.targetableActions.indexOf(cardEventAction.action) === -1) continue;
for(let arg of cardEventAction.args){
//check for recursive step if arg is another action (for a turn timer for instance)
if(arg.action){
let recursivePieceIds = this.findActionTargets([arg], playerId, isSpell);
if(recursivePieceIds){
return recursivePieceIds;
}
}
let selector = arg;
if(arg.attributeSelector){
selector = arg.attributeSelector;
}
//Selectors should always have a left
if(!selector.left) continue;
if(!this.selector.doesSelectorUse(selector, 'TARGET')) continue;
let targetPieceIds = this.selector.selectPossibleTargets(
selector, {controllingPlayerId: playerId, isSpell}
).map(p => p.id);
//only allow max of 1 targetable action per event
return targetPieceIds;
}
}
return null;
}
//look through the pieces for any piece with an ability
//then find the name, charge time, and what the possible targets are
// [
// {pieceId: 2, ability: 'x', abilityCost: 0, abilityChargeTime: 0, abilityCooldown: 0, targetPieceIds: [4,5,6]}
// ]
//Needs to be documented in a better place but the args to the ability event are:
// ability(cost, chargeTime, name, playerSelector?)
// OR
// ability(pieceSelector)
// to trigger on pieces using abilities
findPossibleAbilities(pieces, playerId){
let targets = [];
for(let piece of pieces){
if(!piece.events) continue;
let ability = piece.events.find(e => e.event === 'ability');
if(!ability) continue;
if(!ability.args.length || ability.args[0].left ){
//If the ability event on the piece is to react to other pieces using abilities don't consider it as a piece ability
continue;
}
//Check to see if the piece ability is for this player, either they have the optional arg to select which player or just the default of player
if(ability.args[3]){
let playerSelected = this.selector.selectPlayer(piece.playerId, ability.args[3]);
if(playerSelected !== playerId){ continue; }
} else if(piece.playerId != playerId){
continue;
}
let targetPieceIds = this.findActionTargets(ability.actions, playerId, true) || [];
targets.push({
pieceId: piece.id,
abilityCost: ability.args[0],
abilityChargeTime: ability.args[1],
abilityCooldown: Math.max(0, ability.args[1] - piece.abilityCharge),
ability: ability.args[2],
targetPieceIds
});
}
return targets;
}
//look through cards for any areas when played
// [
// {cardId: 2, event: 'x', area: [Square|Cross...], size: 3, center?: Position}
// ]
findPossibleAreas(cards, playerId){
let areas = [];
for(let card of cards){
if(!card.events) continue;
for(let targetEvent of this.targetableEvents){
let event = card.events.find(e => e.event === targetEvent);
if(!event) continue;
let areaSelector = null;
let alsoNeedsTarget = false;
let moveRestricted = false; //Does the area represent somewhere where a piece needs to move to?
//try to find areas in any of the actions
for(let cardEventAction of event.actions){
if(!this.targetableActions.includes(cardEventAction.action)) continue;
//hacky way to see if the action needs a main target for its selection
alsoNeedsTarget = this.selector.doesSelectorUse(cardEventAction.args[0], 'TARGET');
//For now only a move in area command uses an area that is move restricted
moveRestricted = cardEventAction.action === 'Move';
for(let arg of cardEventAction.args){
//Area selectors will always have a left
if(!arg.left) continue;
areaSelector = this.selector.findSelector(arg, s => s && s.area);
if(areaSelector){
let areaDescrip = this.selector.selectArea(
//note: pass the whole arg in here not just the area selector so it can determine if it's a
//cursor selection or not. But if it's just an area selection in the left node pass that.
//Hacky, I know but works for now.
(arg.left && arg.left.area) ? arg.left : arg,
{isSpell: card.isSpell, controllingPlayerId: playerId}
);