GenerateRecipeJsonCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Module\GameItems\Commands;
  3. use App\Module\Game\DCache\RecipeJsonConfig;
  4. use App\Module\Game\Services\ConsumeService;
  5. use App\Module\Game\Services\RewardService;
  6. use Illuminate\Console\Command;
  7. use App\Module\GameItems\Models\ItemRecipe;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 生成物品合成配方配置表JSON数据命令
  11. *
  12. * 该命令用于从数据库中的物品合成配方表生成JSON数据文件,供客户端使用。
  13. * 生成的JSON文件包含合成配方的基本信息,如ID、名称、产出物品、所需材料等。
  14. * 该命令通常在合成配方数据更新后运行,以确保客户端获取最新的配方数据。
  15. */
  16. class GenerateRecipeJsonCommand extends Command
  17. {
  18. /**
  19. * 命令名称和签名
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'gameitems:generate-recipe-json';
  24. /**
  25. * 命令描述
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Generate recipe.json from ItemRecipe table';
  30. /**
  31. * 生成合成配方JSON数据
  32. *
  33. * @return array|bool 生成的数据或失败标志
  34. */
  35. public static function generateJson()
  36. {
  37. try {
  38. // 查询ItemRecipe表中的数据,并预加载关联数据
  39. $recipes = ItemRecipe::query()
  40. ->with([
  41. 'consumeGroup.consumeItems',
  42. 'rewardGroup.rewardItems',
  43. 'conditionGroup.conditionItems'
  44. ])
  45. ->where('is_active', 1)
  46. ->orderBy('sort_order', 'desc')
  47. ->get()
  48. ->map(function (ItemRecipe $recipe) {
  49. // 构建配方数据
  50. $recipeData = [
  51. 'id' => $recipe->id,
  52. 'name' => $recipe->name,
  53. 'code' => $recipe->code,
  54. 'description' => $recipe->description,
  55. 'success_rate' => $recipe->success_rate,
  56. 'display_attributes'=>$recipe->display_attributes,
  57. 'cooldown_seconds' => $recipe->cooldown_seconds,
  58. 'sort_order' => $recipe->sort_order,
  59. ];
  60. // 消耗组数据
  61. if ($recipe->consume_group_id && $recipe->consumeGroup) {
  62. $consume = ConsumeService::getConsumeGroupAsDeduct($recipe->consume_group_id);
  63. $recipeData['consume_group'] = json_decode($consume->serializeToJsonString(),true);
  64. }
  65. // 奖励组数据
  66. if ($recipe->reward_group_id && $recipe->rewardGroup) {
  67. $reward = RewardService::getRewardGroupAsReward($recipe->reward_group_id);
  68. $recipeData['reward_group'] = json_decode($reward->serializeToJsonString(),true);
  69. }
  70. // 条件组,临时跳过
  71. return $recipeData;
  72. })
  73. ->toArray();
  74. // 准备完整数据,包含生成时间
  75. $data = [
  76. 'generated_ts' => time(),
  77. 'recipes' => $recipes
  78. ];
  79. return $data;
  80. } catch (\Exception $e) {
  81. Log::error('Generate 合成配方 failed: ' . $e->getMessage());
  82. return false;
  83. }
  84. }
  85. /**
  86. * 执行命令
  87. */
  88. public function handle()
  89. {
  90. $this->info('Generating 合成配方 JSON data...');
  91. if (RecipeJsonConfig::getData([], true)) {
  92. $this->info('Successfully generated recipe.json with timestamp');
  93. } else {
  94. $this->error('Failed to generate 合成配方 ');
  95. }
  96. }
  97. }