-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
205 lines (177 loc) · 5.79 KB
/
index.js
File metadata and controls
205 lines (177 loc) · 5.79 KB
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
var port = process.env.PORT || 4000;
var express = require("express");
var socket = require("socket.io");
//App setup
var app = express()
var server = app.listen(port, function(){
console.log("Listening to port " + port);
});
//Static files
app.use(express.static("public"));
//track current games
var allGames = [];
//Socket setup
var io = socket(server);
//Track current connections
var currentHosts = [];
var stillConnectedHosts = [];
//Check what connections are still there, delete games if host is disconnected
//use setInterval(function, 3000) and setTimeout(function, 3000)
setInterval(function(){
stillConnectedHosts = [];
io.sockets.emit("checkIfOnline");
setTimeout(function(){
console.log("connected hosts: " + currentHosts + "\nreally: " + stillConnectedHosts);
currentHosts.forEach(host => {
if(stillConnectedHosts.indexOf(host) == -1){
console.log("Game closed with hostId: " + host);
allGames.forEach(game => {
if(game.hostId == host){
io.sockets.emit("closeGame", {
gameId: game.gameId
});
allGames.splice(allGames.indexOf(game), 1);
}
});
}
});
console.log(allGames);
currentHosts = stillConnectedHosts;
}, 200);
}, 20000);
io.on("connection", function(socket){
//console.log("made connection to " + socket.id);
socket.on("stillConnected", function(data){
stillConnectedHosts.push(data.hostId);
});
//Receive data and create a new game
socket.on("newGame", function(data){
currentHosts.push(data.hostId);
allGames.push({
gameId: data.gameId,
hostId: data.hostId,
clients: []
});
});
//Join request for game
socket.on("joinRequest", function(data){
console.log(data.gameId);
//Check if game exists with given gameId
allGames.forEach(game => {
if(game.gameId == data.gameId){
var newPlayer = true;
game.clients.forEach(client => {
if(client.name == data.clientName){
newPlayer = false;
}
});
if(newPlayer){
//Game exists and name is not already given, add client to game
game.clients.push({
clientId: data.clientId,
name: data.clientName
});
//Check if Host
console.log("Players of game " + game.gameId + ": " + game.clients.length + " id+ " + data.clientId);
if(game.clients.length == 1){
console.log("new Host: " + data.clientId);
io.sockets.emit("makeHost",{
clientId: data.clientId
});
}
//Respond to client, in this case the client can join
io.sockets.emit("response", {
canJoin: true,
clientId: data.clientId,
gameId: data.gameId,
});
//Update player list
io.sockets.emit("updatePlayers", {
newClients: game.clients,
gameId: data.gameId
});
}else{
//Respond to client, in this case the client can not join because the name is already given
io.sockets.emit("response", {
canJoin: false,
clientId: data.clientId,
gameId: data.gameId
});
}
}
});
//socket.disconnect();
});
socket.on("requestStart", function(data){
io.sockets.emit("beginGame",{
gameId: data.gameId
});
});
//Start a new game
socket.on("startGame", function(data){
var allClients;
allGames.forEach(game => {
if(game.gameId == data.gameId){
allClients = game.clients;
}
});
io.sockets.emit("continueToGame", {
gameId: data.gameId,
clients: allClients
});
});
socket.on("requestPlayers", function(data){
var allClients;
allGames.forEach(game => {
if(game.gameId == data.gameId){
allClients = game.clients;
}
});
socket.emit("sendPlayers", {
clients: allClients
});
});
socket.on("sendAnswer", function(data){
io.sockets.emit("receiveAnswer", {
gameId: data.gameId,
clientId: data.clientId,
answer: data.answer
});
});
socket.on("drink", function(data){
io.sockets.emit("drink", {
clientId: data.clientId,
type: data.type
});
});
socket.on("sendOptions", function(data){
io.sockets.emit("receiveOptions", {
options: data.options,
gameId: data.gameId
});
});
socket.on("newRound", function(data){
io.sockets.emit("newRound", {
gameId: data.gameId,
clientId: data.clientId
})
});
socket.on("isOnline", function(data){
allClients.push(data.clientId);
});
//Skip question
socket.on("skip", function(data){
io.sockets.emit("skip", {
gameId: data.gameId
});
});
//Send / Emit data to every client !!!!! Different from socket.emit
});
var game = {
gameId: 0,
hostId: 0,
clients: {
clientId: 0,
name: ""
}
};