GameConfigLogic.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. return $default;
  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. return false;
  78. }
  79. }
  80. /**
  81. * 获取布尔值配置
  82. *
  83. * @param string $key 配置键名
  84. * @param bool $default 默认值
  85. * @return bool
  86. */
  87. public static function getBool(string $key, bool $default = false): bool
  88. {
  89. return (bool) self::get($key, $default);
  90. }
  91. /**
  92. * 设置布尔值配置
  93. *
  94. * @param string $key 配置键名
  95. * @param bool $value 配置值
  96. * @return bool
  97. */
  98. public static function setBool(string $key, bool $value): bool
  99. {
  100. return self::set($key, $value ? '1' : '0');
  101. }
  102. /**
  103. * 获取整数配置
  104. *
  105. * @param string $key 配置键名
  106. * @param int $default 默认值
  107. * @return int
  108. */
  109. public static function getInt(string $key, int $default = 0): int
  110. {
  111. return (int) self::get($key, $default);
  112. }
  113. /**
  114. * 获取浮点数配置
  115. *
  116. * @param string $key 配置键名
  117. * @param float $default 默认值
  118. * @return float
  119. */
  120. public static function getFloat(string $key, float $default = 0.0): float
  121. {
  122. return (float) self::get($key, $default);
  123. }
  124. /**
  125. * 获取字符串配置
  126. *
  127. * @param string $key 配置键名
  128. * @param string $default 默认值
  129. * @return string
  130. */
  131. public static function getString(string $key, string $default = ''): string
  132. {
  133. return (string) self::get($key, $default);
  134. }
  135. /**
  136. * 获取JSON配置
  137. *
  138. * @param string $key 配置键名
  139. * @param array $default 默认值
  140. * @return array
  141. */
  142. public static function getArray(string $key, array $default = []): array
  143. {
  144. $value = self::get($key, $default);
  145. return is_array($value) ? $value : $default;
  146. }
  147. /**
  148. * 获取分组配置
  149. *
  150. * @param GameConfigGroup $group 配置分组
  151. * @return array
  152. */
  153. public static function getGroupConfigs(GameConfigGroup $group): array
  154. {
  155. try {
  156. $configs = GameConfig::where('group', $group)
  157. ->where('is_enabled', true)
  158. ->orderBy('sort_order')
  159. ->get();
  160. $result = [];
  161. foreach ($configs as $config) {
  162. $result[$config->key] = $config->getTypedValue();
  163. }
  164. return $result;
  165. } catch (\Exception $e) {
  166. Log::error("获取分组配置失败: {$group->value}", ['error' => $e->getMessage()]);
  167. return [];
  168. }
  169. }
  170. /**
  171. * 清除配置缓存
  172. *
  173. * @param string|null $key 配置键名,为空时清除所有配置缓存
  174. * @return void
  175. */
  176. public static function clearCache(?string $key = null): void
  177. {
  178. if ($key) {
  179. Cache::forget(self::CACHE_PREFIX . $key);
  180. } else {
  181. // 清除所有游戏配置缓存
  182. $keys = Cache::get('game_config_keys', []);
  183. foreach ($keys as $cacheKey) {
  184. Cache::forget($cacheKey);
  185. }
  186. Cache::forget('game_config_keys');
  187. }
  188. }
  189. /**
  190. * 重置配置为默认值
  191. *
  192. * @param string $key 配置键名
  193. * @return bool
  194. */
  195. public static function resetToDefault(string $key): bool
  196. {
  197. try {
  198. $config = GameConfig::where('key', $key)->first();
  199. if (!$config) {
  200. Log::warning("游戏配置项不存在: {$key}");
  201. return false;
  202. }
  203. if ($config->is_readonly) {
  204. Log::warning("游戏配置项为只读: {$key}");
  205. return false;
  206. }
  207. $config->resetToDefault();
  208. $config->save();
  209. // 清除缓存
  210. self::clearCache($key);
  211. Log::info("游戏配置项已重置为默认值: {$key}");
  212. return true;
  213. } catch (\Exception $e) {
  214. Log::error("重置游戏配置失败: {$key}", ['error' => $e->getMessage()]);
  215. return false;
  216. }
  217. }
  218. /**
  219. * 检查配置是否存在
  220. *
  221. * @param string $key 配置键名
  222. * @return bool
  223. */
  224. public static function exists(string $key): bool
  225. {
  226. return GameConfig::where('key', $key)->exists();
  227. }
  228. /**
  229. * 获取所有配置(按分组)
  230. *
  231. * @return array
  232. */
  233. public static function getAllConfigs(): array
  234. {
  235. try {
  236. $configs = GameConfig::orderBy('group')->orderBy('sort_order')->get();
  237. $result = [];
  238. foreach ($configs as $config) {
  239. $groupName = $config->group->value;
  240. if (!isset($result[$groupName])) {
  241. $result[$groupName] = [
  242. 'name' => $config->group->getName(),
  243. 'description' => $config->group->getDescription(),
  244. 'configs' => []
  245. ];
  246. }
  247. $result[$groupName]['configs'][] = [
  248. 'key' => $config->key,
  249. 'name' => $config->name,
  250. 'description' => $config->description,
  251. 'type' => $config->type,
  252. 'value' => $config->getTypedValue(),
  253. 'default_value' => $config->type->castValue($config->default_value),
  254. 'is_enabled' => $config->is_enabled,
  255. 'is_readonly' => $config->is_readonly,
  256. ];
  257. }
  258. return $result;
  259. } catch (\Exception $e) {
  260. Log::error("获取所有游戏配置失败", ['error' => $e->getMessage()]);
  261. return [];
  262. }
  263. }
  264. }