Skip to content

Commit

Permalink
Initial implementation of getting match assignments from supabase
Browse files Browse the repository at this point in the history
mock data (and mock events) are used. Really bad code. Please clean up for me

Co-Authored-By: Amaar Chughtai <[email protected]>
  • Loading branch information
ThatXliner and xDragonDev committed Feb 20, 2024
1 parent ed581e1 commit 04288b2
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 9 deletions.
53 changes: 47 additions & 6 deletions apps/expo/src/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function SelectDemoItem(props: SelectProps) {
);
}
interface MatchScoutAssignment {
alliance: "red" | "blue";
// alliance: "red" | "blue";
team: number;
red: [number, number, number];
blue: [number, number, number];
Expand Down Expand Up @@ -342,14 +342,55 @@ function MatchScoutAssignment({
// </KeyboardAvoidingView>
// );
// }

// import matchScoutAssignments from "../../../../packages/api/src/TBA/fetchMatches";
export default function HomeScreen() {
const utils = api.useUtils();
const { data: matches } = api.scouting.matchesInEvent.useQuery({
event: "test",
});
console.log(matches);
// VERY BAD CODE
const teams = {
red: matches?.filter((x) => x.alliance.startsWith("red")) ?? [],
blue: matches?.filter((x) => x.alliance.startsWith("blue")) ?? [],
};
console.log(teams);
function getUniqueTeamsAcrossAlliances() {
const uniqueTeams = new Set<number>();
matches?.forEach((x) => {
uniqueTeams.add(parseInt(x.teamNum));
});
return Array.from(uniqueTeams);
}

// const { data: posts } = api.post.all.useQuery();
const exampleMatchScoutAssignments: MatchScoutAssignment[] = [
{ alliance: "red", team: 5, red: [1, 2, 3], blue: [4, 5, 6] },
];
const matchScoutAssignments: MatchScoutAssignment[] =
getUniqueTeamsAcrossAlliances().map((x) => {
return {
team: x,
red: teams.red.map((x) => x.teamNum) as unknown as [
number,
number,
number,
], //.filter((y) => y.teamNum === x).map((y) => y.matchNum),
blue: teams.blue.map((x) => x.teamNum) as unknown as [
number,
number,
number,
], //.filter((y) => y.teamNum === x).map((y) => y.matchNum),
};
}); //
// matches.map((x) => {
// return {
// alliance: x.alliance.slice(0, -1),
// team: parseInt(x.teamNum),
// red,
// };
// });
// // [
// { alliance: "red", team: 5, red: [9, 2, 3], blue: [4, 5, 6] },
// { alliance: "red", team: 5, red: [8, 2, 3], blue: [4, 5, 6] },
// ];

return (
<SafeAreaView className="bg-zinc-900">
Expand Down Expand Up @@ -381,7 +422,7 @@ export default function HomeScreen() {
</Pressable>
{/* <SelectDemoItem /> */}
<FlashList
data={exampleMatchScoutAssignments}
data={matchScoutAssignments}
estimatedItemSize={20}
ItemSeparatorComponent={() => <View className="h-2" />}
renderItem={(p) => <MatchScoutAssignment assignment={p.item} />}
Expand Down
10 changes: 7 additions & 3 deletions apps/expo/src/app/match/Teleop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import ActionGrid, { History } from "./components/ActionGrid";
import { useEffect, useState } from "react";
import type { UltimateHistory } from "./types";

export default function Teleop({ setUltimateHistory }: { setUltimateHistory: (history: UltimateHistory) => void }}) {
export default function Teleop({
setUltimateHistory,
}: {
setUltimateHistory: (history: UltimateHistory) => void;
}) {
const [history, setHistory] = useState<History>([]);
const intakeActions = [
"Human",
Expand All @@ -20,8 +24,8 @@ export default function Teleop({ setUltimateHistory }: { setUltimateHistory: (hi
];
const shooterActions = ["Speaker", "Amp", "Miss S📢", "Miss Amp"];
useEffect(() => {
setUltimateHistory({ log: history })
}, [history])
setUltimateHistory({ log: history });
}, [history]);
return (
<View className="mt-5">
<Text className="pl-3 text-lg">Intake</Text>
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/root.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { attendanceRouter } from "./router/attendance";
import { authRouter } from "./router/auth";
import { postRouter } from "./router/post";
import { scoutingRouter } from "./router/scouting";
import { createTRPCRouter } from "./trpc";

export const appRouter = createTRPCRouter({
auth: authRouter,
post: postRouter,
attendance: attendanceRouter,
scouting: scoutingRouter,
});

// export type definition of API
Expand Down
99 changes: 99 additions & 0 deletions packages/api/src/router/scouting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { TRPCError } from "@trpc/server";
import { nanoid } from "nanoid";
import { z } from "zod";

import { desc, eq, schema } from "@acme/db";
import { matches } from "@acme/db/schema";
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";

export const scoutingRouter = createTRPCRouter({
// createMatch: publicProcedure.input(z.object({id:z.string({})}))
// updateMatch: publicProcedure.input()
matchesInEvent: publicProcedure
.input(z.object({ event: z.string() }))
.query(({ ctx, input }) => {
return ctx.db
.select()
.from(matches)
.where(eq(matches.eventId, input.event));
}),
all: publicProcedure.query(({ ctx }) => {
return ctx.db.query.post.findMany({
with: { author: true },
orderBy: desc(schema.post.id),
limit: 10,
});
}),

byId: publicProcedure
.input(z.object({ id: z.string() }))
.query(({ ctx, input }) => {
return ctx.db.query.post.findFirst({
with: { author: true },
where: eq(schema.post.id, input.id),
});
}),

create: protectedProcedure
.input(
z.object({
title: z.string().min(1),
content: z.string().min(1),
}),
)
.mutation(async ({ ctx, input }) => {
function getNameFromUser() {
const meta = ctx.user.user_metadata;
if (typeof meta.name === "string") return meta.name;
if (typeof meta.full_name === "string") return meta.full_name;
if (typeof meta.user_name === "string") return meta.user_name;
return "[redacted]";
}

const authorId = await ctx.db.query.profile
.findFirst({
where: eq(schema.profile.id, ctx.user.id),
})
.then(async (profile) => {
if (profile) return profile.id;
const [newProfile] = await ctx.db
.insert(schema.profile)
.values({
id: ctx.user.id,
name: getNameFromUser(),
image: ctx.user.user_metadata.avatar_url as string | undefined,
email: ctx.user.email,
isLead: false,
isMentor: false,
role: "default",
})
.returning();

return newProfile!.id;
});

return ctx.db.insert(schema.post).values({
id: nanoid(),
authorId,
title: input.title,
content: input.content,
});
}),

delete: protectedProcedure
.input(z.string())
.mutation(async ({ ctx, input }) => {
const post = await ctx.db.query.post.findFirst({
where: eq(schema.post.id, input),
});

if (post?.authorId !== ctx.user.id) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Only the author is allowed to delete the post",
});
}

return ctx.db.delete(schema.post).where(eq(schema.post.id, input));
}),
});

0 comments on commit 04288b2

Please sign in to comment.