Added corrections to user profile and admin match and game history

This commit is contained in:
2026-01-03 00:41:24 +00:00
parent 1a7916a836
commit 894a38bb27
6 changed files with 1240 additions and 706 deletions
+86 -19
View File
@@ -17,6 +17,7 @@ class GameController extends Controller
$user = Auth::user();
$query = Game::query()->with(["winner", "player1", "player2"]);
// Se não for Admin, mostra apenas os jogos do utilizador
if ($user->type !== 'A') {
$query->where(function ($q) use ($user) {
$q->where('player1_user_id', $user->id)
@@ -40,15 +41,68 @@ class GameController extends Controller
$query->where("status", $request->status);
}
if ($request->has("sort_direction") && in_array($request->sort_direction, ["asc", "desc"])) {
if ($request->has("sort_direction") && in_array($request->sort_direction, ["asc", "desc"])) {
$query->orderBy("began_at", $request->sort_direction);
}else{
} else {
$query->orderBy("began_at", "desc");
}
return response()->json($query->paginate(15));
}
// --- MÉTODOS ADICIONADOS ---
// Host a new multiplayer Game
public function host(Request $request)
{
$request->validate([
'type' => 'required|in:3,9'
]);
$user = $request->user();
// Check for active game
$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();
if ($activeGame) {
return response()->json(["message" => "You already have an active game."], 400);
}
$game = Game::create([
'type' => $request->type,
'status' => 'Pending',
'player1_user_id' => $user->id,
'player2_user_id' => self::GHOST_ID, // Ghost ID = 1 (Waiting for player)
'began_at' => now(),
'player1_points' => 0,
'player2_points' => 0,
]);
return response()->json($game, 201);
}
// List open games (Available to join)
public function open(Request $request)
{
$type = $request->query('type', '9');
$GHOST_ID = 1;
$games = Game::where('status', 'Pending')
->where('player2_user_id', $GHOST_ID) // Games waiting for P2
->where('type', $type)
->with('player1')
->orderBy('began_at', 'asc')
->paginate(10);
return response()->json($games);
}
// ---------------------------
public function store(Request $request)
{
$this->authorize('create', Game::class);
@@ -78,6 +132,7 @@ class GameController extends Controller
$initialStatus = $request->input('status', 'Pending');
// If specific player 2 is provided, auto-start game
if ($player2Id !== self::GHOST_ID && !$request->has('status')) {
$initialStatus = 'Playing';
}
@@ -104,24 +159,36 @@ class GameController extends Controller
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 if already full or started
if ($game->status !== 'Pending' || $game->player2_user_id !== self::GHOST_ID) {
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);
}
// Check if joining user has another active game
$activeGame = Game::where(function ($q) use ($request) {
$q->where("player1_user_id", $request->user()->id)
->orWhere("player2_user_id", $request->user()->id);
})
->whereIn("status", ["Pending", "Playing"])
->exists();
if ($activeGame) {
return response()->json(["message" => "You already have an active game."], 400);
}
$game->update([
'player2_user_id' => $request->user()->id,
'status' => 'Playing',
]);
return response()->json($game);
}
// 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);
@@ -177,4 +244,4 @@ class GameController extends Controller
'game' => $game->fresh(),
]);
}
}
}
+73 -2
View File
@@ -3,12 +3,10 @@
namespace App\Http\Controllers;
use App\Models\MatchGame;
use App\Models\Game;
use App\Models\CoinTransaction;
use App\Models\CoinTransactionType;
use Illuminate\Http\Request;
use App\Http\Requests\StoreMatchRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class MatchController extends Controller
@@ -196,4 +194,77 @@ class MatchController extends Controller
return response()->json($match);
});
}
public function host(Request $request)
{
$request->validate([
'type' => 'required|in:3,9',
'stake' => 'required|integer|min:0'
]);
$user = $request->user();
$stake = $request->stake;
if ($user->coins_balance < $stake) {
return response()->json(['message' => 'Insufficient coin balance'], 400);
}
$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();
if ($activeMatch) {
return response()->json(['message' => 'You already have an active match.'], 400);
}
return DB::transaction(function () use ($request, $user, $stake) {
$GHOST_ID = 1;
$user->decrement('coins_balance', $stake);
$stakeType = CoinTransactionType::firstOrCreate(
['name' => 'Match stake'],
['type' => 'D'] // Debit
);
$match = MatchGame::create([
'type' => $request->type,
'status' => 'Pending',
'player1_user_id' => $user->id,
'player2_user_id' => $GHOST_ID,
'winner_user_id' => null,
'loser_user_id' => null,
'stake' => $stake,
'began_at' => now(),
'player1_marks' => 0, 'player2_marks' => 0,
'player1_points' => 0, 'player2_points' => 0,
]);
CoinTransaction::create([
'user_id' => $user->id,
'match_id' => $match->id,
'coin_transaction_type_id' => $stakeType->id,
'transaction_datetime' => now(),
'coins' => -$stake,
]);
return response()->json($match, 201);
});
}
public function open(Request $request)
{
$type = $request->query('type', '9');
$GHOST_ID = 1;
$matches = MatchGame::where('status', 'Pending')
->where('player2_user_id', $GHOST_ID)
->where('type', $type)
->with('player1')
->orderBy('began_at', 'asc')
->paginate(10);
return response()->json($matches);
}
}