Skip to content

Commit 74983b8

Browse files
committed
finish tzar board and add public board
1 parent 86da3d4 commit 74983b8

File tree

6 files changed

+353
-44
lines changed

6 files changed

+353
-44
lines changed

next.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {}
2+
const nextConfig = {
3+
reactStrictMode: false,
4+
};
35

4-
module.exports = nextConfig
6+
module.exports = nextConfig;

src/app/globals.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
}
7777

7878
main {
79-
@apply max-w-6xl mx-auto my-3;
79+
@apply max-w-7xl mx-auto my-3;
8080
}
8181

8282
p {

src/components/game/game-board.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
"use client";
22

3-
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
43
import { useEffect, useOptimistic, useState } from "react";
54
import { Card, CardHeader, CardTitle } from "../ui/card";
65
import { cn } from "@/lib/utils";
7-
import { useToast } from "../ui/use-toast";
86
import { Button } from "../ui/button";
97
import { playWhiteCard } from "@/actions/game/actions";
8+
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
9+
import { useRouter } from "next/navigation";
1010

1111
export default function GameBoard({
12-
rounds,
1312
game,
1413
connectedUser,
1514
blackCard,
1615
whiteCards,
1716
playedCards,
18-
users,
17+
round,
18+
roundUsers,
1919
}: {
20-
rounds: Round[];
20+
round: Round;
2121
game: Game;
2222
connectedUser: string;
2323
blackCard: Card;
2424
whiteCards: { user_id: string; cards: Card[] }[];
2525
playedCards: Card[];
2626
users: User[];
27+
roundUsers: RoundUser[];
2728
}) {
28-
const supabase = createClientComponentClient<Database>();
29+
const supabase = createClientComponentClient();
2930
const [selectedCard, setSelectedCard] = useState<Card[]>(playedCards);
3031
const cards = whiteCards.filter(uCard => uCard.user_id === connectedUser)[0].cards;
3132
const enoughCardSelected = selectedCard.length === blackCard.pick;
32-
const { toast } = useToast();
33+
const [URoundUsers, setRoundUsers] = useState<RoundUser[]>(roundUsers);
34+
const router = useRouter();
3335

3436
const [optimisticPlayedCard, updateOptimisticPlayedCard] = useOptimistic<Card[], Card[]>(
3537
playedCards,
@@ -47,26 +49,23 @@ export default function GameBoard({
4749
event: "UPDATE",
4850
schema: "public",
4951
table: "rounds_users",
50-
filter: `round_id=eq.${rounds.at(-1)?.id}`,
52+
filter: `round_id=eq.${round.id}`,
5153
},
5254
payload => {
5355
console.log(payload);
54-
console.log("as played", payload.new.has_played);
55-
console.log("is me", payload.new.user_id !== connectedUser); // TODO: not working
56-
if (payload.new.has_played && payload.new.user_id !== connectedUser) {
57-
toast({
58-
title: `${users.find(user => user.id === payload.new.user_id)?.username} just played!`,
59-
description: `X player left!`,
60-
});
61-
}
56+
const updatedUserRound = URoundUsers.map(ru => (ru.id === payload.new.id ? payload.new.id : ru));
57+
setRoundUsers(updatedUserRound);
6258
}
6359
)
6460
.subscribe();
6561

62+
if (URoundUsers.every(roundUser => roundUser.has_played)) {
63+
router.refresh(); // update the view to be able to select the cards
64+
}
6665
return () => {
6766
supabase.removeChannel(channel);
6867
};
69-
}, [supabase, rounds, toast, connectedUser, users]);
68+
}, [supabase, round.id, URoundUsers, router]);
7069

