Added dummy user to create a game

This commit is contained in:
2025-12-29 11:15:49 +00:00
committed by FernandoJVideira
parent 8f7102b335
commit d29e44c61c
+20 -59
View File
@@ -10,6 +10,8 @@ use App\Models\MatchGame;
class GameController extends Controller class GameController extends Controller
{ {
const GHOST_ID = 1;
public function index(Request $request) public function index(Request $request)
{ {
$query = Game::query()->with(["winner", "player1", "player2"]); $query = Game::query()->with(["winner", "player1", "player2"]);
@@ -47,51 +49,34 @@ class GameController extends Controller
$user = Auth::user(); $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) { $activeGame = Game::where(function ($q) use ($user) {
$q->where("player1_user_id", $user->id)->orWhere( $q->where("player1_user_id", $user->id)
"player2_user_id", ->orWhere("player2_user_id", $user->id);
$user->id,
);
}) })
->whereIn("status", ["Pending", "Playing"]) ->whereIn("status", ["Pending", "Playing"])
->exists(); ->exists();
if ($activeMatch || $activeGame) { if ($activeGame) {
return response()->json( return response()->json(["message" => "You already have an active game."], 400);
["message" => "You already have an active game or match."],
400,
);
} }
$game = Game::create([ $game = Game::create([
"type" => $validated["type"], "type" => $validated["type"],
"status" => "Pending", "status" => "Pending",
"player1_user_id" => $user->id, "player1_user_id" => $user->id,
"player2_user_id" => null, "player2_user_id" => self::GHOST_ID,
"winner_user_id" => null, "winner_user_id" => self::GHOST_ID,
"loser_user_id" => null, "loser_user_id" => self::GHOST_ID,
"began_at" => now(), "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" => "Multiplayer game room created",
"game" => $game, "game" => $game,
], ], 201);
201,
);
} }
public function join(Game $game) public function join(Game $game)
@@ -99,35 +84,14 @@ class GameController extends Controller
$user = Auth::user(); $user = Auth::user();
if ($game->player1_user_id == $user->id) { if ($game->player1_user_id == $user->id) {
return response()->json( return response()->json([
[ "message" => "You are the host. Waiting for opponent...",
"message" => "Waiting for opponent...", "game" => $game
"game" => $game, ], 200);
],
200,
);
} }
if ($game->status !== "Pending") { if ($game->player2_user_id !== self::GHOST_ID) {
return response()->json( return response()->json(["message" => "Game is full"], 400);
["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,
);
} }
$game->update([ $game->update([
@@ -135,13 +99,10 @@ class GameController extends Controller
"player2_user_id" => $user->id, "player2_user_id" => $user->id,
]); ]);
return response()->json( return response()->json([
[ "message" => "Joined successfully!",
"message" => "Joined successfully! Game starts now.",
"game" => $game, "game" => $game,
], ], 200);
200,
);
} }
public function show(Game $game) public function show(Game $game)