This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
273 lines (248 loc) · 7.82 KB
/
index.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
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const cors = require('cors');
const port = process.env.PORT || 6969;
const rooms = {};
app.use(cors());
http.listen(port, () => {
console.log(`Draftster API running on ${port}`);
});
io.on('connection', (socket) => {
// The user disconnected
socket.on('disconnect', () => {
if (socket.id.includes('-')) {
const data = socket.id.split('-');
const name = data[0];
const roomId = data[1];
// If the room exists and this player belongs to it
if (
rooms.hasOwnProperty(roomId) &&
rooms[roomId].players.includes(name)
) {
// Add player to disconnected players array
rooms[roomId].disconnectedPlayers.push(name);
const updatedDisconnectedPlayers = rooms[roomId].disconnectedPlayers;
// Remove user from room
// const updatedPlayers = rooms[roomId].players.filter(
// (value) => value != name
// );
// Remove user stock picks
//delete rooms[roomId].playerPicks[name];
//const updatedPlayerPicks = rooms[roomId].playerPicks;
// If all the players left, delete the room
//if (updatedPlayers.length === 0) {
//delete rooms[roomId];
//} else {
//rooms[roomId].players = updatedPlayers;
// send new players array too
//Notify users of disconnect
socket.to(roomId).emit('userDisconnected', {
name: name,
disconnectedPlayers: updatedDisconnectedPlayers,
//updatedPlayers: updatedPlayers,
//updatedPlayerPicks: updatedPlayerPicks,
});
//}
}
}
});
// The user creates a room
socket.on('createRoom', (data, fn) => {
const roomId = data.roomId;
const hostName = data.name;
// Check if room id is valid
if (roomId !== '') {
// Check if room has been created
if (rooms[roomId] === undefined) {
if (hostName !== '' && !hostName.includes('-')) {
// Create and join the room
socket.join(roomId);
socket.id = hostName + '-' + roomId;
rooms[roomId] = {
id: roomId,
host: hostName,
players: [hostName],
disconnectedPlayers: [],
messages: [],
currentTurn: 0,
currentRound: 1,
inProgress: false,
availablePicks: ['MSFT', 'AAPL', 'TSLA', 'NFLX'],
playerPicks: {},
};
rooms[roomId].playerPicks[hostName] = [];
// Success
return fn({
room: rooms[roomId],
});
} else {
// The room exists but the player name is invalid
return fn({
error: `That name is not valid. Make sure it's not empty and doesn't include dashes.`,
});
}
} else {
// The room already exists
return fn({
error: `That room id already exists, please choose another.`,
});
}
} else {
// The room name is invalid
return fn({
error: `That room id is not valid. Make sure it's not empty and doesn't include dashes.`,
});
}
});
// The user joins a room
socket.on('joinRoom', (data, fn) => {
const roomId = data.roomId;
const playerName = data.name;
// Check if room has been created
if (rooms.hasOwnProperty(roomId)) {
// Check if room's draft is already in progress
if (rooms[roomId].disconnectedPlayers.includes(playerName)) {
// Remove player from disconnected players
const updatedPlayers = rooms[roomId].disconnectedPlayers.filter(
(value) => value != playerName
);
rooms[roomId].disconnectedPlayers = updatedPlayers;
// Create and join the room
socket.join(roomId);
socket.id = playerName + '-' + roomId;
const players = rooms[roomId].players;
const playerPicks = rooms[roomId].playerPicks;
const disconnectedPlayers = rooms[roomId].disconnectedPlayers;
// Notify players a new user has joined
io.to(roomId).emit('userConnected', {
name: playerName,
players: players,
playerPicks: playerPicks,
disconnectedPlayers: disconnectedPlayers,
});
// Both fields are good!
// Send message history as well
return fn({
room: rooms[roomId],
});
} else if (!rooms[roomId].inProgress) {
// Check if player name is available
if (!rooms[roomId].players.includes(playerName)) {
if (playerName !== '' && !playerName.includes('-')) {
// Create and join the room
socket.join(roomId);
socket.id = playerName + '-' + roomId;
// Add player to room and send new data to players
rooms[roomId].players.push(playerName);
rooms[roomId].playerPicks[playerName] = [];
const players = rooms[roomId].players;
const playerPicks = rooms[roomId].playerPicks;
const disconnectedPlayers = rooms[roomId].disconnectedPlayers;
// Notify players a new user has joined
io.to(roomId).emit('userConnected', {
name: playerName,
players: players,
playerPicks: playerPicks,
disconnectedPlayers: disconnectedPlayers,
});
// Both fields are good!
// Send message history as well
return fn({
room: rooms[roomId],
});
} else {
// The room exists but the name is in use
return fn({
error: `That name is not valid. Make sure it's not empty and doesn't include dashes.`,
});
}
} else {
// The room exists but the name is in use
return fn({
error: `That name is already in use in room: ${roomId}`,
});
}
} else {
// The room is in progress so you can't join
return fn({
error: `That room is currently locked and in progress.`,
});
}
} else {
// The room does not exist
return fn({ error: `That room does not exist.` });
}
});
// Message from client
socket.on('message', (data) => {
const message = data.message;
const roomId = data.roomId;
// Save message to room messages history
rooms[roomId].messages.push(message);
// Send to all clients
socket.to(roomId).emit('message', message);
});
// Lock the room to start the draft
// and set to in progress
socket.on('lockRoom', (fn) => {
const data = socket.id.split('-');
const name = data[0];
const roomId = data[1];
if (rooms[roomId].host === name) {
rooms[roomId].inProgress = true;
socket.to(roomId).emit('lockRoom');
} else {
return fn({ error: 'Only the host can start the draft.' });
}
});
socket.on('selectPick', (pick, fn) => {
const data = socket.id.split('-');
const name = data[0];
const roomId = data[1];
// Only process if draft is in progress
if (rooms[roomId].inProgress) {
const currentTurn = rooms[roomId].currentTurn;
// Only pick if it's the players current turn
if (name === rooms[roomId].players[currentTurn]) {
// Add the pick to the player's team
rooms[roomId].playerPicks[name].push(pick);
// Remove the pick from available picks
rooms[roomId].availablePicks = rooms[roomId].availablePicks.filter(
(value) => value != pick
);
const updatedAvailablePicks = rooms[roomId].availablePicks;
const updatedPlayerPicks = rooms[roomId].playerPicks;
// Increment turn
let updatedTurnNum = rooms[roomId].currentTurn + 1;
let updatedRoundNum = rooms[roomId].currentRound;
// Increment round if all players took their turn
if (updatedTurnNum > rooms[roomId].players.length - 1) {
updatedTurnNum = 0;
updatedRoundNum += 1;
}
rooms[roomId].currentTurn = updatedTurnNum;
rooms[roomId].currentRound = updatedRoundNum;
const response = {
updatedAvailablePicks: updatedAvailablePicks,
updatedPlayerPicks: updatedPlayerPicks,
updatedTurnNum: updatedTurnNum,
updatedRoundNum: updatedRoundNum,
};
socket.to(roomId).emit('selectPick', response);
} else {
console.log(currentTurn);
console.log(rooms[roomId].players[currentTurn]);
// It's not the players turn
return fn({
error: `It's not the your turn!`,
});
}
} else {
// The draft is not in progress
return fn({
error: `The draft is not in progress.`,
});
}
});
});