Initialer Laravel Commit für BetiX
This commit is contained in:
63
app/Services/GameService.php
Normal file
63
app/Services/GameService.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\GameBet;
|
||||
use App\Models\User;
|
||||
use App\Services\BonusService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class GameService
|
||||
{
|
||||
public function __construct(protected BonusService $bonusService)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle game outcome and update user balance atomically
|
||||
*/
|
||||
public function handleGameResponse(User $user, array $data): void
|
||||
{
|
||||
$newBalance = $data['balance'] ?? $data['newBalance'] ?? null;
|
||||
$wager = $data['bet'] ?? 0;
|
||||
|
||||
if ($newBalance !== null) {
|
||||
DB::transaction(function() use ($user, $newBalance) {
|
||||
$user->refresh();
|
||||
$user->balance = $newBalance;
|
||||
$user->save();
|
||||
});
|
||||
Log::info("GameService: Synced User {$user->id} balance to $newBalance");
|
||||
}
|
||||
|
||||
// Track wagering if applicable
|
||||
if ($wager > 0) {
|
||||
$this->bonusService->trackWagering($user, (float) $wager);
|
||||
}
|
||||
|
||||
// Log bet if info is available
|
||||
if (isset($data['bet']) || isset($data['win'])) {
|
||||
$this->logBet($user, $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a game bet into the database
|
||||
*/
|
||||
protected function logBet(User $user, array $data): void
|
||||
{
|
||||
try {
|
||||
GameBet::create([
|
||||
'user_id' => $user->id,
|
||||
'game_name' => $data['game'] ?? 'Unknown',
|
||||
'wager_amount' => $data['bet'] ?? 0,
|
||||
'payout_amount' => $data['win'] ?? 0,
|
||||
'payout_multiplier' => ($data['bet'] > 0) ? ($data['win'] / $data['bet']) : 0,
|
||||
'currency' => 'BTX'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error("GameService: Failed to log bet: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user