diff --git a/migrations/DB.sql b/migrations/DB.sql index ebf367c..dc83d0a 100644 --- a/migrations/DB.sql +++ b/migrations/DB.sql @@ -1,26 +1,30 @@ CREATE TYPE tele_action_type AS ENUM ( - 'IntakeTote', - 'IntakeBalloon', - 'TakeBalloonFromCoral', - - 'ScoreYourHeldTote', - 'ScoreOtherHeldTote', - 'ScoreExternalTote', - 'ScoreLowBalloon' + 'IntakeTote', + 'IntakeBalloon', + 'ScoreBalloonInternalTote', + 'ScoreBalloonExternalTote', + 'ScoreBalloonUncontrolledTote', + 'ScoreBalloonLow', + 'EjectBalloon', + 'EjectBunny', + 'EjectTote' ); CREATE TYPE auto_action_type as ENUM ( - 'IntakeTote', - 'IntakeBalloon', - 'TakeBalloonFromCoral', - - 'ScoreYourHeldTote', - 'ScoreOtherHeldTote', - 'ScoreExternalTote', - 'ScoreLowBalloon', + 'IntakeTote', + 'IntakeBalloon', + 'ScoreBalloonInternalTote', + 'ScoreBalloonExternalTote', + 'ScoreBalloonUncontrolledTote', + 'ScoreBalloonLow', + 'EjectBalloon', + 'EjectBunny', + 'EjectTote', 'IntakeBunny', - 'ScoreBunnyTote', - 'ScoreBunnyLowZone' + 'ScoreBunnyInternalTote', + 'ScoreBunnyExternalTote', + 'ScoreBunnyUncontrolledTote', + 'ScoreBunnyLow' ); CREATE TYPE tele_action_data AS ( act tele_action_type, @@ -39,7 +43,6 @@ CREATE TYPE drivetrain_enum as ENUM ( CREATE TABLE "Users"( "id" SERIAL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, - "is_admin" BOOLEAN NOT NULL ); CREATE TABLE "Teams"( "team_key" VARCHAR(255) NOT NULL, diff --git a/src/lib/server-assets/database.ts b/src/lib/server-assets/database.ts index e3cb8c0..6f5e587 100644 --- a/src/lib/server-assets/database.ts +++ b/src/lib/server-assets/database.ts @@ -18,6 +18,9 @@ import type { TeleActionsTM } from '$lib/types'; +// Const b/c of obvious reasons +const event_key = 'orbb2024'; + // Whether or not the database is currently being used const use_db: boolean = USE_DB === 'true'; @@ -218,10 +221,38 @@ export async function insertTeamMatch(match: TeamMatch): Promise { } } +export async function insertUser(name: string): Promise { + if (!use_db) return true; + + try { + await db.query('INSERT INTO "Users" VALUES (DEFAULT, $1)', [name]); + + return true; + } catch (error) { + console.error(error); + return false; + } +} + export async function select(matchkey: string, teamkey: string) { + if (!use_db) return null; + let response = await db.query( 'SELECT * FROM "TeamMatches" WHERE "match_key" = $1 AND "team_key" = $2', [matchkey, teamkey] ); return response.rows; } + +export async function insertMatch(match_key: string): Promise { + if (!use_db) return true; + + try { + await db.query('INSERT INTO "Matches" VALUES ($1, $2)', [match_key, event_key]); + + return true; + } catch (error) { + console.error(error); + return false; + } +} diff --git a/src/routes/admin/+page.svelte b/src/routes/admin/+page.svelte index 0e02949..988466d 100644 --- a/src/routes/admin/+page.svelte +++ b/src/routes/admin/+page.svelte @@ -60,9 +60,18 @@ teams.map((team, i) => [team, colors[i]] as [string, 'red' | 'blue']) ); - const queue_match = () => { + const queue_match = async () => { teams = teams.filter((team) => team != ''); socket.emit('send_match', [match_key, team_color]); + + await fetch('/api/newmatch', { + method: 'POST', + body: JSON.stringify(match_key), + headers: { + 'Content-Type': 'application/json' + } + }); + match_key = ''; teams = ['', '', '', '', '', '']; }; diff --git a/src/routes/api/newmatch/+server.ts b/src/routes/api/newmatch/+server.ts new file mode 100644 index 0000000..ae0000c --- /dev/null +++ b/src/routes/api/newmatch/+server.ts @@ -0,0 +1,8 @@ +import { json } from '@sveltejs/kit'; +import type { RequestHandler } from './$types'; +import { insertMatch } from '$lib/server-assets/database'; + +export const POST: RequestHandler = async ({ request }) => { + const match_key: string = await request.json(); + return json(await insertMatch(match_key)); +}; diff --git a/src/routes/api/submit/+server.ts b/src/routes/api/submit/+server.ts index c59ab37..0cc0cf3 100644 --- a/src/routes/api/submit/+server.ts +++ b/src/routes/api/submit/+server.ts @@ -1,9 +1,9 @@ import type { TeamMatch } from '$lib/types'; import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; -import { insertTeamMatch } from '$lib/server-assets/database'; +import { insertTeamMatch, insertUser } from '$lib/server-assets/database'; export const POST: RequestHandler = async ({ request }) => { const match: TeamMatch = await request.json(); - return json(await insertTeamMatch(match)); + return json((await insertTeamMatch(match)) && (await insertUser(match.scout_id))); };