GenerateDismantleJsonCommand.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Module\GameItems\Commands;
  3. use App\Module\Game\DCache\DismantleJsonConfig;
  4. use Illuminate\Console\Command;
  5. use App\Module\GameItems\Models\ItemDismantleRule;
  6. use App\Module\GameItems\Models\ItemDismantleResult;
  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 GenerateDismantleJsonCommand extends Command
  17. {
  18. /**
  19. * 命令名称和签名
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'gameitems:generate-dismantle-json';
  24. /**
  25. * 命令描述
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Generate dismantle.json from ItemDismantleRule table';
  30. /**
  31. * 生成分解规则JSON数据
  32. *
  33. * @return array|bool 生成的数据或失败标志
  34. */
  35. public static function generateJson()
  36. {
  37. try {
  38. // 查询ItemDismantleRule表中的数据,并预加载关联数据
  39. $rules = ItemDismantleRule::query()
  40. ->with(['item', 'category', 'results.resultItem'])
  41. ->where('is_active', 1)
  42. ->get()
  43. ->map(function ($rule) {
  44. // 处理分解结果数据
  45. $results = $rule->results->map(function ($result) {
  46. return [
  47. 'result_item_id' => $result->result_item_id,
  48. 'result_item_name' => $result->resultItem->name ?? '未知物品',
  49. 'min_quantity' => $result->min_quantity,
  50. 'max_quantity' => $result->max_quantity,
  51. 'base_chance' => $result->base_chance,
  52. 'rarity_factor' => $result->rarity_factor,
  53. 'quality_factor' => $result->quality_factor,
  54. ];
  55. })->toArray();
  56. // 构建规则数据
  57. $ruleData = [
  58. 'id' => $rule->id,
  59. 'priority' => $rule->priority,
  60. 'min_rarity' => $rule->min_rarity,
  61. 'max_rarity' => $rule->max_rarity,
  62. 'results' => $results,
  63. ];
  64. // 根据规则类型添加不同的字段
  65. if ($rule->item_id) {
  66. $ruleData['rule_type'] = 'item';
  67. $ruleData['item_id'] = $rule->item_id;
  68. $ruleData['item_name'] = $rule->item->name ?? '未知物品';
  69. } else {
  70. $ruleData['rule_type'] = 'category';
  71. $ruleData['category_id'] = $rule->category_id;
  72. $ruleData['category_name'] = $rule->category->name ?? '未知分类';
  73. }
  74. return $ruleData;
  75. })
  76. ->toArray();
  77. // 准备完整数据,包含生成时间
  78. $data = [
  79. 'generated_ts' => time(),
  80. 'dismantle_rules' => $rules
  81. ];
  82. // 保存到文件
  83. self::saveJsonToFile($data);
  84. return $data;
  85. } catch (\Exception $e) {
  86. Log::error('Generate dismantle.json failed: ' . $e->getMessage());
  87. return false;
  88. }
  89. }
  90. /**
  91. * 将JSON数据保存到文件
  92. *
  93. * @param array $data 要保存的数据
  94. * @return bool 是否保存成功
  95. */
  96. protected static function saveJsonToFile(array $data): bool
  97. {
  98. try {
  99. // 确保目录存在
  100. $directory = 'public/json';
  101. if (!File::exists($directory)) {
  102. File::makeDirectory($directory, 0755, true);
  103. }
  104. // 将数据保存为JSON文件
  105. $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  106. $filePath = $directory . '/dismantle.json';
  107. File::put($filePath, $jsonContent);
  108. Log::info('Dismantle JSON file saved to: ' . $filePath);
  109. return true;
  110. } catch (\Exception $e) {
  111. Log::error('Save dismantle.json to file failed: ' . $e->getMessage());
  112. return false;
  113. }
  114. }
  115. /**
  116. * 执行命令
  117. */
  118. public function handle()
  119. {
  120. $this->info('Generating dismantle JSON data...');
  121. if (DismantleJsonConfig::getData([], true)) {
  122. $this->info('Successfully generated dismantle.json with timestamp');
  123. $this->info('JSON file saved to public/json/dismantle.json');
  124. } else {
  125. $this->error('Failed to generate dismantle.json');
  126. }
  127. }
  128. }