diff --git a/next.config.js b/next.config.js
index 76e1207..f9a17c8 100644
--- a/next.config.js
+++ b/next.config.js
@@ -9,6 +9,7 @@ const config = {
eslint: {
ignoreDuringBuilds: true,
},
+ reactStrictMode: false,
};
export default config;
diff --git a/src/app/_components/game/configurator/game-configurator.tsx b/src/app/_components/game/configurator/game-configurator.tsx
index a68e343..83b804e 100644
--- a/src/app/_components/game/configurator/game-configurator.tsx
+++ b/src/app/_components/game/configurator/game-configurator.tsx
@@ -5,10 +5,26 @@ import { useGameStore } from "@/lib/store/game-store";
import { InfoIcon, XIcon } from "lucide-react";
import GameConfigForm from "./game-config-form";
import GameVariantSelector from "./game-variant-selector";
+import { api } from "@/trpc/react";
+import { useLobbyStore } from "@/lib/store/lobby-store";
function GameConfigurator() {
+ const updateGameConfig = api.gameConfig.update.useMutation();
const selectGame = useGameStore((state) => state.setSelectedGame);
const selectedGame = useGameStore((state) => state.selectedGame);
+ const setGameConfig = useGameStore((state) => state.setGameConfig);
+ const gameConfig = useGameStore((state) => state.gameConfig);
+ const lobbyId = useLobbyStore((state) => state.lobby.id);
+ const handleUnselectGame = () => {
+ selectGame(undefined);
+ updateGameConfig.mutate({
+ lobbyId,
+ config: {
+ ...gameConfig,
+ gameId: undefined,
+ },
+ });
+ };
return (
@@ -39,7 +55,7 @@ function GameConfigurator() {
variant={"ghost"}
size={"icon"}
className="ml-auto"
- onClick={() => selectGame(undefined)}
+ onClick={handleUnselectGame}
>
diff --git a/src/app/_components/game/game-selector.tsx b/src/app/_components/game/game-selector.tsx
index f404334..b81abe4 100644
--- a/src/app/_components/game/game-selector.tsx
+++ b/src/app/_components/game/game-selector.tsx
@@ -1,13 +1,30 @@
import type { IMinigame } from "@/game-engien";
import { gameLibary } from "@/game-engien/games";
import { useGameStore } from "@/lib/store/game-store";
+import { useLobbyStore } from "@/lib/store/lobby-store";
import { cn } from "@/lib/utils";
+import { api } from "@/trpc/react";
import Image from "next/image";
import React from "react";
+import { toast } from "sonner";
-function GameSelector() {
+function GameSelector({ isAdmin }: { isAdmin: boolean }) {
+ const updateGameConfig = api.gameConfig.update.useMutation();
const selectedGame = useGameStore((state) => state.selectedGame);
const setSelectedGame = useGameStore((state) => state.setSelectedGame);
+ const gameConfig = useGameStore((state) => state.gameConfig);
+ const lobbyId = useLobbyStore((state) => state.lobby.id);
+ const handleGameSelect = (game: IMinigame) => {
+ if (!isAdmin) return toast.error("Only admins can change games");
+ setSelectedGame(game);
+ updateGameConfig.mutate({
+ lobbyId,
+ config: {
+ ...gameConfig,
+ gameId: game.id,
+ },
+ });
+ };
return (
{gameLibary.map((game) => (
@@ -15,7 +32,7 @@ function GameSelector() {
key={game.id}
game={game}
selected={selectedGame?.id === game.id}
- onClick={() => setSelectedGame(game)}
+ onClick={() => handleGameSelect(game)}
/>
))}
diff --git a/src/app/_components/lobby/lobby-page.tsx b/src/app/_components/lobby/lobby-page.tsx
index ff70af9..4319d3b 100644
--- a/src/app/_components/lobby/lobby-page.tsx
+++ b/src/app/_components/lobby/lobby-page.tsx
@@ -15,6 +15,7 @@ import { useSessionPlayerStore } from "@/lib/store/current-player-store";
import { useLobbyStore } from "@/lib/store/lobby-store";
import { Share2, UserPlus } from "lucide-react";
import GameConfigurator from "../game/configurator/game-configurator";
+import { useGameStore } from "@/lib/store/game-store";
function LobbyPage({ initialLobby }: { initialLobby: Lobby }) {
const sessionPlayer = useSessionPlayerStore((state) => state.sessionPlayer);
@@ -25,7 +26,7 @@ function LobbyPage({ initialLobby }: { initialLobby: Lobby }) {
: null;
const isAdmin = isJoined?.role === "admin";
-
+ const gameConfig = useGameStore((state) => state.gameConfig);
return (
@@ -98,7 +99,7 @@ function LobbyPage({ initialLobby }: { initialLobby: Lobby }) {
-
+
diff --git a/src/app/_components/lobby/lobby-provider.tsx b/src/app/_components/lobby/lobby-provider.tsx
index 97766cb..ad49403 100644
--- a/src/app/_components/lobby/lobby-provider.tsx
+++ b/src/app/_components/lobby/lobby-provider.tsx
@@ -1,5 +1,7 @@
import { appRoutes } from "@/config/app.routes";
+import { getGame } from "@/game-engien/games";
import { useSessionPlayerStore } from "@/lib/store/current-player-store";
+import { useGameStore } from "@/lib/store/game-store";
import { useLobbyStore } from "@/lib/store/lobby-store";
import type { Lobby, LobbyMember } from "@/server/db/schema";
import { api } from "@/trpc/react";
@@ -22,6 +24,10 @@ function LobbyProvider({
const addMember = useLobbyStore((state) => state.addMember);
const removeMember = useLobbyStore((state) => state.removeMember);
+ const setGameConfig = useGameStore((state) => state.setGameConfig);
+ const setSelectedGame = useGameStore((state) => state.setSelectedGame);
+ const selectedGame = useGameStore((state) => state.selectedGame);
+
const router = useRouter();
const lobbyId = lobby.id ?? "";
@@ -59,7 +65,32 @@ function LobbyProvider({
},
},
);
+
+ api.gameConfig.onUpdate.useSubscription(
+ { lobbyId },
+ {
+ onData({ data: config }) {
+ if (config) {
+ setGameConfig(config);
+ if (config.gameId !== selectedGame?.id) {
+ const game = getGame(config.gameId!);
+ setSelectedGame(game);
+ }
+ }
+ },
+ },
+ );
+
React.useEffect(() => {
+ const setConfigWithGame = () => {
+ const initialConfig = initialLobby?.gameConfig?.config;
+ setGameConfig(initialConfig ?? {});
+ const game = initialConfig?.gameId?.length
+ ? getGame(initialConfig?.gameId!)
+ : undefined;
+ setSelectedGame(game);
+ };
+ if (initialLobby?.gameConfig?.config) setConfigWithGame();
resetLobby(initialLobby);
setMembers(initialLobby?.members ?? []);
}, [initialLobby]);
diff --git a/src/game-engien/games/index.ts b/src/game-engien/games/index.ts
index f40d651..bfcf662 100644
--- a/src/game-engien/games/index.ts
+++ b/src/game-engien/games/index.ts
@@ -1,5 +1,9 @@
import type { IMinigame } from "..";
+export const getGame = (id: string) => {
+ return gameLibary.find((g) => g.id === id);
+};
+
export const gameLibary: Array