diff --git a/apps/scouting/backend/src/googleSheets.ts b/apps/scouting/backend/src/googleSheets.ts index 53e521b4..8c1cf1a4 100644 --- a/apps/scouting/backend/src/googleSheets.ts +++ b/apps/scouting/backend/src/googleSheets.ts @@ -131,6 +131,7 @@ const structureBeeTeamMatchData = ( comp: row.Comp, notes: row.G_Comments, timesStole: Number(row.T_CollectionZonesOpAlliance), + robotBroke: false, }; }); return unfilteredData.filter((row): row is TeamMatchData => row !== false); @@ -171,8 +172,29 @@ const updateBeeTeamMatchData = async (db: Db, data: string[][]) => { ); return; } - await collection.deleteMany({}); - await collection.insertMany(structured); + + await collection.bulkWrite( + structured.map(({ robotBroke: _ignored, ...sheetFields }) => ({ + updateOne: { + filter: { + teamNumber: sheetFields.teamNumber, + matchNumber: sheetFields.matchNumber, + }, + update: { + $set: sheetFields, + $setOnInsert: { robotBroke: false }, + }, + upsert: true, + }, + })), + ); + + await collection.deleteMany({ + $nor: structured.map(({ teamNumber, matchNumber }) => ({ + teamNumber, + matchNumber, + })), + }); console.log("Updated Bee a scout Team Match Data"); return structured; diff --git a/apps/scouting/backend/src/routes/general-router.ts b/apps/scouting/backend/src/routes/general-router.ts index 9685815b..29a56a52 100644 --- a/apps/scouting/backend/src/routes/general-router.ts +++ b/apps/scouting/backend/src/routes/general-router.ts @@ -33,22 +33,23 @@ export const calculateGeneralForTeam = ( forms: TeamMatchData[], team: string, ): GeneralTeamBeeData => { + const noBrokenForms = forms.filter((form) => !form.robotBroke); const auto = { - ...calculateFuelForTeamPhase(forms.map((form) => form.auto)), - climbPoints: calculateAverage(forms, (form) => + ...calculateFuelForTeamPhase(noBrokenForms.map((form) => form.auto)), + climbPoints: calculateAverage(noBrokenForms, (form) => form.auto.climb ? AUTO_CLIMB_POINTS : AUTO_NO_CLIMB_POINTS, ), }; const tele = { - ...calculateFuelForTeamPhase(forms.map((form) => form.tele)), + ...calculateFuelForTeamPhase(noBrokenForms.map((form) => form.tele)), climbPoints: calculateAverage( - forms, + noBrokenForms, (form) => form.tele.climb.height * TELE_CLIMB_LEVEL_POINTS, ), }; - const defenseGames = forms.filter((form) => form.super.didDefense); - const evasionGames = forms.filter((form) => form.super.didEvasions); + const defenseGames = noBrokenForms.filter((form) => form.super.didDefense); + const evasionGames = noBrokenForms.filter((form) => form.super.didEvasions); return { team, @@ -60,7 +61,7 @@ export const calculateGeneralForTeam = ( climbPoints: auto.climbPoints + tele.climbPoints, }, super: { - driving: calculateAverage(forms, (form) => form.super.driveLevel), + driving: calculateAverage(noBrokenForms, (form) => form.super.driveLevel), defenseRating: calculateAverage( defenseGames, (form) => form.super.defenseLevel, @@ -72,8 +73,9 @@ export const calculateGeneralForTeam = ( ), timesEvaded: evasionGames.length, }, - timesPlayed: forms.length, - timesStole: calculateSum(forms, (form) => form.timesStole), + timesPlayed: noBrokenForms.length, + timesStole: calculateSum(noBrokenForms, (form) => form.timesStole), + timesBroke: calculateSum(forms, (form) => (form.robotBroke ? 1 : 0)), }; }; diff --git a/apps/scouting/backend/src/routes/picklist-router.ts b/apps/scouting/backend/src/routes/picklist-router.ts index c5643b0f..29cea484 100644 --- a/apps/scouting/backend/src/routes/picklist-router.ts +++ b/apps/scouting/backend/src/routes/picklist-router.ts @@ -1,107 +1,107 @@ -//בס"ד + //בס"ד -import { Router } from "express"; -import { flow, pipe } from "fp-ts/lib/function"; -import { map, bind, right, fromEither, bindTo } from "fp-ts/lib/TaskEither"; -import { - createBodyVerificationPipe, - flatTryCatch, - foldResponse, -} from "@repo/flow-utils"; -import { getTotalGeneralData } from "./general-router"; -import { getDb } from "../middleware/db"; -import { StatusCodes } from "http-status-codes"; -import { DataPicklistBee, GeneralBeeData } from "@repo/scouting_types"; -import { right as rightEither } from "fp-ts/lib/Either"; -import { PicklistBee, picklistBeeCodec } from "@repo/scouting_types"; + import { Router } from "express"; + import { flow, pipe } from "fp-ts/lib/function"; + import { map, bind, right, fromEither, bindTo } from "fp-ts/lib/TaskEither"; + import { + createBodyVerificationPipe, + flatTryCatch, + foldResponse, + } from "@repo/flow-utils"; + import { getTotalGeneralData } from "./general-router"; + import { getDb } from "../middleware/db"; + import { StatusCodes } from "http-status-codes"; + import { DataPicklistBee, GeneralBeeData } from "@repo/scouting_types"; + import { right as rightEither } from "fp-ts/lib/Either"; + import { PicklistBee, picklistBeeCodec } from "@repo/scouting_types"; -export const picklistRouter = Router(); + export const picklistRouter = Router(); -export const getPicklistCollection = flow( - getDb, - map((db) => db.collection("picklist")), -); + export const getPicklistCollection = flow( + getDb, + map((db) => db.collection("picklist")), + ); -const createNewPickList = ( - data: GeneralBeeData, - name: string, -): PicklistBee => ({ - name, - list: Object.entries(data) - .map(([team, data]) => ({ - team, - data, - })) - .sort( - (teamA, teamB) => teamB.data.full.fuelScored - teamA.data.full.fuelScored, - //sorts from highest score to lowest - ) - .map(({ team }) => team), -}); + const createNewPickList = ( + data: GeneralBeeData, + name: string, + ): PicklistBee => ({ + name, + list: Object.entries(data) + .map(([team, data]) => ({ + team, + data, + })) + .sort( + (teamA, teamB) => teamB.data.full.fuelScored - teamA.data.full.fuelScored, + //sorts from highest score to lowest + ) + .map(({ team }) => team), + }); -picklistRouter.get("/list/:picklist/:recency", async (req, res) => { - await pipe( - getTotalGeneralData(parseInt(req.params.recency)), - bind("picklistCollection", getPicklistCollection), - bind("name", () => right(req.params.picklist)), - flatTryCatch( - async ({ generalData, picklistCollection, name }) => ({ - generalData, - picklist: - (await picklistCollection.findOne({ name })) ?? - createNewPickList(generalData, name), - }), - (error) => ({ - status: StatusCodes.INTERNAL_SERVER_ERROR, - reason: `Error Getting List From DB: ${error}`, - }), - ), - map(({ generalData, picklist }) => ({ - name: picklist.name, - list: picklist.list.map((team) => generalData[team]), - })), - map((picklist) => picklist satisfies DataPicklistBee), - foldResponse(res), - )(); -}); + picklistRouter.get("/list/:picklist/:recency", async (req, res) => { + await pipe( + getTotalGeneralData(parseInt(req.params.recency)), + bind("picklistCollection", getPicklistCollection), + bind("name", () => right(req.params.picklist)), + flatTryCatch( + async ({ generalData, picklistCollection, name }) => ({ + generalData, + picklist: + (await picklistCollection.findOne({ name })) ?? + createNewPickList(generalData, name), + }), + (error) => ({ + status: StatusCodes.INTERNAL_SERVER_ERROR, + reason: `Error Getting List From DB: ${error}`, + }), + ), + map(({ generalData, picklist }) => ({ + name: picklist.name, + list: picklist.list.map((team) => generalData[team]), + })), + map((picklist) => picklist satisfies DataPicklistBee), + foldResponse(res), + )(); + }); -picklistRouter.post("/list", async (req, res) => { - await pipe( - getPicklistCollection(), - bindTo("picklistCollection"), - bind("picklist", () => - pipe( - rightEither(req), - createBodyVerificationPipe(picklistBeeCodec), - fromEither, + picklistRouter.post("/list", async (req, res) => { + await pipe( + getPicklistCollection(), + bindTo("picklistCollection"), + bind("picklist", () => + pipe( + rightEither(req), + createBodyVerificationPipe(picklistBeeCodec), + fromEither, + ), ), - ), - flatTryCatch( - ({ picklist, picklistCollection }) => - picklistCollection.replaceOne({ name: picklist.name }, picklist, { - upsert: true, //makes it add the picklist if it cant find one with the same name + flatTryCatch( + ({ picklist, picklistCollection }) => + picklistCollection.replaceOne({ name: picklist.name }, picklist, { + upsert: true, //makes it add the picklist if it cant find one with the same name + }), + (error) => ({ + status: StatusCodes.INTERNAL_SERVER_ERROR, + reason: `Error inserting into DB ${error}`, }), - (error) => ({ - status: StatusCodes.INTERNAL_SERVER_ERROR, - reason: `Error inserting into DB ${error}`, - }), - ), - foldResponse(res), - )(); -}); + ), + foldResponse(res), + )(); + }); -picklistRouter.get("/lists", async (req, res) => { - await pipe( - getPicklistCollection(), + picklistRouter.get("/lists", async (req, res) => { + await pipe( + getPicklistCollection(), - flatTryCatch( - (collection) => collection.find().toArray(), - (error) => ({ - status: StatusCodes.INTERNAL_SERVER_ERROR, - reason: `Error inserting into DB ${error}`, - }), - ), - map((lists) => lists.map(({ name }) => name)), - foldResponse(res), - )(); -}); + flatTryCatch( + (collection) => collection.find().toArray(), + (error) => ({ + status: StatusCodes.INTERNAL_SERVER_ERROR, + reason: `Error inserting into DB ${error}`, + }), + ), + map((lists) => lists.map(({ name }) => name)), + foldResponse(res), + )(); + }); diff --git a/apps/scouting/backend/src/routes/team-page-router.ts b/apps/scouting/backend/src/routes/team-page-router.ts index 6bd770e2..f4946ba9 100644 --- a/apps/scouting/backend/src/routes/team-page-router.ts +++ b/apps/scouting/backend/src/routes/team-page-router.ts @@ -10,6 +10,9 @@ import { groupBy } from "fp-ts/lib/NonEmptyArray"; import { TeamMatchData, TeamPageTeamBeeData } from "@repo/scouting_types"; import { calculateSum, firstElement, mapObject } from "@repo/array-functions"; import { calculateGeneralForTeam } from "./general-router"; +import { chain, left, right } from "fp-ts/lib/TaskEither"; + +const MAX_RECENCY = 100; export const teamPageRouter = Router(); @@ -36,11 +39,21 @@ const findDataOverMatches = ( ); }; -export const applyRecency = (forms: TeamMatchData[], recency: number) => - forms +export const applyRecency = (forms: TeamMatchData[], recency: number) => { + const recentForms = forms .sort((formA, formB) => formA.matchNumber - formB.matchNumber) .slice(-recency); + return recency === MAX_RECENCY || recency === 0 + ? recentForms + : recentForms.filter((form) => !form.robotBroke); +}; + +export const getGamesBroken = (forms: TeamMatchData[]): string[] => + forms + .filter((form) => form.robotBroke) + .map((form) => String(form.matchNumber)); + const calculateTeamDataForTeam = ( forms: TeamMatchData[], recency: number, @@ -92,11 +105,12 @@ const calculateTeamDataForTeam = ( }, notes: recentForms.map((form) => form.notes), timesStole: calculateSum(recentForms, (form) => form.timesStole), + qualsRobotBroke: getGamesBroken(forms), }; }; const parseRecency = (recencyString: string) => - parseInt(recencyString ?? "100"); + parseInt(recencyString ?? MAX_RECENCY); teamPageRouter.get("/matches/:recency", async (req, res) => { await pipe( @@ -135,3 +149,41 @@ teamPageRouter.get("/teamNumbers", async (req, res) => { foldResponse(res), )(); }); + +teamPageRouter.patch("/broke/:teamNumber/:matchNumber", async (req, res) => { + const teamNumber = Number(req.params.teamNumber); + const matchNumber = Number(req.params.matchNumber); + + if (!Number.isInteger(teamNumber) || !Number.isInteger(matchNumber)) { + res.status(StatusCodes.BAD_REQUEST).send("Invalid team or match number"); + return; + } + + const robotBroke = + typeof req.body?.robotBroke === "boolean" ? req.body.robotBroke : true; + + await pipe( + getBeeTeamMatchDataCollection(), + flatTryCatch( + (collection) => + collection.updateMany( + { teamNumber, matchNumber }, + { $set: { robotBroke } }, + ), + (error) => ({ + status: StatusCodes.INTERNAL_SERVER_ERROR, + reason: `error updating robotBroke ${error}`, + }), + ), + chain((result) => + result.matchedCount === 0 + ? left({ + status: StatusCodes.NOT_FOUND, + reason: `no form for team ${teamNumber} in match ${matchNumber}`, + }) + : right(result), + ), + map(() => ({ teamNumber, matchNumber, robotBroke })), + foldResponse(res), + )(); +}); diff --git a/apps/scouting/frontend/src/App.tsx b/apps/scouting/frontend/src/App.tsx index d8ae14c2..611adfc4 100644 --- a/apps/scouting/frontend/src/App.tsx +++ b/apps/scouting/frontend/src/App.tsx @@ -15,6 +15,7 @@ import { Tinder } from "./strategy/tabs/Tinder"; import { PitScoutTab } from "./strategy/tabs/pit-scout/PitScoutTab"; import { Picklist } from "./strategy/tabs/picklist/Picklist"; import { PitManageTab } from "./strategy/tabs/pit-scout/PitManageTab"; +import { RobotBrokePage } from "./scouter/pages/RobotBrokePage"; import { ScoutingPass } from "./scouter/pages/ScoutingPass"; const App: FC = () => { @@ -23,6 +24,7 @@ const App: FC = () => { } /> } /> + } /> } /> } /> } /> diff --git a/apps/scouting/frontend/src/scouter/pages/RobotBrokePage.tsx b/apps/scouting/frontend/src/scouter/pages/RobotBrokePage.tsx new file mode 100644 index 00000000..a2b60608 --- /dev/null +++ b/apps/scouting/frontend/src/scouter/pages/RobotBrokePage.tsx @@ -0,0 +1,121 @@ +// בס"ד +import { useState, type FC } from "react"; + +const BROKE_URL = "/api/v1/teamPage/broke"; + +const sendRobotBroke = async ( + teamNumber: number, + matchNumber: number, + robotBroke: boolean, +) => { + const response = await fetch(`${BROKE_URL}/${teamNumber}/${matchNumber}`, { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ robotBroke }), + }); + + if (response.ok) { + alert( + `Team ${teamNumber}, match ${matchNumber}: marked as ${ + robotBroke ? "broke" : "didn't break" + }`, + ); + } else { + alert(`Couldn't update! ${await response.text()}`); + } +}; + +const inputClass = + "w-full px-3 py-2 bg-slate-950 border border-slate-800 rounded-md text-sm text-slate-200 placeholder-slate-600 focus:outline-none focus:border-blue-500 transition-colors"; +const labelClass = + "block text-xs font-semibold uppercase tracking-wider text-slate-400 mb-1.5"; + +export const RobotBrokePage: FC = () => { + const [teamNumber, setTeamNumber] = useState(""); + const [matchNumber, setMatchNumber] = useState(""); + const [robotBroke, setRobotBroke] = useState(false); + const [sending, setSending] = useState(false); + + const canSubmit = + Number.isInteger(Number(teamNumber)) && + Number.isInteger(Number(matchNumber)) && + teamNumber !== "" && + matchNumber !== ""; + + const submit = async () => { + if (!canSubmit) { + alert( + "you have to submit numbers as the team number and match number! fix it and try again.", + ); + return; + } + setSending(true); + try { + await sendRobotBroke(Number(teamNumber), Number(matchNumber), robotBroke); + } catch (error) { + alert(`Couldn't update! ${error}`); + } finally { + setSending(false); + } + }; + + return ( +
+
+
+
+ + setTeamNumber(e.target.value)} + placeholder="e.g., 4590" + className={inputClass} + /> +
+
+ + setMatchNumber(e.target.value)} + placeholder="e.g., 12" + className={inputClass} + /> +
+
+ + + + +
+
+ ); +}; diff --git a/apps/scouting/frontend/src/strategy/tabs/CompareTwo.tsx b/apps/scouting/frontend/src/strategy/tabs/CompareTwo.tsx index 09defe45..333dd1b4 100644 --- a/apps/scouting/frontend/src/strategy/tabs/CompareTwo.tsx +++ b/apps/scouting/frontend/src/strategy/tabs/CompareTwo.tsx @@ -216,6 +216,7 @@ export const CompareTwo: React.FC = () => { periodData={team.total} max={maxTotalScore} title="Full Game" + brokenMatches={team.qualsRobotBroke} /> )} @@ -226,6 +227,7 @@ export const CompareTwo: React.FC = () => { periodData={team.auto} max={maxAutoScore} title="Auto" + brokenMatches={team.qualsRobotBroke} /> )} diff --git a/apps/scouting/frontend/src/strategy/tabs/GeneralDataTable.tsx b/apps/scouting/frontend/src/strategy/tabs/GeneralDataTable.tsx index d87398eb..25e52302 100644 --- a/apps/scouting/frontend/src/strategy/tabs/GeneralDataTable.tsx +++ b/apps/scouting/frontend/src/strategy/tabs/GeneralDataTable.tsx @@ -37,7 +37,8 @@ export type Column = | "Pass Full" | "Climb Full" | "Games Played" - | "Times Stole"; + | "Times Stole" + | "Times Broke"; type DataValue = ClimbLevel | number | undefined; @@ -50,6 +51,7 @@ const columnToKey: Record = { "Pass Tele": ({ tele: { fuelPassed } }) => fuelPassed, "Score Auto": ({ auto: { fuelScored } }) => fuelScored, "Pass Auto": ({ auto: { fuelPassed } }) => fuelPassed, + "Times Broke": ({ timesBroke }) => timesBroke, "Times Defended": ({ super: { timesDefended } }) => timesDefended, "Times Evaded": ({ super: { timesEvaded } }) => timesEvaded, "Score Full": ({ full: { fuelScored } }) => fuelScored, @@ -160,6 +162,7 @@ export const GeneralDataTable: React.FC = ({ createColumn("Score Tele", "text-red-500"), createColumn("Pass Tele", "text-violet-500"), createColumn("Driving", "text-orange-500"), + createColumn("Times Broke", "text-red-400"), createColumn("Times Defended", "text-pink-200"), createColumn("Evasion Rating", "text-purple-500"), createColumn("Times Evaded", "text-purple-200"), diff --git a/apps/scouting/frontend/src/strategy/tabs/team/TeamTab.tsx b/apps/scouting/frontend/src/strategy/tabs/team/TeamTab.tsx index 554734a6..e9b92ae3 100644 --- a/apps/scouting/frontend/src/strategy/tabs/team/TeamTab.tsx +++ b/apps/scouting/frontend/src/strategy/tabs/team/TeamTab.tsx @@ -80,7 +80,7 @@ export const TeamTab: FC = () => { -
+
{ statContent={teamData?.timesStole?.toFixed(2)} statName={"Times Stole"} /> +
{teamData && ( @@ -104,23 +108,26 @@ export const TeamTab: FC = () => { )} - {teamData && (
)} - {teamData && (
- +
)} - {teamData && (
diff --git a/apps/scouting/frontend/src/strategy/tabs/team/ThrowBarChart.tsx b/apps/scouting/frontend/src/strategy/tabs/team/ThrowBarChart.tsx index 100eb219..0f1d95f8 100644 --- a/apps/scouting/frontend/src/strategy/tabs/team/ThrowBarChart.tsx +++ b/apps/scouting/frontend/src/strategy/tabs/team/ThrowBarChart.tsx @@ -18,25 +18,36 @@ export const ThrowBarChart: FC<{ periodData: TeamPageTeamBeeData["auto" | "total"]; max: number; title: string; -}> = ({ periodData, max, title }) => { + brokenMatches: string[]; +}> = ({ periodData, max, title, brokenMatches }) => { + const { fuelScoredPerGame, fuelPassedPerGame } = periodData.fuelPerGame; + + const broken = new Set(brokenMatches); + + const split = (perGame: Record, isBroken: boolean) => + Object.fromEntries( + Object.entries(perGame).map(([match, value]) => [ + match, + broken.has(match) === isBroken ? value : 0, + ]), + ); + const datasets = [ { name: "Scored", color: "violet", - points: Object.fromEntries( - Object.entries(periodData.fuelPerGame.fuelScoredPerGame).map( - ([match, value]) => [match, value], - ), - ), + points: split(fuelScoredPerGame, false), + }, + { name: "Passed", color: "cyan", points: split(fuelPassedPerGame, false) }, + { + name: "Scored (robot broke)", + color: "#b91c1c", + points: split(fuelScoredPerGame, true), }, { - name: "Passed", - color: "cyan", - points: Object.fromEntries( - Object.entries(periodData.fuelPerGame.fuelPassedPerGame).map( - ([match, value]) => [match, value], - ), - ), + name: "Passed (robot broke)", + color: "#f97316", + points: split(fuelPassedPerGame, true), }, ]; diff --git a/packages/array-functions/index.ts b/packages/array-functions/index.ts index 43f8f855..3bb4686d 100644 --- a/packages/array-functions/index.ts +++ b/packages/array-functions/index.ts @@ -10,7 +10,8 @@ export const calculateSum = ( export const calculateAverage = ( arr: T[], transformation: (value: T) => number, -): number => calculateSum(arr, transformation) / arr.length; +): number => + arr.length === 0 ? 0 : calculateSum(arr, transformation) / arr.length; const MEDIAN_LOCATION = 2; export const calculateMedian = ( diff --git a/packages/scouting_types/rebuilt/beeAScout/general.ts b/packages/scouting_types/rebuilt/beeAScout/general.ts index 55b1f894..00ebbe97 100644 --- a/packages/scouting_types/rebuilt/beeAScout/general.ts +++ b/packages/scouting_types/rebuilt/beeAScout/general.ts @@ -18,6 +18,7 @@ export interface GeneralTeamBeeData { }; timesPlayed: number; timesStole: number; + timesBroke: number; } export type GeneralBeeData = Record; diff --git a/packages/scouting_types/rebuilt/beeAScout/teamMatchData.ts b/packages/scouting_types/rebuilt/beeAScout/teamMatchData.ts index 950026dc..8f00f6fd 100644 --- a/packages/scouting_types/rebuilt/beeAScout/teamMatchData.ts +++ b/packages/scouting_types/rebuilt/beeAScout/teamMatchData.ts @@ -43,6 +43,7 @@ const teamMatchDataCodec = t.type({ comp: t.string, notes: t.string, timesStole: t.number, + robotBroke: t.boolean, }); export type TeamMatchData = t.TypeOf; diff --git a/packages/scouting_types/rebuilt/beeAScout/teamPage.ts b/packages/scouting_types/rebuilt/beeAScout/teamPage.ts index cc221162..9cf7ec56 100644 --- a/packages/scouting_types/rebuilt/beeAScout/teamPage.ts +++ b/packages/scouting_types/rebuilt/beeAScout/teamPage.ts @@ -33,6 +33,7 @@ const teamDataCodec = t.type({ super: superCodec, notes: t.array(t.string), timesStole: t.number, + qualsRobotBroke: t.array(t.string), }); export type TeamPageTeamBeeData = t.TypeOf;