-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
177 lines (145 loc) · 4.49 KB
/
server.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
const express = require('express')
const http = require('http')
// our localhost port
const port = 4001
const app = express()
// our server instance
const server = http.createServer(app)
const io = require('socket.io')(server, {
cors: {
origin: '*',
}
});
global.colour = 'white';
global.users = {"host":{},"players":{}}
global.wordList = []
global.timer = null;
global.time = 0
io.on('connection', socket => {
console.log('New client connected', socket.id)
///
socket.on('load game page', (name, id, type) => {
console.log(name, id)
if(id === null) {
return
}
if(type === null) {
if(id in global.users["host"]) {
type = 'host'
}
else if (id in global.users['players']) {
type = 'player'
}
else {
io.to(socket.id).emit('remove cookie');
return
}
}
if(type === "host") {
global.users["host"][id] = ({"name": name, "socket": socket.id});
}
else if (type === "player") {
global.users["players"][id] = ({"name": name, "socket": socket.id, "score": 0, "guesses": new Array(global.wordList.length).fill(false)});
}
console.log("Current players: " + JSON.stringify(global.users, null, 4));
io.to(socket.id).emit('toggle screen', 'Game');
io.to(socket.id).emit('load gamescreen', type, name);
io.emit('update players', global.users.players);
})
///
socket.on('remove player', (id) => {
try {
console.log('removed player', id);
console.log('socket', global.users.players[id].socket)
io.to(global.users.players[id].socket).emit('toggle screen', 'Home');
io.to(global.users.players[id].socket).emit('remove cookie');
delete global.users.players[id];
} catch{
console.log('Did not find player', id);
}
io.emit('update players', global.users.players);
console.log("Current players: " + JSON.stringify(global.users, null, 4));
})
///
socket.on('get players', () => {
io.to(socket.id).emit('update players', global.users.players);
})
///
socket.on('clear wordList', () => {
global.wordList = []
for (var playerId in global.users.players) {
global.users.players[playerId]["guesses"] = [];
}
io.emit('update players', global.users.players);
io.emit('update wordList', global.wordList);
})
///
socket.on('add word', (word) => {
newWord = {"word": word, "visible": false}
global.wordList.push(newWord);
for (var playerId in global.users.players) {
global.users.players[playerId]["guesses"].push(false);
}
io.emit('update players', global.users.players);
io.emit('update wordList', global.wordList);
})
///
socket.on('toggle player guess for word', (playerId, index) => {
global.users.players[playerId]["guesses"][index] = !global.users.players[playerId]["guesses"][index]
io.emit('update players', global.users.players);
io.emit('update wordList', global.wordList);
})
///
socket.on('toggle word visibility', (index) => {
global.wordList[index]["visible"] = !global.wordList[index]["visible"]
io.emit('update wordList', global.wordList);
})
///
socket.on('remove word', (index) => {
global.wordList.splice(index,1)
for (var playerId in global.users.players) {
global.users.players[playerId]["guesses"].splice(index, 1);
}
io.emit('update players', global.users.players);
io.emit('update wordList', global.wordList);
})
///
socket.on('fetch wordlist', () => {
io.emit('update wordList', global.wordList);
})
///
socket.on('add score', (playerId) => {
global.users.players[playerId]["score"] += 1;
io.emit('update players', global.users.players);
})
///
socket.on('subtract score', (playerId) => {
global.users.players[playerId]["score"] -= 1;
io.emit('update players', global.users.players);
})
///
socket.on('disconnect', () => {
console.log('user disconnected')
})
///
socket.on('start timer', () => {
console.log("Start timer")
try {
clearInterval(global.timer)
console.log("Clear timer")
}
catch{}
global.time = 45
global.timer = setInterval(() => {
console.log("Update timer")
global.time = global.time - 1;
io.emit('update timer', global.time);
if(global.time <= 0) {
global.time = 45
io.emit('update timer', global.time);
clearInterval(global.timer)
}
}, 1000);
});
})
server.listen(port, () => console.log(`Listening on port ${port}`))