-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d97b111
commit abdf6d3
Showing
3 changed files
with
106 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |