| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace App\Module\Pet\Commands;
- use App\Module\GameItems\Config\NumericAttributesWhitelist;
- use App\Module\Pet\Models\PetConfig;
- use App\Module\Pet\Models\PetLevelConfig;
- use App\Module\Pet\Models\PetSkill;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * 生成宠物配置表JSON数据命令
- *
- * 该命令用于从数据库中的宠物相关表生成宠物JSON数据文件,供客户端使用。
- * 生成的JSON文件包含宠物的基本信息、等级配置和技能配置等。
- * 该命令通常在宠物配置更新后运行,以确保客户端获取最新的宠物数据。
- */
- class GeneratePetJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'pet:generate-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate pet configuration JSON files from database';
- /**
- * 生成宠物配置JSON数据
- *
- * @return array 生成的数据
- */
- public static function generateJson()
- {
- try {
- // 生成宠物基础配置JSON
- $petConfigData = self::generatePetConfigJson();
- // 生成宠物等级配置JSON
- $petLevelConfigData = self::generatePetLevelConfigJson();
- // 生成宠物技能配置JSON
- $petSkillConfigData = self::generatePetSkillConfigJson();
- // 返回所有配置数据
- return [
- 'success' => true,
- 'generated_ts' => time(),
- 'message' => '宠物配置已拆分为三个独立文件,请使用各自的缓存类获取'
- ];
- } catch (\Exception $e) {
- Log::error('Generate pet JSON failed: ' . $e->getMessage());
- return [
- 'success' => false,
- 'error' => $e->getMessage()
- ];
- }
- }
- /**
- * 生成宠物基础配置JSON
- *
- * @return array 宠物基础配置数据
- */
- public static function generatePetConfigJson()
- {
- try {
- // 查询宠物配置表数据
- $petConfigs = PetConfig::all()
- ->map(function ($config) {
- // 过滤numeric_attributes,只保留白名单中的属性
- $numericAttributes = null;
- if ($config->numeric_attributes) {
- $numericAttributes = NumericAttributesWhitelist::filter($config->numeric_attributes);
- }
- return [
- 'id' => $config->id,
- 'pet_type' => $config->pet_type,
- 'display_attributes' => $config->display_attributes,
- 'numeric_attributes' => $numericAttributes
- ];
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- return [
- 'generated_ts' => time(),
- 'pets' => $petConfigs,
- 'success' => true
- ];
- } catch (\Exception $e) {
- Log::error('Generate pet config JSON failed: ' . $e->getMessage());
- return [
- 'generated_ts' => time(),
- 'pets' => [],
- 'success' => false,
- 'error' => $e->getMessage()
- ];
- }
- }
- /**
- * 生成宠物等级配置JSON
- *
- * @return array 宠物等级配置数据
- */
- public static function generatePetLevelConfigJson()
- {
- try {
- // 查询宠物等级配置表数据
- $petLevelConfigs = PetLevelConfig::orderBy('level')
- ->get()
- ->map(function ($config) {
- // 过滤numeric_attributes,只保留白名单中的属性
- $numericAttributes = null;
- if ($config->numeric_attributes) {
- $numericAttributes = NumericAttributesWhitelist::filter($config->numeric_attributes);
- }
- return [
- 'level' => $config->level,
- 'exp_required' => $config->exp_required,
- 'skills' => $config->skills,
- 'display_attributes' => $config->display_attributes,
- 'numeric_attributes' => $numericAttributes
- ];
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- return [
- 'generated_ts' => time(),
- 'pet_levels' => $petLevelConfigs,
- 'success' => true
- ];
- } catch (\Exception $e) {
- Log::error('Generate pet level config JSON failed: ' . $e->getMessage());
- return [
- 'generated_ts' => time(),
- 'pet_levels' => [],
- 'success' => false,
- 'error' => $e->getMessage()
- ];
- }
- }
- /**
- * 生成宠物技能配置JSON
- *
- * @return array 宠物技能配置数据
- */
- public static function generatePetSkillConfigJson()
- {
- try {
- // 查询宠物技能配置表数据
- $petSkills = PetSkill::all()
- ->map(function ($skill) {
- // 添加显示属性字段,如果存在的话
- $displayAttributes = null;
- if (property_exists($skill, 'display_attributes')) {
- $displayAttributes = $skill->display_attributes;
- }
- return [
- 'id' => $skill->id,
- 'skill_name' => $skill->skill_name,
- 'stamina_cost' => $skill->stamina_cost,
- 'cool_down' => $skill->cool_down,
- 'effect_desc' => $skill->effect_desc,
- 'display_attributes' => $displayAttributes
- ];
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- return [
- 'generated_ts' => time(),
- 'pet_skills' => $petSkills,
- 'success' => true
- ];
- } catch (\Exception $e) {
- Log::error('Generate pet skill config JSON failed: ' . $e->getMessage());
- return [
- 'generated_ts' => time(),
- 'pet_skills' => [],
- 'success' => false,
- 'error' => $e->getMessage()
- ];
- }
- }
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('Generating pet configuration JSON files...');
- try {
- // 生成宠物基础配置JSON
- $petConfigData = self::generatePetConfigJson();
- $this->info('Pet configs: ' . count($petConfigData['pets']));
- // 生成宠物等级配置JSON
- $petLevelConfigData = self::generatePetLevelConfigJson();
- $this->info('Pet level configs: ' . count($petLevelConfigData['pet_levels']));
- // 生成宠物技能配置JSON
- $petSkillConfigData = self::generatePetSkillConfigJson();
- $this->info('Pet skill configs: ' . count($petSkillConfigData['pet_skills']));
- // 使用各自的缓存类保存数据
- $this->info('Saving data to cache...');
- \App\Module\Game\DCache\PetConfigJsonConfig::getData([], true);
- \App\Module\Game\DCache\PetLevelJsonConfig::getData([], true);
- \App\Module\Game\DCache\PetSkillJsonConfig::getData([], true);
- $this->info('Successfully generated pet configuration JSON files.');
- } catch (\Exception $e) {
- $this->error('Failed to generate pet configuration JSON files: ' . $e->getMessage());
- }
- }
- }
|