-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketioRouter.ts
260 lines (244 loc) · 8.31 KB
/
socketioRouter.ts
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
import app from './index';
import { createServer } from 'http';
import { Server } from 'socket.io';
import helperFunctions from './socketHelperFunctions';
import powerUps from './powerUps';
import IgameState from './interfaces/gameState.interface';
import Iuser from './interfaces/user.interface';
const gameState: IgameState = {};
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: '*',
methods: ['GET', 'POST', 'PUT'],
},
});
io.on('connection', async (socket) => {
const { roomId } = socket.handshake.query;
socket.join(`${roomId}`);
await helperFunctions
.joinUser(`${roomId}`, socket.id, gameState)
.then(() => {
const usersArray = helperFunctions.getPlayers(gameState, roomId);
io.to(`${roomId}`).emit('playerInfo', usersArray);
})
.catch((err) => {
console.error(err);
});
socket.on('userInfo', ({ userName, color, rounds }) => {
const curUser = gameState[`${roomId}`].users[socket.id];
const updatedUser = {
...curUser,
userName: userName,
color: color,
};
const isReady = helperFunctions.checkIfReady(updatedUser);
updatedUser.isReady = isReady;
gameState[`${roomId}`].users[socket.id] = updatedUser;
if (curUser.isHost) {
gameState[`${roomId}`].rounds = parseInt(rounds);
gameState[`${roomId}`].currRound = 1;
}
const usersArray = helperFunctions.getPlayers(gameState, roomId);
io.to(`${roomId}`).emit('playerInfo', usersArray);
io.to(`${socket.id}`).emit(
'getGameState',
gameState[`${roomId}`].rounds,
gameState[`${roomId}`].currRound,
);
});
socket.on('applyPower', ({ power, userName }) => {
//apply power to chosen user and update available power ups for current user
const curUser = gameState[`${roomId}`].users[socket.id];
const loser = Object.values(gameState[`${roomId}`].users).filter(
(user) => user.userName === userName,
)[0];
let updatedParagraph = '';
let newPowerups: any = [];
if (power === 'scramble') {
updatedParagraph = powerUps.scrambleWord(loser.userParagraph);
newPowerups = [
...loser.appliedPUs,
{ id: 'scramble', powerUp: 'ScrambleCard' },
];
} else if (power === 'longWord') {
updatedParagraph = powerUps.insertLongWord(loser.userParagraph);
newPowerups = [
...loser.appliedPUs,
{ id: 'longWord', powerUp: 'LongWordCard' },
];
} else if (power === 'symbols') {
updatedParagraph = powerUps.insertSymbols(loser.userParagraph);
newPowerups = [
...loser.appliedPUs,
{ id: 'symbols', powerUp: 'SymbolsCard' },
];
}
const updatedLoser = {
...loser,
userParagraph: updatedParagraph,
appliedPUs: newPowerups,
};
const updatedcurUser = {
...curUser,
availablePUs: [],
isReady: true,
};
gameState[`${roomId}`].users[loser.userId] = updatedLoser;
gameState[`${roomId}`].users[socket.id] = updatedcurUser;
const usersArray = helperFunctions.getPlayers(gameState, roomId);
usersArray.sort((a, b): number => {
return a.rank - b.rank;
});
io.to(`${roomId}`).emit('playerInfo', usersArray);
});
socket.on('syncStart', () => {
io.to(`${roomId}`).emit('startRace');
const timeNow = Date.now();
const raceStartTime = timeNow + 5000;
gameState[`${roomId}`].startTime = raceStartTime;
io.to(`${roomId}`).emit('startTime', `${raceStartTime}`);
});
socket.on('position', ({ currIndex }) => {
const currPositions = gameState[`${roomId}`].positions;
const color = gameState[`${roomId}`].users[socket.id].color;
const newPositions = {
...currPositions,
[socket.id]: { currIndex, color },
};
gameState[`${roomId}`].positions = newPositions;
});
setInterval(() => {
io.in(`${roomId}`).emit('positions', gameState[`${roomId}`].positions);
}, 500);
socket.on(
'finishRace',
async ({ endTime, startTime, allKeyPresses, length }) => {
const { finishTime, WPM, accuracy } = helperFunctions.calculateResults(
endTime,
startTime,
allKeyPresses,
length,
);
const currUser = gameState[`${roomId}`].users[socket.id];
//calculate new average wpm for a palyer
const { newWPMHistory, newAVG } = helperFunctions.calculateAverageWPM(
currUser,
WPM,
);
const updatedCurUSer = {
...currUser,
WPMHistory: newWPMHistory,
WPMAverage: newAVG,
gameData: { finishTime, WPM, accuracy },
};
gameState[`${roomId}`].users[socket.id] = updatedCurUSer;
const usersArray = helperFunctions.getPlayers(gameState, roomId);
io.to(`${roomId}`).emit('results', usersArray);
io.to(`${socket.id}`).emit(
'getGameState',
gameState[`${roomId}`].rounds,
gameState[`${roomId}`].currRound,
);
},
);
socket.on('nextRound', async () => {
const usersInRoom = gameState[`${roomId}`].users;
//sort players by average wpm
let usersArray = helperFunctions.getPlayers(gameState, roomId);
usersArray.sort((a, b): number => {
return b.WPMAverage - a.WPMAverage;
});
//get new parapraph and update current round
const newParagraph = await helperFunctions.getRandomParagraph();
const newGameState = {
...gameState[`${roomId}`],
paragraph: newParagraph,
currRound:
gameState[`${roomId}`].currRound + 1 < gameState[`${roomId}`].rounds
? gameState[`${roomId}`].currRound + 1
: gameState[`${roomId}`].rounds,
};
gameState[`${roomId}`] = newGameState;
//update ranks, assign power ups and clean gamedata
const powers = helperFunctions.givePowers(usersArray.length);
for (const user in usersInRoom) {
const newRank = usersArray.findIndex((el) => el.userId === user) + 1;
const updatedUser: Iuser = {
...usersInRoom[user],
userParagraph: gameState[`${roomId}`].paragraph!,
rank: newRank,
appliedPUs: [],
availablePUs: powers.find((el: { rank: number }) => el.rank === newRank)
? [powers.find((el: { rank: number }) => el.rank === newRank).power]
: [],
gameData: {
finishTime: '',
WPM: undefined,
accuracy: undefined,
},
};
const isReady = helperFunctions.checkIfReady(updatedUser);
updatedUser.isReady = isReady;
usersInRoom[user] = updatedUser;
}
//send everyone to lobby
//send players info
usersArray = helperFunctions.getPlayers(gameState, roomId);
//sort by rank
usersArray.sort((a, b): number => {
return a.rank - b.rank;
});
socket.to(`${roomId}`).emit('navigateToLobby');
io.to(`${roomId}`).emit('playerInfo', usersArray);
io.to(`${socket.id}`).emit(
'getGameState',
gameState[`${roomId}`].rounds,
gameState[`${roomId}`].currRound,
);
});
socket.on('playAgain', async () => {
const newParagraph = await helperFunctions.getRandomParagraph();
const newGameState = {
...gameState[`${roomId}`],
paragraph: newParagraph,
currRound: gameState[`${roomId}`].rounds && 1,
};
gameState[`${roomId}`] = newGameState;
const usersInRoom = gameState[`${roomId}`].users;
for (const user in usersInRoom) {
const newUserInfo: Iuser = {
...usersInRoom[user],
gameData: {
finishTime: '',
WPM: undefined,
accuracy: undefined,
},
appliedPUs: [],
availablePUs: [],
WPMHistory: [],
userParagraph: gameState[`${roomId}`].paragraph!,
};
usersInRoom[user] = newUserInfo;
}
const usersArray = helperFunctions.getPlayers(gameState, roomId);
io.to(`${roomId}`).emit('playerInfo', usersArray);
socket.to(`${roomId}`).emit('navigateToLobby');
io.to(`${socket.id}`).emit(
'getGameState',
gameState[`${roomId}`].rounds,
gameState[`${roomId}`].currRound,
);
});
socket.on('sendToFinal', () => {
io.to(`${roomId}`).emit('navigateToFinal');
});
socket.on('disconnect', () => {
delete gameState[`${roomId}`].users[socket.id];
socket.leave(`${roomId}`);
io.to(`${roomId}`).emit('playerDisconnect');
const usersArray = helperFunctions.getPlayers(gameState, roomId);
io.to(`${roomId}`).emit('playerInfo', usersArray);
});
});
export default server;