GenerateRecipeJsonCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 App\Module\GameItems\Models\ItemRecipeMaterial;
  7. use Illuminate\Support\Facades\File;
  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(['resultItem', 'materials.item'])
  41. ->where('is_active', 1)
  42. ->get()
  43. ->map(function ($recipe) {
  44. // 处理材料数据
  45. $materials = $recipe->materials->map(function ($material) {
  46. return [
  47. 'item_id' => $material->item_id,
  48. 'item_name' => $material->item->name ?? '未知物品',
  49. 'quantity' => $material->quantity,
  50. 'is_consumed' => $material->is_consumed,
  51. ];
  52. })->toArray();
  53. // 构建配方数据
  54. return [
  55. 'id' => $recipe->id,
  56. 'name' => $recipe->name,
  57. 'result_item_id' => $recipe->result_item_id,
  58. 'result_item_name' => $recipe->resultItem->name ?? '未知物品',
  59. 'result_min_quantity' => $recipe->result_min_quantity,
  60. 'result_max_quantity' => $recipe->result_max_quantity,
  61. 'success_rate' => $recipe->success_rate,
  62. 'coin_cost' => $recipe->coin_cost,
  63. 'level_required' => $recipe->level_required,
  64. 'is_default_unlocked' => $recipe->is_default_unlocked,
  65. 'unlock_condition' => $recipe->unlock_condition,
  66. 'cooldown_seconds' => $recipe->cooldown_seconds,
  67. 'category_id' => $recipe->category_id,
  68. 'materials' => $materials,
  69. ];
  70. })
  71. ->toArray();
  72. // 准备完整数据,包含生成时间
  73. $data = [
  74. 'generated_ts' => time(),
  75. 'recipes' => $recipes
  76. ];
  77. // 保存到文件
  78. self::saveJsonToFile($data);
  79. return $data;
  80. } catch (\Exception $e) {
  81. Log::error('Generate recipe.json failed: ' . $e->getMessage());
  82. return false;
  83. }
  84. }
  85. /**
  86. * 将JSON数据保存到文件
  87. *
  88. * @param array $data 要保存的数据
  89. * @return bool 是否保存成功
  90. */
  91. protected static function saveJsonToFile(array $data): bool
  92. {
  93. try {
  94. // 确保目录存在
  95. $directory = 'public/json';
  96. if (!File::exists($directory)) {
  97. File::makeDirectory($directory, 0755, true);
  98. }
  99. // 将数据保存为JSON文件
  100. $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  101. $filePath = $directory . '/recipe.json';
  102. File::put($filePath, $jsonContent);
  103. Log::info('Recipe JSON file saved to: ' . $filePath);
  104. return true;
  105. } catch (\Exception $e) {
  106. Log::error('Save recipe.json to file failed: ' . $e->getMessage());
  107. return false;
  108. }
  109. }
  110. /**
  111. * 执行命令
  112. */
  113. public function handle()
  114. {
  115. $this->info('Generating recipe JSON data...');
  116. if (RecipeJsonConfig::getData([], true)) {
  117. $this->info('Successfully generated recipe.json with timestamp');
  118. $this->info('JSON file saved to public/json/recipe.json');
  119. } else {
  120. $this->error('Failed to generate recipe.json');
  121. }
  122. }
  123. }