gameclear.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Model\User;
  5. use App\Model\Room;
  6. use App\Model\RoomUser;
  7. use Illuminate\Support\Facades\Redis;
  8. class gameclear extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'game:clear';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '清理异常房间跟用户、机器人状态';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. while (1) {
  39. $start_at = time() - 300;
  40. $rooms = Room::where([
  41. ["is_end", 0]
  42. ["start_at", "<", date("Y-m-d H:i:s", $start_at)]
  43. ])->pluck("room_id");
  44. if($rooms){
  45. // 更新房间用户状态
  46. foreach ($rooms as $k => $room) {
  47. RoomUser::where("room_id",$room)->udpate(["state"=>2]);
  48. }
  49. }
  50. // 获取在线机器人
  51. $robots = User::where(["is_robot"=>1, "is_login"=>1])->pluck("room_id");
  52. if($robots){
  53. // 获取机器人游戏状态
  54. foreach ($robots as $k => $robot) {
  55. // 判断是否在游戏中
  56. $is_in_game = RoomUser::where(["user_id"=>$robot,"state"=>1])->count();
  57. if(!$is_in_game){
  58. // 更新登录状态
  59. User::where("user_id",$robot)->update(["is_login"=>0]);
  60. }
  61. }
  62. }
  63. // 每 5 分钟执行一次
  64. sleep(300);
  65. }
  66. }
  67. }