| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- class AddWoodRecipes extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'recipes:add-wood';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '添加木材相关的合成配方数据';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $this->info('开始添加木材相关配方数据...');
- // 配方数据
- $recipes = [
- [
- 'name' => '木板制作',
- 'result_item_id' => 33, // 木材ID(因为木板可能不存在,先用木材)
- 'result_min_quantity' => 4,
- 'result_max_quantity' => 4,
- 'success_rate' => 1.0, // 100%成功率
- 'coin_cost' => json_encode(['gold' => 2]),
- 'cooldown_seconds' => 0,
- 'level_required' => 1,
- 'is_default_unlocked' => 1,
- 'is_active' => 1,
- 'unlock_condition' => json_encode([]),
- 'materials' => [
- [
- 'item_id' => 33, // 木材ID
- 'quantity' => 1,
- 'is_consumed' => 1
- ]
- ]
- ],
- [
- 'name' => '高级木材加工',
- 'result_item_id' => 33, // 木材ID
- 'result_min_quantity' => 2,
- 'result_max_quantity' => 3,
- 'success_rate' => 0.8, // 80%成功率
- 'coin_cost' => json_encode(['gold' => 15]),
- 'cooldown_seconds' => 60,
- 'level_required' => 5,
- 'is_default_unlocked' => 0,
- 'is_active' => 1,
- 'unlock_condition' => json_encode(['level' => 5]),
- 'materials' => [
- [
- 'item_id' => 33, // 木材ID
- 'quantity' => 2,
- 'is_consumed' => 1
- ]
- ]
- ]
- ];
- try {
- DB::beginTransaction();
- foreach ($recipes as $recipeData) {
- $this->info("添加配方: {$recipeData['name']}");
- // 提取材料数据
- $materials = $recipeData['materials'];
- unset($recipeData['materials']);
- // 添加时间戳
- $recipeData['created_at'] = now();
- $recipeData['updated_at'] = now();
- // 插入配方
- $recipeId = DB::table('item_recipes')->insertGetId($recipeData);
- // 插入材料
- foreach ($materials as $material) {
- $material['recipe_id'] = $recipeId;
- $material['created_at'] = now();
- $material['updated_at'] = now();
- DB::table('item_recipe_materials')->insert($material);
- $this->line(" - 添加材料: 物品ID {$material['item_id']}, 数量 {$material['quantity']}");
- }
- $this->info("配方 '{$recipeData['name']}' 添加成功,ID: {$recipeId}");
- $this->line('');
- }
- DB::commit();
- $this->info('所有木材相关配方添加完成!');
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error('错误: ' . $e->getMessage());
- return 1;
- }
- return 0;
- }
- }
|