-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.ts
152 lines (124 loc) · 4.46 KB
/
ws.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
import type { TeamMatch } from '$lib/types';
import { Server } from 'socket.io';
import { type ViteDevServer } from 'vite';
const info = (s: string) => console.log(`\x1b[32m ${s} \x1b[0m`);
const sid_to_username: Map<string, string> = new Map();
let robot_queue: [string, 'red' | 'blue'][] = [];
let curr_match_key: string = '';
const webSocketServer = {
name: 'webSocketServer',
configureServer(server: ViteDevServer) {
if (!server.httpServer) return;
const io = new Server(server.httpServer);
io.use((socket, next) => {
const username = socket.handshake.auth.username;
if (!username) {
return next(new Error('invalid username'));
}
// erroring in prod
let old_entries = Object.entries(sid_to_username).find(
([_key, value]) => value === username
);
if (old_entries) {
old_entries
.map(([key, _value]) => key)
.forEach((key) => sid_to_username.delete(key));
}
sid_to_username.set(socket.id, username);
next();
});
io.on('connect', (socket) => {
if (socket.handshake.auth.token === 'celary') {
info('Admin Aquired');
socket.join('admin_room');
}
socket.on('join_queue', () => {
const username = sid_to_username.get(socket.id);
const team_data = robot_queue.pop();
if (!team_data) {
io.to('admin_room').emit('scout_joined_queue', username);
socket.join('scout_queue');
return;
}
io.to('admin_room').emit('robot_left_queue', team_data);
socket.emit('time_to_scout', [curr_match_key, ...team_data]);
});
socket.on('leave_scout_queue', (scout_id: string) => {
const scout_sid = Object.entries(sid_to_username)
.filter(([_sid, scout]) => scout === scout_id)
.map(([sid, _]) => sid)[0];
// This event exist in the cast that the scout removed itself from the queue
io.emit('scout_left_queue', scout_id);
// This event exists in the case that the admin removed the scout from the queue
// io.to(scout_sid).emit('you_left_queue');
io.sockets.sockets.get(scout_sid)?.leave('scout_queue');
});
socket.on('leave_robot_queue', (robot: [string, 'red' | 'blue']) => {
const robotsEqual = (
robot1: [string, 'red' | 'blue'],
robot2: [string, 'red' | 'blue']
): boolean => {
return robot1[0] === robot2[0] && robot1[1] === robot2[1];
};
const index = robot_queue.findIndex((robot_t) => robotsEqual(robot_t, robot));
if (index === -1) return;
robot_queue.splice(index, 1);
});
socket.on(
'send_match',
async ([match_key, teams]: [string, [string, 'red' | 'blue'][]]) => {
if (!socket.rooms.has('admin_room')) return;
// TODO: New TeamMatch in DB request here
info(`${match_key}: ${teams}`);
robot_queue = [];
const scout_queue = (await io.in('scout_queue').fetchSockets()).reverse();
for (const socket of scout_queue) {
const team_data = teams.pop();
if (!team_data) break;
const username = sid_to_username.get(socket.id);
if (!username) {
console.error('Scout in queue not in map');
continue;
}
socket.leave('scout_queue');
socket.emit('time_to_scout', [match_key, ...team_data]);
io.to('admin_room').emit('scout_left_queue', username);
}
io.to('admin_room').emit('robot_joined_queue', teams);
robot_queue.push(...teams);
// Update all connected sockets with new match info (for cosmetic purposes)
io.emit('new_match', match_key);
curr_match_key = match_key;
}
);
// Event-listener sockets that were offline to sync back up with the current match key
socket.on('get_curr_match', (callback) => {
callback({
curr_match_key
});
});
socket.on('get_robot_queue', (callback) => {
callback({
robots: robot_queue ?? []
});
});
socket.on('get_scout_queue', async (callback) => {
callback({
scouts: ((await io.in('scout_queue').fetchSockets()) ?? []).reverse()
});
});
// For these two, the team match has already been sent or removed by the client sending a request to the server
socket.on('submit_team_match', (team_match: TeamMatch) => {
io.to('admin_room').emit('new_team_match', team_match);
});
socket.on('failed_submit_team_match', (team_match: TeamMatch) => {
io.to('admin_room').emit('failed_team_match', team_match);
});
socket.on('disconnect', (_reason) => {
const scout_id = sid_to_username.get(socket.id);
io.to('admin_room').emit('scout_left_queue', scout_id);
});
});
}
};
export default webSocketServer;