From abdf6d30b893a96209065105e7c48e6a2314c7a4 Mon Sep 17 00:00:00 2001 From: Brandon-Harad Date: Wed, 13 Nov 2024 23:51:12 +0000 Subject: [PATCH] style: format files --- src/lib/server-assets/database.ts | 176 ++++++++++++++++-------------- src/lib/types.ts | 10 +- src/routes/api/submit/+server.ts | 8 +- 3 files changed, 106 insertions(+), 88 deletions(-) diff --git a/src/lib/server-assets/database.ts b/src/lib/server-assets/database.ts index 2887641..efd2095 100644 --- a/src/lib/server-assets/database.ts +++ b/src/lib/server-assets/database.ts @@ -1,98 +1,116 @@ -import pg from 'pg' -const { Client } = pg +import pg from 'pg'; +const { Client } = pg; import { - DB_HOST, - DB_USER, - DB_PASSWORD, - POSTGRES_DATABASE, - DB_PORT, - USE_DB -} from '$env/static/private' + DB_HOST, + DB_USER, + DB_PASSWORD, + POSTGRES_DATABASE, + DB_PORT, + USE_DB +} from '$env/static/private'; import type { AutoAction, AutoActionData, TeamMatch, TeleAction, TeleActionData } from '$lib/types'; -// Whether or not the database is currently being used -const use_db : boolean = USE_DB === "true"; +// Whether or not the database is currently being used +const use_db: boolean = USE_DB === 'true'; const db = new Client({ - user: DB_USER, - password: DB_PASSWORD, - host: DB_HOST, - port: DB_PORT, - database: POSTGRES_DATABASE, + user: DB_USER, + password: DB_PASSWORD, + host: DB_HOST, + port: DB_PORT, + database: POSTGRES_DATABASE }); await db.connect(); // Returns the number of occurences of the action val with success status success in the auto-actions of match -function countAuto(val : AutoAction, success : boolean, match : TeamMatch): number { - return match.auto_actions.filter((data) => data.action === val && data.success == success).length; +function countAuto(val: AutoAction, success: boolean, match: TeamMatch): number { + return match.auto_actions.filter((data) => data.action === val && data.success == success) + .length; } // Returns the number of occurences of the action val with success status success in the tele-actions of match -function countTele(val : TeleAction, success : boolean, match : TeamMatch): number { - return match.tele_actions.filter((data) => data.action === val && data.success == success).length; +function countTele(val: TeleAction, success: boolean, match: TeamMatch): number { + return match.tele_actions.filter((data) => data.action === val && data.success == success) + .length; } +export async function insertTeamMatch(match: TeamMatch): Promise { + if (!use_db) return true; -export async function insertTeamMatch(match : TeamMatch): Promise { - if (!use_db) return true; + let { team_key, match_key, scout_id, skill, notes, broke, died, auto_actions, tele_actions } = + match; - let {team_key, match_key, scout_id, skill, notes, broke, died, auto_actions, tele_actions} = 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]) + ); + 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]) + ); + const auto_id = auto_query.rows[0]; - try { + await db.query('INSERT INTO TeamMatches VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)', [ + scout_id, + match_key, + team_key, + skill, + notes, + broke, + died, + tele_id, + auto_id + ]); - 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]) - ); - 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]) - ); - 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, - notes, - broke, - died, - tele_id, - auto_id - ] - ); - - return true; - } catch (error) { - console.error(error); - return false; - } -}; + return true; + } catch (error) { + console.error(error); + return false; + } +} diff --git a/src/lib/types.ts b/src/lib/types.ts index b4ce3ea..ed5f45a 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -2,10 +2,10 @@ export type TeamMatch = { team_key: string; match_key: string; scout_id: string; - skill : number; - notes : string; - broke : boolean; - died : boolean; + skill: number; + notes: string; + broke: boolean; + died: boolean; auto_actions: AutoActionData[]; tele_actions: TeleActionData[]; }; @@ -33,4 +33,4 @@ export type TeleAction = | 'ScoreBalloonLow'; export type ItemInputState = 'Intake' | 'Score' | 'None'; export type TeleInputState = TeleAction | ItemInputState; -export type AutoInputState = TeleInputState | BunnyAction; \ No newline at end of file +export type AutoInputState = TeleInputState | BunnyAction; diff --git a/src/routes/api/submit/+server.ts b/src/routes/api/submit/+server.ts index 5a3fc79..c59ab37 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 { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { insertTeamMatch } from '$lib/server-assets/database'; export const POST: RequestHandler = async ({ request }) => { - const match : TeamMatch = await request.json(); - return json(await insertTeamMatch(match)); -}; \ No newline at end of file + const match: TeamMatch = await request.json(); + return json(await insertTeamMatch(match)); +};