forked from johreh/gloomycompanion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbilityDeck.jsx
289 lines (263 loc) · 8.74 KB
/
AbilityDeck.jsx
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
import PropTypes from 'prop-types';
import React from 'react';
import AbilityCardBack from './AbilityCardBack';
import AbilityCardFront from './AbilityCardFront';
import ButtonDiv from './ButtonDiv';
import Card from './Card';
import DeckState from './DeckState';
import StatIcon from './StatIcon';
import { DECKS } from './cards';
import { attributes_to_lines, immunities_to_lines, notes_to_lines, special_to_lines } from './macros';
import { MONSTER_STATS } from './monster_stats';
import * as css from './style/Card.scss';
export default function AbilityDeck(props) {
// FIXME: Reshuffle animation
// FIXME: Draw animation repeats when unhiding
const { spec } = props;
// Calculate monster stats and text that goes on every card
let stats = {};
let attack = [0, 0];
let move = [0, 0];
let range = [0, 0];
let health = [0, 0];
let extra_lines = [];
let attributes = [];
const isBoss = spec.class === DECKS.Boss.class;
if (isBoss && spec.name !== 'Boss') {
const bossName = spec.name.replace('Boss: ', '');
stats = MONSTER_STATS.bosses[bossName].level[spec.level];
attack = [stats.attack];
move = [stats.move];
range = [stats.range];
health = [stats.health];
if (stats.immunities) {
extra_lines = extra_lines.concat(immunities_to_lines(stats.immunities));
}
if (stats.notes) {
extra_lines = extra_lines.concat(notes_to_lines(stats.notes));
}
} else if (!isBoss) {
const stats = MONSTER_STATS.monsters[spec.name].level[spec.level];
attack = [stats.normal.attack, stats.elite.attack];
move = [stats.normal.move, stats.elite.move];
range = [stats.normal.range, stats.elite.range];
health = [stats.normal.health, stats.elite.health];
attributes = [stats.normal.attributes, stats.elite.attributes];
extra_lines = extra_lines.concat(attributes_to_lines(attributes));
}
// Parse the attributes and store them in structures we will use when we need to render base
// stats. We divide attributes into three categories.
// 1) Integer attributes. Things like, "Shield 5" or "Target 2", or even "Retaliate 2: Range 2".
// 2) Bool attributes. Things like "Flying", or "Poison".
// 3) Text attributes. Things like "Attacker gains disadvantage"
const attr_parse = /^%(\S+)%(\s+([0-9]+))?(:\s*%(\S+)%(\s+([0-9]+)))?$/;
const int_attr_dict = { };
const txt_attr_dict = { };
const bool_attrs = [[], []];
for (const i in attributes) {
for (const j in attributes[i]) {
const text = attributes[i][j];
const match = attr_parse.exec(text);
const name = match ? match[1] : text;
if (!match) {
// No match => Text attribute
if (!(name in txt_attr_dict)) {
txt_attr_dict[name] = [0, 0];
}
txt_attr_dict[name][i] = 1;
} else
if (!match[3]) {
// No number after the icon => bool attribute
bool_attrs[i].push(name);
} else {
// Must be an integer attribute, perhaps with an extra modifier.
if (!(name in int_attr_dict)) {
int_attr_dict[name] = [[0], [0]];
}
int_attr_dict[name][i] = [match[3]];
if (match[5]) {
int_attr_dict[name][i].push(match[5]);
if (match[7]) {
int_attr_dict[name][i].push(match[7]);
}
}
}
}
}
const renderCard = (card, zIndex, cardClasses, faceUp) => {
// Apply Special1/2 macros for Boss cards
let cards_lines = card.starting_lines;
if (isBoss) {
let new_lines = [];
for (const line of cards_lines) {
new_lines = new_lines.concat(special_to_lines(line, stats.special1, stats.special2));
}
cards_lines = new_lines;
}
return (
<Card
classes={cardClasses}
key={card.id}
deckType="ability"
faceUp={faceUp}
zIndex={zIndex}
renderBack={() => (
<AbilityCardBack name={spec.name} level={spec.level.toString()} />
)}
renderFront={() => (
<AbilityCardFront
initiative={card.initiative}
name={spec.name}
shuffle={card.shuffle_next}
lines={cards_lines.concat(extra_lines)}
attack={attack}
move={move}
range={range}
level={spec.level.toString()}
health={health}
/>
)}
/>
);
};
const renderBaseStats = () => {
if (!props.showBaseStats) {
return null;
}
// Transform [a, b, c, ...] into [[a], [b], [c], ...]
const wrap_array = x => x.map(y => [y]);
const intStatLine = (iconName, data) => {
const bossData = (data.length === 1);
const empty = (data[0][0] === 0) && (bossData || (data[1][0] === 0));
if (empty) {
return null;
}
const render_val = val => val.map((v, k) => {
if ((k % 2) != 0) {
return <StatIcon name={v} className={css.intStatIcon} />;
}
return (v == 0) ? '-' : String(v);
});
return bossData
? (
<tr>
<td className={css.baseStateId}>
<StatIcon name={iconName} className={css.intStatIcon} />
</td>
<td className={css.baseStatRight}>{render_val(data[0])}</td>
</tr>
)
: (
<tr>
<td className={css.baseStatLeft}>{render_val(data[0])}</td>
<td className={css.baseStatId}>
<StatIcon name={iconName} className={css.intStatIcon} />
</td>
<td className={css.baseStatRight}>{render_val(data[1])}</td>
</tr>
);
};
const boolStatLine = (mobType, data) => {
if (!data.length) {
return null;
}
const icons = data.map(iconName => <StatIcon name={iconName} className={css.boolStatIcon} />);
return (
<tr>
<td>{mobType}</td>
<td>{icons}</td>
</tr>
);
};
const attr_lines = Object.keys(int_attr_dict).map(key =>
intStatLine(key, int_attr_dict[key]));
return (
<div className={css.baseStats}>
<table>
{ intStatLine('heal', wrap_array(health)) }
{ intStatLine('move', wrap_array(move)) }
{ intStatLine('attack', wrap_array(attack)) }
{ intStatLine('range', wrap_array(range)) }
{attr_lines}
</table>
{ (bool_attrs[0].length || bool_attrs[1].length) ? <hr /> : null }
<table>
{ boolStatLine('N', bool_attrs[0]) }
{ boolStatLine('E', bool_attrs[1]) }
</table>
</div>
);
};
const renderBossExtras = () => {
const text_attrs = Object.keys(txt_attr_dict).map((key) => {
const who = txt_attr_dict[key];
let modifier = null;
if (!who[0]) {
modifier = ' (elite only)';
} else
if (!who[1]) {
modifier = ' (normal only)';
}
return <li>{key}{modifier}</li>;
});
const immunity_line = () => {
if (!stats.immunities) {
return null;
}
const Icon = x => <StatIcon name={x} className={css.boolStatIcon} />;
const icons = stats.immunities
? Object.keys(stats.immunities).map(key => Icon(stats.immunities[key]))
: null;
return <li>Immunities: {icons}</li>;
};
const notes = () => (stats.notes ? <li>{stats.notes}</li> : null);
return (
<div className={css.baseStats}>
<ul>
{ text_attrs }
{ immunity_line() }
{ notes() }
</ul>
</div>
);
};
const [topDraw] = props.deckState.draw_pile;
const [topDiscard, sndDiscard] = props.deckState.discard;
const pullAnim = props.showFlip ? css.pull : null;
const liftAnim = props.showFlip ? css.lift : null;
return (
<div
className={props.hidden ? css.hiddenDeck : css.abilityDeckContainer}
>
<div className={css.rowContainer}>
<ButtonDiv
className={css.cardContainer}
onClick={props.onClick}
>
{topDraw ? renderCard(topDraw, -7, [css.draw], false) : null}
{topDiscard ? renderCard(topDiscard, -3, [css.discard, pullAnim], true) : null}
{sndDiscard ? renderCard(sndDiscard, -4, [css.discard, liftAnim], true) : null}
</ButtonDiv>
{ props.showBaseStats ? renderBaseStats() : null }
</div>
{ props.showBaseStats ? renderBossExtras() : null }
</div>
);
}
AbilityDeck.defaultProps = {
hidden: false,
showBaseStats: false,
};
AbilityDeck.propTypes = {
spec: PropTypes.shape({
id: PropTypes.string.isRequired,
class: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
level: PropTypes.number.isRequired,
}).isRequired,
deckState: PropTypes.instanceOf(DeckState).isRequired,
onClick: PropTypes.func.isRequired,
showFlip: PropTypes.bool.isRequired,
hidden: PropTypes.bool,
showBaseStats: PropTypes.bool,
};