From d29e44c61ce8b256ac7fb15b33c08d1843ddeec3 Mon Sep 17 00:00:00 2001 From: Tiago Ramos <2222055@my.ipleiria.pt> Date: Sun, 28 Dec 2025 16:30:18 +0000 Subject: [PATCH] Added dummy user to create a game --- api/app/Http/Controllers/GameController.php | 89 ++++++--------------- 1 file changed, 25 insertions(+), 64 deletions(-) diff --git a/api/app/Http/Controllers/GameController.php b/api/app/Http/Controllers/GameController.php index 15061f8..1a245fc 100644 --- a/api/app/Http/Controllers/GameController.php +++ b/api/app/Http/Controllers/GameController.php @@ -10,6 +10,8 @@ use App\Models\MatchGame; class GameController extends Controller { + const GHOST_ID = 1; + public function index(Request $request) { $query = Game::query()->with(["winner", "player1", "player2"]); @@ -47,51 +49,34 @@ class GameController extends Controller $user = Auth::user(); - $activeMatch = MatchGame::where(function ($q) use ($user) { - $q->where("player1_user_id", $user->id)->orWhere( - "player2_user_id", - $user->id, - ); - }) - ->whereIn("status", ["Pending", "Playing"]) - ->exists(); - $activeGame = Game::where(function ($q) use ($user) { - $q->where("player1_user_id", $user->id)->orWhere( - "player2_user_id", - $user->id, - ); + $q->where("player1_user_id", $user->id) + ->orWhere("player2_user_id", $user->id); }) - ->whereIn("status", ["Pending", "Playing"]) - ->exists(); + ->whereIn("status", ["Pending", "Playing"]) + ->exists(); - if ($activeMatch || $activeGame) { - return response()->json( - ["message" => "You already have an active game or match."], - 400, - ); + if ($activeGame) { + return response()->json(["message" => "You already have an active game."], 400); } $game = Game::create([ "type" => $validated["type"], "status" => "Pending", "player1_user_id" => $user->id, - "player2_user_id" => null, - "winner_user_id" => null, - "loser_user_id" => null, + "player2_user_id" => self::GHOST_ID, + "winner_user_id" => self::GHOST_ID, + "loser_user_id" => self::GHOST_ID, "began_at" => now(), "player1_points" => 0, "player2_points" => 0, "custom" => null, ]); - return response()->json( - [ - "message" => "Multiplayer game room created", - "game" => $game, - ], - 201, - ); + return response()->json([ + "message" => "Multiplayer game room created", + "game" => $game, + ], 201); } public function join(Game $game) @@ -99,35 +84,14 @@ class GameController extends Controller $user = Auth::user(); if ($game->player1_user_id == $user->id) { - return response()->json( - [ - "message" => "Waiting for opponent...", - "game" => $game, - ], - 200, - ); + return response()->json([ + "message" => "You are the host. Waiting for opponent...", + "game" => $game + ], 200); } - if ($game->status !== "Pending") { - return response()->json( - ["message" => "Game is full or already started"], - 400, - ); - } - - $activeGame = Game::where(function ($query) use ($user) { - $query - ->where("player1_user_id", $user->id) - ->orWhere("player2_user_id", $user->id); - }) - ->whereIn("status", ["Pending", "Playing"]) - ->exists(); - - if ($activeGame) { - return response()->json( - ["message" => "You are already in another active game."], - 400, - ); + if ($game->player2_user_id !== self::GHOST_ID) { + return response()->json(["message" => "Game is full"], 400); } $game->update([ @@ -135,13 +99,10 @@ class GameController extends Controller "player2_user_id" => $user->id, ]); - return response()->json( - [ - "message" => "Joined successfully! Game starts now.", - "game" => $game, - ], - 200, - ); + return response()->json([ + "message" => "Joined successfully!", + "game" => $game, + ], 200); } public function show(Game $game)