Skip to content

Commit

Permalink
feat: functional queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-Harad committed Dec 10, 2024
1 parent 5e9cead commit c72336b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 44 deletions.
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DB_HOST = 0
DB_USER = 0
DB_PASSWORD = 0
POSTGRES_DATABASE = 0
USE_DB = false
DB_PORT = 0
DB_HOST = "0"
DB_USER = "0"
DB_PASSWORD = "0"
POSTGRES_DATABASE = "0"
USE_DB = "false"
DB_PORT = "0"
24 changes: 14 additions & 10 deletions migrations/DB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CREATE TYPE auto_action_type as ENUM (
'ScoreYourHeldTote',
'ScoreOtherHeldTote',
'ScoreExternalTote',
'ScoreLowBalloon'
'ScoreLowBalloon',

'IntakeBunny',
'ScoreBunnyTote',
Expand Down Expand Up @@ -55,12 +55,12 @@ ALTER TABLE
"Matches" ADD PRIMARY KEY("match_key");
CREATE TABLE "TeamMatches"(
"id" SERIAL PRIMARY KEY,
"scout_id" SMALLINT NOT NULL,
"scout_id" VARCHAR(255) NOT NULL,
"match_key" VARCHAR(255) NOT NULL,
"team_key" VARCHAR(255) NOT NULL,
"skill_field_awareness" SMALLINT NOT NULL,
"skill_quickness" SMALLINT NOT NULL,
"notes" TEXT NOT NULL,
"notes" VARCHAR(255) NOT NULL,
"broke" BOOLEAN NOT NULL,
"died" BOOLEAN NOT NULL,
"tele_action_id" SMALLINT NOT NULL,
Expand All @@ -86,7 +86,7 @@ CREATE TABLE "TeleActions"(
"score_uncontrolled_failure" SMALLINT NOT NULL,
"bunny_eject_success" SMALLINT NOT NULL,
"bunny_eject_failure" SMALLINT NOT NULL,
"actions" tele_action_data[] NOT NULL
"actions" tele_action_data[]
);
CREATE TABLE "AutoActions"(
"id" SERIAL PRIMARY KEY,
Expand All @@ -110,15 +110,19 @@ CREATE TABLE "AutoActions"(
"bunny_intake_failure" SMALLINT NOT NULL,
"bunny_eject_success" SMALLINT NOT NULL,
"bunny_eject_failure" SMALLINT NOT NULL,
"bunny_tote_success" SMALLINT NOT NULL,
"bunny_tote_failure" SMALLINT NOT NULL,
"bunny_internal_success" SMALLINT NOT NULL,
"bunny_internal_failure" SMALLINT NOT NULL,
"bunny_external_success" SMALLINT NOT NULL,
"bunny_external_failure" SMALLINT NOT NULL,
"bunny_uncontrolled_success" SMALLINT NOT NULL,
"bunny_uncontrolled_failure" SMALLINT NOT NULL,
"bunny_low_success" SMALLINT NOT NULL,
"bunny_low_failure" SMALLINT NOT NULL,
"actions" auto_action_data[] NOT NULL
"actions" auto_action_data[]
);
CREATE TABLE "AllianceMatches"(
"id" SERIAL PRIMARY KEY,
"scout_id" SMALLINT NOT NULL,
"scout_id" VARCHAR(255) NOT NULL,
"match_key" VARCHAR(255) NOT NULL,
"alliance_key" VARCHAR(255) NOT NULL,
"one_strengths" VARCHAR(255) NOT NULL,
Expand All @@ -132,7 +136,7 @@ CREATE TABLE "AllianceMatches"(
);
CREATE TABLE "TeamEvents"(
"id" SERIAL PRIMARY KEY,
"scout_id" SMALLINT NOT NULL,
"scout_id" VARCHAR(255) NOT NULL,
"team_key" VARCHAR(255) NOT NULL,
"width" SMALLINT NOT NULL,
"length" SMALLINT NOT NULL,
Expand All @@ -145,7 +149,7 @@ CREATE TABLE "TeamEvents"(
"number_bunny_autos" SMALLINT NOT NULL,
"polish" SMALLINT NOT NULL,
"robot_notes" VARCHAR(255) NOT NULL,
"minibot_notes" SMALLINT NOT NULL
"minibot_notes" VARCHAR(255) NOT NULL
);
-- ALTER TABLE
-- "TeamEvents" ADD CONSTRAINT "teamevents_scout_id_foreign" FOREIGN KEY("scout_id") REFERENCES "Users"("id");
Expand Down
44 changes: 26 additions & 18 deletions src/lib/server-assets/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import type {
TeamMatch,
TeleAction,
TeleActionData,
TeleActionsTM
TeleActionsTM,
} from '$lib/types';

// Whether or not the database is currently being used
const use_db: boolean = USE_DB === 'true';

console.log(Number.parseInt(DB_PORT))

const db = new Client({
user: DB_USER,
password: DB_PASSWORD,
host: DB_HOST,
port: DB_PORT,
port: Number.parseInt(DB_PORT),
database: POSTGRES_DATABASE
});
await db.connect();
Expand Down Expand Up @@ -105,6 +107,15 @@ function matchToAutoActionsTM(match: TeamMatch): AutoActionsTM {
};
}

// Black magic string sorcery to turn a string of actions into an sql-suitable form
function convertTele(arr : TeleActionData[]) : string {
return "ARRAY[" + arr.map((a) => 'ROW(\'' + a.action + '\', ' + a.success + ')').join(', ') + "]::tele_action_data[]"
}

function convertAuto(arr : AutoActionData[]) : string {
return "ARRAY[" + arr.map((a) => 'ROW(\'' + a.action + '\', ' + a.success + ')').join(', ') + "]::auto_action_data[]"
}

export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
if (!use_db) return true;

Expand All @@ -114,18 +125,17 @@ export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
scout_id,
match_key,
team_key,
skill_field_awareness,
skill_quickness,
awareness,
quickness,
notes,
broke,
died
} = match;

try {
const tele_query = await db.query(
'INSERT INTO "TeleActions" VALUES (DEFAULT, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)',
'INSERT INTO "TeleActions" VALUES (DEFAULT, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, ' + convertTele(teledata.actions) + ") RETURNING *",
[
teledata.id,
teledata.tote_intake_success,
teledata.tote_intake_failure,
teledata.tote_eject_success,
Expand All @@ -144,15 +154,13 @@ export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
teledata.score_uncontrolled_failure,
teledata.bunny_eject_success,
teledata.bunny_eject_failure,
teledata.actions
]
);
const tele_id = tele_query.rows[0];

const tele_id = tele_query.rows[0].id;
const auto_query = await db.query(
'INSERT INTO "AutoActions" VALUES (DEFAULT, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30)',
'INSERT INTO "AutoActions" VALUES (DEFAULT, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, ' + convertAuto(autodata.actions) + ') RETURNING *',
[
autodata.id,
autodata.tote_intake_success,
autodata.tote_intake_failure,
autodata.tote_eject_success,
Expand All @@ -169,10 +177,10 @@ export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
autodata.score_external_failure,
autodata.score_uncontrolled_success,
autodata.score_uncontrolled_failure,
autodata.bunny_eject_success,
autodata.bunny_eject_failure,
autodata.bunny_intake_success,
autodata.bunny_intake_failure,
autodata.bunny_eject_success,
autodata.bunny_eject_failure,
autodata.bunny_internal_success,
autodata.bunny_internal_failure,
autodata.bunny_external_success,
Expand All @@ -181,19 +189,18 @@ export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
autodata.bunny_uncontrolled_failure,
autodata.bunny_low_success,
autodata.bunny_low_failure,
autodata.actions
]
);
const auto_id = auto_query.rows[0];
const auto_id = auto_query.rows[0].id;

await db.query(
'INSERT INTO "TeamMatches" VALUES (DEFAULT, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)',
[
scout_id,
match_key,
team_key,
skill_field_awareness,
skill_quickness,
awareness,
quickness,
notes,
broke,
died,
Expand All @@ -210,8 +217,9 @@ export async function insertTeamMatch(match: TeamMatch): Promise<boolean> {
}

export async function select(matchkey: string, teamkey: string) {
return await db.query('SELECT * FROM "TeamMatches" WHERE match_key = $1 AND team_key = $2', [
let response = await db.query('SELECT * FROM "TeamMatches" WHERE "match_key" = $1 AND "team_key" = $2', [
matchkey,
teamkey
]);
return response.rows;
}
6 changes: 4 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export type TeamMatch = {
scout_id: string;
match_key: string;
team_key: string;
skill_field_awareness: number;
skill_quickness: number;
quickness: number;
awareness: number;
notes: string;
broke: boolean;
died: boolean;
Expand All @@ -69,11 +69,13 @@ export type TeamMatch = {
export type AutoActionData = {
action: AutoAction;
success: boolean;
ok: boolean
};

export type TeleActionData = {
action: TeleAction;
success: boolean;
ok: boolean;
};

// Action Types
Expand Down
8 changes: 4 additions & 4 deletions src/routes/scout/[team_data]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// The furthest index in actions that was made during auto
let furthest_auto_index = $state(0);
let speed = $state(3);
let quickness = $state(3);
let awareness = $state(3);
let broke = $state(false);
let died = $state(false);
Expand All @@ -46,8 +46,8 @@
scout_id,
team_key: data.team_key,
match_key: data.match_key,
skill_quickness: speed,
skill_field_awareness: awareness,
quickness,
awareness,
broke,
died,
notes,
Expand Down Expand Up @@ -116,7 +116,7 @@
bind:displaying={timelineExtended}
/>
{:else}
<Postmatch bind:awareness bind:speed bind:broke bind:died bind:notes />
<Postmatch bind:awareness bind:quickness bind:broke bind:died bind:notes />

<button onclick={submit} class="mt-auto w-full rounded bg-gunmetal p-2 font-bold"
>Submit</button
Expand Down
8 changes: 4 additions & 4 deletions src/routes/scout/[team_data]/Postmatch.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
let {
speed = $bindable(),
quickness = $bindable(),
awareness = $bindable(),
broke = $bindable(),
died = $bindable(),
notes = $bindable()
}: {
speed: number;
quickness: number;
awareness: number;
broke: boolean;
died: boolean;
Expand All @@ -23,8 +23,8 @@
>Driver Skill
<div class="flex w-full justify-items-stretch text-base">
<div class="w-1/2 p-2 text-center">
Quickness: {speed}
<input type="range" min="0" max="5" step="1" bind:value={speed} />
Quickness: {quickness}
<input type="range" min="0" max="5" step="1" bind:value={quickness} />
</div>
<div class="w-1/2 p-2 text-center">
Field awareness: {awareness}
Expand Down

0 comments on commit c72336b

Please sign in to comment.