diff --git a/src/components/Lobby.tsx b/src/components/Lobby.tsx index 2161303..3c619d4 100644 --- a/src/components/Lobby.tsx +++ b/src/components/Lobby.tsx @@ -74,7 +74,7 @@ const Lobby: React.FC = () => { await host.leave(account, game_id); } - set_game_id(0); + set_game_id(undefined); set_game_state(GameState.MainMenu); } catch (error: any) { toast({ @@ -86,7 +86,9 @@ const Lobby: React.FC = () => { const kickPlayer = async (player_index: number) => { try { - await host.kick(account, game_id, player_index); + if (game_id) { + await host.kick(account, game_id, player_index); + } } catch (error: any) { toast({ variant: 'destructive', @@ -97,7 +99,9 @@ const Lobby: React.FC = () => { const transferHost = async (player_index: number) => { try { - await host.transfer(account, game_id, player_index); + if (game_id) { + await host.transfer(account, game_id, player_index); + } } catch (error: any) { toast({ variant: 'destructive', diff --git a/src/components/MainMenu.tsx b/src/components/MainMenu.tsx index 8b0f70b..d15caf9 100644 --- a/src/components/MainMenu.tsx +++ b/src/components/MainMenu.tsx @@ -26,14 +26,21 @@ const MainMenu: React.FC = () => { // if player is host of a game, go to the lobby useEffect(() => { + console.log(game); + console.log(player); + console.log(account.address); + + if ((game && game.over == 1) || (player && player.rank != 0)) { + return; + } if (player) { set_game_id(player.game_id); set_game_state(GameState.Lobby); - } else if (game) { + } else if (game && game.over != 1) { set_game_id(game.id); set_game_state(GameState.Lobby); } - }, [game, player]); + }, [game, player, account]); const createNewGame = async () => { if (!player_name) { diff --git a/src/components/OverlayEndGame.tsx b/src/components/OverlayEndGame.tsx index f489972..77dab33 100644 --- a/src/components/OverlayEndGame.tsx +++ b/src/components/OverlayEndGame.tsx @@ -2,6 +2,9 @@ import { Medal, Trophy } from 'lucide-react'; import React, { useState } from 'react'; import { avatars } from '../utils/pfps'; import { Button } from './ui/button'; +import { useElementStore } from '@/utils/store'; +import GameState from '@/utils/gamestate'; +import { useMe } from '@/hooks/useMe'; interface OverlayEndGameProps { players: any; @@ -11,7 +14,8 @@ interface OverlayEndGameProps { const OverlayEndGame: React.FC = ({ me, players }) => { const text = 'Game Over'; const [showOverlay, setShowOverlay] = useState(true); - + const { set_game_state, set_game_id } = useElementStore((state) => state); + const { setMe } = useMe(); const getColorRGB = (colorName: string) => { switch (colorName) { case 'bronze': @@ -25,6 +29,13 @@ const OverlayEndGame: React.FC = ({ me, players }) => { setShowOverlay(false); }; + const handleGoMenu = () => { + set_game_id(undefined); + set_game_state(GameState.MainMenu); + // setMe(undefined); + setShowOverlay(false); + }; + return ( <> {showOverlay && ( @@ -66,6 +77,9 @@ const OverlayEndGame: React.FC = ({ me, players }) => { ))} + )} diff --git a/src/components/map/Map.tsx b/src/components/map/Map.tsx index c6a3269..251a11f 100644 --- a/src/components/map/Map.tsx +++ b/src/components/map/Map.tsx @@ -44,7 +44,7 @@ const Map = () => { useElementStore((state) => state); const surrender = async () => { - if (game_id) await play.surrender(account, game_id); + if (game_id !== undefined && game_id !== null) await play.surrender(account, game_id); }; const { setShowTuto } = useTutorial(); diff --git a/src/hooks/useMe.ts b/src/hooks/useMe.ts index 448e967..3d7a03f 100644 --- a/src/hooks/useMe.ts +++ b/src/hooks/useMe.ts @@ -4,7 +4,7 @@ import { useDojo } from '@/dojo/useDojo'; import { useTurn } from './useTurn'; import { Player } from '@/utils/types'; -export function useMe(): { me: Player | null; isItMyTurn: boolean } { +export function useMe(): { me: Player | null; isItMyTurn: boolean; setMe: any } { const { setup: { account: { account }, @@ -17,7 +17,7 @@ export function useMe(): { me: Player | null; isItMyTurn: boolean } { const { players } = useGetPlayers(); useEffect(() => { - if (players.length > 0 && account.address) { + if (players && players.length > 0 && account.address) { const me = players.find((p) => p.address === account.address); if (!me) return; setMe(me); @@ -27,5 +27,6 @@ export function useMe(): { me: Player | null; isItMyTurn: boolean } { return { me, isItMyTurn: me?.index === turn, + setMe, }; } diff --git a/src/utils/store.ts b/src/utils/store.ts index c9fa5b0..a9defe9 100644 --- a/src/utils/store.ts +++ b/src/utils/store.ts @@ -10,7 +10,7 @@ export enum Phase { interface State { game_id: number | undefined; - set_game_id: (game_id: number) => void; + set_game_id: (game_id: number | undefined) => void; game_state: GameState; set_game_state: (game_state: GameState) => void; current_source: number | null; @@ -34,8 +34,8 @@ interface State { } export const useElementStore = create((set) => ({ - game_id: 0, - set_game_id: (game_id: number) => set(() => ({ game_id })), + game_id: undefined, + set_game_id: (game_id: number | undefined) => set(() => ({ game_id })), game_state: GameState.MainMenu, set_game_state: (game_state: GameState) => set(() => ({ game_state })), current_source: null,