From 10508cc0b191230b6e44a3d4ade335eab5c7fd73 Mon Sep 17 00:00:00 2001 From: FernandoJVideira <03.pleaser-minster@icloud.com> Date: Tue, 23 Dec 2025 12:36:56 +0000 Subject: [PATCH] fix: GameControler create game fixed --- api/app/Http/Controllers/GameController.php | 151 ++++++++++++-------- 1 file changed, 93 insertions(+), 58 deletions(-) diff --git a/api/app/Http/Controllers/GameController.php b/api/app/Http/Controllers/GameController.php index d798c24..bc04482 100644 --- a/api/app/Http/Controllers/GameController.php +++ b/api/app/Http/Controllers/GameController.php @@ -12,17 +12,25 @@ class GameController extends Controller { public function index(Request $request) { - $query = Game::query()->with(['winner', 'player1', 'player2']); + $query = Game::query()->with(["winner", "player1", "player2"]); - if ($request->has('type') && in_array($request->type, ['3', '9'])) { - $query->where('type', $request->type); + if ($request->has("type") && in_array($request->type, ["3", "9"])) { + $query->where("type", $request->type); } - if ($request->has('status') && in_array($request->status, ['Pending', 'Playing', 'Ended', 'Interrupted'])) { - $query->where('status', $request->status); + if ( + $request->has("status") && + in_array($request->status, [ + "Pending", + "Playing", + "Ended", + "Interrupted", + ]) + ) { + $query->where("status", $request->status); } - $query->orderBy('began_at', 'desc'); + $query->orderBy("began_at", "desc"); return response()->json($query->paginate(15)); } @@ -30,42 +38,56 @@ class GameController extends Controller public function store(Request $request) { $validated = $request->validate([ - 'type' => 'required|in:3,9', + "type" => "required|in:3,9", ]); $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(); + $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); - })->whereIn('status', ['Pending', 'Playing'])->exists(); + $q->where("player1_user_id", $user->id)->orWhere( + "player2_user_id", + $user->id, + ); + }) + ->whereIn("status", ["Pending", "Playing"]) + ->exists(); if ($activeMatch || $activeGame) { - return response()->json(['message' => 'You already have an active game or match.'], 400); + return response()->json( + ["message" => "You already have an active game or match."], + 400, + ); } $game = Game::create([ - 'type' => $validated['type'], - 'status' => 'Pending', - 'player1_user_id' => $user->id, - 'player2_user_id' => $user->id, - 'winner_user_id' => $user->id, - 'loser_user_id' => $user->id, - 'began_at' => now(), - 'player1_points' => 0, - 'player2_points' => 0, - 'custom' => null + "type" => $validated["type"], + "status" => "Pending", + "player1_user_id" => $user->id, + "player2_user_id" => null, + "winner_user_id" => null, + "loser_user_id" => null, + "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) @@ -73,66 +95,79 @@ 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" => "Waiting for opponent...", + "game" => $game, + ], + 200, + ); } - if ($game->status !== 'Pending') { - return response()->json(['message' => 'Game is full or already started'], 400); + 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); + $query + ->where("player1_user_id", $user->id) + ->orWhere("player2_user_id", $user->id); }) - ->whereIn('status', ['Pending', 'Playing']) - ->exists(); + ->whereIn("status", ["Pending", "Playing"]) + ->exists(); if ($activeGame) { - return response()->json(['message' => 'You are already in another active game.'], 400); + return response()->json( + ["message" => "You are already in another active game."], + 400, + ); } $game->update([ - 'status' => 'Playing', - 'player2_user_id' => $user->id, + "status" => "Playing", + "player2_user_id" => $user->id, ]); - return response()->json([ - 'message' => 'Joined successfully! Game starts now.', - 'game' => $game - ], 200); + return response()->json( + [ + "message" => "Joined successfully! Game starts now.", + "game" => $game, + ], + 200, + ); } public function show(Game $game) { - $game->load(['player1', 'player2', 'winner']); + $game->load(["player1", "player2", "winner"]); return response()->json($game); } public function update(Request $request, Game $game) { - if ($game->status == 'Ended') { - return response()->json(['message' => 'Game already ended'], 400); + if ($game->status == "Ended") { + return response()->json(["message" => "Game already ended"], 400); } $data = $request->validate([ - 'status' => 'in:Playing,Ended,Interrupted', - 'winner_user_id' => 'exists:users,id', - 'loser_user_id' => 'exists:users,id', - 'player1_points' => 'integer', - 'player2_points' => 'integer', - 'is_draw' => 'boolean', - 'total_time' => 'numeric' + "status" => "in:Playing,Ended,Interrupted", + "winner_user_id" => "exists:users,id", + "loser_user_id" => "exists:users,id", + "player1_points" => "integer", + "player2_points" => "integer", + "is_draw" => "boolean", + "total_time" => "numeric", ]); - if (isset($data['status']) && $data['status'] === 'Ended') { - $data['ended_at'] = now(); + if (isset($data["status"]) && $data["status"] === "Ended") { + $data["ended_at"] = now(); } $game->update($data); return response()->json($game); } -} \ No newline at end of file +}