GenerateRecipeJsonCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Module\GameItems\Commands;
  3. use App\Module\Game\DCache\RecipeJsonConfig;
  4. use Illuminate\Console\Command;
  5. use App\Module\GameItems\Models\ItemRecipe;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 生成物品合成配方配置表JSON数据命令
  9. *
  10. * 该命令用于从数据库中的物品合成配方表生成JSON数据文件,供客户端使用。
  11. * 生成的JSON文件包含合成配方的基本信息,如ID、名称、产出物品、所需材料等。
  12. * 该命令通常在合成配方数据更新后运行,以确保客户端获取最新的配方数据。
  13. */
  14. class GenerateRecipeJsonCommand extends Command
  15. {
  16. /**
  17. * 命令名称和签名
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'gameitems:generate-recipe-json';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Generate recipe.json from ItemRecipe table';
  28. /**
  29. * 生成合成配方JSON数据
  30. *
  31. * @return array|bool 生成的数据或失败标志
  32. */
  33. public static function generateJson()
  34. {
  35. try {
  36. // 查询ItemRecipe表中的数据,并预加载关联数据
  37. $recipes = ItemRecipe::query()
  38. ->with([
  39. 'consumeGroup.consumeItems',
  40. 'rewardGroup.rewardItems',
  41. 'conditionGroup.conditionItems',
  42. 'materials.item' // 保留兼容性
  43. ])
  44. ->where('is_active', 1)
  45. ->orderBy('sort_order', 'desc')
  46. ->get()
  47. ->map(function ($recipe) {
  48. // 构建配方数据
  49. $recipeData = [
  50. 'id' => $recipe->id,
  51. 'name' => $recipe->name,
  52. 'code' => $recipe->code,
  53. 'description' => $recipe->description,
  54. 'success_rate' => $recipe->success_rate,
  55. 'cooldown_seconds' => $recipe->cooldown_seconds,
  56. 'sort_order' => $recipe->sort_order,
  57. ];
  58. // 消耗组数据
  59. if ($recipe->consume_group_id && $recipe->consumeGroup) {
  60. $recipeData['consume_group'] = [
  61. 'id' => $recipe->consumeGroup->id,
  62. 'name' => $recipe->consumeGroup->name,
  63. 'items' => $recipe->consumeGroup->consumeItems->map(function ($item) {
  64. return [
  65. 'consume_type' => $item->consume_type,
  66. 'target_id' => $item->target_id,
  67. 'quantity' => $item->quantity,
  68. ];
  69. })->toArray()
  70. ];
  71. }
  72. // 奖励组数据
  73. if ($recipe->reward_group_id && $recipe->rewardGroup) {
  74. $recipeData['reward_group'] = [
  75. 'id' => $recipe->rewardGroup->id,
  76. 'name' => $recipe->rewardGroup->name,
  77. 'is_random' => $recipe->rewardGroup->is_random,
  78. 'random_count' => $recipe->rewardGroup->random_count,
  79. 'items' => $recipe->rewardGroup->rewardItems->map(function ($item) {
  80. return [
  81. 'reward_type' => $item->reward_type,
  82. 'target_id' => $item->target_id,
  83. 'quantity' => $item->quantity,
  84. 'weight' => $item->weight,
  85. 'is_guaranteed' => $item->is_guaranteed,
  86. ];
  87. })->toArray()
  88. ];
  89. }
  90. // 条件组数据
  91. if ($recipe->condition_group_id && $recipe->conditionGroup) {
  92. $recipeData['condition_group'] = [
  93. 'id' => $recipe->conditionGroup->id,
  94. 'name' => $recipe->conditionGroup->name,
  95. 'logic_type' => $recipe->conditionGroup->logic_type,
  96. 'items' => $recipe->conditionGroup->conditionItems->map(function ($item) {
  97. return [
  98. 'condition_type' => $item->condition_type,
  99. 'target_id' => $item->target_id,
  100. 'operator' => $item->operator,
  101. 'value' => $item->value,
  102. ];
  103. })->toArray()
  104. ];
  105. }
  106. // 兼容旧系统:处理材料数据
  107. if ($recipe->materials && $recipe->materials->isNotEmpty()) {
  108. $recipeData['legacy_materials'] = $recipe->materials->map(function ($material) {
  109. return [
  110. 'item_id' => $material->item_id,
  111. 'item_name' => $material->item->name ?? '未知物品',
  112. 'quantity' => $material->quantity,
  113. 'is_consumed' => $material->is_consumed,
  114. ];
  115. })->toArray();
  116. }
  117. return $recipeData;
  118. })
  119. ->toArray();
  120. // 准备完整数据,包含生成时间
  121. $data = [
  122. 'generated_ts' => time(),
  123. 'recipes' => $recipes
  124. ];
  125. return $data;
  126. } catch (\Exception $e) {
  127. Log::error('Generate recipe.json failed: ' . $e->getMessage());
  128. return false;
  129. }
  130. }
  131. /**
  132. * 执行命令
  133. */
  134. public function handle()
  135. {
  136. $this->info('Generating recipe JSON data...');
  137. if (RecipeJsonConfig::getData([], true)) {
  138. $this->info('Successfully generated recipe.json with timestamp');
  139. } else {
  140. $this->error('Failed to generate recipe.json');
  141. }
  142. }
  143. }