From 3913eea1a94a7ea13012779ff2f472e50c84cdc9 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Tue, 8 Feb 2022 09:29:20 +0200 Subject: [PATCH] Fetch match results --- app/models/LFGMatch.server.ts | 4 ++ app/routes/play/match.$id.tsx | 83 ++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/app/models/LFGMatch.server.ts b/app/models/LFGMatch.server.ts index e3835ce5f..0a2c0b681 100644 --- a/app/models/LFGMatch.server.ts +++ b/app/models/LFGMatch.server.ts @@ -12,6 +12,10 @@ export function findById(id: string) { mode: true, }, }, + winnerGroupId: true, + }, + orderBy: { + order: "asc", }, }, groups: { include: { members: { include: { user: true } } } }, diff --git a/app/routes/play/match.$id.tsx b/app/routes/play/match.$id.tsx index 3a2ca68c3..08b7c6814 100644 --- a/app/routes/play/match.$id.tsx +++ b/app/routes/play/match.$id.tsx @@ -107,7 +107,11 @@ interface LFGMatchLoaderData { mapList: { name: string; mode: Mode; + /** Did 0 index group or 1 index group take this map */ + winner?: number; }[]; + /** The final score. Shown if match is concluded */ + score?: [number, number]; } export const loader: LoaderFunction = async ({ params, context }) => { @@ -131,30 +135,55 @@ export const loader: LoaderFunction = async ({ params, context }) => { const isCaptain = match.groups.some((g) => g.members.some((m) => m.user.id === user?.id && m.captain) ); + const groups = match.groups + .sort((a, b) => { + const aIsOwnGroup = a.members.some((m) => user?.id === m.user.id); + const bIsOwnGroup = b.members.some((m) => user?.id === m.user.id); + + return Number(bIsOwnGroup) - Number(aIsOwnGroup); + }) + .map((g) => { + return { + id: g.id, + members: g.members.map((g) => ({ + id: g.user.id, + discordId: g.user.discordId, + discordAvatar: g.user.discordAvatar, + discordName: g.user.discordName, + discordDiscriminator: g.user.discordDiscriminator, + })), + }; + }); + const score = match.stages[0].winnerGroupId + ? match.stages.reduce( + (acc: [number, number], stage) => { + if (!stage.winnerGroupId) return acc; + if (stage.winnerGroupId === groups[0].id) acc[0]++; + else acc[1]++; + return acc; + }, + [0, 0] + ) + : undefined; return json({ isCaptain, isRanked, isOwnMatch, - groups: match.groups - .sort((a, b) => { - const aIsOwnGroup = a.members.some((m) => user?.id === m.user.id); - const bIsOwnGroup = b.members.some((m) => user?.id === m.user.id); + groups, + score, + mapList: match.stages + .map(({ stage, winnerGroupId }) => { + const winner = () => { + if (!winnerGroupId) return undefined; - return Number(bIsOwnGroup) - Number(aIsOwnGroup); - }) - .map((g) => { - return { - id: g.id, - members: g.members.map((g) => ({ - id: g.user.id, - discordId: g.user.discordId, - discordAvatar: g.user.discordAvatar, - discordName: g.user.discordName, - discordDiscriminator: g.user.discordDiscriminator, - })), + return groups[0].id === winnerGroupId ? 0 : 1; }; - }), - mapList: match.stages.map(({ stage }) => stage), + return { + ...stage, + winner: winner(), + }; + }) + .filter((stage) => !score || typeof stage.winner === "number"), }); }; @@ -210,14 +239,16 @@ export default function LFGMatchPage() { )} - + {!data.score && ( + + )} ); }