From 2b15b8a2c6dba0cd34b0672c235a37acc9d37aee Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Fri, 29 Nov 2024 13:18:34 -0800 Subject: [PATCH] wip: start scout queue display --- src/routes/admin/+page.svelte | 50 +++++++++++++++++++++++------------ ws.ts | 14 ++++++++-- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index e9489a8..2cb6b0a 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -7,6 +7,10 @@ } }); + socket.on('scout_queued', (scout: string) => { + scout_queue.push(scout); + }); + let match_key: string = $state(''); const colors = ['red', 'red', 'red', 'blue', 'blue', 'blue'] as const; let teams: string[] = $state(['', '', '', '', '', '']); @@ -14,6 +18,8 @@ teams.map((team, i) => [team, colors[i]] as [string, 'red' | 'blue']) ); + let scout_queue: string[] = $state([]); + const queue_match = () => { socket.emit('send_match', [match_key, team_color]); match_key = ''; @@ -21,25 +27,35 @@ }; -
- - -
-
-

Red

-

Blue

+
+
+ + +
+
+

Red

+

Blue

+
+
+ {#each teams as _, i} + + {/each} +
-
- {#each teams as _, i} - + + +
+
+ Queued Scouts +
+ {#each scout_queue as scout} +

{scout}

{/each}
- -
diff --git a/ws.ts b/ws.ts index 4e330fd..e583ba4 100644 --- a/ws.ts +++ b/ws.ts @@ -1,6 +1,7 @@ import { Server } from 'socket.io'; import { type ViteDevServer } from 'vite'; +const sid_to_scout: Map = new Map(); const robotQueue: [string, 'red' | 'blue'][] = []; let curr_match_key: string = ''; @@ -13,16 +14,22 @@ const webSocketServer = { io.on('connect', (socket) => { if (socket.handshake.auth.token === 'celary') socket.join('admin_room'); - socket.on('join_queue', (_) => { + socket.on('join_queue', (scout_id) => { + sid_to_scout.set(socket.id, scout_id); + const team_data = robotQueue.pop(); if (!team_data) { + io.to('admin_room').emit('scout_joined_queue', scout_id); socket.join('scout_queue'); return; } socket.emit('time_to_scout', [curr_match_key, ...team_data]); }); - socket.on('leave_queue', (_) => { + socket.on('leave_queue', () => { + const scout_id = sid_to_scout.get(socket.id); + + io.in('admin_room').emit('scout_left_queue', scout_id); socket.leave('scout_queue'); }); @@ -37,7 +44,10 @@ const webSocketServer = { const team_data = teams.pop(); if (!team_data) break; + const scout_id = sid_to_scout.get(sid); + 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]); } }