GenerateFarmLandConfigJson.php 3.7 KB

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