| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Module\Game\Models;
- use App\Module\Game\Enums\GameConfigGroup;
- use App\Module\Game\Enums\GameConfigType;
- use UCore\ModelCore;
- /**
- * 游戏配置模型
- *
- * field start
- * @property int $id 配置ID,主键
- * @property string $key 配置键名,唯一标识
- * @property string $name 配置名称
- * @property string $description 配置描述
- * @property \App\Module\Game\Enums\GameConfigGroup $group 配置分组
- * @property \App\Module\Game\Enums\GameConfigType $type 配置类型:1布尔值,2整数,3小数,4字符串,5JSON
- * @property string $value 配置值
- * @property string $default_value 默认值
- * @property array $options 可选项配置(JSON格式)
- * @property string $validation_rules 验证规则
- * @property bool $is_enabled 是否启用:0禁用,1启用
- * @property bool $is_readonly 是否只读:0可编辑,1只读
- * @property int $sort_order 排序权重
- * @property string $remark 备注说明
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class GameConfig extends ModelCore
- {
- protected $table = 'game_configs';
- protected $fillable = [
- 'key',
- 'name',
- 'description',
- 'group',
- 'type',
- 'value',
- 'default_value',
- 'options',
- 'validation_rules',
- 'is_enabled',
- 'is_readonly',
- 'sort_order',
- 'remark',
- ];
- protected $casts = [
- 'group' => GameConfigGroup::class,
- 'type' => GameConfigType::class,
- 'is_enabled' => 'boolean',
- 'is_readonly' => 'boolean',
- 'sort_order' => 'integer',
- 'options' => 'array',
- ];
- /**
- * 获取配置的实际值(根据类型转换)
- */
- public function getTypedValue()
- {
- if (!$this->is_enabled) {
- return $this->type->castValue($this->default_value);
- }
- return $this->type->castValue($this->value);
- }
- /**
- * 设置配置值(根据类型转换)
- */
- public function setTypedValue($value): bool
- {
- if (!$this->type->validateValue($value)) {
- return false;
- }
- // 对于JSON类型,如果传入的是数组,转换为JSON字符串
- if ($this->type === GameConfigType::JSON && is_array($value)) {
- $value = json_encode($value, JSON_UNESCAPED_UNICODE);
- }
- $this->value = $value;
- return true;
- }
- /**
- * 获取配置的显示值
- */
- public function getDisplayValue(): string
- {
- $value = $this->getTypedValue();
- return match($this->type) {
- GameConfigType::BOOLEAN => $value ? '是' : '否',
- GameConfigType::JSON => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : (string)$value,
- default => (string)$value,
- };
- }
- /**
- * 获取配置的默认显示值
- */
- public function getDefaultDisplayValue(): string
- {
- $value = $this->type->castValue($this->default_value);
- return match($this->type) {
- GameConfigType::BOOLEAN => $value ? '是' : '否',
- GameConfigType::JSON => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : (string)$value,
- default => (string)$value,
- };
- }
- /**
- * 重置为默认值
- */
- public function resetToDefault(): void
- {
- $this->value = $this->default_value;
- }
- /**
- * 检查配置是否有效
- */
- public function isValid(): bool
- {
- return $this->is_enabled && !empty($this->key) && !empty($this->name);
- }
- /**
- * 获取配置的完整描述
- */
- public function getFullDescription(): string
- {
- $desc = $this->description;
- if ($this->remark) {
- $desc .= "\n备注:" . $this->remark;
- }
- return $desc;
- }
- }
|