diff --git a/app/(main)/(routes)/matches/(components)/MatchCard/index.tsx b/app/(main)/(routes)/matches/(components)/MatchCard/index.tsx index 9a58cd4..734e665 100644 --- a/app/(main)/(routes)/matches/(components)/MatchCard/index.tsx +++ b/app/(main)/(routes)/matches/(components)/MatchCard/index.tsx @@ -1,32 +1,26 @@ import { Avatar, AvatarGroup } from "@/components/Avatar"; import { CardBody, CardHeader } from "@/components/Card"; import { Divider } from "@/components/Divider"; -import { serverServices } from "@/lib/services/server"; +import { Match } from "@/lib/type"; import { dayjs } from "@/lib/utils/date"; import { NavigationCard } from "./(components)/Card"; -export async function MatchCard({ - matchId, - date, -}: { - matchId: string; - date: string; -}) { - const { getMatch } = serverServices(); - const [match] = await Promise.all([getMatch({ matchId })]); - +export function MatchCard({ match, userId }: { match: Match; userId: string }) { + const { createdAt } = match; const today = dayjs(); - const targetDate = dayjs(date); + const targetDate = dayjs(createdAt); const isSameYear = today.isSame(targetDate, "year"); const displayDate = isSameYear - ? dayjs(date).format("M/D") - : dayjs(date).format("YYYY/M/D"); + ? dayjs(createdAt).format("M/D") + : dayjs(createdAt).format("YYYY/M/D"); + + const data = match.players.find((player) => player.id === userId)!; return ( - +
-

{displayDate}

+

{displayDate}

{match.players.map((player) => ( @@ -36,12 +30,36 @@ export async function MatchCard({ - {/*
- -
*/} +
+
+
平均着順
+
+ {data.averageRank ?? "なし"} +
+
+
+ + + + + + + {match.rule.playersCount === 4 && } + + + + + + + + {match.rule.playersCount === 4 && ( + + )} + + +
1位2位3位4位
{data.rankCounts[0]}{data.rankCounts[1]}{data.rankCounts[2]}{data.rankCounts[3]}
+
+
); diff --git a/app/(main)/(routes)/matches/page.tsx b/app/(main)/(routes)/matches/page.tsx index 2817e5b..f631c3c 100644 --- a/app/(main)/(routes)/matches/page.tsx +++ b/app/(main)/(routes)/matches/page.tsx @@ -3,11 +3,10 @@ import { CreateMatchButton } from "./(components)/CreateMatchButton"; import { MatchCard } from "./(components)/MatchCard"; export default async function Matches() { - const { getMatches } = serverServices(); + const { getMatches, getUser } = serverServices(); // TODO: infinite scroll - const matches = await getMatches({}); - + const [matches, user] = await Promise.all([getMatches({}), getUser()]); return (

成績表

@@ -21,7 +20,7 @@ export default async function Matches() {
    {matches?.map((match) => (
  • - +
  • ))}
diff --git a/lib/services/features/match.ts b/lib/services/features/match.ts index fb035f0..81ed5c3 100644 --- a/lib/services/features/match.ts +++ b/lib/services/features/match.ts @@ -250,8 +250,8 @@ function formatMatch(match: { id: profiles.id, name: profiles.name, janrecoId: profiles.janreco_id, - rankCounts: new Array(match.match_players.length).fill(0), - averageRank: 0, + rankCounts: new Array(rule.players_count).fill(0), + averageRank: null, totalScore: 0, chipCount: chip_count, result: 0, @@ -268,11 +268,16 @@ function formatMatch(match: { }); players.forEach((player) => { - player.averageRank = - player.rankCounts.reduce( - (acc, rankCount, rank) => acc + rankCount * (rank + 1), - 0, - ) / match.games.length; + if (player.rankCounts.reduce((acc, cur) => acc + cur, 0) > 0) { + player.averageRank = Number( + ( + player.rankCounts.reduce( + (acc, cur, index) => acc + cur * (index + 1), + 0, + ) / player.rankCounts.reduce((acc, cur) => acc + cur, 0) + ).toFixed(2), + ); + } player.result = (player.chipCount ?? 0) * rule.chip_rate + player.totalScore * rule.rate; }); diff --git a/lib/services/features/profile.ts b/lib/services/features/profile.ts index df5f77d..5450e1c 100644 --- a/lib/services/features/profile.ts +++ b/lib/services/features/profile.ts @@ -1,9 +1,16 @@ import { PostgrestError } from "@supabase/supabase-js"; -import { Profile } from "@/lib/type"; +import { Profile, User } from "@/lib/type"; import { Supabase } from "."; export function profileService(supabase: Supabase) { return { + async getUser(): Promise { + const userResponse = await supabase.auth.getUser(); + if (userResponse.error) throw userResponse.error; + const user = userResponse.data.user; + return user; + }, + async getUserProfile(): Promise { const userResponse = await supabase.auth.getUser(); if (userResponse.error) throw userResponse.error; diff --git a/lib/type.ts b/lib/type.ts index 6facaf2..bcff8a7 100644 --- a/lib/type.ts +++ b/lib/type.ts @@ -1,5 +1,8 @@ +import { User as SupabaseUser } from "@supabase/supabase-js"; import { calcMethods, chipRates, rates } from "./config"; +export type User = SupabaseUser; + export type Profile = { id: string; name: string | null; @@ -19,7 +22,7 @@ export type Match = { export type MatchPlayer = Profile & { rankCounts: number[]; - averageRank: number; + averageRank: number | null; totalScore: number; chipCount: number | null; result: number;