Skip to content

Commit

Permalink
Merge branch 'Brandon/Database' of https://github.com/flamingchickens…
Browse files Browse the repository at this point in the history
…1540/inflatedchickens into Brandon/Database
  • Loading branch information
Brandon-Harad committed Nov 13, 2024
2 parents bb94b2c + abdf6d3 commit e8cf106
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 88 deletions.
176 changes: 97 additions & 79 deletions src/lib/server-assets/database.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
if (!use_db) return true;

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;

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;
}
}
10 changes: 5 additions & 5 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};
Expand Down Expand Up @@ -33,4 +33,4 @@ export type TeleAction =
| 'ScoreBalloonLow';
export type ItemInputState = 'Intake' | 'Score' | 'None';
export type TeleInputState = TeleAction | ItemInputState;
export type AutoInputState = TeleInputState | BunnyAction;
export type AutoInputState = TeleInputState | BunnyAction;
8 changes: 4 additions & 4 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 { 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));
};
const match: TeamMatch = await request.json();
return json(await insertTeamMatch(match));
};

0 comments on commit e8cf106

Please sign in to comment.