-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardSelector.js
100 lines (86 loc) · 3.1 KB
/
CardSelector.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
import {Union, Intersection, Difference} from '../util/SetOps.js';
//Recursive card selector that takes the selector args from cardlang
export default class CardSelector{
constructor(selector, cardSelectorParams, isDirectorySelect){
this.isDirectorySelect = isDirectorySelect;
this.inPlayCards = selector.cardState.cards;
this.directory = Object.keys(selector.cardDirectory.directory).map(k => selector.cardDirectory.directory[k]);
this.allCards = isDirectorySelect ? this.directory : this.inPlayCards;
this.controllingPlayerId = cardSelectorParams.controllingPlayerId;
this.selector = selector;
this.cardSelectorParams = cardSelectorParams;
}
Select(selector){
if(selector.tag){
return this.allCards.filter(p => p.tags.includes(selector.tag));
}
if(selector.id){
return this.allCards.filter(p => p.cardTemplateId == selector.id);
}
//base case
else if(typeof selector == 'string'){
switch(selector){
case 'FRIENDLY':
return this.allCards.filter(p => p.playerId == this.controllingPlayerId);
break;
case 'ENEMY':
return this.allCards.filter(p => p.playerId != this.controllingPlayerId);
break;
case 'MINION':
return this.allCards.filter(p => p.isMinion);
break;
case 'SPELL':
return this.allCards.filter(p => p.isSpell);
break;
case 'HERO':
return this.allCards.filter(p => p.isHero);
break;
case 'MELEE':
return this.allCards.filter(p => !p.range);
break;
case 'RANGED':
return this.allCards.filter(p => p.range);
break;
case 'DECK':
return this.allCards.filter(p => p.inDeck);
break;
case 'HAND':
return this.allCards.filter(p => p.inHand);
break;
case 'DIRECTORY':
return this.allCards.filter(p => !p.uncollectible); //Actual directory selection handled in allCards in constructor
break;
default:
throw 'Invalid card type selector ' + selector;
}
}
if(!selector.left){
throw 'Selector must have left hand side selector';
}
//first check if this is a compare expression
if(selector.compareExpression){
return this.selector.compareExpression(selector, this.allCards, this.cardSelectorParams, this.selector.selectCards);
}
//ordinary case of recursing the piece selections
let leftResult = this.Select(selector.left);
if(selector.op && selector.right){
let rightResult = this.Select(selector.right);
let cardEquality = this.isDirectorySelect ?
(a,b) => a.cardTemplateId === b.cardTemplateId :
(a,b) => a.id === b.id ;
switch(selector.op){
case '|':
return Union(leftResult, rightResult, cardEquality);
break;
case '&':
return Intersection(leftResult, rightResult, cardEquality);
break;
case '-':
return Difference(leftResult, rightResult, cardEquality);
break;
}
}else{
return leftResult;
}
}
}