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 []; } } }