-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
311 lines (269 loc) · 10.2 KB
/
server.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
const express = require('express');
const exphbs = require('express-handlebars');
const cookieParser = require('cookie-parser');
const crypto = require('crypto');
const fs = require('fs');
const HERO_CARDS = require('./heroCards.json');
const VILLAIN_CARDS = require('./villainCards.json');
const SESSION_COOKIE = "session";
const SESSION_LEN = 32; // The number of bytes in a session token.
const PLAYERS_DIR = './data/players';
const app = express();
const port = process.env.PORT || 3000;
var players = {};
// Load datafiles (synchronously so that we don't
// start the server until we're finished)
fs.mkdirSync(PLAYERS_DIR, {recursive: true});
var files = fs.readdirSync(PLAYERS_DIR);
for (var i = 0; i < files.length; i++) {
if (files[i].endsWith('.json')) {
console.log("Loading", files[i]);
var json = fs.readFileSync(PLAYERS_DIR + '/' + files[i]);
var session = files[i].substr(0, files[i].length - '.json'.length);
players[session] = JSON.parse(json);
}
}
/// Performs an in-place Fisher-Yates shuffle. Also returns the array.
/// If bias is true, shuffle is biased so that elements from the front of the array
/// are more likely to stay closer to the front of the array. This is so that
/// the player will be more likely to encounter the weaker villains earlier in the game.
function shuffle(array, biasFactor) {
if (!biasFactor) biasFactor = 1;
for (var i = 0; i < array.length; i++) {
// Grab a random element from the unshuffled portion
// of the array and swap it with this index.
var targetIndex = i + Math.floor(Math.pow(Math.random(), biasFactor) * (array.length - i));
var old = array[i];
array[i] = array[targetIndex];
array[targetIndex] = old;
}
return array;
}
// Initializes a player.
function newPlayer() {
var level1Heroes = [...HERO_CARDS.keys()].filter((index) => HERO_CARDS[index].cost == 1);
var level2Heroes = [...HERO_CARDS.keys()].filter((index) => HERO_CARDS[index].cost == 2);
var unshuffledVillainDeck = [...VILLAIN_CARDS.keys()];
unshuffledVillainDeck.sort((a, b) =>
(VILLAIN_CARDS[b].attack - VILLAIN_CARDS[a].attack),
);
var villainDeck = shuffle(unshuffledVillainDeck, 2.2);
//console.log(villainDeck.map((a) => VILLAIN_CARDS[a].attack))
var player = {
// The currently active hero cards. Each element
// is an object with integer properties 'index' and 'level'.
heroes: [],
// The currently active villain cards. Each element
// is an object with one integer property, 'index'.
villains: [],
money: 2,
score: 0,
// An array of indices in HERO_CARDS.
level1Deck: shuffle(level1Heroes),
level2Deck: shuffle(level2Heroes),
// An array of indices in VILLAIN_CARDS.
villainDeck: villainDeck
};
player.heroes.push({
index: player.level1Deck.pop(),
level: 0
});
for (var i = 0; i < 3; i++) {
player.villains.push({
index: player.villainDeck.pop()
});
}
return player;
}
/// A middleware that looks up the player's session information.
/// The session token is stored in req.session, and the player
/// information is stored in req.player. After the request is processed,
/// the session cookie is set on the response and the player is saved to disk.
function getPlayerMiddleware(req, res, next) {
req.session = req.cookies[SESSION_COOKIE];
if (req.session !== undefined) req.player = players[req.session];
if (req.player === undefined) {
req.session = crypto.randomBytes(SESSION_LEN).toString('hex');
req.player = newPlayer();
}
res.cookie(SESSION_COOKIE, req.session, {sameSite: 'Lax'});
next();
// Save the (potentially-updated) player state
players[req.session] = req.player;
var json = JSON.stringify(req.player);
fs.writeFile(`${PLAYERS_DIR}/${req.session}.json`, json, (err) => {
if (err) console.log(`Failed to write player data: ${err}`);
});
}
/// Looks up cards for a given player (converting from the indexes
/// in the player object to the names and stats).
function getCardInfo(player) {
//console.log(player);
return {
heroes: player.heroes.map((card) => HERO_CARDS[card.index].levels[card.level]),
villains: player.villains.map((card) => VILLAIN_CARDS[card.index])
};
}
/// Returns the stat boost the first card gets when fighting the second.
function checkStats(first, second) {
function checkStat(firstStat, secondStat) {
if (firstStat === undefined || secondStat === undefined) return 0;
else return firstStat;
}
return checkStat(first.tech, second.power) +
checkStat(first.power, second.brawn) +
checkStat(first.brawn, second.tech);
}
app.use(cookieParser());
app.use(express.json());
app.use(getPlayerMiddleware);
app.engine('handlebars', exphbs({ defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.status(200).render('game', {
score: req.player.score,
money: req.player.money,
...getCardInfo(req.player)
});
});
app.post('/new', (req, res) => {
// Reset the session and redirect.
req.player = newPlayer();
res.redirect(303, '/');
});
app.post('/play', (req, res) => {
var player = req.player;
if (req.body && req.body.hero && req.body.villain) {
// Get the index of the selected cards in the player's hand
var heroIndex = player.heroes.findIndex((card) => HERO_CARDS[card.index].levels[card.level].name === req.body.hero);
var villainIndex = player.villains.findIndex((card) => VILLAIN_CARDS[card.index].name === req.body.villain);
if (heroIndex == -1 || villainIndex == -1) {
res.status(404).send("You don't have that card.");
return;
}
var hero = HERO_CARDS[player.heroes[heroIndex].index].levels[player.heroes[heroIndex].level];
var villain = VILLAIN_CARDS[player.villains[villainIndex].index];
while (true) {
var heroDice = Math.floor(Math.random() * 6) + 1;
var villDice = Math.floor(Math.random() * 6) + 1;
var heroBoost = checkStats(hero, villain);
var villBoost = checkStats(villain, hero);
var heroScore = heroDice + hero.attack + heroBoost;
var villScore = villDice + villain.attack + villBoost;
if (heroScore == villScore) continue; // Tie; roll again.
var win = (heroScore > villScore);
var endgame = false;
if (win) {
player.money += villain.cost;
player.score += villain.cost;
// The villain card is lost.
player.villains.splice(villainIndex, 1);
if (player.villainDeck.length != 0) {
player.villains.push({index: player.villainDeck.pop()});
} else {
// The deck is out of villain cards.
if (player.villains.length === 0) {
endgame = true;
}
}
} else {
// The hero card is lost.
player.heroes.splice(heroIndex, 1);
if (player.heroes.length === 0 && player.money === 0) {
endgame = true;
}
}
if (endgame) {
// Reset the player's session to restart the game.
player = newPlayer();
req.player = player;
}
res.status(200).send({
win: win,
money: player.money,
score: player.score,
hero: {
dice: heroDice,
attack: hero.attack,
boost: heroBoost,
total: heroScore
},
villain: {
dice: villDice,
attack: villain.attack,
boost: villBoost,
total: villScore
},
cards: getCardInfo(player),
endgame: endgame
});
break;
}
} else {
res.status(400).send('Invalid request');
}
});
app.post('/upgrade/:card', (req, res) => {
var player = req.player;
var card = req.params.card;
if (card == "1") {
if (player.level1Deck.length == 0) {
res.status(403).send('Level 1 deck empty');
return;
}
if (player.money < 1) {
res.status(403).send("You don't have enough points");
return;
}
player.money -= 1;
player.heroes.push({
index: player.level1Deck.pop(),
level: 0
});
} else if (card == "2") {
if (player.level2Deck.length == 0) {
res.status(403).send('Level 1 deck empty');
return;
}
if (player.money < 2) {
res.status(403).send("You don't have enough points");
return;
}
player.money -= 2;
player.heroes.push({
index: player.level2Deck.pop(),
level: 0
});
} else {
// Get the index of the selected card in the player's hand
var heroIndex = player.heroes.findIndex((playerCard) =>
HERO_CARDS[playerCard.index].levels[playerCard.level].name === card
);
if (heroIndex == -1) {
res.status(403).send("You don't have that card.");
return;
}
if (player.money < 1) {
res.status(403).send("You don't have enough points");
return;
}
var newLevel = player.heroes[heroIndex].level + 1;
if (newLevel >= HERO_CARDS[player.heroes[heroIndex].index].levels.length) {
res.status(403).send("That card is already fully upgraded.");
return;
}
player.money -= 1;
player.heroes[heroIndex].level = newLevel;
}
res.status(200).send({
money: player.money,
cards: getCardInfo(player)
});
});
app.use('*', (req, res) => {
res.status(404).render('404');
});
app.listen(port, () => {
console.log(`Server listening http://localhost:${port}`);
});