-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrow.js
424 lines (397 loc) · 14.9 KB
/
arrow.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
'use strict'
const express = require('express')
const http = require('http')
const fs = require('fs')
var app = express()
var server = http.createServer(app)
var io = require('socket.io')(server)
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/client/arrow.html`)
})
app.use(express.static(`${__dirname}/client`))
const unix = '/run/games/arrow.socket'
server.listen(unix)
console.log(`server started on ${unix}`)
server.on('listening', () => fs.chmodSync(unix, 0o777))
const saveFile = 'arrow.json'
const games = JSON.parse(fs.readFileSync(saveFile, 'utf8'))
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))
}
process.on('SIGINT', () => { saveGames(); fs.unlinkSync(unix); process.exit() })
process.on('uncaughtExceptionMonitor', saveGames)
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 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 }))
})
io.in(room).emit('updateGames', data)
}
function makePieces() {
const pieces = []
for (let d0 = 0; d0 < 2; d0++)
for (let d1 = 0; d1 < 2; d1++)
for (let d2 = 0; d2 < 2; d2++)
for (let d3 = 0; d3 < 2; d3++)
pieces.push({d: [d0, d1, d2, d3]})
return pieces
}
function lineIndex(x, i, j) {
if (x === 0) return 4 * i + j
if (x === 1) return 4 * j + i
if (x === 2 && i === 0) return 4 * j + j
return 4 * j + (3 - j)
}
function markArrow(board) {
let marked = false
for (let x = 0; x < 3; x++) {
const imax = x < 2 ? 4 : 2
for (let i = 0; i < imax; i++) {
let ds = [0, 0, 0, 0]
let j, d
for (j = 0; j < 4; j++) {
const k = lineIndex(x, i, j)
if (!board[k]) { j = 5; break }
for (d = 0; d < 4; d++)
ds[d] += board[k].d[d]
}
if (j === 5) continue
for (d = 0; d < 4; d++) {
if (ds[d] === 0 || ds[d] === 4) {
for (j = 0; j < 4; j++) {
const k = lineIndex(x, i, j)
board[k].arrow = true
}
marked = true
}
}
}
}
return marked
}
const stateKeys = {
game: [
'players', 'started', 'picking', 'dropping',
'board', 'pieces', 'ended'
],
board: true, pieces: true,
players: [ 'current', 'winner' ]
}
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)
game.undoLog.push(entry)
io.in(gameName).emit('showUndo', true)
}
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 = `Archer${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)
socket.join(`${gameName}spectators`)
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')
socket.emit('updateBoard', game.board)
socket.emit('updatePieces', { pieces: game.pieces })
}
}
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)) {
if (socket.rooms.size === 2 && socket.rooms.has(socket.id) && socket.rooms.has('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')
socket.emit('updateBoard', game.board)
const data = { pieces: game.pieces }
const current = game.players.find(player => player.current)
if (current) data.currentPlayer = current.name
socket.emit('updatePieces', data)
if (game.undoLog.length) socket.emit('showUndo', true)
}
else {
console.log(`error: ${socket.playerName} rejoining ${gameName} while in ${socket.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.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}.`)
}
}
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('updatePlayers', game.players)
io.in(gameName).emit('updateBoard', game.board)
const data = { pieces: game.pieces }
const current = game.players.find(player => player.current)
if (current) data.currentPlayer = current.name
io.in(gameName).emit('updatePieces', data)
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 > 1) {
console.log(`starting ${gameName}`)
game.started = true
game.undoLog = []
game.pieces = makePieces()
game.board = []
for (let i = 0; i < 16; i++) game.board[i] = null
const current = game.players[Math.floor(Math.random() * game.players.length)]
current.current = true
game.picking = true
io.in(gameName).emit('gameStarted')
io.in(gameName).emit('updatePlayers', game.players)
io.in(gameName).emit('updateBoard', game.board)
io.in(gameName).emit('updatePieces', { pieces: game.pieces, currentPlayer: current.name })
}
else {
socket.emit('errorMsg', 'Error: not enough players to start.')
}
}
else {
console.log(`${socket.playerName} attempted to start ${gameName} again`)
socket.emit('errorMsg', `Error: ${gameName} has already started.`)
}
}))
socket.on('pickRequest', pieceIndex => inGame((gameName, game) => {
if (game.picking) {
const currentIndex = game.players.findIndex(player => player.socketId === socket.id)
if (0 <= currentIndex) {
const player = game.players[currentIndex]
if (player.current &&
Number.isInteger(pieceIndex) && 0 <= pieceIndex && pieceIndex < game.pieces.length &&
game.pieces[pieceIndex] !== null) {
appendUndo(gameName)
game.pieces[pieceIndex].selected = true
let nextIndex = currentIndex + 1
if (nextIndex === game.players.length) nextIndex = 0
const nextPlayer = game.players[nextIndex]
delete player.current
nextPlayer.current = true
delete game.picking
game.dropping = true
io.in(gameName).emit('updatePlayers', game.players)
io.in(gameName).emit('updatePieces', { pieces: game.pieces, currentPlayer: nextPlayer.name })
}
else {
console.log(`${socket.playerName} played invalid data or is not current`)
socket.emit('errorMsg', `Error: invalid play data.`)
}
}
else {
console.log(`${socket.playerName} not found as player in ${gameName}`)
socket.emit('errorMsg', `Error: could not find you as a player.`)
}
}
else {
console.log(`${socket.playerName} picking in wrong state for ${gameName}`)
socket.emit('errorMsg', `Error: ${gameName} is not open for picking.`)
}
}))
socket.on('dropRequest', placeIndex => inGame((gameName, game) => {
if (game.dropping) {
const currentIndex = game.players.findIndex(player => player.socketId === socket.id)
const pieceIndex = game.pieces.findIndex(piece => piece && piece.selected)
if (0 <= currentIndex) {
const player = game.players[currentIndex]
if (player.current && 0 <= pieceIndex &&
Number.isInteger(placeIndex) && 0 <= placeIndex && placeIndex < game.board.length &&
game.board[placeIndex] === null) {
appendUndo(gameName)
delete game.dropping
const piece = game.pieces[pieceIndex]
game.pieces[pieceIndex] = null
game.board[placeIndex] = piece
const data = { pieces: game.pieces }
if (markArrow(game.board)) {
game.ended = true
delete player.current
player.winner = true
io.in(gameName).emit('updatePlayers', game.players)
}
else {
data.currentPlayer = player.name
game.picking = true
}
io.in(gameName).emit('updateBoard', game.board)
io.in(gameName).emit('updatePieces', data)
}
else {
console.log(`${socket.playerName} played invalid data or is not current`)
socket.emit('errorMsg', `Error: invalid play data.`)
}
}
else {
console.log(`${socket.playerName} not found as player in ${gameName}`)
socket.emit('errorMsg', `Error: could not find you as a player.`)
}
}
else {
console.log(`${socket.playerName} dropping in wrong state for ${gameName}`)
socket.emit('errorMsg', `Error: ${gameName} is not open for dropping.`)
}
}))
socket.on('disconnecting', () => {
console.log(`${socket.playerName} exiting ${socket.gameName}`)
const gameName = socket.gameName
const game = games[gameName]
if (game) {
if (!game.started) {
const notThisPlayer = player => player.socketId !== socket.id
game.players = game.players.filter(notThisPlayer)
game.spectators = game.spectators.filter(notThisPlayer)
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)
}
if (game.timeout && game.players.every(player => !player.socketId)) {
console.log(`pausing ${gameName} since all players are disconnected`)
clearTimeout(game.timeout)
delete game.timeout
}
}
}
updateGames()
}
})
socket.on('deleteGame', gameName => {
delete games[gameName]
updateGames()
})
socket.on('saveGames', saveGames)
})