SyntaxSquad/DADProject#15 feat: Added Multiplayer View and Stuff
This commit is contained in:
@@ -13,10 +13,10 @@ class GameController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', Game::class);
|
||||
|
||||
|
||||
$user = Auth::user();
|
||||
$query = Game::query()->with(["winner", "player1", "player2"]);
|
||||
|
||||
|
||||
// Filter games based on user type
|
||||
if ($user->type !== 'A') {
|
||||
// Players can only see games they participate in
|
||||
@@ -55,7 +55,7 @@ class GameController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->authorize('create', Game::class);
|
||||
|
||||
|
||||
$validated = $request->validate([
|
||||
"type" => "required|in:3,9",
|
||||
]);
|
||||
@@ -77,7 +77,7 @@ class GameController extends Controller
|
||||
"type" => $validated["type"],
|
||||
"status" => "Pending",
|
||||
"player1_user_id" => $user->id,
|
||||
"player2_user_id" => self::GHOST_ID,
|
||||
"player2_user_id" => self::GHOST_ID,
|
||||
"winner_user_id" => self::GHOST_ID,
|
||||
"loser_user_id" => self::GHOST_ID,
|
||||
"began_at" => now(),
|
||||
@@ -92,38 +92,30 @@ class GameController extends Controller
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function join(Game $game)
|
||||
{
|
||||
$this->authorize('join', $game);
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
if ($game->player1_user_id == $user->id) {
|
||||
return response()->json([
|
||||
"message" => "You are the host. Waiting for opponent...",
|
||||
"game" => $game
|
||||
], 200);
|
||||
}
|
||||
|
||||
if ($game->player2_user_id !== self::GHOST_ID) {
|
||||
return response()->json(["message" => "Game is full"], 400);
|
||||
}
|
||||
|
||||
$game->update([
|
||||
"status" => "Playing",
|
||||
"player2_user_id" => $user->id,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
"message" => "Joined successfully!",
|
||||
"game" => $game,
|
||||
], 200);
|
||||
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);
|
||||
}
|
||||
|
||||
// Prevent joining your own game
|
||||
if ($game->player1_user_id === $request->user()->id) {
|
||||
return response()->json(['message' => 'Cannot join your own game'], 400);
|
||||
}
|
||||
|
||||
$game->update([
|
||||
'player2_user_id' => $request->user()->id,
|
||||
'status' => 'Playing',
|
||||
]);
|
||||
|
||||
return response()->json($game);
|
||||
}
|
||||
|
||||
public function show(Game $game)
|
||||
{
|
||||
$this->authorize('view', $game);
|
||||
|
||||
|
||||
$game->load(["player1", "player2", "winner"]);
|
||||
return response()->json($game);
|
||||
}
|
||||
@@ -131,7 +123,7 @@ class GameController extends Controller
|
||||
public function update(Request $request, Game $game)
|
||||
{
|
||||
$this->authorize('update', $game);
|
||||
|
||||
|
||||
if ($game->status == "Ended") {
|
||||
return response()->json(["message" => "Game already ended"], 400);
|
||||
}
|
||||
@@ -158,13 +150,13 @@ class GameController extends Controller
|
||||
public function resign(Game $game)
|
||||
{
|
||||
$this->authorize('resign', $game);
|
||||
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
|
||||
$game->update([
|
||||
'status' => 'Ended',
|
||||
'winner_user_id' => $game->player1_user_id === $user->id
|
||||
? $game->player2_user_id
|
||||
'winner_user_id' => $game->player1_user_id === $user->id
|
||||
? $game->player2_user_id
|
||||
: $game->player1_user_id,
|
||||
'loser_user_id' => $user->id,
|
||||
'ended_at' => now(),
|
||||
|
||||
+18
-4
@@ -47,8 +47,8 @@ Route::prefix('v1')->group(function () {
|
||||
});
|
||||
|
||||
Route::prefix('games')->group(function () {
|
||||
// Host a new multiplayer game
|
||||
Route::post('/host', function (\Illuminate\Http\Request $request) {
|
||||
//Host a new multiplayer game - MUST come before resource routes
|
||||
Route::post('host', function (\Illuminate\Http\Request $request) {
|
||||
$request->validate([
|
||||
'type' => 'required|in:3,9'
|
||||
]);
|
||||
@@ -61,7 +61,21 @@ Route::prefix('v1')->group(function () {
|
||||
'began_at' => now(),
|
||||
]);
|
||||
|
||||
return response()->json($game);
|
||||
return response()->json($game);
|
||||
});
|
||||
|
||||
// List open games (available to join)
|
||||
Route::get('open', function (\Illuminate\Http\Request $request) {
|
||||
$type = $request->query('type', '9');
|
||||
|
||||
$games = \App\Models\Game::where('status', 'Pending')
|
||||
->where('player2_user_id', 1) // Only games waiting for a second player
|
||||
->where('type', $type)
|
||||
->with('player1') // Load player1 relationship
|
||||
->orderBy('began_at', 'asc')
|
||||
->paginate(10);
|
||||
|
||||
return response()->json($games);
|
||||
});
|
||||
|
||||
Route::get('/', [GameController::class, 'index']);
|
||||
@@ -75,7 +89,7 @@ Route::prefix('v1')->group(function () {
|
||||
|
||||
Route::prefix('matches')->group(function () {
|
||||
Route::apiResource('/', MatchController::class)->parameters(['' => 'match']);
|
||||
Route::post('/{match}/join', [MatchController::class, 'join']);
|
||||
Route::post('/{match}/join', [MatchController::class, 'join']);
|
||||
});
|
||||
|
||||
// Admin Routes
|
||||
|
||||
Reference in New Issue
Block a user