From 2b15b8a2c6dba0cd34b0672c235a37acc9d37aee Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Fri, 29 Nov 2024 13:18:34 -0800 Subject: [PATCH 1/9] 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]); } } From a65714f254582b4a9a740479ec1628dc7a90a8d0 Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Fri, 29 Nov 2024 19:05:27 -0800 Subject: [PATCH 2/9] feat: scout queue on admin dash w/ removing --- src/routes/admin/+page.svelte | 36 +++++++++++++++++------ src/routes/queue/+page.svelte | 14 ++++++--- tailwind.config.js => tailwind.config.cjs | 0 ws.ts | 14 ++++++--- 4 files changed, 47 insertions(+), 17 deletions(-) rename tailwind.config.js => tailwind.config.cjs (100%) diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index 2cb6b0a..e343cb5 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -1,16 +1,25 @@
@@ -50,11 +66,13 @@
-
- Queued Scouts -
+
+

Queued Scouts

+
{#each scout_queue as scout} -

{scout}

+ {/each}
diff --git a/src/routes/queue/+page.svelte b/src/routes/queue/+page.svelte index 70bf9ec..965f6f7 100644 --- a/src/routes/queue/+page.svelte +++ b/src/routes/queue/+page.svelte @@ -1,19 +1,25 @@ diff --git a/tailwind.config.js b/tailwind.config.cjs similarity index 100% rename from tailwind.config.js rename to tailwind.config.cjs diff --git a/ws.ts b/ws.ts index e583ba4..dd9c404 100644 --- a/ws.ts +++ b/ws.ts @@ -26,10 +26,16 @@ const webSocketServer = { socket.emit('time_to_scout', [curr_match_key, ...team_data]); }); - socket.on('leave_queue', () => { - const scout_id = sid_to_scout.get(socket.id); - - io.in('admin_room').emit('scout_left_queue', scout_id); + socket.on('leave_queue', (scout_id: string) => { + const scout_sid = sid_to_scout + .entries() + .filter(([_sid, scout]) => scout === scout_id) + .map(([sid, _]) => sid) + .toArray()[0]; + // 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 + io.to(scout_sid).emit('you_left_queue'); socket.leave('scout_queue'); }); From aea7ceee26336e9e6ed025a2eba36ca2148854bd Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Fri, 29 Nov 2024 19:46:49 -0800 Subject: [PATCH 3/9] wip: robot_queue in admin-dash --- src/routes/admin/+page.svelte | 33 ++++++++++++++++++++++++++++++++- src/routes/queue/+page.svelte | 3 +-- ws.ts | 28 ++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index e343cb5..55e07e8 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -2,6 +2,7 @@ import { io, Socket } from 'socket.io-client'; let scout_queue: string[] = $state([]); + let robot_queue: string[] = $state([]); let socket: Socket = io({ auth: { @@ -9,6 +10,17 @@ } }); + socket.on('robot_joined_queue', (robot: string[]) => { + robot_queue.concat(robot); + }); + + socket.on('robot_left_queue', (robot: string) => { + const index = robot_queue.indexOf(robot); + if (index === -1) return; + + robot_queue.splice(index, 1); + }); + socket.on('scout_joined_queue', (scout: string) => { scout_queue.push(scout); }); @@ -39,7 +51,16 @@ scout_queue.splice(index, 1); - socket.emit('leave_queue', scout_id); + socket.emit('leave_scout_queue', scout_id); + }; + + const remove_robot = (robot: string) => { + const index = robot_queue.indexOf(robot); + if (index === -1) return; + + robot_queue.splice(index, 1); + + socket.emit('leave_robot_queue', robot); }; @@ -76,4 +97,14 @@ {/each}
+
+

Queued Robots

+
+ {#each robot_queue as robot} + + {/each} +
+
diff --git a/src/routes/queue/+page.svelte b/src/routes/queue/+page.svelte index 965f6f7..5267d2d 100644 --- a/src/routes/queue/+page.svelte +++ b/src/routes/queue/+page.svelte @@ -1,7 +1,6 @@ diff --git a/ws.ts b/ws.ts index dd9c404..772b128 100644 --- a/ws.ts +++ b/ws.ts @@ -1,8 +1,9 @@ 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 = new Map(); -const robotQueue: [string, 'red' | 'blue'][] = []; +const robot_queue: [string, 'red' | 'blue'][] = []; let curr_match_key: string = ''; const webSocketServer = { @@ -12,21 +13,25 @@ const webSocketServer = { const io = new Server(server.httpServer); io.on('connect', (socket) => { - if (socket.handshake.auth.token === 'celary') socket.join('admin_room'); + 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); - const team_data = robotQueue.pop(); + const team_data = robot_queue.pop(); if (!team_data) { io.to('admin_room').emit('scout_joined_queue', scout_id); 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_queue', (scout_id: string) => { + socket.on('leave_scout_queue', (scout_id: string) => { const scout_sid = sid_to_scout .entries() .filter(([_sid, scout]) => scout === scout_id) @@ -36,7 +41,14 @@ const webSocketServer = { 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 io.to(scout_sid).emit('you_left_queue'); - socket.leave('scout_queue'); + io.sockets.sockets.get(scout_sid)?.leave('scout_queue'); + }); + + socket.on('leave_robot_queue', (robot: string) => { + const index = robot_queue.findIndex(([id, _color]) => id === robot); + if (index === -1) return; + + robot_queue.splice(index, 1); }); socket.on('send_match', ([match_key, teams]: [string, [string, 'red' | 'blue'][]]) => { @@ -58,7 +70,11 @@ const webSocketServer = { } } - robotQueue.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); From cf00b305f10231a429ec13afc7322519a4f1b954 Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Sat, 30 Nov 2024 22:21:31 -0800 Subject: [PATCH 4/9] wip: persisten session_id --- src/routes/queue/+page.svelte | 12 +++++- ws.ts | 70 ++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/routes/queue/+page.svelte b/src/routes/queue/+page.svelte index 5267d2d..bde4f72 100644 --- a/src/routes/queue/+page.svelte +++ b/src/routes/queue/+page.svelte @@ -1,12 +1,20 @@
@@ -109,4 +124,17 @@ {/each}
+
+

Team-Match Submissions

+
+ {#each submitted_team_matches as team_match} + + {/each} +
+
diff --git a/src/routes/scout/[team_data]/+page.svelte b/src/routes/scout/[team_data]/+page.svelte index 404c38d..a1c5420 100644 --- a/src/routes/scout/[team_data]/+page.svelte +++ b/src/routes/scout/[team_data]/+page.svelte @@ -6,10 +6,11 @@ import type { PageData } from './$types'; import { ArrowRight, ArrowLeft } from 'lucide-svelte'; import { browser } from '$app/environment'; + import { io, Socket } from 'socket.io-client'; const { data }: { data: PageData } = $props(); - const scout_id = browser ? (localStorage?.getItem('scout_id') ?? '') : ''; + const username = browser ? (localStorage?.getItem('username') ?? '') : ''; let actions: AutoActionData[] = $state([]); let held: AutoHeldItems = $state({ @@ -36,12 +37,18 @@ gamePhase = gamePhase === 'Post' ? 'Tele' : gamePhase === 'Tele' ? 'Auto' : 'Auto'; // Last case should never happen } + const socket = io({ + auth: { + username + } + }); + function submit() { const auto_actions = actions.slice(0, furthest_auto_index + 1); const tele_actions = actions.slice(furthest_auto_index + 1) as TeleActionData[]; // TODO: Add verification function to ensure that this always works const match: TeamMatch = { id: 0, - scout_id, + scout_id: username, team_key: data.team_key, match_key: data.match_key, skill, @@ -51,6 +58,8 @@ auto_actions, tele_actions }; + + socket.emit('submit_team_match', match); } diff --git a/ws.ts b/ws.ts index 215b881..fa040ac 100644 --- a/ws.ts +++ b/ws.ts @@ -1,3 +1,4 @@ +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`); @@ -111,6 +112,20 @@ const webSocketServer = { socket.on('request_curr_match', () => { socket.emit('new_match', curr_match_key); }); + + socket.on('remove_team_match', (team_match: TeamMatch) => { + if (!socket.rooms.has('admin_room')) return; + + info( + `TeamMatch: ${team_match.match_key}$_{team_match.team_key} was removed by the admin` + ); + // TODO: Emit message from the db to remove team_match + }); + + socket.on('submit_team_match', (team_match: TeamMatch) => { + console.log('log match'); + io.to('admin_room').emit('new_team_match', team_match); + }); }); } }; From 760bc48ced978f71518727c9d13b79af7029878b Mon Sep 17 00:00:00 2001 From: Azalea Colburn Date: Fri, 6 Dec 2024 21:25:43 -0800 Subject: [PATCH 9/9] fix: submissions appear on the admin dashboard --- src/routes/admin/+page.svelte | 4 +++- src/routes/scout/[team_data]/+page.svelte | 1 + ws.ts | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index 2019861..12c3d97 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -132,7 +132,9 @@ onclick={() => remove_submission(team_match)} class="rounded border p-2 text-center" > - {team_match} +
+ {team_match.match_key}:{team_match.team_key} +
{/each}
diff --git a/src/routes/scout/[team_data]/+page.svelte b/src/routes/scout/[team_data]/+page.svelte index d8600cb..36533b4 100644 --- a/src/routes/scout/[team_data]/+page.svelte +++ b/src/routes/scout/[team_data]/+page.svelte @@ -7,6 +7,7 @@ import { ArrowRight, ArrowLeft } from 'lucide-svelte'; import { browser } from '$app/environment'; import Postmatch from './Postmatch.svelte'; + import { io } from 'socket.io-client'; const { data }: { data: PageData } = $props(); diff --git a/ws.ts b/ws.ts index fa040ac..6cdff8c 100644 --- a/ws.ts +++ b/ws.ts @@ -117,7 +117,7 @@ const webSocketServer = { if (!socket.rooms.has('admin_room')) return; info( - `TeamMatch: ${team_match.match_key}$_{team_match.team_key} was removed by the admin` + `TeamMatch: ${team_match.match_key}_${team_match.team_key} was removed by the admin` ); // TODO: Emit message from the db to remove team_match });