-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
494 lines (420 loc) · 17.8 KB
/
index.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
import compression from 'compression'
import ejs from 'ejs'
import express from 'express'
import { createServer } from 'http'
import path from 'path'
import { Server } from 'socket.io'
const app = express()
const server = createServer(app)
const io = new Server(server)
app.use(compression())
app.use('/styles', express.static(path.resolve('static/styles')))
app.use('/img', express.static(path.resolve('static/styles/img')))
app.use('/emotes', express.static(path.resolve('static/styles/img/emotes')))
app.use('/js', express.static(path.resolve('static/js')))
app.engine('html', ejs.renderFile)
app.set('view engine', 'html')
app.get('/create', async function (request, response) {
let roomId = await createRoom()
console.log(`Комната создана: ${roomId}`)
response.redirect(`/room/${roomId}`)
})
app.get('/create/:idLength', async function (request, response) {
let idLength = parseInt(request.params.idLength) || ID_LENGTH
let roomId = await createRoom(idLength <= 20 ? idLength : ID_LENGTH)
console.log(`Комната создана: ${roomId} с длиной ID ${idLength}`)
response.redirect(`/room/${roomId}`)
})
app.get('/', async function (request, response) {
response.render(path.resolve('static/html/start.html'))
})
app.get('/room/:roomId', async function (request, response) {
let roomId = request.params.roomId
let currentRoom = await getRoomById(roomId)
if (currentRoom) {
console.log(`Комната найдена: ${roomId}`)
response.render(path.resolve('static/html/index.html'), { roomId: roomId })
} else {
response.redirect('/')
}
})
app.get('/*', async function (request, response) {
response.redirect('/')
})
const rooms = []
const offsets = [
{ x: -1, y: -1 },
{ x: -1, y: 0 },
{ x: -1, y: 1 },
{ x: 0, y: -1 },
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 1, y: -1 },
{ x: 1, y: 0 },
{ x: 1, y: 1 }
]
const autoMissOffsets = [
{ x: -1, y: -1 },
{ x: -1, y: 1 },
{ x: 0, y: 0 },
{ x: 1, y: -1 },
{ x: 1, y: 1 }
]
io.on('connection', async function (socket) {
console.log(`Успешное соединение ${socket.id}`)
socket.ready = false
socket.lastReadyToggle = null
socket.shootedCells = []
socket.wantsToRematch = false
socket.ships = []
socket.role = ''
socket.winner = false
socket.lastMessageTime = null
socket.on('connect to room', async function (data) {
if (!data || !data.roomId) return
let currentRoom = await getRoomById(data.roomId)
if (currentRoom) {
if (!currentRoom.participant) {
socket.emit('connect to room', { roomId: currentRoom.id })
} else {
socket.emit('connecting room is full')
}
} else {
socket.emit('connecting room not found')
}
})
socket.on('join', async function (data) {
if (!data || !data.roomId) return
let currentRoom = await getRoomById(data.roomId)
if (currentRoom) {
console.log(`Подключение ${socket.id} в ${currentRoom.id}`)
if (!currentRoom.host) {
socket.roomId = data.roomId
currentRoom.host = socket
socket.role = 'host'
socket.emit('success joining', { roomId: currentRoom.id })
if (currentRoom.nextParticipant && currentRoom.nextParticipant.connected) {
currentRoom.nextParticipant.emit('connect to room', { roomId: currentRoom.id })
delete currentRoom.nextParticipant
socket.emit('set role', { role: 'host' })
} else socket.emit('set role', { role: 'host', showAlert: true })
if (currentRoom.chat) {
for (let message of currentRoom.chat) {
socket.emit('add message', message)
}
}
} else if (currentRoom.host.id !== socket.id && !currentRoom.participant && !currentRoom.gameStarted) {
socket.roomId = data.roomId
currentRoom.participant = socket
socket.role = 'participant'
socket.emit('success joining', { roomId: currentRoom.id })
socket.emit('set role', { role: 'participant' })
currentRoom.host.emit('participant connected')
if (currentRoom.host.ready) socket.emit('opponent ready')
if (currentRoom.chat) {
for (let message of currentRoom.chat) {
socket.emit('add message', message)
}
}
} else {
socket.emit('room is full')
}
} else {
socket.emit('exit from room')
}
})
socket.on('send message', async function (data) {
if (!data || !data.roomId || !data.role || !data.text) return
let currentRoom = await getRoomById(data.roomId)
if (currentRoom && new Date() - socket.lastMessageTime > 300) {
socket.lastMessageTime = new Date()
let newMessage = new Message(data.roomId, data.role, String(data.text).substr(0, 1000))
currentRoom.chat.push(newMessage)
if (currentRoom.chat.length > 20) currentRoom.chat.shift()
for (let socket of currentRoom.sockets) {
socket.emit('add message', newMessage)
}
console.log(`Отправка сообщения в комнату ${socket.roomId}`)
}
})
socket.on('toggle ready', async function (data) {
if (!data || !data.ships) return
if (new Date() - socket.lastReadyToggle > 3000) {
let currentRoom = await getRoomById(socket.roomId)
if (!currentRoom.gameStarted) {
if (!socket.ready) {
socket.ready = true
socket.ships = data.ships
socket.lastReadyToggle = new Date()
socket.emit('ready')
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
if (currentRoom.host === socket && currentRoom.participant) currentRoom.participant.emit('opponent ready')
else if (currentRoom.participant === socket) currentRoom.host.emit('opponent ready')
if (currentRoom.host.ready && currentRoom.participant && currentRoom.participant.ready) {
currentRoom.gameStarted = true
for (let socket of currentRoom.sockets) socket.emit('game started', { currentMove: currentRoom.currentMove })
}
}
} else {
socket.ready = false
socket.ships = []
socket.lastReadyToggle = new Date()
socket.emit('unready')
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
if (currentRoom.host === socket && currentRoom.participant) currentRoom.participant.emit('opponent unready')
else if (currentRoom.participant === socket) currentRoom.host.emit('opponent unready')
}
}
}
}
})
socket.on('shoot request', async function (data) {
if (!data || !data.x || !data.y) return
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
let targetSocket
if (data.x < 0 || data.x > 10 || data.y < 0 || data.y > 10) return
if (currentRoom.host === socket && currentRoom.currentMove === 'host') targetSocket = currentRoom.participant
else if (currentRoom.participant === socket && currentRoom.currentMove === 'participant') targetSocket = currentRoom.host
if (!targetSocket) return socket.emit('console log', 'Не Ваш ход')
for (let cell of socket.shootedCells) {
if (cell.x === data.x && cell.y === data.y) return socket.emit('console log', 'Вы уже стреляли сюда')
}
let shootedShip = (function () {
for (let ship of targetSocket.ships) {
for (let segment of ship.segments) {
if (segment.x === data.x && segment.y === data.y) return ship
}
}
})()
if (shootedShip) {
for (let segment of shootedShip.segments) {
if (segment.x === data.x && segment.y === data.y) {
segment.shooted = true
break
}
}
for (let offset of autoMissOffsets) {
if (data.x + offset.x < 1 || data.x + offset.x > 10 || data.y + offset.y < 1 || data.y + offset.y > 10) continue
if (socket.shootedCells.findIndex((cell) => cell.x === data.x + offset.x && cell.y === data.y + offset.y) !== -1) continue
socket.shootedCells.push({ x: data.x + offset.x, y: data.y + offset.y })
}
} else {
socket.shootedCells.push({ x: data.x, y: data.y })
if (currentRoom.host === socket && currentRoom.currentMove === 'host') currentRoom.currentMove = 'participant'
else if (currentRoom.participant === socket && currentRoom.currentMove === 'participant') currentRoom.currentMove = 'host'
}
let destroyedShip
if (shootedShip && shootedShip.segments.every((segment) => segment.shooted)) destroyedShip = shootedShip
if (destroyedShip) {
for (let segment of destroyedShip.segments) {
for (let offset of offsets) {
if (segment.x + offset.x < 1 || segment.x + offset.x > 10 || segment.y + offset.y < 1 || segment.y + offset.y > 10) continue
if (socket.shootedCells.findIndex((cell) => cell.x === segment.x + offset.x && cell.y === segment.y + offset.y) !== -1) continue
socket.shootedCells.push({ x: segment.x + offset.x, y: segment.y + offset.y })
}
}
}
for (let socket of currentRoom.sockets) {
socket.emit('shoot response', {
x: data.x,
y: data.y,
hitted: !!shootedShip,
shooter: this.role,
currentMove: currentRoom.currentMove,
destroyedShip: destroyedShip
})
}
if (targetSocket.ships.every((ship) => ship.segments.every((segment) => segment.shooted))) {
socket.winner = true
for (let socket of currentRoom.sockets) {
socket.emit('game over', { winner: this.role })
}
}
}
})
socket.on('get rooms', async function () {
socket.emit('console log', `Количество комнат: ${rooms.length}`)
console.log(rooms)
})
socket.on('get shooted cells', async function () {
socket.emit('console log', `Координаты: ${JSON.stringify(socket.shootedCells)}`)
console.log(socket.shootedCells)
})
socket.on('get room id request', async function () {
socket.emit('get room id response', { roomId: socket.roomId })
})
socket.on('wants to make rematch', async function () {
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
socket.wantsToRematch = true
if (currentRoom.host === socket && currentRoom.participant) {
if (currentRoom.participant && currentRoom.participant.wantsToRematch) {
currentRoom.rematch()
} else if (currentRoom.participant) {
currentRoom.participant.emit('opponent wants rematch')
}
} else if (currentRoom.participant && currentRoom.participant == socket) {
if (currentRoom.host.wantsToRematch) {
currentRoom.rematch()
} else {
currentRoom.host.emit('opponent wants rematch')
}
}
}
})
socket.on('stopped writing', async function () {
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
if (currentRoom.host === socket && currentRoom.participant) {
currentRoom.participant.emit('clear opponent writes timeout')
} else if (currentRoom.participant === socket) {
currentRoom.host.emit('clear opponent writes timeout')
}
}
})
socket.on('writing', async function () {
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
if (currentRoom.host === socket && currentRoom.participant) {
currentRoom.participant.emit('update opponent writes timeout')
} else if (currentRoom.participant === socket) {
currentRoom.host.emit('update opponent writes timeout')
}
}
})
socket.on('disconnect', async function () {
console.log(`Отключение ${socket.id}`)
console.log(`Отключились из ${socket.roomId}`)
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
if (currentRoom.gameStarted) {
for (let socket of currentRoom.sockets) {
socket.emit('opponent disconnect while game')
}
await deleteRoomById(currentRoom.id)
} else {
if (currentRoom.host === socket) {
if (currentRoom.participant && !currentRoom.host.wantsToRematch && !currentRoom.participant.wantsToRematch) currentRoom.participant.emit('host leave')
await deleteRoomById(currentRoom.id)
} else if (currentRoom.participant === socket) {
currentRoom.participant = null
currentRoom.host.emit('participant disconnected')
}
}
}
})
// Читы
socket.on('use cheats', async function () {
let currentRoom = await getRoomById(socket.roomId)
if (currentRoom) {
let targetSocket
if (currentRoom.host === socket && currentRoom.participant && currentRoom.participant.ships) {
targetSocket = currentRoom.participant
} else if (currentRoom.participant === socket && currentRoom.host.ships) {
targetSocket = currentRoom.host
}
if (targetSocket) {
let targetSegments = []
for (let ship of targetSocket.ships) {
for (let segment of ship.segments) {
targetSegments.push(segment)
}
}
socket.emit('use cheats', targetSegments)
}
}
})
})
function randInt(min, max) {
return ~~(Math.random() * (max - min + 1) + min)
}
class Message {
constructor(roomId, role, text, date = new Date()) {
this.roomId = roomId
this.role = role
this.text = text
this.date = date
}
}
class Room {
constructor(id) {
this.id = id || generateRoomId()
this.host = null
this.participant = null
this.currentMove = 'host'
this.gameStarted = false
this.chat = []
this.creationTime = new Date()
}
get sockets() {
let sockets = []
if (this.host) sockets.push(this.host)
if (this.participant) sockets.push(this.participant)
return sockets
}
async rematch() {
this.gameStarted = false
for (let socket of this.sockets) {
socket.emit('opponents want rematch')
}
let newRoomId = await createRoom()
this.host.emit('connect to room', { roomId: newRoomId })
let newRoom = await getRoomById(newRoomId)
newRoom.nextParticipant = this.participant
let currentRoom = await getRoomById(this.id)
if (currentRoom.chat) {
for (let message of currentRoom.chat) {
newRoom.chat.push(Object.assign({}, message, { roomId: newRoomId }))
}
}
if (!currentRoom.participant?.winner) {
newRoom.currentMove = 'participant'
}
}
}
async function checkForEmptyRooms() {
for (let room of rooms) {
if (!room.host && new Date() - room.creationTime > 15 * 1000) {
console.log(`Удаление комнаты: ${room.id}`)
await deleteRoomById(room.id)
return checkForEmptyRooms()
}
}
}
setInterval(checkForEmptyRooms, 10 * 1000)
async function getRoomById(id) {
for (let room of rooms) {
if (room.id === id) {
return room
}
}
}
async function deleteRoomById(roomId) {
for (let i = 0; i < rooms.length; i++) {
if (rooms[i].id === roomId) {
rooms.splice(i, 1)
i--
}
}
}
const ID_LENGTH = 5
const ID_CHARSET = 'abcdefghikmnopqrstuvwxyzABCDEFGHKLMNOPQRSTUVWXYZ1234567890'
async function generateRoomId(idLength = ID_LENGTH) {
let id = ''
for (let i = 0; i < idLength; i++) id += ID_CHARSET.charAt(randInt(0, ID_CHARSET.length - 1))
if (await getRoomById(id)) return generateRoomId(idLength + 1)
else return id
}
async function createRoom(idLength = ID_LENGTH) {
await checkForEmptyRooms()
let roomId = await generateRoomId(idLength)
rooms.push(new Room(roomId))
return roomId
}
const PORT = process.env.PORT || 3000
server.listen(PORT, function () {
console.log(`Сервер запущен на порту: ${PORT}`)
})