feature/websockets-matches #95

Merged
Edd merged 3 commits from feature/websockets-matches into develop 2026-01-02 22:23:21 +00:00
3 changed files with 46 additions and 42 deletions
Showing only changes of commit a804f41956 - Show all commits
+21 -8
View File
@@ -55,6 +55,10 @@ class GameController extends Controller
$validated = $request->validate([ $validated = $request->validate([
"type" => "required|in:3,9", "type" => "required|in:3,9",
"player2_user_id" => "nullable|integer|exists:users,id",
"match_id" => "nullable|integer|exists:matches,id",
"status" => "nullable|in:Pending,Playing",
"began_at" => "nullable|date"
]); ]);
$user = Auth::user(); $user = Auth::user();
@@ -70,27 +74,36 @@ class GameController extends Controller
return response()->json(["message" => "You already have an active game."], 400); return response()->json(["message" => "You already have an active game."], 400);
} }
$player2Id = $request->input('player2_user_id', self::GHOST_ID);
$initialStatus = $request->input('status', 'Pending');
if ($player2Id !== self::GHOST_ID && !$request->has('status')) {
$initialStatus = 'Playing';
}
$game = Game::create([ $game = Game::create([
"type" => $validated["type"], "type" => $validated["type"],
"status" => "Pending", "status" => $initialStatus,
"player1_user_id" => $user->id, "player1_user_id" => $user->id,
"player2_user_id" => self::GHOST_ID, "player2_user_id" => $player2Id,
"winner_user_id" => self::GHOST_ID, "winner_user_id" => null,
"loser_user_id" => self::GHOST_ID, "loser_user_id" => null,
"began_at" => now(), "match_id" => $request->input('match_id', null),
"began_at" => $request->input('began_at', now()),
"player1_points" => 0, "player1_points" => 0,
"player2_points" => 0, "player2_points" => 0,
"custom" => null, "custom" => null,
]); ]);
return response()->json([ return response()->json([
"message" => "Multiplayer game room created", "message" => "Game created",
"game" => $game, "game" => $game,
], 201); ], 201);
} }
public function join(Request $request, Game $game) public function join(Request $request, Game $game)
{ {
// Prevent joining if already full or started // Prevent joining if already full or started
if ($game->status !== 'Pending' || $game->player2_user_id !== 1) { if ($game->status !== 'Pending' || $game->player2_user_id !== 1) {
return response()->json(['message' => 'Game is not available'], 400); return response()->json(['message' => 'Game is not available'], 400);
-2
View File
@@ -1,6 +1,4 @@
import { addUser, removeUser } from "../state/connection.js"; import { addUser, removeUser } from "../state/connection.js";
import { addUser, removeUser } from "../state/connection.js";
const HEARTBEAT_INTERVAL = 30000; // 30 seconds const HEARTBEAT_INTERVAL = 30000; // 30 seconds
const HEARTBEAT_TIMEOUT = 10000; // 10 seconds const HEARTBEAT_TIMEOUT = 10000; // 10 seconds
+4 -11
View File
@@ -2,11 +2,11 @@ import BiscaGame from "../classes/BiscaGame.js";
const activeGames = new Map(); const activeGames = new Map();
const createGame = (id, type, player1, player2) => { export const createGame = (id, type, player1, player2) => {
const existingGame = activeGames.get(id); const existingGame = activeGames.get(id);
if (existingGame) { if (existingGame) {
console.log( console.log(
`[Game State] Game ${id} already exists, returning existing instance`, `[Game State] Game ${id} already exists, returning existing instance`
); );
return existingGame; return existingGame;
} }
@@ -20,11 +20,11 @@ const createGame = (id, type, player1, player2) => {
return game; return game;
}; };
const getGame = (id) => { export const getGame = (id) => {
return activeGames.get(id); return activeGames.get(id);
}; };
const removeGame = (id) => { export const removeGame = (id) => {
const game = activeGames.get(id); const game = activeGames.get(id);
if (game) { if (game) {
game.stopTimer(); game.stopTimer();
@@ -32,10 +32,3 @@ const removeGame = (id) => {
console.log(`[Game State] Game removed: ${id}`); console.log(`[Game State] Game removed: ${id}`);
} }
}; };
module.exports = {
createGame,
getGame,
removeGame,
};