GenerateRecipeJsonCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Module\GameItems\Commands;
  3. use App\Module\Game\DCache\RecipeJsonConfig;
  4. use App\Module\GameItems\Config\NumericAttributesWhitelist;
  5. use Illuminate\Console\Command;
  6. use App\Module\GameItems\Models\ItemRecipe;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 生成物品合成配方配置表JSON数据命令
  10. *
  11. * 该命令用于从数据库中的物品合成配方表生成JSON数据文件,供客户端使用。
  12. * 生成的JSON文件包含合成配方的基本信息,如ID、名称、产出物品、所需材料等。
  13. * 该命令通常在合成配方数据更新后运行,以确保客户端获取最新的配方数据。
  14. */
  15. class GenerateRecipeJsonCommand extends Command
  16. {
  17. /**
  18. * 命令名称和签名
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'gameitems:generate-recipe-json';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = 'Generate recipe.json from ItemRecipe table';
  29. /**
  30. * 生成合成配方JSON数据
  31. *
  32. * @return array|bool 生成的数据或失败标志
  33. */
  34. public static function generateJson()
  35. {
  36. try {
  37. // 查询ItemRecipe表中的数据,并预加载关联数据
  38. $recipes = ItemRecipe::query()
  39. ->with([
  40. 'resultItem' => function ($query) {
  41. $query->select(['id', 'name', 'numeric_attributes']);
  42. },
  43. 'materials.item'
  44. ])
  45. ->where('is_active', 1)
  46. ->get()
  47. ->map(function ($recipe) {
  48. // 处理材料数据
  49. $materials = $recipe->materials->map(function ($material) {
  50. return [
  51. 'item_id' => $material->item_id,
  52. 'item_name' => $material->item->name ?? '未知物品',
  53. 'quantity' => $material->quantity,
  54. 'is_consumed' => $material->is_consumed,
  55. ];
  56. })->toArray();
  57. // 构建配方数据
  58. $recipeData = [
  59. 'id' => $recipe->id,
  60. 'name' => $recipe->name,
  61. 'result_item_id' => $recipe->result_item_id,
  62. 'result_item_name' => $recipe->resultItem->name ?? '未知物品',
  63. 'result_min_quantity' => $recipe->result_min_quantity,
  64. 'result_max_quantity' => $recipe->result_max_quantity,
  65. 'success_rate' => $recipe->success_rate,
  66. 'coin_cost' => $recipe->coin_cost,
  67. 'level_required' => $recipe->level_required,
  68. 'is_default_unlocked' => $recipe->is_default_unlocked,
  69. 'unlock_condition' => $recipe->unlock_condition,
  70. 'cooldown_seconds' => $recipe->cooldown_seconds,
  71. 'category_id' => $recipe->category_id,
  72. 'materials' => $materials,
  73. ];
  74. // 如果结果物品有数值属性,使用白名单过滤
  75. if ($recipe->resultItem && $recipe->resultItem->numeric_attributes) {
  76. $numericAttributes = NumericAttributesWhitelist::filter($recipe->resultItem->numeric_attributes);
  77. if (!empty($numericAttributes)) {
  78. $recipeData['result_numeric_attributes'] = $numericAttributes;
  79. }
  80. }
  81. return $recipeData;
  82. })
  83. ->toArray();
  84. // 准备完整数据,包含生成时间
  85. $data = [
  86. 'generated_ts' => time(),
  87. 'recipes' => $recipes
  88. ];
  89. return $data;
  90. } catch (\Exception $e) {
  91. Log::error('Generate recipe.json failed: ' . $e->getMessage());
  92. return false;
  93. }
  94. }
  95. /**
  96. * 执行命令
  97. */
  98. public function handle()
  99. {
  100. $this->info('Generating recipe JSON data...');
  101. if (RecipeJsonConfig::getData([], true)) {
  102. $this->info('Successfully generated recipe.json with timestamp');
  103. } else {
  104. $this->error('Failed to generate recipe.json');
  105. }
  106. }
  107. }