GenerateFarmLandConfigJson.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Models\FarmLandType;
  4. use App\Module\Farm\Models\FarmLandUpgradeConfig;
  5. use Illuminate\Console\Command;
  6. /**
  7. * 生成土地配置表JSON数据命令
  8. *
  9. * 该命令用于从数据库中的土地类型表和土地升级配置表生成JSON数据文件,供客户端使用。
  10. * 生成的JSON文件包含土地类型的基本信息,如类型ID、名称、产量加成、灾害抵抗等,以及土地升级路径和所需材料。
  11. * 该命令通常在土地配置数据更新后运行,以确保客户端获取最新的配置数据。
  12. */
  13. class GenerateFarmLandConfigJson extends Command
  14. {
  15. /**
  16. * 命令名称
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'farm:generate-land-json';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '生成土地配置JSON文件';
  27. /**
  28. * 生成土地配置JSON数据
  29. *
  30. * @return array|bool 生成的数据或失败标志
  31. */
  32. public static function generateJson()
  33. {
  34. try {
  35. // 获取所有土地类型
  36. $landTypes = FarmLandType::orderBy('id')->get();
  37. // 获取所有土地升级配置
  38. $upgradeConfigs = FarmLandUpgradeConfig::with(['fromType', 'toType'])->get();
  39. // 准备JSON数据
  40. $jsonData = [
  41. 'generated_ts' => time(),
  42. 'land_types' => [],
  43. 'upgrade_paths' => []
  44. ];
  45. // 处理土地类型数据
  46. foreach ($landTypes as $type) {
  47. $jsonData['land_types'][] = [
  48. 'id' => $type->id,
  49. 'name' => $type->name,
  50. 'code' => $type->code,
  51. 'output_bonus' => $type->output_bonus,
  52. 'disaster_resistance' => $type->disaster_resistance,
  53. 'unlock_house_level' => $type->unlock_house_level,
  54. 'is_special' => $type->is_special,
  55. 'display_attributes' => $type->display_attributes,
  56. 'description' => $type->description,
  57. ];
  58. }
  59. // 处理土地升级路径数据
  60. foreach ($upgradeConfigs as $config) {
  61. // 获取完整的材料内容和条件内容
  62. $materials = $config->getUpgradeMaterials();
  63. $conditions = $config->getUpgradeConditions();
  64. $jsonData['upgrade_paths'][] = [
  65. 'from_type_id' => $config->from_type_id,
  66. 'to_type_id' => $config->to_type_id,
  67. 'materials' => $materials, // 直接输出材料条目
  68. 'conditions' => $conditions,
  69. 'materials_group_id' => $config->materials,
  70. 'conditions_group_id' => $config->conditions,
  71. ];
  72. }
  73. return $jsonData;
  74. } catch (\Exception $e) {
  75. if (php_sapi_name() === 'cli') {
  76. echo "Error: Generate farm_land.json failed: {$e->getMessage()}\n";
  77. }
  78. return false;
  79. }
  80. }
  81. /**
  82. * 执行命令
  83. *
  84. * @return int
  85. */
  86. public function handle()
  87. {
  88. $this->info('开始生成土地配置JSON文件...');
  89. try {
  90. // 通过缓存类生成JSON
  91. $result = \App\Module\Game\DCache\FarmLandJsonConfig::getData([], true);
  92. if ($result !== false) {
  93. $this->info('土地配置JSON文件生成成功');
  94. $this->info('共生成 ' . count($result['land_types']) . ' 条土地类型数据');
  95. $this->info('共生成 ' . count($result['upgrade_paths']) . ' 条升级路径数据');
  96. return Command::SUCCESS;
  97. } else {
  98. $this->error('生成土地配置JSON文件失败');
  99. return Command::FAILURE;
  100. }
  101. } catch (\Exception $e) {
  102. $this->error('生成土地配置JSON文件失败: ' . $e->getMessage());
  103. return Command::FAILURE;
  104. }
  105. }
  106. }