diff --git a/apps/scouting/backend/src/main.ts b/apps/scouting/backend/src/main.ts index b673113..ef3be0b 100644 --- a/apps/scouting/backend/src/main.ts +++ b/apps/scouting/backend/src/main.ts @@ -21,55 +21,9 @@ const app = express(); const defaultPort = 4590; const port = process.env.BACKEND_PORT ?? defaultPort; +app.use(express.json()); app.use("/api/v1", apiRouter); app.listen(port, () => { console.log(`Production server running at http://localhost:${port}`); -}); - -const coralCounter: GameEventsCounter = { - L1: 0, - L2: 0, - L3: 0, - L4: 0, -}; - -const algaeCounter: GameEventsCounter = { - Processor: 0, - Net: 0, -}; - -const coral: GameObject = { - name: "coral", - gameEvents: coralCounter, -}; - -const algae: GameObject = { - name: "algae", - gameEvents: algaeCounter, -}; - -const scoringCalculator: ScoringCalculator = { - gameObjectsScoringData: [], -}; - -addGameEvent(coral, "L1"); -addGameEvent(coral, "L2"); -addGameEvent(algae, "Net"); - -const coralWithPoints: GameObjectWithPoints = { - gameObject: coral, - calculatePoints: calculatePointsCoral, - calculateRP: calculateRPCoral, -}; - -const algaeWithPoints: GameObjectWithPoints = { - gameObject: algae, - calculatePoints: calculatePointsAlgae, - calculateRP: calculateRPAlgae, -}; - -addScoring(scoringCalculator, coralWithPoints); -addScoring(scoringCalculator, algaeWithPoints); - -console.log(scoringCalculator); +}); \ No newline at end of file diff --git a/apps/scouting/backend/src/middleware/verification.ts b/apps/scouting/backend/src/middleware/verification.ts new file mode 100644 index 0000000..a691801 --- /dev/null +++ b/apps/scouting/backend/src/middleware/verification.ts @@ -0,0 +1,20 @@ +// בס"ד +import type { RequestHandler } from "express"; +import { isLeft } from "fp-ts/lib/Either"; +import { StatusCodes } from "http-status-codes"; +import { PathReporter } from "io-ts/lib/PathReporter"; +import type { Props, TypeC } from "io-ts"; + +export const verifyBody = + (typeToCheck: TypeC): RequestHandler => + (req, res, next): void => { + const result = typeToCheck.decode(req.body); + + if (isLeft(result)) { + res + .status(StatusCodes.NOT_ACCEPTABLE) + .json({ errors: PathReporter.report(result) }); + } else { + next(); + } + }; diff --git a/apps/scouting/backend/src/routes/index.ts b/apps/scouting/backend/src/routes/index.ts index b62ad50..4275197 100644 --- a/apps/scouting/backend/src/routes/index.ts +++ b/apps/scouting/backend/src/routes/index.ts @@ -1,9 +1,11 @@ // בס"ד import { Router } from "express"; import { StatusCodes } from "http-status-codes"; +import { tbaRouter } from "./tba"; export const apiRouter = Router(); +apiRouter.use("/tba", tbaRouter); apiRouter.get("/health", (req, res) => { res.status(StatusCodes.OK).send({ message: "Healthy!" }); -}); \ No newline at end of file +}); diff --git a/apps/scouting/backend/src/routes/tba.ts b/apps/scouting/backend/src/routes/tba.ts new file mode 100644 index 0000000..413fb8e --- /dev/null +++ b/apps/scouting/backend/src/routes/tba.ts @@ -0,0 +1,43 @@ +// בס"ד +import axios, { type AxiosRequestConfig } from "axios"; +import { Router } from "express"; +import { verifyBody } from "../middleware/verification"; +import { + type TBAMatch, + type TBAMatchesProps, + matchesProps, +} from "../../../common/types/TBAMatch"; +import { StatusCodes } from "http-status-codes"; +import type { ScoreBreakdown2025 } from "../../../common/types/ScoreBreakdown2025"; +import type { BodiedRequest } from "../../../common/types/Express"; + +export const tbaRouter = Router(); + +const TBA_KEY = process.env.TBA_API_KEY ?? "yourtbakey"; + +const fetchTba = async ( + route: string, + config?: AxiosRequestConfig +): Promise => { + return axios + .get(`https://www.thebluealliance.com/api/v3${route}`, { + headers: { "X-TBA-Auth-Key": TBA_KEY }, + ...config, + }) + .then((value) => value.data); +}; + +tbaRouter.post( + "/matches", + verifyBody(matchesProps), + (req: BodiedRequest, res) => { + fetchTba>(`/event/${req.body.event}/matches`) + .then((value) => { + console.log(value); + res.status(StatusCodes.OK).json({ value }); + }) + .catch((error: unknown) => { + console.error(error); + }); + } +); diff --git a/apps/scouting/common/types/Express.ts b/apps/scouting/common/types/Express.ts new file mode 100644 index 0000000..02f4c83 --- /dev/null +++ b/apps/scouting/common/types/Express.ts @@ -0,0 +1,7 @@ +// בס"ד + +import type { Request } from "express"; + +export interface BodiedRequest extends Request { + body: Body; +} diff --git a/apps/scouting/common/types/ScoreBreakdown2025.ts b/apps/scouting/common/types/ScoreBreakdown2025.ts new file mode 100644 index 0000000..9d440c2 --- /dev/null +++ b/apps/scouting/common/types/ScoreBreakdown2025.ts @@ -0,0 +1,57 @@ +// בס"ד + +type YesNo = "Yes" | "No"; + +type Climb = "Park" | "DeepCage" | "ShallowCage" | "None"; + +interface ReefRow { + nodeA: boolean; + nodeB: boolean; + nodeC: boolean; + nodeD: boolean; + nodeE: boolean; +} +interface Reef { + botRow: ReefRow; + midRow: ReefRow; + tba_botRowCount: number; + tba_midRowCount: number; + tba_topRowCount: number; + topRow: ReefRow; + trough: number; +} +export interface ScoreBreakdown2025 { + adjustPoints: number; + algaePoints: number; + autoBonusAchieved: boolean; + autoCoralCount: number; + autoCoralPoints: number; + autoLineRobot1: YesNo; + autoLineRobot2: YesNo; + autoLineRobot3: YesNo; + autoMobilityPoints: number; + autoPoints: number; + autoReef: Reef; + bargeBonusAchieved: boolean; + coopertitionCriteriaMet: boolean; + coralBonusAchieved: boolean; + endGameBargePoints: number; + endGameRobot1: Climb; + endGameRobot2: Climb; + endGameRobot3: Climb; + foulCount: number; + foulPoints: number; + g206Penalty: boolean; + g410Penalty: boolean; + g418Penalty: boolean; + g428Penalty: boolean; + netAlgaeCount: number; + rp: number; + techFoulCount: number; + teleopCoralCount: number; + teleopCoralPoints: number; + teleopPoints: number; + teleopReef: Reef; + totalPoints: number; + wallAlgaeCount: number; +} diff --git a/apps/scouting/common/types/TBAMatch.ts b/apps/scouting/common/types/TBAMatch.ts new file mode 100644 index 0000000..b480ee9 --- /dev/null +++ b/apps/scouting/common/types/TBAMatch.ts @@ -0,0 +1,44 @@ +// בס"ד +import * as t from "io-ts"; +import type { AtMost } from "./Utils"; + +export const matchesProps = t.type({ + event: t.string, +}); + +export type TBAMatchesProps = t.TypeOf; + +type MaxTeamsInAlliance = 3; +type TeamKeys = AtMost; + +export interface TBAAlliance { + score: number; + team_keys: TeamKeys; + surrogate_team_keys: TeamKeys; + dq_team_keys: TeamKeys; +} + +export interface TBAMatch { + key: "string"; + comp_level: "qm"; + set_number: number; + match_number: number; + alliances: { + red: TBAAlliance; + blue: TBAAlliance; + }; + winning_alliance: "red" | "blue" | ""; // "" is a tie + event_key: string; + time: number; + actual_time: number; + predicted_time: number; + post_result_time: number; + score_breakdown: { + red: AllianceBreakdown; + blue: AllianceBreakdown; + } & MiscBreakdown; + videos: { + type: string; + key: string; + }[]; +} diff --git a/apps/scouting/common/types/Utils.ts b/apps/scouting/common/types/Utils.ts new file mode 100644 index 0000000..3d7101b --- /dev/null +++ b/apps/scouting/common/types/Utils.ts @@ -0,0 +1,13 @@ +// בס"ד + + +type AtMostUnion = + T extends [infer _, ...infer Rest] + ? T | AtMostUnion + : []; + + +export type AtMost = + Result['length'] extends N + ? Result | AtMostUnion + : AtMost; \ No newline at end of file diff --git a/apps/scouting/frontend/vite.config.ts b/apps/scouting/frontend/vite.config.ts index 37eb678..6c693d8 100644 --- a/apps/scouting/frontend/vite.config.ts +++ b/apps/scouting/frontend/vite.config.ts @@ -6,7 +6,12 @@ import tailwindcss from "@tailwindcss/vite"; // https://vite.dev/config/ export default defineConfig({ - server: { port: 80 }, + server: { port: 80, proxy: { + '/api/v1': { + target: "http://localhost:4590", + changeOrigin: true, + } + } }, plugins: [ react({ babel: { diff --git a/eslint.config.mts b/eslint.config.mts index 9693ea8..3239219 100644 --- a/eslint.config.mts +++ b/eslint.config.mts @@ -109,7 +109,7 @@ export default defineConfig([ "no-magic-numbers": "off", "@typescript-eslint/no-magic-numbers": [ "error", - { ignoreTypeIndexes: true }, + { ignoreTypeIndexes: true, ignoreNumericLiteralTypes: true }, ], "@typescript-eslint/no-meaningless-void-operator": "error", "@typescript-eslint/no-misused-new": "error", @@ -154,7 +154,18 @@ export default defineConfig([ "no-unused-expressions": "off", "@typescript-eslint/no-unused-expressions": "warn", "no-unused-vars": "off", - "@typescript-eslint/no-unused-vars": "warn", + "@typescript-eslint/no-unused-vars": [ + "warn", + { + args: "all", + argsIgnorePattern: "^_", + caughtErrors: "all", + caughtErrorsIgnorePattern: "^_", + destructuredArrayIgnorePattern: "^_", + varsIgnorePattern: "^_", + ignoreRestSiblings: true, + }, + ], "no-use-before-define": "off", "@typescript-eslint/no-use-before-define": "error", "no-useless-constructor": "off", diff --git a/package-lock.json b/package-lock.json index 6ccb01c..8bddf38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,12 +12,15 @@ ], "dependencies": { "@tailwindcss/vite": "^4.1.17", + "axios": "^1.13.2", "dotenv": "^17.2.3", "dotenv-cli": "^11.0.0", "eslint-plugin-bsd": "^1.0.0", "eslint-plugin-react": "^7.37.5", "express": "^5.1.0", + "fp-ts": "^2.16.11", "http-status-codes": "^2.3.0", + "io-ts": "^2.2.22", "tailwindcss": "^4.1.17", "tsx": "^4.21.0" }, @@ -1475,6 +1478,12 @@ "node": ">= 0.4" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" + }, "node_modules/available-typed-arrays": { "version": "1.0.7", "license": "MIT", @@ -1488,6 +1497,17 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/axios": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.13.2.tgz", + "integrity": "sha512-VPk9ebNqPcy5lRGuSlKx752IlDatOjT9paPlm8A7yOuW2Fbvp4X3JznJtT4f0GzGLLiWE9W8onz51SqLYwzGaA==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.4", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/babel-plugin-react-compiler": { "version": "19.1.0-rc.3", "dev": true, @@ -1730,6 +1750,18 @@ "version": "1.1.4", "license": "MIT" }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/common": { "resolved": "packages/common", "link": true @@ -1907,6 +1939,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/depd": { "version": "2.0.0", "license": "MIT", @@ -2705,6 +2746,26 @@ "license": "ISC", "peer": true }, + "node_modules/follow-redirects": { + "version": "1.15.11", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz", + "integrity": "sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.5", "license": "MIT", @@ -2718,6 +2779,43 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/form-data": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz", + "integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.2", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/form-data/node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/form-data/node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/forwarded": { "version": "0.2.0", "license": "MIT", @@ -2725,6 +2823,12 @@ "node": ">= 0.6" } }, + "node_modules/fp-ts": { + "version": "2.16.11", + "resolved": "https://registry.npmjs.org/fp-ts/-/fp-ts-2.16.11.tgz", + "integrity": "sha512-LaI+KaX2NFkfn1ZGHoKCmcfv7yrZsC3b8NtWsTVQeHkq4F27vI5igUuO53sxqDEa2gNQMHFPmpojDw/1zmUK7w==", + "license": "MIT" + }, "node_modules/fresh": { "version": "2.0.0", "license": "MIT", @@ -3052,6 +3156,15 @@ "node": ">= 0.4" } }, + "node_modules/io-ts": { + "version": "2.2.22", + "resolved": "https://registry.npmjs.org/io-ts/-/io-ts-2.2.22.tgz", + "integrity": "sha512-FHCCztTkHoV9mdBsHpocLpdTAfh956ZQcIkWQxxS0U5HT53vtrcuYdQneEJKH6xILaLNzXVl2Cvwtoy8XNN0AA==", + "license": "MIT", + "peerDependencies": { + "fp-ts": "^2.5.0" + } + }, "node_modules/ipaddr.js": { "version": "1.9.1", "license": "MIT", @@ -4064,6 +4177,12 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, "node_modules/pstree.remy": { "version": "1.1.8", "dev": true, @@ -5438,6 +5557,15 @@ "packages/common": { "version": "1.0.0", "license": "ISC" + }, + "packages/type-verification": { + "name": "@repo/type-verification", + "version": "0.0.0", + "extraneous": true, + "dependencies": { + "fp-ts": "^2.16.11", + "io-ts": "^2.2.22" + } } } } diff --git a/package.json b/package.json index 35155bd..9b08368 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,15 @@ ], "dependencies": { "@tailwindcss/vite": "^4.1.17", + "axios": "^1.13.2", "dotenv": "^17.2.3", "dotenv-cli": "^11.0.0", "eslint-plugin-bsd": "^1.0.0", "eslint-plugin-react": "^7.37.5", "express": "^5.1.0", + "fp-ts": "^2.16.11", "http-status-codes": "^2.3.0", + "io-ts": "^2.2.22", "tailwindcss": "^4.1.17", "tsx": "^4.21.0" } diff --git a/turbo.json b/turbo.json index 72419fe..20953ef 100644 --- a/turbo.json +++ b/turbo.json @@ -1,8 +1,13 @@ { "$schema": "https://turborepo.com/schema.json", "ui": "tui", - "globalPassThroughEnv": ["FRONTEND_PORT","BACKEND_PORT","NODE_ENV"], - + "globalPassThroughEnv": [ + "FRONTEND_PORT", + "BACKEND_PORT", + "NODE_ENV", + "TBA_API_KEY" + ], + "tasks": { "build": { "dependsOn": ["^build"],