GenerateFarmHouseConfigJson.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Models\FarmHouseConfig;
  4. use Illuminate\Console\Command;
  5. /**
  6. * 生成房屋配置表JSON数据命令
  7. *
  8. * 该命令用于从数据库中的房屋配置表生成JSON数据文件,供客户端使用。
  9. * 生成的JSON文件包含房屋等级的基本信息,如等级、产出加成、特殊土地上限等。
  10. * 该命令通常在房屋配置数据更新后运行,以确保客户端获取最新的配置数据。
  11. */
  12. class GenerateFarmHouseConfigJson extends Command
  13. {
  14. /**
  15. * 命令名称
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'farm:generate-house-json';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '生成房屋配置JSON文件';
  26. /**
  27. * 生成房屋配置JSON数据
  28. *
  29. * @param bool $saveToFile 是否保存到文件
  30. * @return array|bool 生成的数据或失败标志
  31. */
  32. public static function generateJson()
  33. {
  34. try {
  35. // 获取所有房屋配置
  36. /**
  37. * @var FarmHouseConfig $config
  38. */
  39. $configs = FarmHouseConfig::orderBy('level')->get();
  40. // 准备JSON数据
  41. $jsonData = [
  42. 'generated_ts' => time(),
  43. 'house_configs' => []
  44. ];
  45. foreach ($configs as $config) {
  46. // 获取升级所需材料
  47. $materials = $config->getUpgradeMaterials();
  48. $jsonData['house_configs'][] = [
  49. 'level' => $config->level,
  50. 'output_bonus' => $config->output_bonus,
  51. 'special_land_limit' => $config->special_land_limit,
  52. 'upgrade_materials' => $materials,
  53. 'materials_group_id' => $config->upgrade_materials,
  54. 'downgrade_days' => $config->downgrade_days,
  55. ];
  56. }
  57. return $jsonData;
  58. } catch (\Exception $e) {
  59. if (php_sapi_name() === 'cli') {
  60. echo "Error: Generate farm_house.json failed: {$e->getMessage()}\n";
  61. }
  62. return false;
  63. }
  64. }
  65. /**
  66. * 执行命令
  67. *
  68. * @return int
  69. */
  70. public function handle()
  71. {
  72. $this->info('开始生成房屋配置JSON文件...');
  73. try {
  74. // 通过缓存类生成JSON
  75. $result = \App\Module\Game\DCache\FarmHouseJsonConfig::getData([], true);
  76. if ($result !== false) {
  77. $this->info('房屋配置JSON文件生成成功');
  78. $this->info('共生成 ' . count($result['house_configs']) . ' 条配置数据');
  79. return Command::SUCCESS;
  80. } else {
  81. $this->error('生成房屋配置JSON文件失败');
  82. return Command::FAILURE;
  83. }
  84. } catch (\Exception $e) {
  85. $this->error('生成房屋配置JSON文件失败: ' . $e->getMessage());
  86. return Command::FAILURE;
  87. }
  88. }
  89. }