7170
const handleSelectedCard = (card: Card) => {
7271
setSelectedCard(prev => {
@@ -77,13 +76,12 @@ export default function GameBoard({
7776
});
7877
};
7978

80-
if (!rounds || rounds.length === 0) return <div>no rounds</div>;
8179
if (blackCard.pick === null) return <div>error blackcard mal formated... {blackCard.id}</div>;
8280

8381
return (
8482
<main className="flex flex-col h-[90vh]">
8583
<div id="black_card" className="mx-auto">
86-
{blackCard && <BlackCard cardContent={blackCard} />}
84+
<BlackCard cardContent={blackCard} />
8785
</div>
8886
<div id="game_data" className="flex-grow justify-center items-center flex"></div>
8987
<div id="hand" className="relative">

src/components/game/game-lobby.tsx

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import WaitingRoom from "./waiting-room";
22
import GameBoard from "./game-board";
33
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
44
import { cookies } from "next/headers";
5+
import TzarBoard from "./tzar-board";
6+
import TzarBoardPublic from "./tzar-board-public";
57

68
export default async function GameLobby({
79
game,
@@ -20,10 +22,21 @@ export default async function GameLobby({
2022
const isGameInProgress = rounds !== null && rounds.length > 0;
2123

2224
if (isGameInProgress) {
25+
const currentRound = rounds.at(-1)!;
26+
27+
const { data: round_users } = await supabase.from("rounds_users").select();
28+
29+
if (!round_users) {
30+
return <div>error: No User not found in current round</div>;
31+
}
32+
33+
const isRoundFinished = round_users.filter(ru => !ru.is_tzar).every(round_user => round_user.has_played); // TODO: add timer too
34+
const round_user = round_users.find(ru => ru.user_id === connectedUser)!;
35+
2336
const { data: blackCard } = await supabase
2437
.from("cards")
2538
.select()
26-
.match({ id: rounds.at(-1)?.black_card })
39+
.match({ id: currentRound.black_card })
2740
.single();
2841

2942
if (!blackCard) {
@@ -64,40 +77,72 @@ export default async function GameLobby({
6477
};
6578
});
6679

67-
const { data: round_user_id } = await supabase
68-
.from("rounds_users")
80+
let playedCards: Card[] = [];
81+
const { data: round_users_cards } = await supabase
82+
.from("rounds_users_cards")
6983
.select()
70-
.match({ user_id: connectedUser })
71-
.single();
84+
.in(
85+
"rounds_user_id",
86+
round_users.map(ru => ru.id)
87+
);
7288

73-
let playedCards: Card[] = [];
74-
if (round_user_id !== null) {
75-
const { data: playedCardIds, error } = await supabase
76-
.from("rounds_users_cards")
89+
if (round_users_cards !== null) {
90+
const { data } = await supabase
91+
.from("cards")
92+
.select()
93+
.in(
94+
"id",
95+
round_users_cards.map(ruc => ruc.card_id)
96+
);
97+
playedCards = data ?? [];
98+
}
99+
100+
// TODO: make sure we don't make non necessary request when we go there
101+
if (isRoundFinished) {
102+
return (
103+
<TzarBoardPublic
104+
playedCards={playedCards}
105+
users={users}
106+
roundUsers={round_users}
107+
round={currentRound}
108+
blackCard={blackCard}
109+
roundUsersCards={round_users_cards ?? []}
110+
isTzar={round_user.is_tzar}
111+
connectedUser={connectedUser}
112+
/>
113+
);
114+
}
115+
116+
if (round_user.is_tzar) {
117+
return <TzarBoard users={users} roundUsers={round_users} round={currentRound} blackCard={blackCard} />;
118+
}
119+
120+
const { data: userPlayedCardIds } = await supabase
121+
.from("rounds_users_cards")
122+
.select()
123+
.match({ rounds_user_id: round_user.id });
124+
125+
if (userPlayedCardIds !== null) {
126+
const { data } = await supabase
127+
.from("cards")
77128
.select()
78-
.match({ rounds_user_id: round_user_id.id });
79-
80-
if (playedCardIds !== null) {
81-
const { data } = await supabase
82-
.from("cards")
83-
.select()
84-
.in(
85-
"id",
86-
playedCardIds.map(card => card.card_id)
87-
);
88-
playedCards = data ?? [];
89-
}
129+
.in(
130+
"id",
131+
userPlayedCardIds.map(card => card.card_id)
132+
);
133+
playedCards = data ?? [];
90134
}
91135

92136
return (
93137
<GameBoard
94-
rounds={rounds}
138+
round={currentRound}
95139
game={game}
96140
blackCard={blackCard}
97141
whiteCards={whiteCards}
98142
connectedUser={connectedUser}
99143
playedCards={playedCards}
100144
users={users}
145+
roundUsers={round_users}
101146
/>
102147
);
103148
}

0 commit comments

Comments
 (0)