-
Notifications
You must be signed in to change notification settings - Fork 5
/
waitingSocket.js
59 lines (55 loc) · 2.36 KB
/
waitingSocket.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
const {User} = require('./user')
const {Game,addPlayerToGame,drawCardToPlayer,checkWinCondition,distributeInitialCard,playCard,passTurn,restartGame,addBot} = require('./model')
const jwt = require('jsonwebtoken')
const _ = require('lodash')
module.exports = (io)=>{
const waitingRoom = io.of('/waitingRoom')
const waiting = []
const users = []
const updatePlayers = async ()=>{
console.log(waiting)
if(waiting.length<=1) return
const game = new Game({gameMode:'Competitive Player'})
const firstCard = game.deck.splice(_.random(game.deck.length-1),1)[0]
game.currentTopCard = firstCard
await game.save()
let i =0
while(i<4){
if(!waiting[0]){
i=5
break
}
i++
console.log(waiting,users)
const socketid = waiting.splice(0,1)[0]
const user = users.splice(0,1)[0]
const userid = await addPlayerToGame(game,user.username,user._id,user.points)
const token = jwt.sign({id:game._id,userid},process.env.SECRET_KEY)
waitingRoom.to(socketid).emit('Game Found',{token,playerid:userid})
}
await updatePlayers()
}
waitingRoom.on('connect',async (socket)=>{
const criticalError = (message)=>{
console.log(message)
socket.emit("Critical Error",message)
return socket.disconnect()
}
socket.on('disconnect',()=>{
console.log('disconnect')
waiting.splice(waiting.indexOf(socket.id),1)
})
const token = socket.handshake.query.userToken
if(!token || token ==='null') return criticalError("Token not found")
const {id} = jwt.verify(token,process.env.SECRET_KEY)
if(!id) return criticalError("Token is invalid")
const user = await User.findById(id)
if(!user) return criticalError("User not found")
const index = users.findIndex((e)=>e._id===id)
if(index>=0) waiting.splice(index,1)
if(index>=0)users.splice(index,1)
waiting.push(socket.id)
users.push(user)
await updatePlayers()
})
}