| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Models\FarmHouseConfig;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * 生成房屋配置表JSON数据命令
- *
- * 该命令用于从数据库中的房屋配置表生成JSON数据文件,供客户端使用。
- * 生成的JSON文件包含房屋等级的基本信息,如等级、产出加成、特殊土地上限等。
- * 该命令通常在房屋配置数据更新后运行,以确保客户端获取最新的配置数据。
- */
- class GenerateFarmHouseConfigJson extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:generate-house-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '生成房屋配置JSON文件';
- /**
- * 生成房屋配置JSON数据
- *
- * @param bool $saveToFile 是否保存到文件
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 禁用查询日志以避免权限问题
- DB::disableQueryLog();
- // 获取所有房屋配置
- $configs = FarmHouseConfig::orderBy('level')->get();
- // 准备JSON数据
- $jsonData = [
- 'generated_ts' => time(),
- 'house_configs' => []
- ];
- foreach ($configs as $config) {
- $jsonData['house_configs'][] = [
- 'level' => $config->level,
- 'output_bonus' => $config->output_bonus,
- 'special_land_limit' => $config->special_land_limit,
- 'upgrade_materials' => $config->upgrade_materials,
- 'downgrade_days' => $config->downgrade_days,
- ];
- }
- return $jsonData;
- } catch (\Exception $e) {
- if (php_sapi_name() === 'cli') {
- echo "Error: Generate farm_house.json failed: {$e->getMessage()}\n";
- }
- return false;
- }
- }
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->info('开始生成房屋配置JSON文件...');
- try {
- // 直接调用静态方法生成JSON,而不通过缓存类
- $result = self::generateJson();
- if ($result !== false) {
- $this->info('房屋配置JSON文件生成成功');
- $this->info('共生成 ' . count($result['house_configs']) . ' 条配置数据');
- return Command::SUCCESS;
- } else {
- $this->error('生成房屋配置JSON文件失败');
- return Command::FAILURE;
- }
- } catch (\Exception $e) {
- $this->error('生成房屋配置JSON文件失败: ' . $e->getMessage());
- return Command::FAILURE;
- }
- }
- }
|