Skip to content

Commit

Permalink
feat: finished submit function
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-Harad committed Dec 1, 2024
1 parent 71942fe commit a76b60d
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 103 deletions.
101 changes: 55 additions & 46 deletions migrations/DB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ CREATE TYPE auto_action_data AS (
success BOOLEAN
);

CREATE TABLE "Users"(
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"is_admin" BOOLEAN NOT NULL,
"slack_token" VARCHAR(255) NOT NULL
);
ALTER TABLE
"Users" ADD PRIMARY KEY("id");
CREATE TABLE "EventState"(
"event_key" VARCHAR(255) NOT NULL,
"next_match" VARCHAR(255) NULL,
"last_match" VARCHAR(255) NULL
);
ALTER TABLE
"EventState" ADD PRIMARY KEY("event_key");
CREATE TABLE "Teams"("team_key" SMALLINT NOT NULL);
ALTER TABLE
"Teams" ADD PRIMARY KEY("team_key");
Expand All @@ -40,81 +55,75 @@ CREATE TABLE "Matches"(
);
ALTER TABLE
"Matches" ADD PRIMARY KEY("match_key");
CREATE TABLE "Users"(
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"is_admin" BOOLEAN NOT NULL,
"slack_token" VARCHAR(255) NOT NULL
CREATE TABLE "TeamMatches"(
"team_action_id" BIGINT 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,
"broke" BOOLEAN NOT NULL,
"died" BOOLEAN NOT NULL,
"tele_id" SMALLINT NOT NULL,
"auto_id" SMALLINT NOT NULL
);
ALTER TABLE
"Users" ADD PRIMARY KEY("id");
CREATE TABLE "AutoActions"(
"TeamMatches" ADD PRIMARY KEY("team_action_id");
CREATE TABLE "TeleActions"(
"id" SMALLINT NOT NULL,
"tote_intake_success" SMALLINT NOT NULL,
"tote_intake_failure" SMALLINT NOT NULL,
"tote_eject_success" SMALLINT NOT NULL,
"tote_eject_failure" SMALLINT NOT NULL,
"balloon_intake_success" SMALLINT NOT NULL,
"balloon_intake_failure" SMALLINT NOT NULL,
"balloon_eject_success" SMALLINT NOT NULL,
"balloon_eject_failure" SMALLINT NOT NULL,
"score_low_success" SMALLINT NOT NULL,
"score_low_failure" SMALLINT NOT NULL,
"score_internal_sucess" SMALLINT NOT NULL,
"score_internal_success" SMALLINT NOT NULL,
"score_internal_failure" SMALLINT NOT NULL,
"score_external_success" SMALLINT NOT NULL,
"score_external_failure" SMALLINT NOT NULL,
"score_other_robot_success" SMALLINT NOT NULL,
"score_other_robot_failure" SMALLINT NOT NULL,
"bunny_intake_success" SMALLINT NOT NULL,
"bunny_intake_failure" SMALLINT NOT NULL,
"bunny_tote_success" SMALLINT NOT NULL,
"bunny_tote_failure" SMALLINT NOT NULL,
"bunny_low_success" SMALLINT NOT NULL,
"bunny_low_failure" SMALLINT NOT NULL,
"actions" auto_action_data[]
"score_uncontrolled_success" SMALLINT NOT NULL,
"score_uncontrolled_failure" SMALLINT NOT NULL,
"bunny_eject_success" SMALLINT NOT NULL,
"bunny_eject_failure" SMALLINT NOT NULL,
"actions" tele_action_data[]
);
ALTER TABLE
"AutoActions" ADD PRIMARY KEY("id");
CREATE TABLE "TeleActions"(
"TeleActions" ADD PRIMARY KEY("id");
CREATE TABLE "AutoActions"(
"id" SMALLINT NOT NULL,
"tote_intake_success" SMALLINT NOT NULL,
"tote_intake_failure" SMALLINT NOT NULL,
"tote_eject_success" SMALLINT NOT NULL,
"tote_eject_failure" SMALLINT NOT NULL,
"balloon_intake_success" SMALLINT NOT NULL,
"bollon_intake_failure" SMALLINT NOT NULL,
"balloon_intake_failure" SMALLINT NOT NULL,
"balloon_eject_success" SMALLINT NOT NULL,
"balloon_eject_failure" SMALLINT NOT NULL,
"score_low_success" SMALLINT NOT NULL,
"score_low_failure" SMALLINT NOT NULL,
"score_internal_success" SMALLINT NOT NULL,
"score_internal_sucess" SMALLINT NOT NULL,
"score_internal_failure" SMALLINT NOT NULL,
"score_external_success" SMALLINT NOT NULL,
"score_external_failure" SMALLINT NOT NULL,
"score_other_robot_success" SMALLINT NOT NULL,
"score_other_robot_failure" SMALLINT NOT NULL,
"actions" tele_action_data[]
);
ALTER TABLE
"TeleActions" ADD PRIMARY KEY("id");
CREATE TABLE "TeamMatches"(
"team_action_id" BIGINT NOT NULL,
"scout_id" VARCHAR(255) NOT NULL,
"match_key" VARCHAR(255) NOT NULL,
"team_key" VARCHAR(255) NOT NULL,
"skill" SMALLINT NOT NULL,
"notes" TEXT NOT NULL,
"broke" BOOLEAN NOT NULL,
"died" BOOLEAN NOT NULL,
"tele_id" SMALLINT NOT NULL,
"auto_id" SMALLINT NOT NULL
);
ALTER TABLE
"TeamMatches" ADD PRIMARY KEY("team_action_id");
CREATE TABLE "EventState"(
"event_key" VARCHAR(255) NOT NULL,
"next_match" VARCHAR(255) NULL,
"last_match" VARCHAR(255) NULL
"score_uncontrolled_success" SMALLINT NOT NULL,
"score_uncontrolled_failure" SMALLINT NOT NULL,
"bunny_intake_success" SMALLINT NOT NULL,
"bunny_intake_failure" SMALLINT NOT NULL,
"bunny_intake_success" SMALLINT NOT NULL,
"bunny_intake_failure" SMALLINT NOT NULL,
"bunny_tote_success" SMALLINT NOT NULL,
"bunny_tote_failure" SMALLINT NOT NULL,
"bunny_low_success" SMALLINT NOT NULL,
"bunny_low_failure" SMALLINT NOT NULL,
"actions" auto_action_data[]
);
ALTER TABLE
"EventState" ADD PRIMARY KEY("event_key");
"AutoActions" ADD PRIMARY KEY("id");
ALTER TABLE
"TeamMatches" ADD CONSTRAINT "teammatches_team_key_foreign" FOREIGN KEY("team_key") REFERENCES "Teams"("team_key");
ALTER TABLE
Expand Down
120 changes: 70 additions & 50 deletions src/lib/server-assets/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DB_PORT,
USE_DB
} from '$env/static/private';
import type { AutoAction, AutoActionData, TeamMatch, TeleAction, TeleActionData } from '$lib/types';
import type { ActionsTM, AutoAction, AutoActionData, AutoActionsTM, TeamMatch, TeleAction, TeleActionData, TeleActionsTM } from '$lib/types';

// Whether or not the database is currently being used
const use_db: boolean = USE_DB === 'true';
Expand All @@ -34,73 +34,93 @@ function countTele(val: TeleAction, success: boolean, match: TeamMatch): number
.length;
}

// Returns the TeleActionsTM object associated to any TeamMatch by counting the occurrences of each TeleAction and then passing along the array of TeleActionDatas.
function matchToTeleActionsTM(match : TeamMatch): TeleActionsTM {
return {
id : match.id,
tote_intake_success: countTele('IntakeTote', true, match),
tote_intake_failure: countTele('IntakeTote', false, match),
tote_eject_success: countTele('EjectTote', true, match),
tote_eject_failure: countTele('EjectTote', false, match),
balloon_intake_success: countTele('IntakeBalloon', true, match),
balloon_intake_failure: countTele('IntakeBalloon', false , match),
balloon_eject_success: countTele('EjectBalloon', true , match),
balloon_eject_failure: countTele('EjectBalloon', false , match),
score_low_success: countTele('ScoreBalloonLow', true, match),
score_low_failure: countTele('ScoreBalloonLow', false, match),
score_internal_success: countTele('ScoreBalloonInternalTote', true, match),
score_internal_failure: countTele('ScoreBalloonInternalTote', false, match),
score_external_success: countTele('ScoreBalloonExternalTote', true, match),
score_external_failure: countTele('ScoreBalloonExternalTote', false, match),
score_uncontrolled_success: countTele('ScoreBalloonUncontrolledTote', true, match),
score_uncontrolled_failure: countTele('ScoreBalloonUncontrolledTote', false, match),
bunny_eject_success: countTele('EjectBunny', false, match),
bunny_eject_failure: countTele('EjectBunny', false, match),
actions: match.tele_actions
}
}

// Returns the AutoActionsTM object associated to any TeamMatch by counting the occurrences of each AutoAction and then passing along the array of AutoActionDatas.
function matchToAutoActionsTM(match : TeamMatch) : AutoActionsTM {
return {
id : match.id,
tote_intake_success: countAuto('IntakeTote', true, match),
tote_intake_failure: countAuto('IntakeTote', false, match),
tote_eject_success: countAuto('EjectTote', true, match),
tote_eject_failure: countAuto('EjectTote', false, match),
balloon_intake_success: countAuto('IntakeBalloon', true, match),
balloon_intake_failure: countAuto('IntakeBalloon', false , match),
balloon_eject_success: countAuto('EjectBalloon', true , match),
balloon_eject_failure: countAuto('EjectBalloon', false , match),
score_low_success: countAuto('ScoreBalloonLow', true, match),
score_low_failure: countAuto('ScoreBalloonLow', false, match),
score_internal_success: countAuto('ScoreBalloonInternalTote', true, match),
score_internal_failure: countAuto('ScoreBalloonInternalTote', false, match),
score_external_success: countAuto('ScoreBalloonExternalTote', true, match),
score_external_failure: countAuto('ScoreBalloonExternalTote', false, match),
score_uncontrolled_success: countAuto('ScoreBalloonUncontrolledTote', true, match),
score_uncontrolled_failure: countAuto('ScoreBalloonUncontrolledTote', false, match),
bunny_eject_success: countAuto('EjectBunny', false, match),
bunny_eject_failure: countAuto('EjectBunny', false, match),
bunny_intake_success: countAuto('IntakeBunny', true, match),
bunny_intake_failure: countAuto('IntakeBunny', false, match),
bunny_internal_success: countAuto('ScoreBunnyInternalTote', true, match),
bunny_internal_failure: countAuto('ScoreBunnyInternalTote', false, match),
bunny_external_success: countAuto('ScoreBunnyExternalTote', true, match),
bunny_external_failure: countAuto('ScoreBunnyExternalTote', false, match),
bunny_uncontrolled_success: countAuto('ScoreBunnyUncontrolledTote', true, match),
bunny_uncontrolled_failure: countAuto('ScoreBunnyUncontrolledTote', false, match),
bunny_low_success: countAuto('ScoreBunnyLow', true, match),
bunny_low_failure: countAuto('ScoreBunnyLow', false, match),
actions: match.auto_actions
}
}

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

let { team_key, match_key, scout_id, skill, notes, broke, died, auto_actions, tele_actions } =
match;
const teledata = matchToTeleActionsTM(match);
const autodata = matchToAutoActionsTM(match);
let {scout_id, match_key, team_key, skill_field_awareness, skill_quickness, notes, broke, died} = match

try {
const tele_query = await db.query(
'INSERT INTO TeleActions VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)',
(
(
[
'IntakeTote',
'EjectTote',
'IntakeBalloon',
'ScoreBalloonLow',
'ScoreYourHeldTote',
'ScoreExternalTote',
'ScoreOtherRobotTote'
] as TeleAction[]
)
.flatMap((val) => [
{ action: val, success: true },
{ action: val, success: false }
])
.map((val) => countTele(val.action, val.success, match)) as (
| number
| TeleActionData[]
)[]
).concat([tele_actions])
[teledata.id, teledata.tote_intake_success, teledata.tote_intake_failure, teledata.tote_eject_success, teledata.tote_eject_failure, teledata.balloon_intake_success, teledata.balloon_intake_failure, teledata.balloon_eject_success, teledata.balloon_eject_failure, teledata.score_low_success, teledata.score_low_failure, teledata.score_internal_success, teledata.score_internal_failure, teledata.score_external_success, teledata.score_external_failure, teledata.score_uncontrolled_success, teledata.score_uncontrolled_failure, teledata.bunny_eject_success, teledata.bunny_eject_failure, teledata.actions]
);
const tele_id = tele_query.rows[0];

const auto_query = await db.query(
'INSERT INTO AutoActions VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)',
(
(
[
'IntakeTote',
'EjectTote',
'IntakeBalloon',
'ScoreBalloonLow',
'ScoreYourHeldTote',
'ScoreExternalTote',
'ScoreOtherRobotTote',
'IntakeBunny',
'ScoreBunnyTote',
'ScoreBunnyLow'
] as AutoAction[]
)
.flatMap((val) => [
{ action: val, success: true },
{ action: val, success: false }
])
.map((val) => countAuto(val.action, val.success, match)) as (
| number
| AutoActionData[]
)[]
).concat([auto_actions])
'INSERT INTO AutoActions VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)', [autodata.id, autodata.tote_intake_success, autodata.tote_intake_failure, autodata.tote_eject_success, autodata.tote_eject_failure, autodata.balloon_intake_success, autodata.balloon_intake_failure, autodata.balloon_eject_success, autodata.balloon_eject_failure, autodata.score_low_success, autodata.score_low_failure, autodata.score_internal_success,autodata. score_internal_failure, autodata.score_external_success, 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_internal_success, autodata.bunny_internal_failure, autodata.bunny_external_success,autodata.bunny_external_failure, autodata.bunny_uncontrolled_success, autodata.bunny_uncontrolled_failure, autodata.bunny_low_success, autodata.bunny_low_failure, autodata.actions]
);
const auto_id = auto_query.rows[0];

await db.query('INSERT INTO TeamMatches VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)', [
scout_id,
match_key,
team_key,
skill,
skill_field_awareness,
skill_quickness,
notes,
broke,
died,
Expand Down
22 changes: 15 additions & 7 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export type User = {
export type AutoActionsTM = ActionsTM & {
bunny_intake_success: number;
bunny_intake_failure: number;
bunny_tote_success: number;
bunny_tote_failure: number;
bunny_internal_success: number;
bunny_internal_failure: number;
bunny_external_success: number;
bunny_external_failure: number;
bunny_uncontrolled_success: number;
bunny_uncontrolled_failure: number;
bunny_low_success: number;
bunny_low_failure: number;
actions: AutoActionData[];
Expand All @@ -33,23 +37,28 @@ export type ActionsTM = {
tote_eject_success: number;
tote_eject_failure: number;
balloon_intake_success: number;
bollon_intake_failure: number;
balloon_intake_failure: number;
balloon_eject_success: number;
balloon_eject_failure: number;
score_low_success: number;
score_low_failure: number;
score_internal_success: number;
score_internal_failure: number;
score_external_success: number;
score_external_failure: number;
score_other_robot_success: number;
score_other_robot_failure: number;
score_uncontrolled_success: number;
score_uncontrolled_failure: number;
bunny_eject_success: number;
bunny_eject_failure: number;
};

export type TeamMatch = {
id: number;
scout_id: string;
match_key: string;
team_key: string;
skill: number;
skill_field_awareness: number;
skill_quickness: number;
notes: string;
broke: boolean;
died: boolean;
Expand All @@ -72,7 +81,6 @@ export type TeleActionData = {
export type TeleAction =
| 'IntakeTote'
| 'IntakeBalloon'
| 'IntakeBalloonCoral'
| 'ScoreBalloonInternalTote' // Held by scorer
| 'ScoreBalloonExternalTote' // Held by alliance member
| 'ScoreBalloonUncontrolledTote'
Expand Down

0 comments on commit a76b60d

Please sign in to comment.