GameConfigLogic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace App\Module\Game\Logics;
  3. use App\Module\Game\Enums\GameConfigGroup;
  4. use App\Module\Game\Models\GameConfig;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 游戏配置逻辑层
  9. */
  10. class GameConfigLogic
  11. {
  12. /**
  13. * 缓存前缀
  14. */
  15. private const CACHE_PREFIX = 'game_config:';
  16. /**
  17. * 缓存时间(秒)
  18. */
  19. private const CACHE_TTL = 3600;
  20. /**
  21. * 获取配置值
  22. *
  23. * @param string $key 配置键名
  24. * @param mixed $default 默认值
  25. * @return mixed
  26. */
  27. public static function get(string $key, $default = null)
  28. {
  29. try {
  30. $cacheKey = self::CACHE_PREFIX . $key;
  31. return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($key, $default) {
  32. $config = GameConfig::where('key', $key)->first();
  33. if (!$config) {
  34. Log::warning("游戏配置项不存在: {$key}");
  35. return $default;
  36. }
  37. if (!$config->is_enabled) {
  38. return $config->type->castValue($config->default_value) ?? $default;
  39. }
  40. return $config->getTypedValue() ?? $default;
  41. });
  42. } catch (\Exception $e) {
  43. Log::error("获取游戏配置失败: {$key}", ['error' => $e->getMessage()]);
  44. throw $e; // 重新抛出异常,不隐藏错误
  45. }
  46. }
  47. /**
  48. * 设置配置值
  49. *
  50. * @param string $key 配置键名
  51. * @param mixed $value 配置值
  52. * @return bool
  53. */
  54. public static function set(string $key, $value): bool
  55. {
  56. try {
  57. $config = GameConfig::where('key', $key)->first();
  58. if (!$config) {
  59. Log::warning("游戏配置项不存在: {$key}");
  60. return false;
  61. }
  62. if ($config->is_readonly) {
  63. Log::warning("游戏配置项为只读: {$key}");
  64. return false;
  65. }
  66. if (!$config->setTypedValue($value)) {
  67. Log::warning("游戏配置值类型不匹配: {$key}", ['value' => $value, 'type' => $config->type->value]);
  68. return false;
  69. }
  70. $config->save();
  71. // 清除缓存
  72. self::clearCache($key);
  73. Log::info("游戏配置项已更新: {$key}", ['old_value' => $config->getOriginal('value'), 'new_value' => $value]);
  74. return true;
  75. } catch (\Exception $e) {
  76. Log::error("设置游戏配置失败: {$key}", ['error' => $e->getMessage()]);
  77. // 重新抛出异常,不隐藏错误
  78. throw $e;
  79. }
  80. }
  81. /**
  82. * 获取布尔值配置
  83. *
  84. * @param string $key 配置键名
  85. * @param bool $default 默认值
  86. * @return bool
  87. */
  88. public static function getBool(string $key, bool $default = false): bool
  89. {
  90. return (bool) self::get($key, $default);
  91. }
  92. /**
  93. * 设置布尔值配置
  94. *
  95. * @param string $key 配置键名
  96. * @param bool $value 配置值
  97. * @return bool
  98. */
  99. public static function setBool(string $key, bool $value): bool
  100. {
  101. return self::set($key, $value ? '1' : '0');
  102. }
  103. /**
  104. * 获取整数配置
  105. *
  106. * @param string $key 配置键名
  107. * @param int $default 默认值
  108. * @return int
  109. */
  110. public static function getInt(string $key, int $default = 0): int
  111. {
  112. return (int) self::get($key, $default);
  113. }
  114. /**
  115. * 获取浮点数配置
  116. *
  117. * @param string $key 配置键名
  118. * @param float $default 默认值
  119. * @return float
  120. */
  121. public static function getFloat(string $key, float $default = 0.0): float
  122. {
  123. return (float) self::get($key, $default);
  124. }
  125. /**
  126. * 获取字符串配置
  127. *
  128. * @param string $key 配置键名
  129. * @param string $default 默认值
  130. * @return string
  131. */
  132. public static function getString(string $key, string $default = ''): string
  133. {
  134. return (string) self::get($key, $default);
  135. }
  136. /**
  137. * 获取JSON配置
  138. *
  139. * @param string $key 配置键名
  140. * @param array $default 默认值
  141. * @return array
  142. */
  143. public static function getArray(string $key, array $default = []): array
  144. {
  145. $value = self::get($key, $default);
  146. return is_array($value) ? $value : $default;
  147. }
  148. /**
  149. * 获取分组配置
  150. *
  151. * @param GameConfigGroup $group 配置分组
  152. * @return array
  153. */
  154. public static function getGroupConfigs(GameConfigGroup $group): array
  155. {
  156. try {
  157. $configs = GameConfig::where('group', $group)
  158. ->where('is_enabled', true)
  159. ->orderBy('sort_order')
  160. ->get();
  161. $result = [];
  162. foreach ($configs as $config) {
  163. $result[$config->key] = $config->getTypedValue();
  164. }
  165. return $result;
  166. } catch (\Exception $e) {
  167. Log::error("获取分组配置失败: {$group->value}", ['error' => $e->getMessage()]);
  168. return [];
  169. }
  170. }
  171. /**
  172. * 清除配置缓存
  173. *
  174. * @param string|null $key 配置键名,为空时清除所有配置缓存
  175. * @return void
  176. */
  177. public static function clearCache(?string $key = null): void
  178. {
  179. if ($key) {
  180. Cache::forget(self::CACHE_PREFIX . $key);
  181. } else {
  182. // 清除所有游戏配置缓存
  183. $keys = Cache::get('game_config_keys', []);
  184. foreach ($keys as $cacheKey) {
  185. Cache::forget($cacheKey);
  186. }
  187. Cache::forget('game_config_keys');
  188. }
  189. }
  190. /**
  191. * 重置配置为默认值
  192. *
  193. * @param string $key 配置键名
  194. * @return bool
  195. */
  196. public static function resetToDefault(string $key): bool
  197. {
  198. try {
  199. $config = GameConfig::where('key', $key)->first();
  200. if (!$config) {
  201. Log::warning("游戏配置项不存在: {$key}");
  202. return false;
  203. }
  204. if ($config->is_readonly) {
  205. Log::warning("游戏配置项为只读: {$key}");
  206. return false;
  207. }
  208. $config->resetToDefault();
  209. $config->save();
  210. // 清除缓存
  211. self::clearCache($key);
  212. Log::info("游戏配置项已重置为默认值: {$key}");
  213. return true;
  214. } catch (\Exception $e) {
  215. Log::error("重置游戏配置失败: {$key}", ['error' => $e->getMessage()]);
  216. return false;
  217. }
  218. }
  219. /**
  220. * 检查配置是否存在
  221. *
  222. * @param string $key 配置键名
  223. * @return bool
  224. */
  225. public static function exists(string $key): bool
  226. {
  227. return GameConfig::where('key', $key)->exists();
  228. }
  229. /**
  230. * 获取所有配置(按分组)
  231. *
  232. * @return array
  233. */
  234. public static function getAllConfigs(): array
  235. {
  236. try {
  237. $configs = GameConfig::orderBy('group')->orderBy('sort_order')->get();
  238. $result = [];
  239. foreach ($configs as $config) {
  240. $groupName = $config->group->value;
  241. if (!isset($result[$groupName])) {
  242. $result[$groupName] = [
  243. 'name' => $config->group->getName(),
  244. 'description' => $config->group->getDescription(),
  245. 'configs' => []
  246. ];
  247. }
  248. $result[$groupName]['configs'][] = [
  249. 'key' => $config->key,
  250. 'name' => $config->name,
  251. 'description' => $config->description,
  252. 'type' => $config->type,
  253. 'value' => $config->getTypedValue(),
  254. 'default_value' => $config->type->castValue($config->default_value),
  255. 'is_enabled' => $config->is_enabled,
  256. 'is_readonly' => $config->is_readonly,
  257. ];
  258. }
  259. return $result;
  260. } catch (\Exception $e) {
  261. Log::error("获取所有游戏配置失败", ['error' => $e->getMessage()]);
  262. return [];
  263. }
  264. }
  265. }