Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip GoBackMenu #67

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/Lobby.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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',
Expand All @@ -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',
Expand Down
11 changes: 9 additions & 2 deletions src/components/MainMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion src/components/OverlayEndGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,7 +14,8 @@ interface OverlayEndGameProps {
const OverlayEndGame: React.FC<OverlayEndGameProps> = ({ 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':
Expand All @@ -25,6 +29,13 @@ const OverlayEndGame: React.FC<OverlayEndGameProps> = ({ me, players }) => {
setShowOverlay(false);
};

const handleGoMenu = () => {
set_game_id(undefined);
set_game_state(GameState.MainMenu);
// setMe(undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce que le setMe est utilisé autre part ? On dirait que comme cette ligne est commenté il n'est pas utilisé?

setShowOverlay(false);
};

return (
<>
{showOverlay && (
Expand Down Expand Up @@ -66,6 +77,9 @@ const OverlayEndGame: React.FC<OverlayEndGameProps> = ({ me, players }) => {
</div>
))}
</div>
<Button className="mt-4" onClick={handleGoMenu}>
Back to lobby
</Button>
</div>
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useMe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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);
Expand All @@ -27,5 +27,6 @@ export function useMe(): { me: Player | null; isItMyTurn: boolean } {
return {
me,
isItMyTurn: me?.index === turn,
setMe,
};
}
6 changes: 3 additions & 3 deletions src/utils/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,8 +34,8 @@ interface State {
}

export const useElementStore = create<State>((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,
Expand Down