| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Module\GameItems\Commands;
- use App\Module\Game\DCache\RecipeJsonConfig;
- use App\Module\Game\Services\ConsumeService;
- use App\Module\Game\Services\RewardService;
- 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'
- ])
- ->where('is_active', 1)
- ->orderBy('sort_order', 'desc')
- ->get()
- ->map(function (ItemRecipe $recipe) {
- // 构建配方数据
- $recipeData = [
- 'id' => $recipe->id,
- 'name' => $recipe->name,
- 'code' => $recipe->code,
- 'description' => $recipe->description,
- 'success_rate' => $recipe->success_rate,
- 'display_attributes'=>$recipe->display_attributes,
- 'cooldown_seconds' => $recipe->cooldown_seconds,
- 'sort_order' => $recipe->sort_order,
- ];
- // 消耗组数据
- if ($recipe->consume_group_id && $recipe->consumeGroup) {
- $consume = ConsumeService::getConsumeGroupAsDeduct($recipe->consume_group_id);
- $recipeData['consume_group'] = json_decode($consume->serializeToJsonString(),true);
- }
- // 奖励组数据
- if ($recipe->reward_group_id && $recipe->rewardGroup) {
- $reward = RewardService::getRewardGroupAsReward($recipe->reward_group_id);
- $recipeData['reward_group'] = json_decode($reward->serializeToJsonString(),true);
- }
- // 条件组,临时跳过
- return $recipeData;
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- $data = [
- 'generated_ts' => time(),
- 'recipes' => $recipes
- ];
- return $data;
- } catch (\Exception $e) {
- Log::error('Generate 合成配方 failed: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('Generating 合成配方 JSON data...');
- if (RecipeJsonConfig::getData([], true)) {
- $this->info('Successfully generated recipe.json with timestamp');
- } else {
- $this->error('Failed to generate 合成配方 ');
- }
- }
- }
|