recylerobots.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GatewayWorker\Lib\Gateway;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Model\User;
  7. use App\Model\Room;
  8. use App\Model\RoomUser;
  9. class recylerobots extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'game:recylerobots';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '机器人资源回收';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. //注册地址
  40. Gateway::$registerAddress = '127.0.0.1:1238';
  41. while(1){
  42. //获取所有机器人
  43. $robots = User::where("is_login", 1)->where("is_robot", 1)->get();
  44. if($robots){
  45. foreach ($robots as $robot) {
  46. //检测是否在线
  47. if(Gateway::isUidOnline($robot->user_id)){
  48. //检测游戏是否结束
  49. $has_game = RoomUser::where("user_id", $robot->user_id)->where("state", 1)->count();
  50. if(!$has_game){
  51. $message['type'] = "game_end";
  52. Gateway::sendToUid($robot->user_id, json_encode($message));
  53. }
  54. }else{
  55. $update_data["is_login"] = 0;
  56. $update_data["client_id"] = '';
  57. User::where("user_id", $robot->user_id)->update($update_data);
  58. }
  59. }
  60. }
  61. sleep(10);
  62. }
  63. }
  64. }