-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
265 lines (205 loc) · 6.5 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
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
const fs = require('fs');
const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const request = require('request')
const app = express();
const port = process.env.PORT || 5500;
app.use(express.static('public'));
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "http://localhost:5500",
methods: ["GET", "POST"]
}
});
app.get('/', (req, res) => res.sendFile(__dirname + '/index.html'));
server.listen(port, () => {
console.log('Listen on port 3000');
});
//Data structure for players
let rooms = {};
let players = {};
let data = getData();
io.on('connection',connected);
//Hello World line taken from the express website
//The 'connection' is a reserved event name in socket.io
//For whenever a connection is established between the server and a client
function connected(socket){
socket.on('disconnect', function(){
if(players[socket.id] != undefined){
delete rooms[players[socket.id].lobby].players[socket.id];
rooms[players[socket.id].lobby].playerCount -= 1;
}
delete players[socket.id];
console.log("Goodbye client with id "+ socket.id);
console.log("Current number of players: "+ Object.keys(players).length);
for(let room in rooms){
for(let player in rooms[room].players) {
io.to(player).emit('updatePlayers', currentLobby.players);
}
}
for(let room in rooms){
if(rooms[room].playerCount == 0) {
delete rooms[room];
}
}
console.log(rooms);
});
socket.on('CreateRoom', () => {
newRoomID = makeRoomId();
rooms[newRoomID] = new Room(newRoomID);
console.log("Room created: " + newRoomID);
io.to(socket.id).emit('createdRoomSuccess', newRoomID);
})
socket.on('JoinRoomWithCode', data => {
for(let roomID in rooms){
if(roomID == data.roomID){
currentLobby = rooms[roomID]
if(currentLobby.playerCount >= 8 || currentLobby.inGame){
return;
}
currentLobby.players[socket.id] = new Player(data.name, roomID, 0);
currentLobby.playerCount += 1;
players[socket.id] = currentLobby.players[socket.id];
console.log("New client connected to Lobby, with id: " + socket.id);
console.log("Current number of players in Lobby: "+ Object.keys(currentLobby.players).length);
console.log("players dictionary: ", currentLobby.players);
socket.join(data);
io.to(socket.id).emit('joinedRoomSuccess', newRoomID);
for(let player in currentLobby.players) {
io.to(player).emit('updatePlayers', currentLobby.players);
}
}
}
});
socket.on('startGame', () => {
console.log("Game Started in Room: " + players[socket.id].lobby);
for(let player in rooms[players[socket.id].lobby].players) {
io.to(player).emit('gameStartedSuccess', "LIGMA");
}
for(let player in rooms[players[socket.id].lobby].players) {
io.to(player).emit('updatePlayers', currentLobby.players);
}
startGame(players[socket.id].lobby);
});
socket.on('addScore', (data) => {
console.log(data)
rooms[players[socket.id].lobby].players[socket.id].score += parseInt(data.replace('s Left!',''));
for(let player in rooms[players[socket.id].lobby].players) {
io.to(player).emit('updatePlayers', rooms[players[socket.id].lobby].players);
}
});
}
function startGame(id){
var counter = 20;
var rounds = 0;
rooms[id].inGame = true;
var WinnerCountdown = setInterval(function(){
if(rooms[id] == undefined){
return;
}
for(let player in rooms[id].players) {
io.to(player).emit('counter', counter);
}
if (counter === 20) {
Qdata = getNextQuestionData();
for(let player in rooms[id].players) {
io.to(player).compress(false).emit('nextRound', Qdata);
}
} else if(counter === 0) {
counter = 21;
rounds += 1;
}
counter--;
if (rounds === 10) {
clearInterval(WinnerCountdown);
for(let player in rooms[id].players) {
io.to(player).emit('EndGame', "LIGMA");
}
timeOut = setTimeout(function() {
for(let player in rooms[id].players) {
rooms[id].players[player].score = 0;
}
for(let player in rooms[id].players) {
io.to(player).emit('goToLobby', "LIGMA");
}
for(let player in rooms[id].players) {
io.to(player).emit('updatePlayers', rooms[id].players);
}
rooms[id].inGame = false;
},10000);
}
},
1000);
}
function getNextQuestionData(){
let returnVal = {};
songInd = Math.floor(Math.random() * data.length);
song = data[songInd]
returnVal['songURI'] = song['uri'].substring(14)
questionType = Math.floor(Math.random() * 3);
switch(questionType){
case 0:
returnVal['question'] = "What is the name of this song?";
returnVal['answer'] = song['name'];
for(let i = 1; i < 4; i++){
newInt = Math.floor(Math.random() * data.length);
returnVal['alt' + i] = data[newInt]['name'];
}
break;
case 1:
returnVal['question'] = "Who is the artist?";
returnVal['answer'] = song['artist_name'];
for(let i = 1; i < 4; i++){
newInt = Math.floor(Math.random() * data.length);
returnVal['alt' + i] = data[newInt]['artist_name'];
}
break;
case 2:
returnVal['question'] = "What album is this song from?";
returnVal['answer'] = song['album'];
for(let i = 1; i < 4; i++){
newInt = Math.floor(Math.random() * data.length);
returnVal['alt' + i] = data[newInt]['album'];
}
break;
}
return returnVal;
}
/// Helper Functions
function makeRoomId() {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var charactersLength = characters.length;
do {
for ( var i = 0; i < 5; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
}
while(rooms[result] != undefined);
return result;
}
//Room Class
class Room{
constructor(id){
this.id = id;
this.players = {};
this.playerCount = 0;
this.inGame = false;
}
}
//Player Class
class Player{
constructor(name, lobby,score){
this.name = name;
this.lobby = lobby;
this.score = score;
}
}
//Obtain API Keys
function getData(){
let rawdata = fs.readFileSync('songData.json');
let apiKeys = JSON.parse(rawdata);
return apiKeys;
}