Version 1.0 #102

Merged
FernandoJVideira merged 163 commits from develop into master 2026-01-03 14:49:00 +00:00
2 changed files with 114 additions and 83 deletions
Showing only changes of commit 28466ecbbd - Show all commits
+93 -58
View File
@@ -12,17 +12,25 @@ class GameController extends Controller
{ {
public function index(Request $request) 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'])) { if ($request->has("type") && in_array($request->type, ["3", "9"])) {
$query->where('type', $request->type); $query->where("type", $request->type);
} }
if ($request->has('status') && in_array($request->status, ['Pending', 'Playing', 'Ended', 'Interrupted'])) { if (
$query->where('status', $request->status); $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)); return response()->json($query->paginate(15));
} }
@@ -30,42 +38,56 @@ class GameController extends Controller
public function store(Request $request) public function store(Request $request)
{ {
$validated = $request->validate([ $validated = $request->validate([
'type' => 'required|in:3,9', "type" => "required|in:3,9",
]); ]);
$user = Auth::user(); $user = Auth::user();
$activeMatch = MatchGame::where(function ($q) use ($user) { $activeMatch = MatchGame::where(function ($q) use ($user) {
$q->where('player1_user_id', $user->id) $q->where("player1_user_id", $user->id)->orWhere(
->orWhere('player2_user_id', $user->id); "player2_user_id",
})->whereIn('status', ['Pending', 'Playing'])->exists(); $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) $q->where("player1_user_id", $user->id)->orWhere(
->orWhere('player2_user_id', $user->id); "player2_user_id",
})->whereIn('status', ['Pending', 'Playing'])->exists(); $user->id,
);
})
->whereIn("status", ["Pending", "Playing"])
->exists();
if ($activeMatch || $activeGame) { 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([ $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' => $user->id, "player2_user_id" => null,
'winner_user_id' => $user->id, "winner_user_id" => null,
'loser_user_id' => $user->id, "loser_user_id" => null,
'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', [
'game' => $game "message" => "Multiplayer game room created",
], 201); "game" => $game,
],
201,
);
} }
public function join(Game $game) public function join(Game $game)
@@ -73,66 +95,79 @@ 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' => 'Waiting for opponent...', [
'game' => $game "message" => "Waiting for opponent...",
], 200); "game" => $game,
],
200,
);
} }
if ($game->status !== 'Pending') { if ($game->status !== "Pending") {
return response()->json(['message' => 'Game is full or already started'], 400); return response()->json(
["message" => "Game is full or already started"],
400,
);
} }
$activeGame = Game::where(function ($query) use ($user) { $activeGame = Game::where(function ($query) use ($user) {
$query->where('player1_user_id', $user->id) $query
->orWhere('player2_user_id', $user->id); ->where("player1_user_id", $user->id)
->orWhere("player2_user_id", $user->id);
}) })
->whereIn('status', ['Pending', 'Playing']) ->whereIn("status", ["Pending", "Playing"])
->exists(); ->exists();
if ($activeGame) { 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([ $game->update([
'status' => 'Playing', "status" => "Playing",
'player2_user_id' => $user->id, "player2_user_id" => $user->id,
]); ]);
return response()->json([ return response()->json(
'message' => 'Joined successfully! Game starts now.', [
'game' => $game "message" => "Joined successfully! Game starts now.",
], 200); "game" => $game,
],
200,
);
} }
public function show(Game $game) public function show(Game $game)
{ {
$game->load(['player1', 'player2', 'winner']); $game->load(["player1", "player2", "winner"]);
return response()->json($game); return response()->json($game);
} }
public function update(Request $request, Game $game) public function update(Request $request, Game $game)
{ {
if ($game->status == 'Ended') { if ($game->status == "Ended") {
return response()->json(['message' => 'Game already ended'], 400); return response()->json(["message" => "Game already ended"], 400);
} }
$data = $request->validate([ $data = $request->validate([
'status' => 'in:Playing,Ended,Interrupted', "status" => "in:Playing,Ended,Interrupted",
'winner_user_id' => 'exists:users,id', "winner_user_id" => "exists:users,id",
'loser_user_id' => 'exists:users,id', "loser_user_id" => "exists:users,id",
'player1_points' => 'integer', "player1_points" => "integer",
'player2_points' => 'integer', "player2_points" => "integer",
'is_draw' => 'boolean', "is_draw" => "boolean",
'total_time' => 'numeric' "total_time" => "numeric",
]); ]);
if (isset($data['status']) && $data['status'] === 'Ended') { if (isset($data["status"]) && $data["status"] === "Ended") {
$data['ended_at'] = now(); $data["ended_at"] = now();
} }
$game->update($data); $game->update($data);
return response()->json($game); return response()->json($game);
} }
} }
+21 -25
View File
@@ -2,37 +2,33 @@
use App\Http\Controllers\AuthController; use App\Http\Controllers\AuthController;
use App\Http\Controllers\GameController; use App\Http\Controllers\GameController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\MatchController; use App\Http\Controllers\MatchController;
use App\Http\Controllers\UserController;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
// Authentication Routes
# Authentication Routes
Route::post('/login', [AuthController::class, 'login']); Route::post('/login', [AuthController::class, 'login']);
Route::middleware('auth:sanctum')->group(function () {
Route::get('/users/me', function (Request $request) {
return $request->user();
});
Route::post('logout', [AuthController::class, 'logout']);
});
Route::post('/register', [AuthController::class, 'register']); Route::post('/register', [AuthController::class, 'register']);
# Game Routes - Protected
Route::middleware('auth:sanctum')->group(function () { Route::middleware('auth:sanctum')->group(function () {
Route::apiResource('games', GameController::class); Route::post('logout', [AuthController::class, 'logout']);
Route::post('games', [GameController::class, 'store']);
Route::post('/games/{game}/resign', [GameController::class, 'resign']);
Route::get('/users/{user}/matches', [UserController::class, 'getMatches']);
Route::post('games/{game}/join', [GameController::class, 'join']);
});
Route::middleware('auth:sanctum')->group(function () { Route::prefix('users')->group(function () {
Route::get('matches', [MatchController::class, 'index']); Route::get('/me', function (Request $request) {
Route::post('matches', [MatchController::class, 'store']); return $request->user();
Route::get('matches/{match}', [MatchController::class, 'show']); });
Route::post('matches/{match}/join', [MatchController::class, 'join']); Route::get('/{user}/matches', [UserController::class, 'getMatches']);
Route::put('matches/{match}', [MatchController::class, 'update']); });
});
Route::prefix('games')->group(function () {
Route::apiResource('/', GameController::class)->parameters(['' => 'game']);
Route::post('/{game}/resign', [GameController::class, 'resign']);
Route::post('/{game}/join', [GameController::class, 'join']);
});
Route::prefix('matches')->group(function () {
Route::apiResource('/', MatchController::class)->parameters(['' => 'match']);
Route::post('/{match}/join', [MatchController::class, 'join']);
});
});