Files
chatroom/app/Models/GameConfig.php

81 lines
1.9 KiB
PHP

<?php
/**
* 文件功能:游戏配置模型
*
* 集中管理所有娱乐游戏的开关与参数,提供统一的读取接口。
* 游戏服务层通过 GameConfig::forGame($key) 读取当前配置。
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class GameConfig extends Model
{
protected $fillable = [
'game_key',
'name',
'icon',
'description',
'enabled',
'params',
];
/**
* 属性类型转换。
*/
protected function casts(): array
{
return [
'enabled' => 'boolean',
'params' => 'array',
];
}
// ─── 静态工具方法 ─────────────────────────────────────────────────
/**
* 根据游戏标识获取配置(带缓存)。
*
* @param string $key 游戏标识,如 'baccarat'
*/
public static function forGame(string $key): ?static
{
return Cache::remember("game_config:{$key}", 60, fn () => static::where('game_key', $key)->first());
}
/**
* 判断游戏是否已开启。
*/
public static function isEnabled(string $key): bool
{
return (bool) static::forGame($key)?->enabled;
}
/**
* 获取指定游戏的某个参数值,不存在时返回默认值。
*
* @param string $key 游戏标识
* @param string $param 参数名
* @param mixed $default 默认值
*/
public static function param(string $key, string $param, mixed $default = null): mixed
{
return static::forGame($key)?->params[$param] ?? $default;
}
/**
* 清除游戏配置缓存。
*/
public function clearCache(): void
{
Cache::forget("game_config:{$this->game_key}");
}
}