-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayByPlay.js
196 lines (175 loc) · 6.39 KB
/
PlayByPlay.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
"use strict";
const tracery = require("tracery-grammar");
const CardDeck = require("./CardDeck");
class PlayByPlay {
constructor(game, deck) {
this.players = game.players;
this.hands = game.hands;
this.winner = game.winner;
this.deck = deck;
this.grammar = this.getGrammar();
}
getGrammar() {
const rules = {
call: [
"#winner# #beats# #loser# with #winningCardName# over #losingCardName#.",
"#winner#'s #winningCardRank# #beats# #loser#'s #losingCardRank#.",
"#winner# with #winningCardName.a# over #losingCardName#.",
"#winner#, #winningCardRank.capitalize# over #losingCardRank#.",
"#winningCardRank.capitalize# over #losingCardRank#, #winner#.",
"#loser#'s #losingCardRank# #loses# to #winner#'s #winningCardRank#.",
"#winner#'s hand, #winningCardRank.capitalize# over #losingCardRank#.",
],
again: [
"#winner# again, #winningCardRank.capitalize# over #losingCardRank#.",
"#winner# wins again. #winningCardRank.capitalize# over #losingCardRank#.",
"Another for #winner# with #winningCardRank.capitalize# over #losingCardRank#.",
"Another for #winner#, #winningCardRank.capitalize# over #losingCardRank#.",
"#winner# this time with the #winningCardName#.",
"#loser# falls to #winner# again, #winningCardRank.capitalize# over #losingCardRank#.",
"#winner# continues streak. #winningCardRank.capitalize# over #losingCardRank#.",
"#winner# keeps going. #winningCardRank.capitalize# over #losingCardRank#.",
"#winner# does it again. #winningCardRank.capitalize# over #losingCardRank#.",
"#call#",
],
tied: [
"Two #card.s#. #war#",
"Two #card.s#. #war#",
"#card.capitalize.s#. #war#",
"#card.capitalize.s# all around. #war#",
],
war: [
"War!",
"War!",
"War!!",
"War!!!",
"We have a war!",
"It's a war folks!",
],
warOver: [
"#winner#'s #winningCardRank# wins the battle taking #prize# cards.",
"#winner# defeats #loser# with #winningCardRank.a#, takes #prize# cards.",
"#loser# loses the battle. #winner# takes #prize# cards.",
],
beats: ["beats", "bests", "tops"],
loses: ["loses", "falls"],
};
const grammar = tracery.createGrammar(rules);
grammar.addModifiers(tracery.baseEngModifiers);
return grammar;
}
createCall(origin, handDetails) {
let rules = `[winner:${handDetails.winner.lastName}]`;
rules += `[loser:${handDetails.loser.lastName}]`;
rules += `[winningCardName:${handDetails.winningCard.name}]`;
rules += `[winningCardRank:${handDetails.winningCard.rank.toLowerCase()}]`;
rules += `[losingCardName:${handDetails.losingCard.name}]`;
rules += `[losingCardRank:${handDetails.losingCard.rank.toLowerCase()}]`;
rules += `[prize:${handDetails.prize}]`;
rules += origin;
return this.grammar.flatten(rules);
}
parseHand(hand) {
let details = {};
details.war = hand.war;
details.prize = hand.prize;
details.leader = hand.counts[0] > hand.counts[1] ? 0 : 1;
if (hand.counts[0] === 0) {
details.gameover = true;
details.winner = this.players[1];
details.loser = this.players[0];
}
if (hand.counts[1] === 0) {
details.gameover = true;
details.winner = this.players[0];
details.loser = this.players[1];
}
if (hand.winners.length == 1) {
details.tie = false;
const winnerIndex = hand.winners[0];
const loserIndex = (hand.winners[0] + 1) % 2;
details.winner = this.players[winnerIndex];
details.loser = this.players[loserIndex];
if (hand.play.length > 1) {
details.winningCard = this.deck.getCard(
hand.play[winnerIndex].identifier
);
details.losingCard = this.deck.getCard(
hand.play[loserIndex].identifier
);
} else {
details.winningCard = this.deck.getCard(hand.play[0].identifier);
}
} else {
details.tie = true;
details.rank = this.deck.getRank(hand.play[0].identifier);
}
return details;
}
getIntro() {
return `Looks like ${this.players[0].fullName} and ${this.players[1].fullName} are ready to start.`;
}
*getCall() {
let spreadAtLastUpdate = 0;
let streak = 0;
let lastWinner = null;
let leader = null;
let lastLeader = null;
let lastLeadChange;
for (let i = 0; i < this.hands.length; i++) {
let hand = this.hands[i];
let handDetails = this.parseHand(hand);
let call = "";
//Track winning streak
streak = lastWinner === handDetails.winner ? streak + 1 : 0;
lastWinner = handDetails.winner;
//Track change in who leads
leader = hand.counts[0] > hand.counts[1] ? 0 : 1;
if (leader !== lastLeader) {
//Leader changed
lastLeadChange = i;
}
lastLeader = leader;
if (!handDetails.gameover) {
if (!handDetails.tie) {
if (!handDetails.war) {
if (streak > 1) {
call += this.createCall("#again#", handDetails);
} else {
call += this.createCall("#call#", handDetails);
}
} else {
call += this.createCall("#warOver#", handDetails);
}
const spread = hand.counts[0] - hand.counts[1];
if (Math.abs(spread - spreadAtLastUpdate) >= 8) {
if (hand.counts[0] !== hand.counts[1]) {
call += ` ${this.players[leader].lastName} leads ${
hand.counts[leader]
} to ${hand.counts[(leader + 1) % 2]}.`;
} else {
call += " The games tied.";
}
spreadAtLastUpdate = spread;
}
} else {
const rank = handDetails.rank.toLowerCase();
call += this.grammar.flatten(`[card:${rank}]#tied#`);
}
} else {
if (handDetails.losingCard) {
call += `${handDetails.winner.lastName}'s ${handDetails.winningCard.name} beats ${handDetails.loser.lastName}'s ${handDetails.losingCard.name} to finish the game.`;
} else {
call += `${handDetails.loser.lastName} is out of cards. That's the game.`;
}
}
yield call;
}
}
getConclusion() {
return `${this.winner} wins in ${this.hands.length < 150 ? "just " : ""}${
this.hands.length
} hands.`;
}
}
module.exports = PlayByPlay;