-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/room matchmaking logic #47
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
base: main
Are you sure you want to change the base?
Changes from all commits
de519ff
9bd845f
c1707a3
8b64149
9df653e
0593066
486f48a
edf2cbd
d0afe44
5f4f9e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||
| import { useEffect, useState } from "react"; | ||||||
| import { Navigate, useNavigate } from "react-router-dom"; | ||||||
|
|
||||||
| import { useRoom } from "../lib/use-room"; | ||||||
|
|
||||||
| export function Lobby() { | ||||||
| const { room, isConnected } = useRoom(); | ||||||
| const navigate = useNavigate(); | ||||||
| const [players, setPlayers] = useState<any[]>([]); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (!room) return; | ||||||
|
|
||||||
| const handleStateChange = (state: any) => { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mamy taki typ
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trzeba stworzyć, bo niestety nie mamy |
||||||
| if (state.playerState?.players) { | ||||||
| setPlayers(Array.from(state.playerState.players.values())); | ||||||
| } | ||||||
|
|
||||||
| if (state.gameStarted) { | ||||||
| navigate("/game"); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| room.onStateChange(handleStateChange); | ||||||
|
|
||||||
| handleStateChange(room.state); | ||||||
|
|
||||||
| return () => { | ||||||
| room.onStateChange.remove(handleStateChange); | ||||||
| }; | ||||||
| }, [room, navigate]); | ||||||
|
|
||||||
| if (!isConnected || !room) return <Navigate to="/start" replace={true} />; | ||||||
|
|
||||||
| const myPlayer = players.find((p) => p.sessionId === room.sessionId); | ||||||
|
|
||||||
| return ( | ||||||
| <div> | ||||||
| <h2>Pokój: {room.roomId}</h2> | ||||||
|
|
||||||
| <ul> | ||||||
| {players.map((p) => ( | ||||||
| <li key={p.sessionId} style={{ margin: "10px 0", fontSize: "20px" }}> | ||||||
| <strong>{p.name || "Anonim"}</strong> - Postać:{" "} | ||||||
| {p.index === 0 ? "Sol" : "Vron"} - | ||||||
| {p.ready ? " ✅ GOTOWY" : " ❌ CZEKA"} | ||||||
| </li> | ||||||
| ))} | ||||||
| </ul> | ||||||
|
|
||||||
| <button | ||||||
| onClick={() => room.send("toggle_ready")} | ||||||
| style={{ | ||||||
| padding: "15px 30px", | ||||||
| fontSize: "18px", | ||||||
| marginTop: "20px", | ||||||
| background: "green", | ||||||
| }} | ||||||
| > | ||||||
| {myPlayer?.ready ? "Cofnij gotowość" : "Daj gotowość"} | ||||||
| </button> | ||||||
| </div> | ||||||
| ); | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.