SyntaxSquad/DADProject#16 feat: Can now start matches, just missing the actuall gameplay
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user