This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
274 lines (231 loc) · 7.74 KB
/
app.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
const room = require('./Room')
const err = require('./errors')
const https = require('https')
const morgan = require('morgan')
const express = require('express')
const app = express()
const http = require('http').Server(app)
const PORT = process.env.PORT || 8000
const URL = process.env.URL || ''
process.env.PWD = process.cwd()
app.use(express.static(process.env.PWD + '/web'));
app.use(morgan('combined'))
// socket.io
const io = require('socket.io')(http)
let room_list = []
let players_map = {}
count = () => {
return Object.keys(players_map).length
}
let disconnection_timeout
io.on('connection', (socket) => {
console.log('unknown connection with id: ' + socket.id)
// timer for any connected socket!
let time_counter = 0;
let timer = setInterval(() => {
socket.emit('timer', time_counter++)
}, 1000)
socket.on('set_timer', (num) => {
if (typeof num === 'number') {
time_counter = num
} else if (typeof num.num === 'number') {
time_counter = num.num
} else if (typeof num === 'string') {
time_counter = parseInt(num)
} else if (typeof num.num === 'string') {
time_counter = parseInt(num.num)
}
})
// main program
let id = socket.id
let current_room, current_room_name
socket.use((packet, next) => {
if (packet[0] === 'cancel') {
next()
} else if (packet[0] === 'init' || packet[0] === 'set_timer' || current_room) {
if (current_room && current_room.get_status() === 0) {
socket.emit('err', err['disable_game'])
return
}
next()
} else {
socket.emit('err', err['init'])
}
});
//functions
function lose(_) {
if (current_room && current_room.is_complete()) {
if (current_room.is_my_turn(id)) {
//TODO: save game information
end(0, `${id} eat toxic chocolate`)
} else {
socket.emit('err', err['turn'])
}
} else {
socket.emit('err', err['wait'])
}
}
function end(code, reason) {
current_room.set_status(-1)
if (current_room.is_complete()) {
let winner = current_room.get_another_player(id)
let loser = current_room.find_by_id(id)
io.to(current_room_name).emit('end', {
code,
reason,
winner: winner.id,
loser: loser.id
})
winner.socket.disconnect()
loser.socket.disconnect()
} else {
let loser = id
io.to(current_room_name).emit('end', {
code,
reason,
loser: loser.id
})
socket.disconnect()
}
current_room.destroy()
room_list = room_list.filter(r => r !== current_room)
console.log(`room ${current_room_name} removed`)
}
// init
socket.once('init', (data) => {
let reconnect_flag = false
id = data.id
if (id in players_map) {
if (players_map[id]) {
return socket.emit('err', err['id'])
} else {
room_list.forEach(room => {
let founded_player = room.find_by_id(id)
if (founded_player) {
current_room = room
room.set_status(1)
current_room_name = current_room.get_name()
socket.join(current_room_name)
founded_player.socket = socket
founded_player.id = id
players_map[id] = socket
if (!room.is_complete()) {
io.emit('wait')
} else {
room.send_in_game_data('in_game')
}
clearTimeout(disconnection_timeout)
reconnect_flag = true
}
});
if (!players_map[id]) {
return socket.emit('err', err['player_id'])
}
}
}
if(reconnect_flag){
return
}
if (!id) {
return socket.emit('err', err['empty_id'])
}
players_map[id] = socket
if(count() === 0){
return socket.emit('err', err['empty_rooms'])
}
if (count() % 2 == 0) {
current_room = room_list[room_list.length - 1]
current_room.set_second_player(id, socket)
current_room_name = current_room.get_name()
} else {
current_room = new room(`room${room_list.length}`, id, socket, data)
current_room_name = current_room.get_name()
room_list.push(current_room)
}
socket.join(current_room_name)
if (current_room.is_complete()) {
current_room.start()
} else {
io.to(current_room_name).emit('wait');
}
console.log(`player ${id} connected`)
})
// event handlers
socket.on('data', (data) => {
if(!data){
socket.emit('err', err['null_data'])
}
if (current_room.is_complete()) {
if (current_room.is_my_turn(id) && !current_room.is_lose(data.num)) {
current_room.change_turn_and_board(data)
current_room.send_data_from(id, data)
} else if (current_room.is_lose(data.num)) {
return lose(data)
} else {
socket.emit('err', err['turn'])
}
} else {
socket.emit('err', err['wait'])
}
})
socket.once('disconnect', () => {
if (current_room) {
//initialized socket
if (current_room.get_status() === -1) {
// room is ended by canceling or long time disconnection
delete players_map[id]
socket.leave(current_room_name)
console.log(`player ${id} removed`)
} else {
// a client disconnected
let life_time = 30000
socket.to(current_room_name).emit('pause', {
life_time
})
current_room.set_status(0)
// suspend
players_map[id] = null
disconnection_timeout = setTimeout(() => {
// check that client comes back or not!
if (!players_map[id]) {
if(current_room.is_complete()) {
let winner = current_room.get_another_player(id)
let loser = current_room.find_by_id(id)
io.to(current_room_name).emit('end', {
code: 2,
reason: `player ${loser.id} disconnected`,
winner: winner.id,
loser: loser.id
})
winner.socket.disconnect()
}
current_room.set_status(-1)
delete players_map[id]
socket.leave(current_room_name)
current_room.destroy()
room_list = room_list.filter(r => r !== current_room)
console.log(`room ${current_room_name} removed`)
}
}, life_time)
}
console.log(`player ${id} disconnected`)
} else {
console.log('unknown disconnection ' + socket.id)
}
clearInterval(timer)
})
socket.once('cancel', () => {
end(1, `${id} left the game`)
})
socket.once('lose', lose)
});
http.listen(PORT, function () {
console.log('listening on ' + PORT);
if (URL !== '') {
setInterval(() => {
https.get(URL).on('err', (e) => {
console.error(e)
})
}, 300000)
}
});