where('type', ITEM_TYPE::CHEST) ->with([ 'chestConfig' => function ($query) { $query->where('is_active', true) ->with([ 'consumeGroup.consumeItems', 'rewardGroup.rewardItems', 'conditionGroup' ]); } ]) ->select(['id', 'name', 'numeric_attributes']) ->get(); // 处理数据,去除不必要的字段 $processedChests = $chests->map(function (Item $chest) { // 检查宝箱是否有配置数据 if (!$chest->chestConfig) { Logger::error("跳过物品 {$chest->id} ,没有宝箱配置"); return null; // 跳过没有配置的宝箱 } // 获取激活的配置(应该只有一个) $config = $chest->chestConfig; if (!$config || !$config->rewardGroup) { Logger::error("跳过宝箱 {$chest->id} ,没有奖励组"); return null; // 跳过没有奖励组的宝箱 } $chestData = [ 'id' => $chest->id, 'name' => $chest->name, 'desc' => $chest->description, ]; // 处理宝箱内容(从奖励组获取) $contents = []; if ($config->rewardGroup && $config->rewardGroup->rewardItems) { foreach ($config->rewardGroup->rewardItems as $rewardItem) { // 转换为 Proto 对象 $rewardProtoObject = $rewardItem->toRewardObject(); $contentData= json_decode($rewardProtoObject->serializeToJsonString(),true); $contents[] = $contentData; } } // 检查处理后的内容是否为空 if (empty($contents)) { Logger::error("跳过宝箱 {$chest->id} ,没有奖励内容"); return null; // 如果没有有效的内容,跳过这个宝箱 } $chestData['contents'] = $contents; // 处理宝箱开启消耗(从消耗组获取) $costs = []; if ($config->consumeGroup && $config->consumeGroup->consumeItems) { foreach ($config->consumeGroup->consumeItems as $consumeItem) { // 转换为 Proto 对象 $consumeProtoObject = $consumeItem->toDeductObject(); $costData= json_decode($consumeProtoObject->serializeToJsonString(),true); $costs[] = $costData; } } $chestData['costs'] = $costs; return $chestData; }) ->filter() // 过滤掉返回值为null的宝箱 ->toArray(); // 准备完整数据,包含生成时间 $data = [ 'generated_ts' => time(), 'chests' => $processedChests ]; return $data; } catch (\Exception $e) { // 不使用Log,直接输出到控制台 echo 'Generate chest.json failed: ' . $e->getMessage() . "\n"; echo $e->getTraceAsString() . "\n"; return false; } } /** * 执行命令 * * @return int */ public function handle() { $this->info('Generating chest JSON data...'); $result = ChestJsonConfig::getData([], true); if ($result && isset($result['chests'])) { $chestCount = count($result['chests']); $contentCount = 0; $costCount = 0; foreach ($result['chests'] as $chest) { $contentCount += count($chest['contents'] ?? []); $costCount += count($chest['costs'] ?? []); } $this->info('Successfully generated chest.json with timestamp'); $this->info("Processed {$chestCount} chests with {$contentCount} content items and {$costCount} cost items"); return 0; // 成功 } else { $this->error('Failed to generate chest.json'); return 1; // 失败 } } }