GameConfig.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Module\Game\Models;
  3. use App\Module\Game\Enums\GameConfigGroup;
  4. use App\Module\Game\Enums\GameConfigType;
  5. use UCore\ModelCore;
  6. /**
  7. * 游戏配置模型
  8. *
  9. * field start
  10. * @property int $id 配置ID,主键
  11. * @property string $key 配置键名,唯一标识
  12. * @property string $name 配置名称
  13. * @property string $description 配置描述
  14. * @property \App\Module\Game\Enums\GameConfigGroup $group 配置分组
  15. * @property \App\Module\Game\Enums\GameConfigType $type 配置类型:1布尔值,2整数,3小数,4字符串,5JSON
  16. * @property string $value 配置值
  17. * @property string $default_value 默认值
  18. * @property array $options 可选项配置(JSON格式)
  19. * @property string $validation_rules 验证规则
  20. * @property bool $is_enabled 是否启用:0禁用,1启用
  21. * @property bool $is_readonly 是否只读:0可编辑,1只读
  22. * @property int $sort_order 排序权重
  23. * @property string $remark 备注说明
  24. * @property \Carbon\Carbon $created_at 创建时间
  25. * @property \Carbon\Carbon $updated_at 更新时间
  26. * field end
  27. */
  28. class GameConfig extends ModelCore
  29. {
  30. protected $table = 'game_configs';
  31. protected $fillable = [
  32. 'key',
  33. 'name',
  34. 'description',
  35. 'group',
  36. 'type',
  37. 'value',
  38. 'default_value',
  39. 'options',
  40. 'validation_rules',
  41. 'is_enabled',
  42. 'is_readonly',
  43. 'sort_order',
  44. 'remark',
  45. ];
  46. protected $casts = [
  47. 'group' => GameConfigGroup::class,
  48. 'type' => GameConfigType::class,
  49. 'is_enabled' => 'boolean',
  50. 'is_readonly' => 'boolean',
  51. 'sort_order' => 'integer',
  52. 'options' => 'array',
  53. ];
  54. /**
  55. * 获取配置的实际值(根据类型转换)
  56. */
  57. public function getTypedValue()
  58. {
  59. if (!$this->is_enabled) {
  60. return $this->type->castValue($this->default_value);
  61. }
  62. return $this->type->castValue($this->value);
  63. }
  64. /**
  65. * 设置配置值(根据类型转换)
  66. */
  67. public function setTypedValue($value): bool
  68. {
  69. if (!$this->type->validateValue($value)) {
  70. return false;
  71. }
  72. // 对于JSON类型,如果传入的是数组,转换为JSON字符串
  73. if ($this->type === GameConfigType::JSON && is_array($value)) {
  74. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  75. }
  76. $this->value = $value;
  77. return true;
  78. }
  79. /**
  80. * 获取配置的显示值
  81. */
  82. public function getDisplayValue(): string
  83. {
  84. $value = $this->getTypedValue();
  85. return match($this->type) {
  86. GameConfigType::BOOLEAN => $value ? '是' : '否',
  87. GameConfigType::JSON => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : (string)$value,
  88. default => (string)$value,
  89. };
  90. }
  91. /**
  92. * 获取配置的默认显示值
  93. */
  94. public function getDefaultDisplayValue(): string
  95. {
  96. $value = $this->type->castValue($this->default_value);
  97. return match($this->type) {
  98. GameConfigType::BOOLEAN => $value ? '是' : '否',
  99. GameConfigType::JSON => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : (string)$value,
  100. default => (string)$value,
  101. };
  102. }
  103. /**
  104. * 重置为默认值
  105. */
  106. public function resetToDefault(): void
  107. {
  108. $this->value = $this->default_value;
  109. }
  110. /**
  111. * 检查配置是否有效
  112. */
  113. public function isValid(): bool
  114. {
  115. return $this->is_enabled && !empty($this->key) && !empty($this->name);
  116. }
  117. /**
  118. * 获取配置的完整描述
  119. */
  120. public function getFullDescription(): string
  121. {
  122. $desc = $this->description;
  123. if ($this->remark) {
  124. $desc .= "\n备注:" . $this->remark;
  125. }
  126. return $desc;
  127. }
  128. }