fix:admin & user match table sort #98
@@ -13,17 +13,26 @@ class MatchController extends Controller
|
|||||||
{
|
{
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$query = MatchGame::query()->with(['winner', 'player1', 'player2']);
|
$query = MatchGame::query()->with(["winner", "player1", "player2"]);
|
||||||
|
|
||||||
if ($request->has('type')) {
|
if ($request->has("type")) {
|
||||||
$query->where('type', $request->type);
|
$query->where("type", $request->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->has('status')) {
|
if ($request->has("status")) {
|
||||||
$query->where('status', $request->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));
|
return response()->json($query->paginate(15));
|
||||||
}
|
}
|
||||||
@@ -32,63 +41,78 @@ class MatchController extends Controller
|
|||||||
{
|
{
|
||||||
$validated = $request->validated();
|
$validated = $request->validated();
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
$stake = $validated['stake'];
|
$stake = $validated["stake"];
|
||||||
|
|
||||||
if ($user->coins_balance < $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) {
|
$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();
|
||||||
|
|
||||||
if ($activeMatch) {
|
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 ($validated, $user, $stake) {
|
return DB::transaction(function () use ($validated, $user, $stake) {
|
||||||
$GHOST_ID = 1;
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
$user->decrement('coins_balance', $stake);
|
$user->decrement("coins_balance", $stake);
|
||||||
|
|
||||||
$stakeType = CoinTransactionType::firstOrCreate(
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
['name' => 'Match stake'],
|
["name" => "Match stake"],
|
||||||
['type' => 'D'] // Debit
|
["type" => "D"], // Debit
|
||||||
);
|
);
|
||||||
|
|
||||||
$match = MatchGame::create([
|
$match = MatchGame::create([
|
||||||
'type' => $validated['type'],
|
"type" => $validated["type"],
|
||||||
'status' => 'Pending',
|
"status" => "Pending",
|
||||||
'player1_user_id' => $user->id,
|
"player1_user_id" => $user->id,
|
||||||
'player2_user_id' => $GHOST_ID,
|
"player2_user_id" => $GHOST_ID,
|
||||||
'winner_user_id' => $GHOST_ID,
|
"winner_user_id" => $GHOST_ID,
|
||||||
'loser_user_id' => $GHOST_ID,
|
"loser_user_id" => $GHOST_ID,
|
||||||
'stake' => $stake,
|
"stake" => $stake,
|
||||||
'began_at' => now(),
|
"began_at" => now(),
|
||||||
'player1_marks' => 0, 'player2_marks' => 0,
|
"player1_marks" => 0,
|
||||||
'player1_points' => 0, 'player2_points' => 0,
|
"player2_marks" => 0,
|
||||||
|
"player1_points" => 0,
|
||||||
|
"player2_points" => 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
CoinTransaction::create([
|
CoinTransaction::create([
|
||||||
'user_id' => $user->id,
|
"user_id" => $user->id,
|
||||||
'match_id' => $match->id,
|
"match_id" => $match->id,
|
||||||
'coin_transaction_type_id' => $stakeType->id,
|
"coin_transaction_type_id" => $stakeType->id,
|
||||||
'transaction_datetime' => now(),
|
"transaction_datetime" => now(),
|
||||||
'coins' => -$stake,
|
"coins" => -$stake,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json(
|
||||||
'message' => 'Match created successfully',
|
[
|
||||||
'match' => $match,
|
"message" => "Match created successfully",
|
||||||
'balance' => $user->coins_balance
|
"match" => $match,
|
||||||
], 201);
|
"balance" => $user->coins_balance,
|
||||||
|
],
|
||||||
|
201,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(MatchGame $match)
|
public function show(MatchGame $match)
|
||||||
{
|
{
|
||||||
$match->load(['player1', 'player2', 'winner']);
|
$match->load(["player1", "player2", "winner"]);
|
||||||
return response()->json($match);
|
return response()->json($match);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,98 +121,115 @@ class MatchController extends Controller
|
|||||||
$user = request()->user();
|
$user = request()->user();
|
||||||
|
|
||||||
if ($match->player1_user_id == $user->id) {
|
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') {
|
if ($match->status !== "Pending") {
|
||||||
return response()->json(['message' => 'Match is full or started'], 400);
|
return response()->json(
|
||||||
|
["message" => "Match is full or started"],
|
||||||
|
400,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->coins_balance < $match->stake) {
|
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) {
|
$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();
|
||||||
|
|
||||||
if ($activeMatch) {
|
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) {
|
return DB::transaction(function () use ($match, $user) {
|
||||||
$user->decrement('coins_balance', $match->stake);
|
$user->decrement("coins_balance", $match->stake);
|
||||||
|
|
||||||
$stakeType = CoinTransactionType::firstOrCreate(
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
['name' => 'Match stake'],
|
["name" => "Match stake"],
|
||||||
['type' => 'D']
|
["type" => "D"],
|
||||||
);
|
);
|
||||||
|
|
||||||
CoinTransaction::create([
|
CoinTransaction::create([
|
||||||
'user_id' => $user->id,
|
"user_id" => $user->id,
|
||||||
'match_id' => $match->id,
|
"match_id" => $match->id,
|
||||||
'coin_transaction_type_id' => $stakeType->id,
|
"coin_transaction_type_id" => $stakeType->id,
|
||||||
'transaction_datetime' => now(),
|
"transaction_datetime" => now(),
|
||||||
'coins' => -$match->stake,
|
"coins" => -$match->stake,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$match->update([
|
$match->update([
|
||||||
'status' => 'Playing',
|
"status" => "Playing",
|
||||||
'player2_user_id' => $user->id
|
"player2_user_id" => $user->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Joined match successfully!',
|
"message" => "Joined match successfully!",
|
||||||
'match' => $match,
|
"match" => $match,
|
||||||
'balance' => $user->coins_balance
|
"balance" => $user->coins_balance,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Request $request, MatchGame $match)
|
public function update(Request $request, MatchGame $match)
|
||||||
{
|
{
|
||||||
if ($match->status == 'Ended') {
|
if ($match->status == "Ended") {
|
||||||
return response()->json(['message' => 'Match already ended'], 400);
|
return response()->json(["message" => "Match 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_marks' => 'integer',
|
"player1_marks" => "integer",
|
||||||
'player2_marks' => 'integer',
|
"player2_marks" => "integer",
|
||||||
'player1_points' => 'integer',
|
"player1_points" => "integer",
|
||||||
'player2_points' => 'integer',
|
"player2_points" => "integer",
|
||||||
'total_time' => 'numeric'
|
"total_time" => "numeric",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return DB::transaction(function () use ($match, $data) {
|
return DB::transaction(function () use ($match, $data) {
|
||||||
|
if (isset($data["status"]) && $data["status"] === "Ended") {
|
||||||
if (isset($data['status']) && $data['status'] === 'Ended') {
|
$data["ended_at"] = now();
|
||||||
$data['ended_at'] = now();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$match->update($data);
|
$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(
|
$payoutType = CoinTransactionType::firstOrCreate(
|
||||||
['name' => 'Match payout'],
|
["name" => "Match payout"],
|
||||||
['type' => 'C'] // Credit
|
["type" => "C"], // Credit
|
||||||
);
|
);
|
||||||
|
|
||||||
$totalPrize = $match->stake * 2;
|
$totalPrize = $match->stake * 2;
|
||||||
|
|
||||||
CoinTransaction::create([
|
CoinTransaction::create([
|
||||||
'user_id' => $match->winner_user_id,
|
"user_id" => $match->winner_user_id,
|
||||||
'match_id' => $match->id,
|
"match_id" => $match->id,
|
||||||
'coin_transaction_type_id' => $payoutType->id,
|
"coin_transaction_type_id" => $payoutType->id,
|
||||||
'transaction_datetime' => now(),
|
"transaction_datetime" => now(),
|
||||||
'coins' => $totalPrize,
|
"coins" => $totalPrize,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$match->winner->increment("coins_balance", $totalPrize);
|
||||||
$match->winner->increment('coins_balance', $totalPrize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json($match);
|
return response()->json($match);
|
||||||
@@ -198,55 +239,67 @@ class MatchController extends Controller
|
|||||||
public function host(Request $request)
|
public function host(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'type' => 'required|in:3,9',
|
"type" => "required|in:3,9",
|
||||||
'stake' => 'required|integer|min:0'
|
"stake" => "required|integer|min:0",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
$stake = $request->stake;
|
$stake = $request->stake;
|
||||||
|
|
||||||
if ($user->coins_balance < $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) {
|
$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();
|
||||||
|
|
||||||
if ($activeMatch) {
|
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) {
|
return DB::transaction(function () use ($request, $user, $stake) {
|
||||||
$GHOST_ID = 1;
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
$user->decrement('coins_balance', $stake);
|
$user->decrement("coins_balance", $stake);
|
||||||
|
|
||||||
$stakeType = CoinTransactionType::firstOrCreate(
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
['name' => 'Match stake'],
|
["name" => "Match stake"],
|
||||||
['type' => 'D'] // Debit
|
["type" => "D"], // Debit
|
||||||
);
|
);
|
||||||
|
|
||||||
$match = MatchGame::create([
|
$match = MatchGame::create([
|
||||||
'type' => $request->type,
|
"type" => $request->type,
|
||||||
'status' => 'Pending',
|
"status" => "Pending",
|
||||||
'player1_user_id' => $user->id,
|
"player1_user_id" => $user->id,
|
||||||
'player2_user_id' => $GHOST_ID,
|
"player2_user_id" => $GHOST_ID,
|
||||||
'winner_user_id' => null,
|
"winner_user_id" => null,
|
||||||
'loser_user_id' => null,
|
"loser_user_id" => null,
|
||||||
'stake' => $stake,
|
"stake" => $stake,
|
||||||
'began_at' => now(),
|
"began_at" => now(),
|
||||||
'player1_marks' => 0, 'player2_marks' => 0,
|
"player1_marks" => 0,
|
||||||
'player1_points' => 0, 'player2_points' => 0,
|
"player2_marks" => 0,
|
||||||
|
"player1_points" => 0,
|
||||||
|
"player2_points" => 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
CoinTransaction::create([
|
CoinTransaction::create([
|
||||||
'user_id' => $user->id,
|
"user_id" => $user->id,
|
||||||
'match_id' => $match->id,
|
"match_id" => $match->id,
|
||||||
'coin_transaction_type_id' => $stakeType->id,
|
"coin_transaction_type_id" => $stakeType->id,
|
||||||
'transaction_datetime' => now(),
|
"transaction_datetime" => now(),
|
||||||
'coins' => -$stake,
|
"coins" => -$stake,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json($match, 201);
|
return response()->json($match, 201);
|
||||||
@@ -255,14 +308,14 @@ class MatchController extends Controller
|
|||||||
|
|
||||||
public function open(Request $request)
|
public function open(Request $request)
|
||||||
{
|
{
|
||||||
$type = $request->query('type', '9');
|
$type = $request->query("type", "9");
|
||||||
$GHOST_ID = 1;
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
$matches = MatchGame::where('status', 'Pending')
|
$matches = MatchGame::where("status", "Pending")
|
||||||
->where('player2_user_id', $GHOST_ID)
|
->where("player2_user_id", $GHOST_ID)
|
||||||
->where('type', $type)
|
->where("type", $type)
|
||||||
->with('player1')
|
->with("player1")
|
||||||
->orderBy('began_at', 'asc')
|
->orderBy("began_at", "asc")
|
||||||
->paginate(10);
|
->paginate(10);
|
||||||
|
|
||||||
return response()->json($matches);
|
return response()->json($matches);
|
||||||
|
|||||||
Reference in New Issue
Block a user