SyntaxSquad/DADProject#16 fix: merge conflics fixed

This commit is contained in:
AfonsoCMSousa
2026-01-03 13:25:21 +00:00
11 changed files with 384 additions and 1718 deletions
+136 -87
View File
@@ -13,17 +13,26 @@ class MatchController extends Controller
{
public function index(Request $request)
{
$query = MatchGame::query()->with(['winner', 'player1', 'player2']);
$query = MatchGame::query()->with(["winner", "player1", "player2"]);
if ($request->has('type')) {
$query->where('type', $request->type);
if ($request->has("type")) {
$query->where("type", $request->type);
}
if ($request->has('status')) {
$query->where('status', $request->status);
if ($request->has("status")) {
$query->where("status", $request->status);
}
$query->orderBy('began_at', 'desc');
if ($request->has("pot") && in_array($request->pot, ["asc", "desc"])) {
$query->orderBy("stake", $request->pot);
} elseif (
$request->has("sort_direction") &&
in_array($request->sort_direction, ["asc", "desc"])
) {
$query->orderBy("began_at", $request->sort_direction);
} else {
$query->orderBy("began_at", "desc");
}
return response()->json($query->paginate(15));
}
@@ -35,13 +44,20 @@ class MatchController extends Controller
$stake = $validated['stake'];
if ($user->coins_balance < $stake) {
return response()->json(['message' => 'Insufficient coin balance'], 400);
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();
$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);
@@ -50,45 +66,50 @@ class MatchController extends Controller
return DB::transaction(function () use ($validated, $user, $stake) {
$GHOST_ID = 1;
$user->decrement('coins_balance', $stake);
$user->decrement("coins_balance", $stake);
$stakeType = CoinTransactionType::firstOrCreate(
['name' => 'Match stake'],
['type' => 'D'] // Debit
["name" => "Match stake"],
["type" => "D"], // Debit
);
$match = MatchGame::create([
'type' => $validated['type'],
'status' => 'Pending',
'player1_user_id' => $user->id,
'player2_user_id' => $GHOST_ID,
'winner_user_id' => $GHOST_ID,
'loser_user_id' => $GHOST_ID,
'stake' => $stake,
'began_at' => now(),
'player1_marks' => 0, 'player2_marks' => 0,
'player1_points' => 0, 'player2_points' => 0,
"type" => $validated["type"],
"status" => "Pending",
"player1_user_id" => $user->id,
"player2_user_id" => $GHOST_ID,
"winner_user_id" => $GHOST_ID,
"loser_user_id" => $GHOST_ID,
"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,
"user_id" => $user->id,
"match_id" => $match->id,
"coin_transaction_type_id" => $stakeType->id,
"transaction_datetime" => now(),
"coins" => -$stake,
]);
return response()->json([
'message' => 'Match created successfully',
'match' => $match,
'balance' => $user->coins_balance
], 201);
return response()->json(
[
"message" => "Match created successfully",
"match" => $match,
"balance" => $user->coins_balance,
],
201,
);
});
}
public function show(MatchGame $match)
{
$match->load(['player1', 'player2', 'winner']);
$match->load(["player1", "player2", "winner"]);
return response()->json($match);
}
@@ -97,31 +118,47 @@ class MatchController extends Controller
$user = request()->user();
if ($match->player1_user_id == $user->id) {
return response()->json(['message' => 'You cannot join your own match'], 400);
return response()->json(
["message" => "You cannot join your own match"],
400,
);
}
if ($match->status !== 'Pending') {
return response()->json(['message' => 'Match is full or started'], 400);
if ($match->status !== "Pending") {
return response()->json(
["message" => "Match is full or started"],
400,
);
}
if ($user->coins_balance < $match->stake) {
return response()->json(['message' => 'Insufficient coin balance to join this match'], 400);
return response()->json(
["message" => "Insufficient coin balance to join this match"],
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();
$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 are already in another active match.'], 400);
return response()->json(
["message" => "You are already in another active match."],
400,
);
}
return DB::transaction(function () use ($match, $user) {
$user->decrement('coins_balance', $match->stake);
$user->decrement("coins_balance", $match->stake);
$stakeType = CoinTransactionType::firstOrCreate(
['name' => 'Match stake'],
['type' => 'D']
["name" => "Match stake"],
["type" => "D"],
);
CoinTransaction::create([
@@ -133,62 +170,63 @@ class MatchController extends Controller
]);
$match->update([
'status' => 'Playing',
'player2_user_id' => $user->id
"status" => "Playing",
"player2_user_id" => $user->id,
]);
return response()->json([
'message' => 'Joined match successfully!',
'match' => $match,
'balance' => $user->coins_balance
"message" => "Joined match successfully!",
"match" => $match,
"balance" => $user->coins_balance,
]);
});
}
public function update(Request $request, MatchGame $match)
{
if ($match->status == 'Ended') {
return response()->json(['message' => 'Match already ended'], 400);
if ($match->status == "Ended") {
return response()->json(["message" => "Match already ended"], 400);
}
$data = $request->validate([
'status' => 'in:Playing,Ended,Interrupted',
'winner_user_id' => 'exists:users,id',
'loser_user_id' => 'exists:users,id',
'player1_marks' => 'integer',
'player2_marks' => 'integer',
'player1_points' => 'integer',
'player2_points' => 'integer',
'total_time' => 'numeric'
"status" => "in:Playing,Ended,Interrupted",
"winner_user_id" => "exists:users,id",
"loser_user_id" => "exists:users,id",
"player1_marks" => "integer",
"player2_marks" => "integer",
"player1_points" => "integer",
"player2_points" => "integer",
"total_time" => "numeric",
]);
return DB::transaction(function () use ($match, $data) {
if (isset($data['status']) && $data['status'] === 'Ended') {
$data['ended_at'] = now();
}
$match->update($data);
if ($match->status === 'Ended' && $match->stake > 0 && $match->winner_user_id) {
if (
$match->status === "Ended" &&
$match->stake > 0 &&
$match->winner_user_id
) {
$payoutType = CoinTransactionType::firstOrCreate(
['name' => 'Match payout'],
['type' => 'C'] // Credit
["name" => "Match payout"],
["type" => "C"], // Credit
);
$totalPrize = $match->stake * 2;
CoinTransaction::create([
'user_id' => $match->winner_user_id,
'match_id' => $match->id,
'coin_transaction_type_id' => $payoutType->id,
'transaction_datetime' => now(),
'coins' => $totalPrize,
"user_id" => $match->winner_user_id,
"match_id" => $match->id,
"coin_transaction_type_id" => $payoutType->id,
"transaction_datetime" => now(),
"coins" => $totalPrize,
]);
$match->winner->increment('coins_balance', $totalPrize);
$match->winner->increment("coins_balance", $totalPrize);
}
return response()->json($match);
@@ -198,34 +236,44 @@ class MatchController extends Controller
public function host(Request $request)
{
$request->validate([
'type' => 'required|in:3,9',
'stake' => 'required|integer|min:0'
"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);
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();
$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 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);
$user->decrement("coins_balance", $stake);
$stakeType = CoinTransactionType::firstOrCreate(
['name' => 'Match stake'],
['type' => 'D'] // Debit
["name" => "Match stake"],
["type" => "D"], // Debit
);
$match = MatchGame::create([
@@ -239,14 +287,15 @@ class MatchController extends Controller
'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,
"user_id" => $user->id,
"match_id" => $match->id,
"coin_transaction_type_id" => $stakeType->id,
"transaction_datetime" => now(),
"coins" => -$stake,
]);
return response()->json($match, 201);
+155 -95
View File
@@ -18,23 +18,23 @@ class UserController extends Controller
*/
public function index(Request $request)
{
$this->authorize('viewAny', User::class);
$this->authorize("viewAny", User::class);
$query = User::query();
if ($request->has('type')) {
$query->where('type', $request->type);
if ($request->has("type")) {
$query->where("type", $request->type);
}
if ($request->has('blocked')) {
$query->where('blocked', $request->type);
if ($request->has("blocked")) {
$query->where("blocked", $request->type);
}
if ($request->has('search')) {
if ($request->has("search")) {
$query->where(function ($q) use ($request) {
$q->where('name', 'like', '%'.$request->search.'%')
->orWhere('email', 'like', '%'.$request->search.'%')
->orWhere('nickname', 'like', '%'.$request->search.'%');
$q->where("name", "like", "%" . $request->search . "%")
->orWhere("email", "like", "%" . $request->search . "%")
->orWhere("nickname", "like", "%" . $request->search . "%");
});
}
@@ -47,16 +47,16 @@ class UserController extends Controller
*/
public function store(Request $request)
{
$this->authorize('create', User::class);
$this->authorize("create", User::class);
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:3',
'type' => 'required|in:A,U',
"name" => "required|string|max:255",
"email" => "required|email|unique:users,email",
"password" => "required|string|min:3",
"type" => "required|in:A,U",
]);
$validated['password'] = Hash::make($validated['password']);
$validated["password"] = Hash::make($validated["password"]);
$user = User::create($validated);
@@ -82,15 +82,15 @@ class UserController extends Controller
$currentUser = Auth::user();
// Admins and owners get full profile, others get limited view
if ($currentUser->type === 'A' || $currentUser->id === $user->id) {
if ($currentUser->type === "A" || $currentUser->id === $user->id) {
return response()->json($user);
} else {
return response()->json([
'id' => $user->id,
'name' => $user->name,
'nickname' => $user->nickname,
'photo_avatar_filename' => $user->photo_avatar_filename,
'type' => $user->type,
"id" => $user->id,
"name" => $user->name,
"nickname" => $user->nickname,
"photo_avatar_filename" => $user->photo_avatar_filename,
"type" => $user->type,
]);
}
}
@@ -101,22 +101,22 @@ class UserController extends Controller
*/
public function update(Request $request, User $user)
{
$this->authorize('update', $user);
$this->authorize("update", $user);
$validated = $request->validate([
'name' => 'sometimes|string|max:255',
'nickname' => [
'sometimes',
'string',
'max:20',
Rule::unique('users')->ignore($user->id),
"name" => "sometimes|string|max:255",
"nickname" => [
"sometimes",
"string",
"max:20",
Rule::unique("users")->ignore($user->id),
],
'email' => [
'sometimes',
'email',
Rule::unique('users')->ignore($user->id),
"email" => [
"sometimes",
"email",
Rule::unique("users")->ignore($user->id),
],
'password' => 'sometimes|string|min:3',
"password" => "sometimes|string|min:3",
]);
// Lógica de Upload de Foto (Exemplo Básico)
@@ -130,26 +130,28 @@ class UserController extends Controller
return response()->json($user);
}
public function updatePassword(Request $request, User $user)
{
$this->authorize('update', $user);
$this->authorize("update", $user);
$validated = $request->validate([
'current_password' => 'required|string',
'new_password' => 'required|string|min:3|confirmed',
"current_password" => "required|string",
"new_password" => "required|string|min:3|confirmed",
]);
// Verificar se a password atual está correta
if (!Hash::check($validated['current_password'], $user->password)) {
return response()->json(['message' => 'Current password is incorrect'], 403);
if (!Hash::check($validated["current_password"], $user->password)) {
return response()->json(
["message" => "Current password is incorrect"],
403,
);
}
// Atualizar para a nova password
$user->password = Hash::make($validated['new_password']);
$user->password = Hash::make($validated["new_password"]);
$user->save();
return response()->json(['message' => 'Password updated successfully']);
return response()->json(["message" => "Password updated successfully"]);
}
/**
@@ -158,24 +160,27 @@ class UserController extends Controller
*/
public function destroy(User $user)
{
$this->authorize('delete', $user);
$this->authorize("delete", $user);
// Check if user has transactions or games - if so, soft delete
$hasTransactions = $user->transactions()->exists();
$hasGames = 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,
);
})->exists();
if ($hasTransactions || $hasGames) {
$user->delete(); // Soft delete
$message = 'User account deactivated (soft-deleted due to transaction/game history)';
$message =
"User account deactivated (soft-deleted due to transaction/game history)";
} else {
$user->forceDelete(); // Hard delete
$message = 'User permanently deleted';
$message = "User permanently deleted";
}
return response()->json(['message' => $message]);
return response()->json(["message" => $message]);
}
/**
@@ -184,37 +189,61 @@ class UserController extends Controller
*/
public function getGames(Request $request, User $user)
{
$this->authorize('view', $user);
$this->authorize("view", $user);
// Define a query base para GAMES
$query = \App\Models\Game::query()
->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,
);
})
->with(['winner', 'player1', 'player2']);
->with(["winner", "player1", "player2"]);
if ($request->has('type')) {
$query->where('type', $request->type);
if ($request->has("type")) {
$query->where("type", $request->type);
}
if ($request->has('status')) {
$query->where('status', $request->status);
if ($request->has("status")) {
$query->where("status", $request->status);
}
$sortDirection = $request->get('sort_direction', 'desc');
$query->orderBy('began_at', $sortDirection);
// Date filtering
if ($request->has("date_from")) {
$query->whereDate("began_at", ">=", $request->date_from);
}
if ($request->has("date_to")) {
$query->whereDate("began_at", "<=", $request->date_to);
}
// Flexible sorting
$sortColumn = $request->get("sort_by", "began_at");
$sortDirection = $request->get("sort_direction", "desc");
// Whitelist sortable columns for security
$allowedColumns = [
"began_at",
"ended_at",
"total_time",
"total_points",
];
if (in_array($sortColumn, $allowedColumns)) {
$query->orderBy($sortColumn, $sortDirection);
} else {
$query->orderBy("began_at", $sortDirection);
}
$games = $query->paginate(10);
$games->getCollection()->transform(function ($game) use ($user) {
if ($game->winner_user_id == $user->id) {
$game->outcome = 'win';
$game->outcome = "win";
} elseif ($game->is_draw) {
$game->outcome = 'draw';
$game->outcome = "draw";
} else {
$game->outcome = 'loss';
$game->outcome = "loss";
}
return $game;
});
@@ -228,35 +257,58 @@ class UserController extends Controller
*/
public function getMatches(Request $request, User $user)
{
$this->authorize('view', $user);
$this->authorize("view", $user);
$query = MatchGame::query()
$query = MatchGame::query()
->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,
);
})
->with(['winner', 'player1', 'player2']);
->with(["winner", "player1", "player2"]);
if ($request->has('type')) {
$query->where('type', $request->type);
}
if ($request->has('status')) {
$query->where('status', $request->status);
if ($request->has("type")) {
$query->where("type", $request->type);
}
$sortDirection = $request->get('sort_direction', 'desc');
$query->orderBy('began_at', $sortDirection);
if ($request->has("status")) {
$query->where("status", $request->status);
}
// Date filtering
if ($request->has("date_from")) {
$query->whereDate("began_at", ">=", $request->date_from);
}
if ($request->has("date_to")) {
$query->whereDate("began_at", "<=", $request->date_to);
}
// Flexible sorting
$sortColumn = $request->get("sort_by", "began_at");
$sortDirection = $request->get("sort_direction", "desc");
// Whitelist sortable columns for security
$allowedColumns = ["began_at", "ended_at", "stake", "total_time"];
if (in_array($sortColumn, $allowedColumns)) {
$query->orderBy($sortColumn, $sortDirection);
} else {
$query->orderBy("began_at", $sortDirection);
}
$matches = $query->paginate(10);
$matches->getCollection()->transform(function ($match) use ($user) {
if ($match->winner_user_id == $user->id) {
$match->outcome = 'win';
} elseif ($match->winner_user_id && $match->winner_user_id != $user->id) {
$match->outcome = 'loss';
$match->outcome = "win";
} elseif (
$match->winner_user_id &&
$match->winner_user_id != $user->id
) {
$match->outcome = "loss";
} else {
$match->outcome = 'interrupted'; // Matches geralmente não empatam (alguém chega a 4 marcas)
$match->outcome = "interrupted"; // Matches geralmente não empatam (alguém chega a 4 marcas)
}
return $match;
});
@@ -266,7 +318,7 @@ class UserController extends Controller
public function block(User $user)
{
$this->authorize('block', $user);
$this->authorize("block", $user);
$user->blocked = true;
$user->save();
@@ -274,43 +326,51 @@ class UserController extends Controller
// Revoke all active tokens for immediate effect
$user->tokens()->delete();
return response()->json(['message' => 'User blocked successfully']);
return response()->json(["message" => "User blocked successfully"]);
}
public function unblock(User $user)
{
$this->authorize('unblock', $user);
$this->authorize("unblock", $user);
$user->blocked = false;
$user->save();
return response()->json(['message' => 'User unblocked successfully']);
return response()->json(["message" => "User unblocked successfully"]);
}
public function getTransactions(Request $request, User $user)
{
if ($request->user()->id !== $user->id && $request->user()->type !== 'A') {
return response()->json(['message' => 'Forbidden'], 403);
if (
$request->user()->id !== $user->id &&
$request->user()->type !== "A"
) {
return response()->json(["message" => "Forbidden"], 403);
}
$query = $user->transactions()->with('type');
$query = $user->transactions()->with("type");
if ($request->has('type')) {
$query->whereHas('type', function ($q) use ($request) {
$q->where('type', $request->type);
if ($request->has("type")) {
$query->whereHas("type", function ($q) use ($request) {
$q->where("type", $request->type);
});
}
if ($request->has('date_from')) {
$query->whereDate('transaction_datetime', '>=', $request->date_from);
if ($request->has("date_from")) {
$query->whereDate(
"transaction_datetime",
">=",
$request->date_from,
);
}
if ($request->has('date_to')) {
$query->whereDate('transaction_datetime', '<=', $request->date_to);
if ($request->has("date_to")) {
$query->whereDate("transaction_datetime", "<=", $request->date_to);
}
$transactions = $query->orderBy('transaction_datetime', 'desc')
->paginate(10);
$transactions = $query
->orderBy("transaction_datetime", "desc")
->paginate(10);
return response()->json($transactions);
}