-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattle.js
101 lines (91 loc) · 3.08 KB
/
battle.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
const { Card } = require('./card');
const { ChallengeView } = require('./challenge');
const BattleType = {
RIVERRACE: 'riverRacePvP',
CASUAL: 'casual2v2',
PVP: 'PVP',
RANKED: 'pathOfLegend',
BOATBATTLE: 'boatBattle'
}
class BattlePlayer {
constructor(tag, name, startingTrophies, crowns, kingTowerHitPoints, princesTowerHitPoints, cards, elixirLeaked) {
this.tag = tag;
this.name = name;
this.startingTrophies = startingTrophies;
this.crowns = crowns;
this.kingTowerHitPoints = kingTowerHitPoints;
this.princesTowerHitPoints = princesTowerHitPoints;
this.cards = cards;
this.elixirLeaked = elixirLeaked;
}
static fromJSON(jsonObject) {
return new BattlePlayer(
jsonObject.tag,
jsonObject.name,
jsonObject.startingTrophies,
jsonObject.crowns,
jsonObject.kingTowerHitPoints,
jsonObject.princesTowerHitPoints,
jsonObject.cards
.filter(card => card.name)
.map(Card.fromJSON),
jsonObject.elixirLeaked);
}
}
class Battle {
constructor(type, time, challengeInfo, boatInfo, isLadderTournament, arena,
gameMode, deckSelection, isHosted, teamOne, teamTwo) {
this.type = type;
this.time = time;
this.challengeInfo = challengeInfo;
this.boatInfo = boatInfo;
this.isLadderTournament = isLadderTournament;
this.arena = arena;
this.gameMode = gameMode;
this.deckSelection = deckSelection;
this.teamOne = teamOne;
this.teamTwo = teamTwo;
this.isHosted = isHosted;
}
static fromJSON(jsonObject) {
return new Battle(
jsonObject.type,
ChallengeView.formatDate(jsonObject.battleTime),
{id: jsonObject.challengeId,
title: jsonObject.challengeTitle,
winsBefore: jsonObject.challengeWinCountBefore},
{side: jsonObject.boatBattleSide,
towersBefore: 3 - jsonObject.prevTowersDestroyed,
towersAfter: jsonObject.remainingTowers},
jsonObject.isLadderTournament,
jsonObject.arena.name,
jsonObject.gameMode,
jsonObject.deckSelection,
jsonObject.isHostedMatch,
jsonObject.team.map(BattlePlayer.fromJSON),
jsonObject.opponent.map(BattlePlayer.fromJSON));
}
toString() {
return `${this.type}: ` +
`${this.teamOne.map(player => player.name).join(' with ')} vs. ` +
`${this.teamTwo.map(player => player.name).join(' with ')}`;
}
}
class BattleLog {
constructor(battles) {
this.battles = battles;
}
static fromJSON(jsonObject) {
const battles = [];
for (const battle of jsonObject) {
battles.push(Battle.fromJSON(battle));
}
return new BattleLog(battles);
}
toString() {
return this.battles
.map(battle => battle.toString())
.join('\n');
}
}
module.exports = { BattleLog };