Skip to content

Commit

Permalink
Database Hotifx (#26)
Browse files Browse the repository at this point in the history
* feat: created DB file

* feat: type model for scouting system

* chore: update svelte

* feat: tried

* feat: nothing

* feat: add none

* feat: added DB.sql and migrations

* refactor: type state model

* feat: state transforms

* add successfail screen

* theme fixes

* fix: package.json

* fix: formating

* fix: dependencies

* format thingy

* feat: implemented submit api

* fix: removed unnecessary line

* refactor: update types to match new schema

* fix: id after clarifying with Brandon

* update dep

* style: format files

* feat: pulled from main

* style: format files

* fix: dependencies

* style: format files

* fix: dependencies

* fix: skill property name

* fix: removed npm config

* fix: bun dependencies

* feat: finished submit function

* feat: read route

* feat: superscouting and pit scouting in db

* fix: DB code

* fix: updated queries to allow ids

* fix: tailwind to CommonJS module

* style: format files

* fix: queries

* style: format files

* fix: types

* style: format files

* feat: hooked up frontend to backend

* fix: queries

* feat: functional queries

* style: format files

* fix: queries returning too much

* more merge

* feat: insert to users and matches

* style: format files

* fix: database enums

* fix: missing comma

---------

Co-authored-by: Brandon Harad <[email protected]>
Co-authored-by: awwpotato <[email protected]>
Co-authored-by: awwpotato <[email protected]>
Co-authored-by: Brandon-Harad <[email protected]>
Co-authored-by: azaleacolburn <[email protected]>
  • Loading branch information
6 people authored Dec 12, 2024
1 parent dd088e6 commit 4b4936a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
41 changes: 22 additions & 19 deletions migrations/DB.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions src/lib/server-assets/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -218,10 +221,38 @@ export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
}
}

export async function insertUser(name: string): Promise<boolean> {
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<boolean> {
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;
}
}
11 changes: 10 additions & 1 deletion src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ['', '', '', '', '', ''];
};
Expand Down
8 changes: 8 additions & 0 deletions src/routes/api/newmatch/+server.ts
Original file line number Diff line number Diff line change
@@ -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));
};
4 changes: 2 additions & 2 deletions src/routes/api/submit/+server.ts
Original file line number Diff line number Diff line change
@@ -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)));
};

0 comments on commit 4b4936a

Please sign in to comment.