109 lines
2.8 KiB
PHP
109 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $character_id
|
|
* @property string $action
|
|
* @property string|null $description
|
|
* @property string|null $ip_address
|
|
* @property string|null $user_agent
|
|
* @property \Illuminate\Support\Carbon $created_at
|
|
*/
|
|
class ActivityLog extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'character_id',
|
|
'action',
|
|
'description',
|
|
'ip_address',
|
|
'user_agent',
|
|
];
|
|
|
|
/**
|
|
* Get the character associated with this activity log.
|
|
*/
|
|
public function character(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Character::class);
|
|
}
|
|
|
|
/**
|
|
* Create a new activity log entry
|
|
*/
|
|
public static function log(Character $character, string $action, string $description = null, string $ipAddress = null, string $userAgent = null): self
|
|
{
|
|
return self::create([
|
|
'character_id' => $character->id,
|
|
'action' => $action,
|
|
'description' => $description,
|
|
'ip_address' => $ipAddress,
|
|
'user_agent' => $userAgent,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Log a crime attempt
|
|
*/
|
|
public static function logCrimeAttempt(Character $character, Crime $crime, bool $success, int $reward = 0): self
|
|
{
|
|
$result = $success ? 'success' : 'failure';
|
|
$description = "Attempted crime: {$crime->name} - {$result}";
|
|
|
|
if ($success) {
|
|
$description .= " (Reward: \${$reward})";
|
|
}
|
|
|
|
return self::log($character, 'crime_attempt', $description);
|
|
}
|
|
|
|
/**
|
|
* Log money transaction
|
|
*/
|
|
public static function logMoneyTransaction(Character $character, string $type, int $amount, string $reason): self
|
|
{
|
|
$action = match($type) {
|
|
'earned' => 'money_earned',
|
|
'spent' => 'money_spent',
|
|
'deposited' => 'money_deposited',
|
|
'withdrew' => 'money_withdrew',
|
|
default => 'money_transaction',
|
|
};
|
|
|
|
$description = "\${$amount} {$type} - {$reason}";
|
|
|
|
return self::log($character, $action, $description);
|
|
}
|
|
|
|
/**
|
|
* Log level up
|
|
*/
|
|
public static function logLevelUp(Character $character, int $newLevel): self
|
|
{
|
|
return self::log(
|
|
$character,
|
|
'level_up',
|
|
"Reached level {$newLevel}"
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Log character creation
|
|
*/
|
|
public static function logCharacterCreated(Character $character): self
|
|
{
|
|
return self::log(
|
|
$character,
|
|
'character_created',
|
|
"New character created: {$character->name}"
|
|
);
|
|
}
|
|
}
|