| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Models\FarmLandType;
- use App\Module\Farm\Models\FarmLandUpgradeConfig;
- use Illuminate\Console\Command;
- /**
- * 生成土地配置表JSON数据命令
- *
- * 该命令用于从数据库中的土地类型表和土地升级配置表生成JSON数据文件,供客户端使用。
- * 生成的JSON文件包含土地类型的基本信息,如类型ID、名称、产量加成、灾害抵抗等,以及土地升级路径和所需材料。
- * 该命令通常在土地配置数据更新后运行,以确保客户端获取最新的配置数据。
- */
- class GenerateFarmLandConfigJson extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:generate-land-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '生成土地配置JSON文件';
- /**
- * 生成土地配置JSON数据
- *
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 获取所有土地类型
- $landTypes = FarmLandType::orderBy('id')->get();
- // 获取所有土地升级配置
- $upgradeConfigs = FarmLandUpgradeConfig::with(['fromType', 'toType'])->get();
- // 准备JSON数据
- $jsonData = [
- 'generated_ts' => time(),
- 'land_types' => [],
- 'upgrade_paths' => []
- ];
- // 处理土地类型数据
- foreach ($landTypes as $type) {
- $jsonData['land_types'][] = [
- 'id' => $type->id,
- 'name' => $type->name,
- 'code' => $type->code,
- 'output_bonus' => $type->output_bonus,
- 'disaster_resistance' => $type->disaster_resistance,
- 'unlock_house_level' => $type->unlock_house_level,
- 'is_special' => $type->is_special,
- 'display_attributes' => $type->display_attributes,
- 'description' => $type->description,
- ];
- }
- // 处理土地升级路径数据
- foreach ($upgradeConfigs as $config) {
- // 获取完整的材料内容和条件内容
- $materials = $config->getUpgradeMaterials();
- $conditions = $config->getUpgradeConditions();
- $jsonData['upgrade_paths'][] = [
- 'from_type_id' => $config->from_type_id,
- 'to_type_id' => $config->to_type_id,
- 'materials' => $materials, // 直接输出材料条目
- 'conditions' => $conditions,
- 'materials_group_id' => $config->materials,
- 'conditions_group_id' => $config->conditions,
- ];
- }
- return $jsonData;
- } catch (\Exception $e) {
- if (php_sapi_name() === 'cli') {
- echo "Error: Generate farm_land.json failed: {$e->getMessage()}\n";
- }
- return false;
- }
- }
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->info('开始生成土地配置JSON文件...');
- try {
- // 通过缓存类生成JSON
- $result = \App\Module\Game\DCache\FarmLandJsonConfig::getData([], true);
- if ($result !== false) {
- $this->info('土地配置JSON文件生成成功');
- $this->info('共生成 ' . count($result['land_types']) . ' 条土地类型数据');
- $this->info('共生成 ' . count($result['upgrade_paths']) . ' 条升级路径数据');
- return Command::SUCCESS;
- } else {
- $this->error('生成土地配置JSON文件失败');
- return Command::FAILURE;
- }
- } catch (\Exception $e) {
- $this->error('生成土地配置JSON文件失败: ' . $e->getMessage());
- return Command::FAILURE;
- }
- }
- }
|