GeneratePetJsonCommand.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Module\Pet\Commands;
  3. use App\Module\GameItems\Config\NumericAttributesWhitelist;
  4. use App\Module\Pet\Models\PetConfig;
  5. use App\Module\Pet\Models\PetLevelConfig;
  6. use App\Module\Pet\Models\PetSkill;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 生成宠物配置表JSON数据命令
  11. *
  12. * 该命令用于从数据库中的宠物相关表生成宠物JSON数据文件,供客户端使用。
  13. * 生成的JSON文件包含宠物的基本信息、等级配置和技能配置等。
  14. * 该命令通常在宠物配置更新后运行,以确保客户端获取最新的宠物数据。
  15. */
  16. class GeneratePetJsonCommand extends Command
  17. {
  18. /**
  19. * 命令名称和签名
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'pet:generate-json';
  24. /**
  25. * 命令描述
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Generate pet configuration JSON files from database';
  30. /**
  31. * 生成宠物配置JSON数据
  32. *
  33. * @return array 生成的数据
  34. */
  35. public static function generateJson()
  36. {
  37. try {
  38. // 生成宠物基础配置JSON
  39. $petConfigData = self::generatePetConfigJson();
  40. // 生成宠物等级配置JSON
  41. $petLevelConfigData = self::generatePetLevelConfigJson();
  42. // 生成宠物技能配置JSON
  43. $petSkillConfigData = self::generatePetSkillConfigJson();
  44. // 返回所有配置数据
  45. return [
  46. 'success' => true,
  47. 'generated_ts' => time(),
  48. 'message' => '宠物配置已拆分为三个独立文件,请使用各自的缓存类获取'
  49. ];
  50. } catch (\Exception $e) {
  51. Log::error('Generate pet JSON failed: ' . $e->getMessage());
  52. return [
  53. 'success' => false,
  54. 'error' => $e->getMessage()
  55. ];
  56. }
  57. }
  58. /**
  59. * 生成宠物基础配置JSON
  60. *
  61. * @return array 宠物基础配置数据
  62. */
  63. public static function generatePetConfigJson()
  64. {
  65. try {
  66. // 查询宠物配置表数据
  67. $petConfigs = PetConfig::all()
  68. ->map(function ($config) {
  69. // 过滤numeric_attributes,只保留白名单中的属性
  70. $numericAttributes = null;
  71. if ($config->numeric_attributes) {
  72. $numericAttributes = NumericAttributesWhitelist::filter($config->numeric_attributes);
  73. }
  74. return [
  75. 'id' => $config->id,
  76. 'pet_type' => $config->pet_type,
  77. 'display_attributes' => $config->display_attributes,
  78. 'numeric_attributes' => $numericAttributes
  79. ];
  80. })
  81. ->toArray();
  82. // 准备完整数据,包含生成时间
  83. return [
  84. 'generated_ts' => time(),
  85. 'pets' => $petConfigs,
  86. 'success' => true
  87. ];
  88. } catch (\Exception $e) {
  89. Log::error('Generate pet config JSON failed: ' . $e->getMessage());
  90. return [
  91. 'generated_ts' => time(),
  92. 'pets' => [],
  93. 'success' => false,
  94. 'error' => $e->getMessage()
  95. ];
  96. }
  97. }
  98. /**
  99. * 生成宠物等级配置JSON
  100. *
  101. * @return array 宠物等级配置数据
  102. */
  103. public static function generatePetLevelConfigJson()
  104. {
  105. try {
  106. // 查询宠物等级配置表数据
  107. $petLevelConfigs = PetLevelConfig::orderBy('level')
  108. ->get()
  109. ->map(function ($config) {
  110. // 过滤numeric_attributes,只保留白名单中的属性
  111. $numericAttributes = null;
  112. if ($config->numeric_attributes) {
  113. $numericAttributes = NumericAttributesWhitelist::filter($config->numeric_attributes);
  114. }
  115. return [
  116. 'level' => $config->level,
  117. 'exp_required' => $config->exp_required,
  118. 'skills' => $config->skills,
  119. 'display_attributes' => $config->display_attributes,
  120. 'numeric_attributes' => $numericAttributes
  121. ];
  122. })
  123. ->toArray();
  124. // 准备完整数据,包含生成时间
  125. return [
  126. 'generated_ts' => time(),
  127. 'pet_levels' => $petLevelConfigs,
  128. 'success' => true
  129. ];
  130. } catch (\Exception $e) {
  131. Log::error('Generate pet level config JSON failed: ' . $e->getMessage());
  132. return [
  133. 'generated_ts' => time(),
  134. 'pet_levels' => [],
  135. 'success' => false,
  136. 'error' => $e->getMessage()
  137. ];
  138. }
  139. }
  140. /**
  141. * 生成宠物技能配置JSON
  142. *
  143. * @return array 宠物技能配置数据
  144. */
  145. public static function generatePetSkillConfigJson()
  146. {
  147. try {
  148. // 查询宠物技能配置表数据
  149. $petSkills = PetSkill::all()
  150. ->map(function ($skill) {
  151. // 添加显示属性字段,如果存在的话
  152. $displayAttributes = null;
  153. if (property_exists($skill, 'display_attributes')) {
  154. $displayAttributes = $skill->display_attributes;
  155. }
  156. return [
  157. 'id' => $skill->id,
  158. 'skill_name' => $skill->skill_name,
  159. 'stamina_cost' => $skill->stamina_cost,
  160. 'cool_down' => $skill->cool_down,
  161. 'effect_desc' => $skill->effect_desc,
  162. 'display_attributes' => $displayAttributes
  163. ];
  164. })
  165. ->toArray();
  166. // 准备完整数据,包含生成时间
  167. return [
  168. 'generated_ts' => time(),
  169. 'pet_skills' => $petSkills,
  170. 'success' => true
  171. ];
  172. } catch (\Exception $e) {
  173. Log::error('Generate pet skill config JSON failed: ' . $e->getMessage());
  174. return [
  175. 'generated_ts' => time(),
  176. 'pet_skills' => [],
  177. 'success' => false,
  178. 'error' => $e->getMessage()
  179. ];
  180. }
  181. }
  182. /**
  183. * 执行命令
  184. */
  185. public function handle()
  186. {
  187. $this->info('Generating pet configuration JSON files...');
  188. try {
  189. // 生成宠物基础配置JSON
  190. $petConfigData = self::generatePetConfigJson();
  191. $this->info('Pet configs: ' . count($petConfigData['pets']));
  192. // 生成宠物等级配置JSON
  193. $petLevelConfigData = self::generatePetLevelConfigJson();
  194. $this->info('Pet level configs: ' . count($petLevelConfigData['pet_levels']));
  195. // 生成宠物技能配置JSON
  196. $petSkillConfigData = self::generatePetSkillConfigJson();
  197. $this->info('Pet skill configs: ' . count($petSkillConfigData['pet_skills']));
  198. // 使用各自的缓存类保存数据
  199. $this->info('Saving data to cache...');
  200. \App\Module\Game\DCache\PetConfigJsonConfig::getData([], true);
  201. \App\Module\Game\DCache\PetLevelJsonConfig::getData([], true);
  202. \App\Module\Game\DCache\PetSkillJsonConfig::getData([], true);
  203. $this->info('Successfully generated pet configuration JSON files.');
  204. } catch (\Exception $e) {
  205. $this->error('Failed to generate pet configuration JSON files: ' . $e->getMessage());
  206. }
  207. }
  208. }