Skip to content

Commit

Permalink
wip: persisten session_id
Browse files Browse the repository at this point in the history
  • Loading branch information
azaleacolburn committed Dec 1, 2024
1 parent aea7cee commit cf00b30
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
12 changes: 10 additions & 2 deletions src/routes/queue/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<script lang="ts">
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { io, Socket } from 'socket.io-client';
const session_id = browser && (localStorage.getItem('session_id') ?? '');
const username = 'test_scout';
let socket: Socket;
socket = io();
socket = io({
auth: {
session_id,
username
}
});
socket.on('connect', () => {
socket.emit('join_queue', 'test_scout');
socket.emit('join_queue');
});
socket.on('time_to_scout', ([match_key, team_key, color]: [string, string, 'red' | 'blue']) => {
Expand Down
70 changes: 45 additions & 25 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Server } from 'socket.io';
import { type ViteDevServer } from 'vite';
const info = (s: string) => console.log(`\x1b[32m ${s} \x1b[0m`);

const sid_to_scout: Map<string, string> = new Map();
const sid_to_username: Map<string, string> = new Map();
const robot_queue: [string, 'red' | 'blue'][] = [];
let curr_match_key: string = '';

Expand All @@ -12,18 +12,34 @@ const webSocketServer = {
if (!server.httpServer) return;
const io = new Server(server.httpServer);

io.use((socket, next) => {
const session_id = socket.handshake.auth.session_id;

const username = socket.handshake.auth.username;
if (!username) {
return next(new Error('invalid username'));
}
if (session_id) sid_to_username.set(session_id, username);
// create new session
next();
});

io.on('connect', (socket) => {
if (socket.handshake.auth.token === 'celary') {
info('Admin Aquired');
socket.join('admin_room');
}

socket.on('join_queue', (scout_id) => {
sid_to_scout.set(socket.id, scout_id);
socket.emit('session', {
session_id: socket.id
});

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', scout_id);
io.to('admin_room').emit('scout_joined_queue', username);
socket.join('scout_queue');
return;
}
Expand All @@ -32,11 +48,12 @@ const webSocketServer = {
});

socket.on('leave_scout_queue', (scout_id: string) => {
const scout_sid = sid_to_scout
const scout_sid = sid_to_username
.entries()
.filter(([_sid, scout]) => scout === scout_id)
.map(([sid, _]) => sid)
.toArray()[0];
console.log(scout_sid);
// This event exist in the cast that the scout removed itself from the queue
io.to('admin_room').emit('scout_left_queue', scout_id);
// This event exists in the case that the admin removed the scout from the queue
Expand All @@ -51,35 +68,38 @@ const webSocketServer = {
robot_queue.splice(index, 1);
});

socket.on('send_match', ([match_key, teams]: [string, [string, 'red' | 'blue'][]]) => {
if (!socket.rooms.has('admin_room')) return;
socket.on(
'send_match',
async ([match_key, teams]: [string, [string, 'red' | 'blue'][]]) => {
if (!socket.rooms.has('admin_room')) return;

const scout_queue: Set<string> | undefined = io
.of('/')
.adapter.rooms.get('scout_queue');
if (scout_queue) {
for (const sid of scout_queue.values()) {
const scout_queue = await io.in('scout_queue').fetchSockets();
for (const socket of scout_queue) {
const team_data = teams.pop();
if (!team_data) break;

const scout_id = sid_to_scout.get(sid);
const username = sid_to_username.get(socket.id);
if (!username) {
console.error('Scout in queue not in map');
continue;
}

socket.leave('scout_queue');
io.to('admin_room').emit('scout_left_queue', scout_id);
io.to(sid).emit('time_to_scout', [match_key, ...team_data]);
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.map(([team, _color]) => team)
);
robot_queue.push(...teams);
io.to('admin_room').emit(
'robot_joined_queue',
teams.map(([team, _color]) => team)
);
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;
});
// 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('request_curr_match', () => {
Expand Down

0 comments on commit cf00b30

Please sign in to comment.