-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-handlers.js
125 lines (107 loc) · 4.21 KB
/
game-handlers.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
const { GAME_START_TIME, GAME_LIFE_TIME, GAME_STATUSES } = require("./consts");
const { getWinner } = require("./helpers");
const disconnectHandler = function(socket){
for (let room in this.rooms) {
if (this.rooms[room].users.indexOf(socket.id) > -1) {
this.rooms[room].users.splice(this.rooms[room].users.indexOf(socket.id), 1);
console.log('disconnect', socket.id);
this.sendToRoom(room,'user-disconneted', {
user: socket.id,
users: this.rooms[room].users
});
}
}
}
/**
* receive the player's touch
* @param {*} socket
* @param {*} data
* @returns
*/
const touchHandler = function(socket, data){
const {touchId} = data
// find user in rooms
const room = Object.values(this.rooms).find(r=> r.users.indexOf(socket.id) > -1)
if(!room) return;
// user alredy submit
if(this.rooms[room.id].gameResult[socket.id]) return
this.rooms[room.id].gameResult[socket.id] = touchId;
}
/**
* the user join the room
* @param {*} socket
* @param {*} data
* @returns
*/
const joinRoomHandler = function(socket, data){
const {roomId} = data
// get or init room
this.rooms[roomId] = this.rooms[roomId] || {
id: roomId,
users: [],
status: GAME_STATUSES.WAITING,
gameResult: {},
finalResult: null,
startIn: GAME_START_TIME / 1000,
}
// check room status
if(this.rooms[roomId].status == GAME_STATUSES.STARTED) return;
// add new user to the room
if(this.rooms[roomId].users.length < 2) {
console.log('joinroom', roomId, socket.id);
this.rooms[roomId].users.push(socket.id)
socket.join(roomId)
process.nextTick(() => {
this.sendToRoom(roomId,'joined', {
roomId: roomId,
users: this.rooms[roomId].users,
})
})
}
// we can start the game now
if(this.rooms[roomId].users.length === 2) {
process.nextTick(() => {
this.rooms[roomId].status = GAME_STATUSES.PRESTARTED;
//start a count down
const startInInterval = setInterval(() => {
this.sendToRoom(roomId,'game-status', {
roomId: roomId,
users: this.rooms[roomId].users,
startIn: this.rooms[roomId].startIn,
status: GAME_STATUSES.PRESTARTED
})
this.rooms[roomId].startIn--;
if(this.rooms[roomId].startIn < 0) {
clearInterval(startInInterval);
this.rooms[roomId].status = GAME_STATUSES.STARTED;
this.sendToRoom(roomId,'game-status', {
status: GAME_STATUSES.STARTED,
gameLifeTime: GAME_LIFE_TIME,
})
}
} , 1000)
// finish the game after GAME_START_TIME + GAME_LIFE_TIME since this
// is the time we need to get the winner
setTimeout(()=>{
if(!this.rooms[roomId]) return
this.rooms[roomId].status = GAME_STATUSES.FINISHED;
this.rooms[roomId].finalResult = getWinner(this.rooms[roomId].gameResult);
this.sendToRoom(roomId,'game-status', {
status: GAME_STATUSES.FINISHED,
gameResult: this.rooms[roomId].gameResult,
finalResult: this.rooms[roomId].finalResult
});
// delete the game's room
delete this.rooms[roomId]
}, GAME_START_TIME + GAME_LIFE_TIME)
})
}
if(this.rooms[roomId].status != GAME_STATUSES.WAITING){
console.log('already ', this.rooms[roomId].status);
}
}
module.exports ={
disconnectHandler,
touchHandler,
joinRoomHandler
}