GenerateDismantleJsonCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Illuminate\Support\Facades\Log;
  7. /**
  8. * 生成物品分解配方配置表JSON数据命令
  9. *
  10. * 该命令用于从数据库中的物品分解规则表生成JSON数据文件,供客户端使用。
  11. * 生成的JSON文件包含分解规则的基本信息,如规则ID、适用物品、分解结果等。
  12. * 该命令通常在分解规则数据更新后运行,以确保客户端获取最新的分解规则数据。
  13. */
  14. class GenerateDismantleJsonCommand extends Command
  15. {
  16. /**
  17. * 命令名称和签名
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'gameitems:generate-dismantle-json';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Generate dismantle.json from ItemDismantleRule table';
  28. /**
  29. * 生成分解规则JSON数据
  30. *
  31. * @return array|bool 生成的数据或失败标志
  32. */
  33. public static function generateJson()
  34. {
  35. try {
  36. // 查询ItemDismantleRule表中的数据,并预加载关联数据
  37. $rules = ItemDismantleRule::query()
  38. ->with(['item', 'category', 'results.resultItem'])
  39. ->where('is_active', 1)
  40. ->get()
  41. ->map(function ($rule) {
  42. // 处理分解结果数据
  43. $results = $rule->results->map(function ($result) {
  44. return [
  45. 'result_item_id' => $result->result_item_id,
  46. 'result_item_name' => $result->resultItem->name ?? '未知物品',
  47. 'min_quantity' => $result->min_quantity,
  48. 'max_quantity' => $result->max_quantity,
  49. 'base_chance' => $result->base_chance,
  50. 'rarity_factor' => $result->rarity_factor,
  51. 'quality_factor' => $result->quality_factor,
  52. ];
  53. })->toArray();
  54. // 构建规则数据
  55. $ruleData = [
  56. 'id' => $rule->id,
  57. 'priority' => $rule->priority,
  58. 'min_rarity' => $rule->min_rarity,
  59. 'max_rarity' => $rule->max_rarity,
  60. 'results' => $results,
  61. ];
  62. // 根据规则类型添加不同的字段
  63. if ($rule->item_id) {
  64. $ruleData['rule_type'] = 'item';
  65. $ruleData['item_id'] = $rule->item_id;
  66. $ruleData['item_name'] = $rule->item->name ?? '未知物品';
  67. } else {
  68. $ruleData['rule_type'] = 'category';
  69. $ruleData['category_id'] = $rule->category_id;
  70. $ruleData['category_name'] = $rule->category->name ?? '未知分类';
  71. }
  72. return $ruleData;
  73. })
  74. ->toArray();
  75. // 准备完整数据,包含生成时间
  76. $data = [
  77. 'generated_ts' => time(),
  78. 'dismantle_rules' => $rules
  79. ];
  80. return $data;
  81. } catch (\Exception $e) {
  82. Log::error('Generate dismantle.json failed: ' . $e->getMessage());
  83. return false;
  84. }
  85. }
  86. /**
  87. * 执行命令
  88. */
  89. public function handle()
  90. {
  91. $this->info('Generating dismantle JSON data...');
  92. if (DismantleJsonConfig::getData([], true)) {
  93. $this->info('Successfully generated dismantle.json with timestamp');
  94. } else {
  95. $this->error('Failed to generate dismantle.json');
  96. }
  97. }
  98. }