orderBy('id')->get(); // 准备JSON数据 $jsonData = [ 'generated_ts' => time(), 'seeds' => [], 'fruit_growth_cycles' => [] ]; // 获取所有果实生长周期配置 $fruitGrowthCycles = FarmFruitGrowthCycle::all()->keyBy('fruit_item_id'); // 处理果实生长周期数据 foreach ($fruitGrowthCycles as $fruitItemId => $cycle) { $jsonData['fruit_growth_cycles'][$fruitItemId] = [ 'sprout_time' => $cycle->sprout_time, 'growth_time' => $cycle->growth_time, 'fruit_time' => $cycle->fruit_time, // 添加果实期时间 'mature_time' => $cycle->mature_time, 'wither_time' => $cycle->wither_time, ]; } // 处理种子基本数据和产出数据 foreach ($seeds as $seed) { // 获取种子的所有产出物品ID $seedOutputs = []; foreach ($seed->outputs as $output) { $seedOutputs[] = $output->item_id; } $jsonData['seeds'][] = [ 'id' => $seed->id, 'name' => $seed->name, 'type' => $seed->type, 'seed_time' => $seed->seed_time, 'min_output' => $seed->min_output, 'max_output' => $seed->max_output, 'disaster_min_output' => $seed->disaster_min_output ?? 0, 'disaster_max_output' => $seed->disaster_max_output ?? 0, 'item_id' => $seed->item_id, 'disaster_resistance' => $seed->disaster_resistance, 'display_attributes' => $seed->display_attributes, 'seed_outputs' => $seedOutputs, // 直接在种子属性中包含产出物品ID数组 ]; } return $jsonData; } catch (\Exception $e) { if (php_sapi_name() === 'cli') { echo "Error: Generate farm_seed.json failed: {$e->getMessage()}\n"; } return false; } } /** * 执行命令 * * @return int */ public function handle() { $this->info('开始生成种子配置JSON文件...'); try { // 通过缓存类生成JSON $result = \App\Module\Game\DCache\FarmSeedJsonConfig::getData([], true); if ($result !== false && is_array($result)) { $this->info('种子配置JSON文件生成成功'); $this->info('共生成 ' . count($result['seeds'] ?? []) . ' 条种子数据'); // 统计总的产出物品数量 $totalOutputs = 0; foreach ($result['seeds'] as $seed) { $totalOutputs += count($seed['seed_outputs'] ?? []); } $this->info('共包含 ' . $totalOutputs . ' 个种子产出物品'); $this->info('共包含 ' . count($result['fruit_growth_cycles'] ?? []) . ' 个果实生长周期配置'); // 显示一些示例数据 if (!empty($result['seeds'])) { $this->info('种子示例数据:'); $firstSeed = $result['seeds'][0]; $this->line(' ID: ' . $firstSeed['id'] . ', 名称: ' . $firstSeed['name']); $this->line(' 产出物品: [' . implode(', ', $firstSeed['seed_outputs'] ?? []) . ']'); } return Command::SUCCESS; } else { $this->error('生成种子配置JSON文件失败'); return Command::FAILURE; } } catch (\Exception $e) { $this->error('生成种子配置JSON文件失败: ' . $e->getMessage()); return Command::FAILURE; } } }