open matches listing with available players

This commit is contained in:
2025-11-28 15:51:55 +05:30
parent a4fc04ace3
commit cb3f5fb5cf
3 changed files with 99 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
import { useState, useEffect } from "react";
import { useNakama } from "./providers/NakamaProvider";
import { Match } from "@heroiclabs/nakama-js";
import Board from "./Board";
import MatchList from "./MatchList";
export default function TicTacToe() {
const [username, setUsername] = useState("");
@@ -11,12 +13,14 @@ export default function TicTacToe() {
]);
const [turn, setTurn] = useState<number>(0);
const [winner, setWinner] = useState<string | null>(null);
const [openMatches, setOpenMatches] = useState<Match[]>([]);
const {
loginOrRegister,
joinMatchmaker,
onMatchData,
sendMatchData,
listOpenMatches,
matchId,
} = useNakama();
@@ -32,6 +36,25 @@ export default function TicTacToe() {
});
}, [onMatchData]);
useEffect(() => {
let active = true;
async function refreshLoop() {
while (active) {
const matches = await listOpenMatches();
setOpenMatches(matches);
await new Promise(res => setTimeout(res, 500)); // 0.5s refresh
}
}
refreshLoop();
return () => {
active = false;
};
}, [listOpenMatches]);
// ------------------------------------------
// CONNECT
// ------------------------------------------
@@ -82,6 +105,9 @@ export default function TicTacToe() {
/>
<button onClick={connect}>Connect</button>
<button onClick={startQueue}>Join Matchmaking</button>
<MatchList
matches={openMatches}
/>
</>
)}