Skip to content

Commit

Permalink
wip: robot_queue in admin-dash
Browse files Browse the repository at this point in the history
  • Loading branch information
azaleacolburn committed Nov 30, 2024
1 parent a65714f commit aea7cee
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
33 changes: 32 additions & 1 deletion src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@
import { io, Socket } from 'socket.io-client';
let scout_queue: string[] = $state([]);
let robot_queue: string[] = $state([]);
let socket: Socket = io({
auth: {
token: 'celary'
}
});
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);
});
Expand Down Expand Up @@ -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);
};
</script>

Expand Down Expand Up @@ -76,4 +97,14 @@
{/each}
</div>
</div>
<div class="mt-4 grid grid-cols-1 grid-rows-10 place-items-center gap-4 text-xl">
<h1 class="row-span-2">Queued Robots</h1>
<div class="row-span-8 grid grid-cols-1 grid-rows-8 gap-2">
{#each robot_queue as robot}
<button onclick={() => remove_robot(robot)} class="rounded border p-2 text-center">
{robot}
</button>
{/each}
</div>
</div>
</div>
3 changes: 1 addition & 2 deletions src/routes/queue/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { io, Socket } from 'socket.io-client';
import { onMount } from 'svelte';
let socket: Socket;
socket = io();
Expand All @@ -19,7 +18,7 @@
});
const leave = () => {
socket.emit('leave_queue', 'test_scout');
socket.emit('leave_scout_queue', 'test_scout');
goto('/');
};
</script>
Expand Down
28 changes: 22 additions & 6 deletions ws.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = new Map();
const robotQueue: [string, 'red' | 'blue'][] = [];
const robot_queue: [string, 'red' | 'blue'][] = [];
let curr_match_key: string = '';

const webSocketServer = {
Expand All @@ -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)
Expand All @@ -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'][]]) => {
Expand All @@ -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);
Expand Down

0 comments on commit aea7cee

Please sign in to comment.