| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Module\GameItems\Commands;
- use App\Module\Game\DCache\ChestJsonConfig;
- use App\Module\GameItems\Config\NumericAttributesWhitelist;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\ItemChestConfig;
- use Illuminate\Console\Command;
- use App\Module\GameItems\Models\Item;
- use UCore\Helper\Logger;
- /**
- * 生成宝箱JSON数据命令
- *
- * 该命令用于从数据库中的宝箱相关表生成宝箱JSON数据文件,供客户端使用。
- * 生成的JSON文件包含宝箱的基本信息和内容配置,如宝箱ID、可能获得的物品及其概率等。
- * 该命令通常在宝箱配置更新后运行,以确保客户端获取最新的宝箱数据。
- */
- class GenerateChestJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'gameitems:generate-chest-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate chest.json from Item-chest table';
- /**
- * 执行命令
- */
- /**
- * 生成宝箱JSON数据
- *
- * @param bool $saveToFile 是否保存到文件
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 查询Item表中的宝箱数据,使用新的组系统
- $chests = Item::query()
- ->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; // 失败
- }
- }
- }
|