SyntaxSquad/DADProject#16 feat: Can now start matches, just missing the actuall gameplay

This commit is contained in:
AfonsoCMSousa
2026-01-03 04:52:02 +00:00
parent 3dcd8df4c7
commit e0ca8e51a6
12 changed files with 793 additions and 490 deletions
+22 -17
View File
@@ -10,7 +10,7 @@ use App\Http\Requests\StoreMatchRequest;
use Illuminate\Support\Facades\DB;
class MatchController extends Controller
{
{
public function index(Request $request)
{
$query = MatchGame::query()->with(['winner', 'player1', 'player2']);
@@ -29,9 +29,9 @@ class MatchController extends Controller
}
public function store(StoreMatchRequest $request)
{
{
$validated = $request->validated();
$user = $request->user();
$user = $request->user();
$stake = $validated['stake'];
if ($user->coins_balance < $stake) {
@@ -43,7 +43,7 @@ class MatchController extends Controller
->orWhere('player2_user_id', $user->id);
})->whereIn('status', ['Pending', 'Playing'])->exists();
if ($activeMatch) {
if ($activeMatch) {
return response()->json(['message' => 'You already have an active match.'], 400);
}
@@ -61,7 +61,7 @@ class MatchController extends Controller
'type' => $validated['type'],
'status' => 'Pending',
'player1_user_id' => $user->id,
'player2_user_id' => $GHOST_ID,
'player2_user_id' => $GHOST_ID,
'winner_user_id' => $GHOST_ID,
'loser_user_id' => $GHOST_ID,
'stake' => $stake,
@@ -129,7 +129,7 @@ class MatchController extends Controller
'match_id' => $match->id,
'coin_transaction_type_id' => $stakeType->id,
'transaction_datetime' => now(),
'coins' => -$match->stake,
'coins' => -$match->stake,
]);
$match->update([
@@ -163,7 +163,7 @@ class MatchController extends Controller
]);
return DB::transaction(function () use ($match, $data) {
if (isset($data['status']) && $data['status'] === 'Ended') {
$data['ended_at'] = now();
}
@@ -171,7 +171,7 @@ class MatchController extends Controller
$match->update($data);
if ($match->status === 'Ended' && $match->stake > 0 && $match->winner_user_id) {
$payoutType = CoinTransactionType::firstOrCreate(
['name' => 'Match payout'],
['type' => 'C'] // Credit
@@ -232,7 +232,7 @@ class MatchController extends Controller
'type' => $request->type,
'status' => 'Pending',
'player1_user_id' => $user->id,
'player2_user_id' => $GHOST_ID,
'player2_user_id' => $GHOST_ID,
'winner_user_id' => null,
'loser_user_id' => null,
'stake' => $stake,
@@ -255,16 +255,21 @@ class MatchController extends Controller
public function open(Request $request)
{
$type = $request->query('type', '9');
$GHOST_ID = 1;
// FIX: Allow matches where P2 is NULL OR 'Ghost' (ID 1)
$query = MatchGame::where('status', 'Pending')
->where(function($q) {
$q->whereNull('player2_user_id')
->orWhere('player2_user_id', 1); // 1 = Ghost ID
});
$matches = MatchGame::where('status', 'Pending')
->where('player2_user_id', $GHOST_ID)
->where('type', $type)
->with('player1')
->orderBy('began_at', 'asc')
if ($request->has('type')) {
$query->where('type', $request->type);
}
$matches = $query->with('player1:id,nickname')
->orderBy('began_at', 'desc')
->paginate(10);
return response()->json($matches);
}
}
}
+4 -3
View File
@@ -58,10 +58,11 @@ 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('host', [MatchController::class, 'host']); // <--- NOVA
Route::post('host', [MatchController::class, 'host']);
Route::get('open', [MatchController::class, 'open']);
Route::post('/{match}/join', [MatchController::class, 'join']);
Route::post('/{match}/start', [MatchController::class, 'start']); // Changed to /start to be explicit
Route::apiResource('/', MatchController::class)->parameters(['' => 'match']);
});
// Admin Routes