Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf238d5451 | ||
|
|
50ae1b7f1f | ||
|
|
5975208877 | ||
|
|
f3e8ffc400 |
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\CoinPurchase;
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
use App\Models\CoinTransactionType;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class CoinPurchaseController extends Controller
|
||||||
|
{
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'euros' => 'required|numeric|min:1|max:100',
|
||||||
|
'payment_type' => 'required|in:MBWAY,IBAN,MB,VISA,PAYPAL',
|
||||||
|
'payment_reference' => [
|
||||||
|
'required',
|
||||||
|
function ($attribute, $value, $fail) use ($request) {
|
||||||
|
$type = $request->payment_type;
|
||||||
|
$regex = null;
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case 'MBWAY': $regex = '/^9[0-9]{8}$/'; break;
|
||||||
|
case 'IBAN': $regex = '/^[A-Z]{2}[0-9]{23}$/'; break;
|
||||||
|
case 'MB': $regex = '/^[0-9]{5}-[0-9]{9}$/'; break;
|
||||||
|
case 'VISA': $regex = '/^4[0-9]{15}$/'; break;
|
||||||
|
case 'PAYPAL':
|
||||||
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
$fail("Invalid PayPal email.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($regex && !preg_match($regex, $value)) {
|
||||||
|
$fail("Invalid format for $type.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
$coinsAmount = $validated['euros'] * 10;
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($user, $validated, $coinsAmount) {
|
||||||
|
|
||||||
|
$type = CoinTransactionType::firstOrCreate(['name' => 'Coin purchase'], ['type' => 'C']);
|
||||||
|
|
||||||
|
$transaction = CoinTransaction::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'coin_transaction_type_id' => $type->id,
|
||||||
|
'transaction_datetime' => now(),
|
||||||
|
'coins' => $coinsAmount,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$purchase = CoinPurchase::create([
|
||||||
|
'purchase_datetime' => now(),
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'coin_transaction_id' => $transaction->id,
|
||||||
|
'euros' => $validated['euros'],
|
||||||
|
'payment_type' => $validated['payment_type'],
|
||||||
|
'payment_reference' => $validated['payment_reference'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user->increment('coins_balance', $coinsAmount);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Purchase successful',
|
||||||
|
'balance' => $user->coins_balance,
|
||||||
|
'transaction_id' => $transaction->id,
|
||||||
|
'purchase' => $purchase,
|
||||||
|
], 201);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,9 +4,7 @@ namespace App\Http\Controllers;
|
|||||||
|
|
||||||
use App\Models\Game;
|
use App\Models\Game;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Requests\StoreGameRequest;
|
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use App\Models\MatchGame;
|
|
||||||
|
|
||||||
class GameController extends Controller
|
class GameController extends Controller
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,9 +4,12 @@ namespace App\Http\Controllers;
|
|||||||
|
|
||||||
use App\Models\MatchGame;
|
use App\Models\MatchGame;
|
||||||
use App\Models\Game;
|
use App\Models\Game;
|
||||||
|
use App\Models\CoinTransaction;
|
||||||
|
use App\Models\CoinTransactionType;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Requests\StoreMatchRequest;
|
use App\Http\Requests\StoreMatchRequest;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class MatchController extends Controller
|
class MatchController extends Controller
|
||||||
{
|
{
|
||||||
@@ -30,42 +33,59 @@ class MatchController extends Controller
|
|||||||
public function store(StoreMatchRequest $request)
|
public function store(StoreMatchRequest $request)
|
||||||
{
|
{
|
||||||
$validated = $request->validated();
|
$validated = $request->validated();
|
||||||
$user = Auth::user();
|
$user = $request->user();
|
||||||
|
$stake = $validated['stake'];
|
||||||
|
|
||||||
|
if ($user->coins_balance < $stake) {
|
||||||
|
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('player2_user_id', $user->id);
|
->orWhere('player2_user_id', $user->id);
|
||||||
})->whereIn('status', ['Pending', 'Playing'])->exists();
|
})->whereIn('status', ['Pending', 'Playing'])->exists();
|
||||||
|
|
||||||
$activeGame = Game::where(function ($q) use ($user) {
|
if ($activeMatch) {
|
||||||
$q->where('player1_user_id', $user->id)
|
return response()->json(['message' => 'You already have an active match.'], 400);
|
||||||
->orWhere('player2_user_id', $user->id);
|
|
||||||
})->whereIn('status', ['Pending', 'Playing'])->exists();
|
|
||||||
|
|
||||||
if ($activeMatch || $activeGame) {
|
|
||||||
return response()->json(['message' => 'You already have an active game or match.'], 400);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return DB::transaction(function () use ($validated, $user, $stake) {
|
||||||
|
$GHOST_ID = 1;
|
||||||
|
|
||||||
|
$user->decrement('coins_balance', $stake);
|
||||||
|
|
||||||
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
|
['name' => 'Match stake'],
|
||||||
|
['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' => $user->id,
|
'player2_user_id' => $GHOST_ID,
|
||||||
'winner_user_id' => $user->id,
|
'winner_user_id' => $GHOST_ID,
|
||||||
'loser_user_id' => $user->id,
|
'loser_user_id' => $GHOST_ID,
|
||||||
'stake' => $validated['stake'],
|
'stake' => $stake,
|
||||||
'began_at' => now(),
|
'began_at' => now(),
|
||||||
'player1_marks' => 0,
|
'player1_marks' => 0, 'player2_marks' => 0,
|
||||||
'player2_marks' => 0,
|
'player1_points' => 0, 'player2_points' => 0,
|
||||||
'player1_points' => 0,
|
]);
|
||||||
'player2_points' => 0,
|
|
||||||
'custom' => null
|
CoinTransaction::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'match_id' => $match->id,
|
||||||
|
'coin_transaction_type_id' => $stakeType->id,
|
||||||
|
'transaction_datetime' => now(),
|
||||||
|
'coins' => -$stake,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Match created successfully',
|
'message' => 'Match created successfully',
|
||||||
'match' => $match
|
'match' => $match,
|
||||||
|
'balance' => $user->coins_balance
|
||||||
], 201);
|
], 201);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(MatchGame $match)
|
public function show(MatchGame $match)
|
||||||
@@ -76,16 +96,19 @@ class MatchController extends Controller
|
|||||||
|
|
||||||
public function join(MatchGame $match)
|
public function join(MatchGame $match)
|
||||||
{
|
{
|
||||||
$user = Auth::user();
|
$user = request()->user();
|
||||||
|
|
||||||
if ($match->player1_user_id == $user->id) {
|
if ($match->player1_user_id == $user->id) {
|
||||||
return response()->json(['message' => 'Waiting for opponent...', 'match' => $match]);
|
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) {
|
||||||
|
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('player2_user_id', $user->id);
|
->orWhere('player2_user_id', $user->id);
|
||||||
@@ -95,6 +118,22 @@ class MatchController extends Controller
|
|||||||
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);
|
||||||
|
|
||||||
|
$stakeType = CoinTransactionType::firstOrCreate(
|
||||||
|
['name' => 'Match stake'],
|
||||||
|
['type' => 'D']
|
||||||
|
);
|
||||||
|
|
||||||
|
CoinTransaction::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'match_id' => $match->id,
|
||||||
|
'coin_transaction_type_id' => $stakeType->id,
|
||||||
|
'transaction_datetime' => now(),
|
||||||
|
'coins' => -$match->stake,
|
||||||
|
]);
|
||||||
|
|
||||||
$match->update([
|
$match->update([
|
||||||
'status' => 'Playing',
|
'status' => 'Playing',
|
||||||
'player2_user_id' => $user->id
|
'player2_user_id' => $user->id
|
||||||
@@ -102,8 +141,10 @@ class MatchController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Joined match successfully!',
|
'message' => 'Joined match successfully!',
|
||||||
'match' => $match
|
'match' => $match,
|
||||||
|
'balance' => $user->coins_balance
|
||||||
]);
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(Request $request, MatchGame $match)
|
public function update(Request $request, MatchGame $match)
|
||||||
@@ -123,12 +164,36 @@ class MatchController extends Controller
|
|||||||
'total_time' => 'numeric'
|
'total_time' => 'numeric'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|
||||||
|
$payoutType = CoinTransactionType::firstOrCreate(
|
||||||
|
['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,
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
$match->winner->increment('coins_balance', $totalPrize);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($match);
|
return response()->json($match);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class CoinPurchase extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'coin_purchases';
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'purchase_datetime',
|
||||||
|
'user_id',
|
||||||
|
'coin_transaction_id',
|
||||||
|
'euros',
|
||||||
|
'payment_type',
|
||||||
|
'payment_reference',
|
||||||
|
'custom'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'purchase_datetime' => 'datetime',
|
||||||
|
'custom' => 'array',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function transaction()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(CoinTransaction::class, 'coin_transaction_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ use App\Http\Controllers\MatchController;
|
|||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
use App\Http\Controllers\StatisticsController;
|
use App\Http\Controllers\StatisticsController;
|
||||||
use App\Http\Controllers\UserController;
|
use App\Http\Controllers\UserController;
|
||||||
|
use App\Http\Controllers\CoinPurchaseController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::prefix('v1')->group(function () {
|
Route::prefix('v1')->group(function () {
|
||||||
@@ -19,6 +20,7 @@ Route::prefix('v1')->group(function () {
|
|||||||
// Authenticated Routes
|
// Authenticated Routes
|
||||||
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
|
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
|
||||||
Route::post('/logout', [AuthController::class, 'logout']);
|
Route::post('/logout', [AuthController::class, 'logout']);
|
||||||
|
Route::post('/coin-purchases', [CoinPurchaseController::class, 'store']);
|
||||||
Route::get('/statistics/me', [StatisticsController::class, 'getPersonalStats']);
|
Route::get('/statistics/me', [StatisticsController::class, 'getPersonalStats']);
|
||||||
Route::prefix('users/me')->group(function () {
|
Route::prefix('users/me')->group(function () {
|
||||||
Route::get('/', [ProfileController::class, 'show']);
|
Route::get('/', [ProfileController::class, 'show']);
|
||||||
|
|||||||
Reference in New Issue
Block a user