AddWoodRecipes.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class AddWoodRecipes extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'recipes:add-wood';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '添加木材相关的合成配方数据';
  19. /**
  20. * Execute the console command.
  21. */
  22. public function handle()
  23. {
  24. $this->info('开始添加木材相关配方数据...');
  25. // 配方数据
  26. $recipes = [
  27. [
  28. 'name' => '木板制作',
  29. 'result_item_id' => 33, // 木材ID(因为木板可能不存在,先用木材)
  30. 'result_min_quantity' => 4,
  31. 'result_max_quantity' => 4,
  32. 'success_rate' => 1.0, // 100%成功率
  33. 'coin_cost' => json_encode(['gold' => 2]),
  34. 'cooldown_seconds' => 0,
  35. 'level_required' => 1,
  36. 'is_default_unlocked' => 1,
  37. 'is_active' => 1,
  38. 'unlock_condition' => json_encode([]),
  39. 'materials' => [
  40. [
  41. 'item_id' => 33, // 木材ID
  42. 'quantity' => 1,
  43. 'is_consumed' => 1
  44. ]
  45. ]
  46. ],
  47. [
  48. 'name' => '高级木材加工',
  49. 'result_item_id' => 33, // 木材ID
  50. 'result_min_quantity' => 2,
  51. 'result_max_quantity' => 3,
  52. 'success_rate' => 0.8, // 80%成功率
  53. 'coin_cost' => json_encode(['gold' => 15]),
  54. 'cooldown_seconds' => 60,
  55. 'level_required' => 5,
  56. 'is_default_unlocked' => 0,
  57. 'is_active' => 1,
  58. 'unlock_condition' => json_encode(['level' => 5]),
  59. 'materials' => [
  60. [
  61. 'item_id' => 33, // 木材ID
  62. 'quantity' => 2,
  63. 'is_consumed' => 1
  64. ]
  65. ]
  66. ]
  67. ];
  68. try {
  69. DB::beginTransaction();
  70. foreach ($recipes as $recipeData) {
  71. $this->info("添加配方: {$recipeData['name']}");
  72. // 提取材料数据
  73. $materials = $recipeData['materials'];
  74. unset($recipeData['materials']);
  75. // 添加时间戳
  76. $recipeData['created_at'] = now();
  77. $recipeData['updated_at'] = now();
  78. // 插入配方
  79. $recipeId = DB::table('item_recipes')->insertGetId($recipeData);
  80. // 插入材料
  81. foreach ($materials as $material) {
  82. $material['recipe_id'] = $recipeId;
  83. $material['created_at'] = now();
  84. $material['updated_at'] = now();
  85. DB::table('item_recipe_materials')->insert($material);
  86. $this->line(" - 添加材料: 物品ID {$material['item_id']}, 数量 {$material['quantity']}");
  87. }
  88. $this->info("配方 '{$recipeData['name']}' 添加成功,ID: {$recipeId}");
  89. $this->line('');
  90. }
  91. DB::commit();
  92. $this->info('所有木材相关配方添加完成!');
  93. } catch (\Exception $e) {
  94. DB::rollBack();
  95. $this->error('错误: ' . $e->getMessage());
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. }