Files
Skunk-Lounge/app/Models/PlayerStats.php

70 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PlayerStats extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'game_id',
'total_winnings',
'total_bets',
'play_count',
'daily_play_amount',
'win_streak',
'last_played_at',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'last_played_at' => 'datetime',
];
/**
* Get the user that owns the stats.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the game for the stats (if per-game).
*/
public function game(): BelongsTo
{
return $this->belongsTo(Game::class);
}
/**
* Custom method to update stats after a game play (example).
*
* @param float $betAmount
* @param float $winAmount
*/
public function updateAfterPlay(float $betAmount, float $winAmount): void
{
$this->increment('play_count');
$this->total_bets += $betAmount;
$this->total_winnings += $winAmount;
$this->daily_play_amount += $betAmount; // Simplify; use cron for daily reset
$this->win_streak = $winAmount > 0 ? $this->win_streak + 1 : 0;
$this->last_played_at = now();
$this->save();
}
}