| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace App\Module\Game\Logics;
- use App\Module\Game\Enums\GameConfigGroup;
- use App\Module\Game\Models\GameConfig;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- /**
- * 游戏配置逻辑层
- */
- class GameConfigLogic
- {
- /**
- * 缓存前缀
- */
- private const CACHE_PREFIX = 'game_config:';
- /**
- * 缓存时间(秒)
- */
- private const CACHE_TTL = 3600;
- /**
- * 获取配置值
- *
- * @param string $key 配置键名
- * @param mixed $default 默认值
- * @return mixed
- */
- public static function get(string $key, $default = null)
- {
- try {
- $cacheKey = self::CACHE_PREFIX . $key;
-
- return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($key, $default) {
- $config = GameConfig::where('key', $key)->first();
-
- if (!$config) {
- Log::warning("游戏配置项不存在: {$key}");
- return $default;
- }
-
- if (!$config->is_enabled) {
- return $config->type->castValue($config->default_value) ?? $default;
- }
-
- return $config->getTypedValue() ?? $default;
- });
- } catch (\Exception $e) {
- Log::error("获取游戏配置失败: {$key}", ['error' => $e->getMessage()]);
- return $default;
- }
- }
- /**
- * 设置配置值
- *
- * @param string $key 配置键名
- * @param mixed $value 配置值
- * @return bool
- */
- public static function set(string $key, $value): bool
- {
- try {
- $config = GameConfig::where('key', $key)->first();
- if (!$config) {
- Log::warning("游戏配置项不存在: {$key}");
- return false;
- }
- if ($config->is_readonly) {
- Log::warning("游戏配置项为只读: {$key}");
- return false;
- }
- if (!$config->setTypedValue($value)) {
- Log::warning("游戏配置值类型不匹配: {$key}", ['value' => $value, 'type' => $config->type->value]);
- return false;
- }
- $config->save();
- // 清除缓存
- self::clearCache($key);
- Log::info("游戏配置项已更新: {$key}", ['old_value' => $config->getOriginal('value'), 'new_value' => $value]);
- return true;
- } catch (\Exception $e) {
- Log::error("设置游戏配置失败: {$key}", ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * 获取布尔值配置
- *
- * @param string $key 配置键名
- * @param bool $default 默认值
- * @return bool
- */
- public static function getBool(string $key, bool $default = false): bool
- {
- return (bool) self::get($key, $default);
- }
- /**
- * 设置布尔值配置
- *
- * @param string $key 配置键名
- * @param bool $value 配置值
- * @return bool
- */
- public static function setBool(string $key, bool $value): bool
- {
- return self::set($key, $value ? '1' : '0');
- }
- /**
- * 获取整数配置
- *
- * @param string $key 配置键名
- * @param int $default 默认值
- * @return int
- */
- public static function getInt(string $key, int $default = 0): int
- {
- return (int) self::get($key, $default);
- }
- /**
- * 获取浮点数配置
- *
- * @param string $key 配置键名
- * @param float $default 默认值
- * @return float
- */
- public static function getFloat(string $key, float $default = 0.0): float
- {
- return (float) self::get($key, $default);
- }
- /**
- * 获取字符串配置
- *
- * @param string $key 配置键名
- * @param string $default 默认值
- * @return string
- */
- public static function getString(string $key, string $default = ''): string
- {
- return (string) self::get($key, $default);
- }
- /**
- * 获取JSON配置
- *
- * @param string $key 配置键名
- * @param array $default 默认值
- * @return array
- */
- public static function getArray(string $key, array $default = []): array
- {
- $value = self::get($key, $default);
- return is_array($value) ? $value : $default;
- }
- /**
- * 获取分组配置
- *
- * @param GameConfigGroup $group 配置分组
- * @return array
- */
- public static function getGroupConfigs(GameConfigGroup $group): array
- {
- try {
- $configs = GameConfig::where('group', $group)
- ->where('is_enabled', true)
- ->orderBy('sort_order')
- ->get();
- $result = [];
- foreach ($configs as $config) {
- $result[$config->key] = $config->getTypedValue();
- }
- return $result;
- } catch (\Exception $e) {
- Log::error("获取分组配置失败: {$group->value}", ['error' => $e->getMessage()]);
- return [];
- }
- }
- /**
- * 清除配置缓存
- *
- * @param string|null $key 配置键名,为空时清除所有配置缓存
- * @return void
- */
- public static function clearCache(?string $key = null): void
- {
- if ($key) {
- Cache::forget(self::CACHE_PREFIX . $key);
- } else {
- // 清除所有游戏配置缓存
- $keys = Cache::get('game_config_keys', []);
- foreach ($keys as $cacheKey) {
- Cache::forget($cacheKey);
- }
- Cache::forget('game_config_keys');
- }
- }
- /**
- * 重置配置为默认值
- *
- * @param string $key 配置键名
- * @return bool
- */
- public static function resetToDefault(string $key): bool
- {
- try {
- $config = GameConfig::where('key', $key)->first();
- if (!$config) {
- Log::warning("游戏配置项不存在: {$key}");
- return false;
- }
- if ($config->is_readonly) {
- Log::warning("游戏配置项为只读: {$key}");
- return false;
- }
- $config->resetToDefault();
- $config->save();
- // 清除缓存
- self::clearCache($key);
- Log::info("游戏配置项已重置为默认值: {$key}");
- return true;
- } catch (\Exception $e) {
- Log::error("重置游戏配置失败: {$key}", ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * 检查配置是否存在
- *
- * @param string $key 配置键名
- * @return bool
- */
- public static function exists(string $key): bool
- {
- return GameConfig::where('key', $key)->exists();
- }
- /**
- * 获取所有配置(按分组)
- *
- * @return array
- */
- public static function getAllConfigs(): array
- {
- try {
- $configs = GameConfig::orderBy('group')->orderBy('sort_order')->get();
-
- $result = [];
- foreach ($configs as $config) {
- $groupName = $config->group->value;
- if (!isset($result[$groupName])) {
- $result[$groupName] = [
- 'name' => $config->group->getName(),
- 'description' => $config->group->getDescription(),
- 'configs' => []
- ];
- }
-
- $result[$groupName]['configs'][] = [
- 'key' => $config->key,
- 'name' => $config->name,
- 'description' => $config->description,
- 'type' => $config->type,
- 'value' => $config->getTypedValue(),
- 'default_value' => $config->type->castValue($config->default_value),
- 'is_enabled' => $config->is_enabled,
- 'is_readonly' => $config->is_readonly,
- ];
- }
-
- return $result;
- } catch (\Exception $e) {
- Log::error("获取所有游戏配置失败", ['error' => $e->getMessage()]);
- return [];
- }
- }
- }
|