PetServiceProvider.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Module\Pet\Providers;
  3. use App\Module\Pet\Commands\GeneratePetJsonCommand;
  4. use App\Module\Pet\Commands\TestStealFunctionCommand;
  5. use App\Module\Pet\Commands\TestPetExpGainedEventCommand;
  6. use App\Module\Pet\Commands\TestPetFeedEventCommand;
  7. use App\Module\Pet\Console\ProcessActiveSkillsCommand;
  8. use App\Module\Pet\Events\PetCreatedEvent;
  9. use App\Module\Pet\Events\PetLevelUpEvent;
  10. use App\Module\Pet\Events\PetRemouldEvent;
  11. use App\Module\Pet\Events\PetSkillUsedEvent;
  12. use App\Module\Pet\Events\PetStatusChangedEvent;
  13. use App\Module\Pet\Events\PetUpdateEvent;
  14. use App\Module\Pet\Listeners\ItemChangedListener;
  15. use App\Module\Pet\Listeners\LoginSuccessListener;
  16. use App\Module\Pet\Logic\PetLogic;
  17. use Illuminate\Support\Facades\Event;
  18. use Illuminate\Support\Facades\Schedule;
  19. use Illuminate\Support\ServiceProvider;
  20. /**
  21. * 宠物模块服务提供者
  22. *
  23. * 负责注册宠物模块的服务、事件监听器、命令等
  24. */
  25. class PetServiceProvider extends ServiceProvider
  26. {
  27. /**
  28. * 事件到监听器的映射
  29. *
  30. * @var array
  31. */
  32. protected $listen = [
  33. // 监听来自其他模块的事件
  34. \App\Module\AppGame\Events\LoginSuccessEvent::class => [
  35. LoginSuccessListener::class,
  36. ]
  37. ];
  38. /**
  39. * 注册服务
  40. *
  41. * @return void
  42. */
  43. public function register()
  44. {
  45. }
  46. /**
  47. * 启动服务
  48. *
  49. * @return void
  50. */
  51. public function boot()
  52. {
  53. // 注册事件监听器
  54. $this->registerEventListeners();
  55. // 注册命令
  56. if ($this->app->runningInConsole()) {
  57. $this->commands([
  58. \App\Module\Pet\Commands\GeneratePetJsonCommand::class, // 生成宠物配置JSON数据命令
  59. \App\Module\Pet\Commands\TestStealFunctionCommand::class, // 测试偷菜功能命令
  60. \App\Module\Pet\Commands\TestPetExpGainedEventCommand::class, // 测试宠物经验增加事件命令
  61. \App\Module\Pet\Commands\TestPetFeedEventCommand::class, // 测试宠物喂养事件命令
  62. \App\Module\Pet\Console\ProcessActiveSkillsCommand::class, // 处理宠物激活技能命令
  63. ]);
  64. }
  65. // 注册宠物定时任务
  66. $this->registerSchedules();
  67. }
  68. /**
  69. * 注册事件监听器
  70. *
  71. * @return void
  72. */
  73. protected function registerEventListeners()
  74. {
  75. foreach ($this->listen as $event => $listeners) {
  76. foreach ($listeners as $listener) {
  77. Event::listen($event, $listener);
  78. }
  79. }
  80. }
  81. /**
  82. * 注册宠物相关的定时任务
  83. *
  84. * 将原本在 routes/console.php 中的宠物调度配置迁移到此处
  85. */
  86. protected function registerSchedules(): void
  87. {
  88. // 在应用完全启动后注册定时任务
  89. $this->app->booted(function () {
  90. // 每分钟处理宠物激活技能
  91. Schedule::command('pet:process-active-skills')
  92. ->everyMinute()
  93. ->description('处理宠物激活技能');
  94. });
  95. }
  96. }