Skip to content

Commit

Permalink
# Lobby-Connections
Browse files Browse the repository at this point in the history
- Displaying lobbies that are joinable
- being able to click and join a lobby and then a game
  • Loading branch information
CheBato committed Nov 30, 2024
1 parent 042620b commit 2ac2133
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
3 changes: 0 additions & 3 deletions src/app/_components/HomePage/PublicGames/PublicGames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ const PublicGames: React.FC<PublicGamesProps> = ({ format }) => {
<Typography variant="h3">{format}</Typography>
<Divider sx={styles.divider} />
<JoinableGame />
<JoinableGame />
<JoinableGame />
<Typography variant="h3">
Request-Undo {format}
</Typography>
<Divider sx={styles.divider} />
<JoinableGame />
<GamesInProgress />
</CardContent>
</Card>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
import React from "react";
import React, { useEffect, useState} from "react";
import { Box, Button, Typography } from "@mui/material";
import { usePathname, useRouter } from "next/navigation";
interface Lobby {
id: string;
name: string;
}

const JoinableGame: React.FC = () => {
const randomGameId = Math.floor(Math.random() * 10000);
//const randomGameId = Math.floor(Math.random() * 10000);
const router = useRouter();
const [lobbies, setLobbies] = useState<Lobby[]>([]);
useEffect(() => {
// Fetch unfilled lobbies from the server
const fetchLobbies = async () => {
try {
const response = await fetch('http://localhost:9500/api/unfilled-lobbies');
if (!response.ok) {
throw new Error(`Error fetching lobbies: ${response.statusText}`);
}
const data: Lobby[] = await response.json();
setLobbies(data);
} catch (error) {
console.error('Error fetching lobbies:', error);
}
};

fetchLobbies();
}, []);

const joinLobby = async (lobbyId: string) => {
try {
const response = await fetch('http://localhost:9500/api/join-lobby', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ lobbyId, userId: 'ThisIsTheWay' }),
});

if (!response.ok) {
const errorData = await response.json();
console.error('Error joining lobby:', errorData.message);
alert(errorData.message);
return;
}
alert('Successfully joined the lobby');
router.push("/lobby?player=ThisIsTheWay");
} catch (error) {
console.error('Error joining lobby:', error);
}
};

//------------------------STYLES------------------------//

Expand All @@ -21,10 +68,14 @@ const JoinableGame: React.FC = () => {
};

return (
<Box sx={styles.box}>
<Typography variant="body1" sx={styles.matchType}>Game #{randomGameId}</Typography>
<Button>Join Game</Button>
</Box>
<>
{lobbies.map((lobby) => (
<Box sx={styles.box} key={lobby.id}>
<Typography variant="body1" sx={styles.matchType}>{lobby.name}</Typography>
<Button onClick={() => joinLobby(lobby.id)}>Join Game</Button>
</Box>
))}
</>
);
};

Expand Down
11 changes: 9 additions & 2 deletions src/app/_components/Lobby/SetUp/SetUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Chat from "@/app/_components/_sharedcomponents/Chat/Chat";
import GameLinkCard from "../_subcomponents/GameLinkCard/GameLinkCard";
import { useGame } from "@/app/_contexts/Game.context";
import { SetUpProps } from "../LobbyTypes";
import { useRouter } from "next/navigation"
import { useRouter, useSearchParams } from "next/navigation"

const SetUp: React.FC<SetUpProps> = ({
chatHistory,
Expand All @@ -19,11 +19,18 @@ const SetUp: React.FC<SetUpProps> = ({

const { sendMessage } = useGame();
const router = useRouter();
const searchParams = useSearchParams();

// Extract the player from the URL query params
const player = searchParams.get("player");

const handleStartGame = () => {
sendMessage("startGame");
router.push("/GameBoard");
if (player){
router.push("/GameBoard?player=" + player);
}else {
router.push("/GameBoard");
}
}

//------------------------STYLES------------------------//
Expand Down

0 comments on commit 2ac2133

Please sign in to comment.