Initialer Laravel Commit für BetiX
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

This commit is contained in:
2026-04-04 18:01:50 +02:00
commit 0280278978
374 changed files with 65210 additions and 0 deletions

View 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());
}
}
}