| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Module\GameItems\Commands;
- use App\Module\Game\DCache\RecipeJsonConfig;
- use Illuminate\Console\Command;
- use App\Module\GameItems\Models\ItemRecipe;
- use Illuminate\Support\Facades\Log;
- /**
- * 生成物品合成配方配置表JSON数据命令
- *
- * 该命令用于从数据库中的物品合成配方表生成JSON数据文件,供客户端使用。
- * 生成的JSON文件包含合成配方的基本信息,如ID、名称、产出物品、所需材料等。
- * 该命令通常在合成配方数据更新后运行,以确保客户端获取最新的配方数据。
- */
- class GenerateRecipeJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'gameitems:generate-recipe-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate recipe.json from ItemRecipe table';
- /**
- * 生成合成配方JSON数据
- *
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 查询ItemRecipe表中的数据,并预加载关联数据
- $recipes = ItemRecipe::query()
- ->with([
- 'consumeGroup.consumeItems',
- 'rewardGroup.rewardItems',
- 'conditionGroup.conditionItems',
- 'materials.item' // 保留兼容性
- ])
- ->where('is_active', 1)
- ->orderBy('sort_order', 'desc')
- ->get()
- ->map(function ($recipe) {
- // 构建配方数据
- $recipeData = [
- 'id' => $recipe->id,
- 'name' => $recipe->name,
- 'code' => $recipe->code,
- 'description' => $recipe->description,
- 'success_rate' => $recipe->success_rate,
- 'cooldown_seconds' => $recipe->cooldown_seconds,
- 'sort_order' => $recipe->sort_order,
- ];
- // 消耗组数据
- if ($recipe->consume_group_id && $recipe->consumeGroup) {
- $recipeData['consume_group'] = [
- 'id' => $recipe->consumeGroup->id,
- 'name' => $recipe->consumeGroup->name,
- 'items' => $recipe->consumeGroup->consumeItems->map(function ($item) {
- return [
- 'consume_type' => $item->consume_type,
- 'target_id' => $item->target_id,
- 'quantity' => $item->quantity,
- ];
- })->toArray()
- ];
- }
- // 奖励组数据
- if ($recipe->reward_group_id && $recipe->rewardGroup) {
- $recipeData['reward_group'] = [
- 'id' => $recipe->rewardGroup->id,
- 'name' => $recipe->rewardGroup->name,
- 'is_random' => $recipe->rewardGroup->is_random,
- 'random_count' => $recipe->rewardGroup->random_count,
- 'items' => $recipe->rewardGroup->rewardItems->map(function ($item) {
- return [
- 'reward_type' => $item->reward_type,
- 'target_id' => $item->target_id,
- 'quantity' => $item->quantity,
- 'weight' => $item->weight,
- 'is_guaranteed' => $item->is_guaranteed,
- ];
- })->toArray()
- ];
- }
- // 条件组数据
- if ($recipe->condition_group_id && $recipe->conditionGroup) {
- $recipeData['condition_group'] = [
- 'id' => $recipe->conditionGroup->id,
- 'name' => $recipe->conditionGroup->name,
- 'logic_type' => $recipe->conditionGroup->logic_type,
- 'items' => $recipe->conditionGroup->conditionItems->map(function ($item) {
- return [
- 'condition_type' => $item->condition_type,
- 'target_id' => $item->target_id,
- 'operator' => $item->operator,
- 'value' => $item->value,
- ];
- })->toArray()
- ];
- }
- // 兼容旧系统:处理材料数据
- if ($recipe->materials && $recipe->materials->isNotEmpty()) {
- $recipeData['legacy_materials'] = $recipe->materials->map(function ($material) {
- return [
- 'item_id' => $material->item_id,
- 'item_name' => $material->item->name ?? '未知物品',
- 'quantity' => $material->quantity,
- 'is_consumed' => $material->is_consumed,
- ];
- })->toArray();
- }
- return $recipeData;
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- $data = [
- 'generated_ts' => time(),
- 'recipes' => $recipes
- ];
- return $data;
- } catch (\Exception $e) {
- Log::error('Generate recipe.json failed: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('Generating recipe JSON data...');
- if (RecipeJsonConfig::getData([], true)) {
- $this->info('Successfully generated recipe.json with timestamp');
- } else {
- $this->error('Failed to generate recipe.json');
- }
- }
- }
|