GeneratePetJsonCommand.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. 'grade_probability' => $config->grade_probability,
  78. 'display_attributes' => $config->display_attributes,
  79. 'numeric_attributes' => $numericAttributes
  80. ];
  81. })
  82. ->toArray();
  83. // 准备完整数据,包含生成时间
  84. return [
  85. 'generated_ts' => time(),
  86. 'pets' => $petConfigs,
  87. 'success' => true
  88. ];
  89. } catch (\Exception $e) {
  90. Log::error('Generate pet config JSON failed: ' . $e->getMessage());
  91. return [
  92. 'generated_ts' => time(),
  93. 'pets' => [],
  94. 'success' => false,
  95. 'error' => $e->getMessage()
  96. ];
  97. }
  98. }
  99. /**
  100. * 生成宠物等级配置JSON
  101. *
  102. * @return array 宠物等级配置数据
  103. */
  104. public static function generatePetLevelConfigJson()
  105. {
  106. try {
  107. // 查询宠物等级配置表数据
  108. $petLevelConfigs = PetLevelConfig::orderBy('level')
  109. ->get()
  110. ->map(function ($config) {
  111. // 过滤numeric_attributes,只保留白名单中的属性
  112. $numericAttributes = null;
  113. if ($config->numeric_attributes) {
  114. $numericAttributes = NumericAttributesWhitelist::filter($config->numeric_attributes);
  115. }
  116. return [
  117. 'level' => $config->level,
  118. 'exp_required' => $config->exp_required,
  119. 'skills' => $config->skills,
  120. 'display_attributes' => $config->display_attributes,
  121. 'numeric_attributes' => $numericAttributes
  122. ];
  123. })
  124. ->toArray();
  125. // 准备完整数据,包含生成时间
  126. return [
  127. 'generated_ts' => time(),
  128. 'pet_levels' => $petLevelConfigs,
  129. 'success' => true
  130. ];
  131. } catch (\Exception $e) {
  132. Log::error('Generate pet level config JSON failed: ' . $e->getMessage());
  133. return [
  134. 'generated_ts' => time(),
  135. 'pet_levels' => [],
  136. 'success' => false,
  137. 'error' => $e->getMessage()
  138. ];
  139. }
  140. }
  141. /**
  142. * 生成宠物技能配置JSON
  143. *
  144. * @return array 宠物技能配置数据
  145. */
  146. public static function generatePetSkillConfigJson()
  147. {
  148. try {
  149. // 查询宠物技能配置表数据
  150. $petSkills = PetSkill::all()
  151. ->map(function ($skill) {
  152. // 添加显示属性字段,如果存在的话
  153. $displayAttributes = null;
  154. if (property_exists($skill, 'display_attributes')) {
  155. $displayAttributes = $skill->display_attributes;
  156. }
  157. return [
  158. 'id' => $skill->id,
  159. 'skill_name' => $skill->skill_name,
  160. 'stamina_cost' => $skill->stamina_cost,
  161. 'cool_down' => $skill->cool_down,
  162. 'effect_desc' => $skill->effect_desc,
  163. 'display_attributes' => $displayAttributes
  164. ];
  165. })
  166. ->toArray();
  167. // 准备完整数据,包含生成时间
  168. return [
  169. 'generated_ts' => time(),
  170. 'pet_skills' => $petSkills,
  171. 'success' => true
  172. ];
  173. } catch (\Exception $e) {
  174. Log::error('Generate pet skill config JSON failed: ' . $e->getMessage());
  175. return [
  176. 'generated_ts' => time(),
  177. 'pet_skills' => [],
  178. 'success' => false,
  179. 'error' => $e->getMessage()
  180. ];
  181. }
  182. }
  183. /**
  184. * 执行命令
  185. */
  186. public function handle()
  187. {
  188. $this->info('Generating pet configuration JSON files...');
  189. try {
  190. // 生成宠物基础配置JSON
  191. $petConfigData = self::generatePetConfigJson();
  192. $this->info('Pet configs: ' . count($petConfigData['pets']));
  193. // 生成宠物等级配置JSON
  194. $petLevelConfigData = self::generatePetLevelConfigJson();
  195. $this->info('Pet level configs: ' . count($petLevelConfigData['pet_levels']));
  196. // 生成宠物技能配置JSON
  197. $petSkillConfigData = self::generatePetSkillConfigJson();
  198. $this->info('Pet skill configs: ' . count($petSkillConfigData['pet_skills']));
  199. // 使用各自的缓存类保存数据
  200. $this->info('Saving data to cache...');
  201. \App\Module\Game\DCache\PetConfigJsonConfig::getData([], true);
  202. \App\Module\Game\DCache\PetLevelJsonConfig::getData([], true);
  203. \App\Module\Game\DCache\PetSkillJsonConfig::getData([], true);
  204. $this->info('Successfully generated pet configuration JSON files.');
  205. } catch (\Exception $e) {
  206. $this->error('Failed to generate pet configuration JSON files: ' . $e->getMessage());
  207. }
  208. }
  209. }