-
Notifications
You must be signed in to change notification settings - Fork 0
/
unite.js
577 lines (538 loc) · 19.7 KB
/
unite.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
'use strict'
const fs = require('fs')
const express = require('express')
const app = express()
const gname = 'unite'
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/client/${gname}.html`)
})
app.use(express.static(`${__dirname}/client`))
const config = require(`${__dirname}/client/config.js`)
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const port = config.ServerPort(gname)
const unix = typeof port === 'string'
server.listen(port)
console.log(`server started on ${port}`)
if (unix)
server.on('listening', () => fs.chmodSync(port, 0o777))
const saveFile = `${gname}.json`
const games = JSON.parse(fs.readFileSync(saveFile, 'utf8'))
const randomLetter = () => String.fromCharCode(65 + Math.random() * 26)
function randomUnusedGameName() {
if (Object.keys(games).length === 26 * 26) {
console.log('all game names in use')
return 'Overflow'
}
let name
do { name = randomLetter() + randomLetter() } while (name in games)
return name
}
function saveGames() {
let toSave = {}
for (const [gameName, game] of Object.entries(games))
if (game.started) toSave[gameName] = game
fs.writeFileSync(saveFile,
JSON.stringify(
toSave,
(k, v) => k === 'socketId' ? null :
k === 'spectators' ? [] : v))
}
function updateGames(room) {
if (!room) room = 'lobby'
const data = []
for (const [gameName, game] of Object.entries(games))
data.push({ name: gameName,
players: game.players.map(player =>
({ name: player.name, socketId: player.socketId, winner: player.winner }))
})
io.in(room).emit('updateGames', data)
}
function shuffleInPlace(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i)
const t = array[i]
array[i] = array[j]
array[j] = t
}
}
function makeDeck() {
const deck = []
for (let s = 0; s < 4; s++) {
const sdeck = []
deck.push(sdeck)
for (let r = 0; r < 10; r++)
sdeck.push(r)
shuffleInPlace(sdeck)
}
return deck
}
function appendLog(gameName, entry) {
const game = games[gameName]
game.log.push(entry)
io.in(gameName).emit('appendLog', entry)
}
const stateKeys = {
game: [
'players', 'started', 'board', 'deck', 'ended'
], board: true, deck: true,
players: [ 'current', 'winner', 'hand' ], hand: true
}
function copy(keys, from, to, restore) {
for (const key of keys)
if (key in from) {
if (stateKeys[key] === true)
to[key] = JSON.parse(JSON.stringify(from[key]))
else if (key === 'players') {
if (!restore)
to.players = from.players.map(_ => ({}))
for (let i = 0; i < from.players.length; i++)
copy(stateKeys.players, from.players[i], to.players[i], restore)
}
else if (stateKeys[key]) {
if (!restore || !(key in to))
to[key] = {}
copy(stateKeys[key], from[key], to[key], restore)
}
else
to[key] = from[key]
}
else if (restore && key in to)
delete to[key]
}
function appendUndo(gameName) {
const game = games[gameName]
const entry = {}
copy(stateKeys.game, game, entry)
entry.logLength = game.log.length
game.undoLog.push(entry)
io.in(gameName).emit('showUndo', true)
}
function updateBoard(gameName, roomName) {
if (!roomName) roomName = gameName
const game = games[gameName]
io.in(roomName).emit('updateBoard', { board: game.board, deck: game.deck })
}
const absRow = (relRow, playerIndex) =>
playerIndex === 0 ? relRow : (2 - relRow)
function validColumns(z) {
const vc = [[],[]]
/*
1 y 3 n 3 n
2 n 2 n 2 y
1 y 1 y 3 n
*/
for (let side = 0; side < 2; side++) {
if (z[0][side] < z[1][side])
vc[side].push(0)
if (z[1][side] < z[0][side] &&
z[1][side] < z[2][side])
vc[side].push(1)
if (z[2][side] < z[1][side])
vc[side].push(2)
}
return vc
}
const cardCmp = (a, b) =>
a.s === b.s ? a.r - b.r : a.s - b.s
function plotColumns(col, dir) {
let t1 = col, t2, t3
if ((col === 1 && dir === 0) || (col === -1 && dir === 1)) {
t2 = t1
t3 = 0
}
else {
const i = dir ? 1 : -1
if (col === 0) {
t2 = i
t3 = i
}
else {
t2 = t1 + i
t3 = t2 + i
}
}
return [t2, t3]
}
function validPlots(board, hand, playerIndex) {
const vp = []
const row = absRow(0, playerIndex)
const L = playerIndex
const R = 1 - playerIndex
const base = playerIndex ? board.b[row].slice().reverse() : board.b[row]
for (let dir = 0; dir < 2; dir++) {
let col = -board.z[row][L]
col += col === -1 ? 1 : 2
for (const baseCard of base) {
const pcs = plotColumns(col, dir)
if (hand.includes(baseCard.r) &&
-board.z[1][L] < pcs[0] && pcs[0] < board.z[1][R] &&
-board.z[2 - row][L] < pcs[1] && pcs[1] < board.z[2 - row][R])
vp.push([col, pcs[0], pcs[1]])
if (col === -1) col = 0
else if (col === 0) col = 1
else col += 2
}
}
return vp
}
function checkPlot(newHand, newBoard, cols, hand, board, playerIndex) {
if (!(Array.isArray(cols) && cols.length === 3)) return false
if (!board.validPlots.some(v => cols.every((c, i) => c === v[i]))) return false
const row = absRow(0, playerIndex)
const colSign = playerIndex ? -1 : 1
const toCards = (r, s) => r === null ? [] : [{r: r, s: s}]
const oldCards = hand.flatMap(toCards)
const newCards = newHand.flatMap(toCards).concat(newBoard)
newCards.sort(cardCmp)
const c1 = board.b[row].find(c => c.c === colSign * cols[0])
const c2 = board.b[1].find(c => c.c === colSign * cols[1])
const c3 = board.b[2 - row].find(c => c.c === colSign * cols[2])
oldCards.push({r: c1.r, s: c1.s})
oldCards.push({r: c2.r, s: c2.s})
oldCards.push({r: c3.r, s: c3.s})
oldCards.sort(cardCmp)
if (oldCards.length !== newCards.length) return false
for (let i = 0; i < oldCards.length; i++)
if (!(oldCards[i].r === newCards[i].r &&
oldCards[i].s === newCards[i].s))
return false
return [c1, c2, c3]
}
const colShift = (row, dir, col) =>
col + dir * (
(Math.abs(col) <= 2 &&
((Math.sign(col) === Math.sign(dir))
=== (Math.abs(col) === 1 && row === 1))) ?
1 : 2)
const cardShift = (row, dir) =>
c => c.c = colShift(row, dir, c.c)
function rebalance(board) {
const imbalances = board.z.map(z => z[1] - z[0])
if (imbalances.every(b => 2 < b)) {
board.z.forEach(z => {
z[0] += 2
z[1] -= 2
})
board.b.forEach((row, i) => row.forEach(cardShift(i, -1)))
}
else if (imbalances.every(b => b < -2)) {
board.z.forEach(z => {
z[0] -= 2
z[1] += 2
})
board.b.forEach((row, i) => row.forEach(cardShift(i, +1)))
}
}
function checkWin(gameName) {
const game = games[gameName]
const winner = game.players.find(player =>
player.hand.every(r => r !== null) &&
(new Set(player.hand)).size === 1)
if (winner) {
game.ended = true
winner.winner = true
appendLog(gameName, `The game ends with ${winner.name} victorious!`)
}
}
io.on('connection', socket => {
console.log(`new connection ${socket.id}`)
socket.emit('ensureLobby')
socket.join('lobby'); updateGames(socket.id)
socket.on('joinRequest', data => {
let game
let gameName = data.gameName
if (!gameName) gameName = randomUnusedGameName()
if (!(gameName in games)) {
console.log(`new game ${gameName}`)
game = { players: [], spectators: [] }
games[gameName] = game
}
else
game = games[gameName]
if (!data.playerName) {
socket.playerName = `Knight${Math.floor(Math.random()*20)}`
console.log(`random name ${socket.playerName} for ${socket.id}`)
}
else {
socket.playerName = data.playerName
console.log(`name ${socket.playerName} supplied for ${socket.id}`)
}
if (data.spectate) {
if (game.spectators.every(spectator => spectator.name !== socket.playerName)) {
console.log(`${socket.playerName} joining ${gameName} as spectator`)
socket.gameName = gameName
socket.leave('lobby'); socket.emit('updateGames', [])
socket.join(gameName)
game.spectators.push({ socketId: socket.id, name: socket.playerName })
socket.emit('joinedGame',
{ gameName: gameName, playerName: socket.playerName, spectating: true })
io.in(gameName).emit('updateSpectators', game.spectators)
socket.emit('updatePlayers', game.players)
if (game.started) {
socket.emit('gameStarted')
updateBoard(gameName, socket.id)
game.log.forEach(entry => socket.emit('appendLog', entry))
}
}
else {
console.log(`${socket.playerName} barred from joining ${gameName} as duplicate spectator`)
socket.emit('errorMsg', `Game ${gameName} already contains spectator ${socket.playerName}.`)
}
}
else if (game.started) {
if (game.players.find(player => player.name === socket.playerName && !player.socketId)) {
const rooms = Object.keys(socket.rooms)
if (rooms.length === 2 && rooms.includes(socket.id) && rooms.includes('lobby')) {
console.log(`${socket.playerName} rejoining ${gameName}`)
socket.gameName = gameName
socket.leave('lobby'); socket.emit('updateGames', [])
socket.join(gameName)
const player = game.players.find(player => player.name === socket.playerName)
player.socketId = socket.id
socket.emit('joinedGame', { gameName: gameName, playerName: socket.playerName })
socket.emit('updateSpectators', game.spectators)
io.in(gameName).emit('updatePlayers', game.players)
socket.emit('gameStarted')
updateBoard(gameName, socket.id)
game.log.forEach(entry => socket.emit('appendLog', entry))
if (game.undoLog.length)
socket.emit('showUndo', true)
}
else {
console.log(`error: ${socket.playerName} rejoining ${gameName} while in ${rooms}`)
socket.emit('errorMsg', 'Error: somehow this connection is already used in another game.')
}
}
else {
console.log(`${socket.playerName} barred from joining ${gameName} as extra player`)
socket.emit('errorMsg', `Game ${gameName} has already started. Try spectating.`)
}
}
else {
if (game.players.length < 2) {
if (game.players.every(player => player.name !== socket.playerName)) {
console.log(`${socket.playerName} joining ${gameName}`)
socket.leave('lobby'); socket.emit('updateGames', [])
socket.join(gameName)
socket.gameName = gameName
game.players.push({ socketId: socket.id, name: socket.playerName })
socket.emit('joinedGame', { gameName: gameName, playerName: socket.playerName })
socket.emit('updateSpectators', game.spectators)
io.in(gameName).emit('updatePlayers', game.players)
}
else {
console.log(`${socket.playerName} barred from joining ${gameName} as duplicate player`)
socket.emit('errorMsg', `Game ${gameName} already contains player ${socket.playerName}.`)
}
}
else {
console.log(`${socket.playerName} barred from joining ${gameName} as extra player`)
socket.emit('errorMsg', `Game ${gameName} is full. Try spectating.`)
}
}
updateGames()
})
function inGame(func) {
const gameName = socket.gameName
const game = games[gameName]
if (game) func(gameName, game)
else {
console.log(`${socket.playerName} failed to find game ${gameName}`)
socket.emit('errorMsg', `Game ${gameName} not found. Try reconnecting.`)
}
}
socket.on('undoRequest', () => inGame((gameName, game) => {
if (game.started && game.undoLog.length) {
const entry = game.undoLog.pop()
copy(stateKeys.game, entry, game, true)
io.in(gameName).emit('removeLog', game.log.length - entry.logLength)
game.log.length = entry.logLength
io.in(gameName).emit('updatePlayers', game.players)
updateBoard(gameName)
if (!game.undoLog.length)
io.in(gameName).emit('showUndo', false)
io.in(gameName).emit('errorMsg', `${socket.playerName} pressed Undo.`)
}
else {
console.log(`error: ${socket.playerName} in ${gameName} tried to undo nothing`)
socket.emit('errorMsg', 'Error: there is nothing to undo.')
}
}))
socket.on('startGame', () => inGame((gameName, game) => {
if (!game.started) {
if (game.players.length === 2) {
console.log(`starting ${gameName}`)
game.started = true
game.undoLog = []
game.log = []
game.deck = makeDeck()
game.players.forEach(player => player.hand = [null, null, null, null])
const playerIndex = Math.floor(Math.random() * 2)
const currentPlayer = game.players[playerIndex]
currentPlayer.current = true
game.board = { b: [[],[],[]], z: [[1,1],[2,2],[1,1]] }
game.board.b[0].push({s: 0, c: 0, r: game.deck[0].pop()})
game.board.b[1].push({s: 0, c: -1, r: game.deck[0].pop()})
game.board.b[1].push({s: 0, c: +1, r: game.deck[0].pop()})
game.board.b[2].push({s: 0, c: 0, r: game.deck[0].pop()})
game.board.validColumns = validColumns(game.board.z)
game.board.validPlots = validPlots(game.board, currentPlayer.hand, playerIndex)
io.in(gameName).emit('gameStarted')
io.in(gameName).emit('updatePlayers', game.players)
updateBoard(gameName)
appendLog(gameName, 'The game begins!')
updateGames()
}
else {
console.log(`${socket.playerName} attempted to start ${gameName} without 2 players`)
socket.emit('errorMsg', `Error: ${gameName} needs exactly 2 players to start.`)
}
}
else {
console.log(`${socket.playerName} attempted to start ${gameName} again`)
socket.emit('errorMsg', `Error: ${gameName} has already started.`)
}
}))
socket.on('takeRequest', data => inGame((gameName, game) => {
if (game.started && !game.ended) {
const suit = data.suit
const playerIndex = game.players.findIndex(player => player.socketId === socket.id)
const player = game.players[playerIndex]
if (0 <= playerIndex && player.current) {
if (Number.isInteger(suit) && 0 <= suit && suit < 4 && game.deck[suit].length &&
((player.hand[suit] === null && !data.pos && !data.keepHand) ||
typeof data.keepHand === 'boolean' &&
data.pos && Number.isInteger(data.pos.row) && 0 <= data.pos.row && data.pos.row < 3 &&
Number.isInteger(data.pos.side) && 0 <= data.pos.side && data.pos.side < 2 &&
game.board.validColumns[data.pos.side].includes(data.pos.row))) {
appendUndo(gameName)
const rank = game.deck[suit].pop()
const handRank = player.hand[suit]
if (handRank !== null) {
const side = data.pos.side
const row = data.pos.row
const col = game.board.z[row][side] * (side ? 1 : -1)
const toBoard = { s: suit, r: rank, c: col }
game.board.z[row][side] += 2
if (!data.keepHand) {
toBoard.r = player.hand[suit]
player.hand[suit] = rank
}
game.board.b[row][['unshift','push'][side]](toBoard)
}
else {
player.hand[suit] = rank
}
appendLog(gameName, {name: player.name, suit: suit, rank: rank,
keepHand: data.keepHand, handRank: handRank})
delete player.current
checkWin(gameName)
if (!game.ended) {
const nextIndex = 1 - playerIndex
const nextPlayer = game.players[nextIndex]
nextPlayer.current = true
rebalance(game.board)
game.board.validColumns = validColumns(game.board.z)
game.board.validPlots = validPlots(game.board, nextPlayer.hand, nextIndex)
}
io.in(gameName).emit('updatePlayers', game.players)
updateBoard(gameName)
}
else {
console.log(`error: ${socket.playerName} in ${gameName} made an invalid take`)
socket.emit('errorMsg', `Error: that is not a valid take.`)
}
}
else {
console.log(`error: ${socket.playerName} in ${gameName} not found or not current`)
socket.emit('errorMsg', 'Error: it is not your turn to take.')
}
}
else {
console.log(`error: ${socket.playerName} in ${gameName} tried taking before start`)
socket.emit('errorMsg', `Error: taking is not currently possible.`)
}
}))
socket.on('reorderRequest', data => inGame((gameName, game) => {
if (game.started && !game.ended) {
const playerIndex = game.players.findIndex(player => player.socketId === socket.id)
const player = game.players[playerIndex]
if (0 <= playerIndex && player.current) {
const boardCards = checkPlot(data.hand, data.board, data.cols,
player.hand, game.board, playerIndex)
if (boardCards) {
appendUndo(gameName)
player.hand = data.hand
const oldCards = boardCards.map(c => ({r: c.r, s: c.s}))
data.board.forEach((c, i) => {
boardCards[i].r = c.r
boardCards[i].s = c.s
})
appendLog(gameName, {name: player.name, oldCards: oldCards, newCards: data.board})
delete player.current
checkWin(gameName)
if (!game.ended) {
const nextIndex = 1 - playerIndex
const nextPlayer = game.players[nextIndex]
nextPlayer.current = true
game.board.validPlots = validPlots(game.board, nextPlayer.hand, nextIndex)
}
io.in(gameName).emit('updatePlayers', game.players)
updateBoard(gameName)
}
else {
console.log(`error: ${socket.playerName} in ${gameName} tried reordering with bad data`)
socket.emit('errorMsg', `Error: reorder data is invalid.`)
}
}
else {
console.log(`error: ${socket.playerName} in ${gameName} not found or not current`)
socket.emit('errorMsg', 'Error: it is not your turn to reorder.')
}
}
else {
console.log(`error: ${socket.playerName} in ${gameName} tried reordering before start`)
socket.emit('errorMsg', `Error: reordering is not currently possible.`)
}
}))
socket.on('disconnecting', () => {
console.log(`${socket.playerName} exiting ${socket.gameName}`)
const gameName = socket.gameName
const game = games[gameName]
if (game) {
if (!game.started) {
game.players = game.players.filter(player => player.socketId !== socket.id)
game.spectators = game.spectators.filter(player => player.socketId !== socket.id)
io.in(gameName).emit('updateSpectators', game.spectators)
io.in(gameName).emit('updatePlayers', game.players)
if (game.players.length === 0 && game.spectators.length === 0) {
console.log(`removing empty game ${gameName}`)
delete games[gameName]
}
}
else {
const spectators = game.spectators.filter(player => player.socketId !== socket.id)
if (spectators.length < game.spectators.length) {
game.spectators = spectators
io.in(gameName).emit('updateSpectators', game.spectators)
}
else {
const player = game.players.find(player => player.socketId === socket.id)
if (player) {
player.socketId = null
io.in(gameName).emit('updatePlayers', game.players)
}
}
}
updateGames()
}
})
socket.on('deleteGame', gameName => {
delete games[gameName]
updateGames()
})
socket.on('saveGames', saveGames)
})
process.on('SIGINT', () => { saveGames(); if (unix) fs.unlinkSync(port); process.exit() })
process.on('uncaughtExceptionMonitor', saveGames)