- Created new Player.tsx component to handle username input, auto-connect, matchmaking, and logout.
- Moved all login, session UI, mode selection, and matchmaking logic from TicTacToe.tsx into Player.tsx. - Added onMatchDataCallback prop support for Player component. - Cleaned up TicTacToe.tsx by removing duplicated login/matchmaking UI and connect logic. - Improved auto-connect on mount via Player.tsx.
This commit is contained in:
82
src/tictactoe/Player.tsx
Normal file
82
src/tictactoe/Player.tsx
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import React, { useEffect, useState } from "react";
|
||||||
|
import Leaderboard from "./Leaderboard";
|
||||||
|
import { useNakama } from "./providers/NakamaProvider";
|
||||||
|
|
||||||
|
export default function Player({
|
||||||
|
onMatchDataCallback,
|
||||||
|
}: {
|
||||||
|
onMatchDataCallback: (msg: any) => void;
|
||||||
|
}) {
|
||||||
|
const {
|
||||||
|
session,
|
||||||
|
matchId,
|
||||||
|
loginOrRegister,
|
||||||
|
logout,
|
||||||
|
onMatchData,
|
||||||
|
joinMatchmaker,
|
||||||
|
} = useNakama();
|
||||||
|
|
||||||
|
const [username, setUsername] = useState(
|
||||||
|
localStorage.getItem("username") ?? ""
|
||||||
|
);
|
||||||
|
const [selectedMode, setSelectedMode] = useState("classic");
|
||||||
|
|
||||||
|
// ------------------------------------------
|
||||||
|
// CONNECT
|
||||||
|
// ------------------------------------------
|
||||||
|
async function handleConnect() {
|
||||||
|
await loginOrRegister(username);
|
||||||
|
|
||||||
|
// Match data listener
|
||||||
|
onMatchData(onMatchDataCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------
|
||||||
|
// MATCHMAKING
|
||||||
|
// ------------------------------------------
|
||||||
|
async function startQueue(selectedMode: string) {
|
||||||
|
const ticket = await joinMatchmaker(selectedMode);
|
||||||
|
console.log("Queued:", ticket);
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
handleConnect();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ marginBottom: "20px" }}>
|
||||||
|
{session && !matchId && (
|
||||||
|
<>
|
||||||
|
<h2>Hello, {session.username}</h2>
|
||||||
|
|
||||||
|
{/* Game mode selection */}
|
||||||
|
<label style={{ display: "block", marginTop: "10px" }}>
|
||||||
|
Select Game Mode:
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<select
|
||||||
|
value={selectedMode}
|
||||||
|
onChange={(e) => setSelectedMode(e.target.value)}
|
||||||
|
style={{ padding: "6px", marginBottom: "10px" }}
|
||||||
|
>
|
||||||
|
<option value="classic">Classic</option>
|
||||||
|
<option value="blitz">Blitz</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
{/* Join matchmaking */}
|
||||||
|
<button onClick={() => startQueue(selectedMode)}>Join Matchmaking</button>
|
||||||
|
<button onClick={() => logout()}>Logout</button>
|
||||||
|
|
||||||
|
{/*/!* List open matches *!/*/}
|
||||||
|
{/*<MatchList matches={openMatches} />*/}
|
||||||
|
<Leaderboard/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{session && matchId && (
|
||||||
|
<>
|
||||||
|
<h2>Go {session.username}!</h2>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useNakama } from "./providers/NakamaProvider";
|
import { useNakama } from "./providers/NakamaProvider";
|
||||||
import Board from "./Board";
|
import Board from "./Board";
|
||||||
import Leaderboard from "./Leaderboard";
|
import Player from "./Player";
|
||||||
// import { Match } from "@heroiclabs/nakama-js";
|
// import { Match } from "@heroiclabs/nakama-js";
|
||||||
// import MatchList from "./MatchList";
|
// import MatchList from "./MatchList";
|
||||||
|
|
||||||
export default function TicTacToe() {
|
export default function TicTacToe() {
|
||||||
const [username, setUsername] = useState(localStorage.getItem("username") ?? "");
|
|
||||||
const [selectedMode, setSelectedMode] = useState("classic");
|
|
||||||
const [board, setBoard] = useState<string[][]>([
|
const [board, setBoard] = useState<string[][]>([
|
||||||
["", "", ""],
|
["", "", ""],
|
||||||
["", "", ""],
|
["", "", ""],
|
||||||
@@ -33,13 +31,11 @@ export default function TicTacToe() {
|
|||||||
setPlayers(state.players || []);
|
setPlayers(state.players || []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
loginOrRegister,
|
|
||||||
logout,
|
|
||||||
joinMatchmaker,
|
|
||||||
onMatchData,
|
onMatchData,
|
||||||
sendMatchData,
|
sendMatchData,
|
||||||
listOpenMatches,
|
// listOpenMatches,
|
||||||
matchId,
|
matchId,
|
||||||
session,
|
session,
|
||||||
} = useNakama();
|
} = useNakama();
|
||||||
@@ -67,20 +63,6 @@ export default function TicTacToe() {
|
|||||||
// };
|
// };
|
||||||
// }, [listOpenMatches]);
|
// }, [listOpenMatches]);
|
||||||
|
|
||||||
// ------------------------------------------
|
|
||||||
// CONNECT
|
|
||||||
// ------------------------------------------
|
|
||||||
async function connect() {
|
|
||||||
await loginOrRegister(username);
|
|
||||||
|
|
||||||
// Match data listener
|
|
||||||
onMatchData(onMatchDataCallback);
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
connect();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// ------------------------------------------
|
// ------------------------------------------
|
||||||
// SEND A MOVE
|
// SEND A MOVE
|
||||||
// ------------------------------------------
|
// ------------------------------------------
|
||||||
@@ -90,57 +72,13 @@ export default function TicTacToe() {
|
|||||||
sendMatchData(matchId, 1, { row, col }); // OpMove=1
|
sendMatchData(matchId, 1, { row, col }); // OpMove=1
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------
|
|
||||||
// MATCHMAKING
|
|
||||||
// ------------------------------------------
|
|
||||||
async function startQueue(selectedMode: string) {
|
|
||||||
const ticket = await joinMatchmaker(selectedMode);
|
|
||||||
console.log("Queued:", ticket);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>Tic Tac Toe Multiplayer</h1>
|
<h1>Tic Tac Toe Multiplayer</h1>
|
||||||
|
|
||||||
{!session && (
|
<Player
|
||||||
<>
|
onMatchDataCallback={onMatchDataCallback}
|
||||||
<input
|
/>
|
||||||
placeholder="username"
|
|
||||||
value={username}
|
|
||||||
disabled={username.length > 0}
|
|
||||||
onChange={(e) => setUsername(e.target.value)}
|
|
||||||
/>
|
|
||||||
<button onClick={connect}>Connect</button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{session && !matchId && (
|
|
||||||
<>
|
|
||||||
<h2>Hello, {session.username}</h2>
|
|
||||||
|
|
||||||
{/* Game mode selection */}
|
|
||||||
<label style={{ display: "block", marginTop: "10px" }}>
|
|
||||||
Select Game Mode:
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<select
|
|
||||||
value={selectedMode}
|
|
||||||
onChange={(e) => setSelectedMode(e.target.value)}
|
|
||||||
style={{ padding: "6px", marginBottom: "10px" }}
|
|
||||||
>
|
|
||||||
<option value="classic">Classic</option>
|
|
||||||
<option value="blitz">Blitz</option>
|
|
||||||
</select>
|
|
||||||
|
|
||||||
{/* Join matchmaking */}
|
|
||||||
<button onClick={() => startQueue(selectedMode)}>Join Matchmaking</button>
|
|
||||||
<button onClick={() => logout()}>Logout</button>
|
|
||||||
|
|
||||||
{/*/!* List open matches *!/*/}
|
|
||||||
{/*<MatchList matches={openMatches} />*/}
|
|
||||||
<Leaderboard/>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{matchId && (
|
{matchId && (
|
||||||
<Board
|
<Board
|
||||||
|
|||||||
Reference in New Issue
Block a user