Final touches

This commit is contained in:
2026-01-02 22:11:47 +00:00
parent 25d21f7cf8
commit a804f41956
3 changed files with 46 additions and 42 deletions
+24 -11
View File
@@ -55,42 +55,55 @@ class GameController extends Controller
$validated = $request->validate([
"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();
$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();
if ($activeGame) {
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([
"type" => $validated["type"],
"status" => "Pending",
"status" => $initialStatus,
"player1_user_id" => $user->id,
"player2_user_id" => self::GHOST_ID,
"winner_user_id" => self::GHOST_ID,
"loser_user_id" => self::GHOST_ID,
"began_at" => now(),
"player2_user_id" => $player2Id,
"winner_user_id" => null,
"loser_user_id" => null,
"match_id" => $request->input('match_id', null),
"began_at" => $request->input('began_at', now()),
"player1_points" => 0,
"player2_points" => 0,
"custom" => null,
]);
return response()->json([
"message" => "Multiplayer game room created",
"message" => "Game created",
"game" => $game,
], 201);
}
public function join(Request $request, Game $game)
{
public function join(Request $request, Game $game)
{
// Prevent joining if already full or started
if ($game->status !== 'Pending' || $game->player2_user_id !== 1) {
return response()->json(['message' => 'Game is not available'], 400);