recylerobots.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. echo Gateway::isUidOnline(3);exit;
  42. while(1){
  43. //获取所有机器人
  44. $robots = User::where("is_login", 1)->where("is_robot", 1)->get();
  45. if($robots){
  46. foreach ($robots as $robot) {
  47. //检测是否在线
  48. if(Gateway::isOnline($robot->client_id)){
  49. //检测游戏是否结束
  50. $has_game = RoomUser::where("user_id", $robot->user_id)->where("state", 1)->count();
  51. if(!$has_game){
  52. $message['type'] = "game_end";
  53. Gateway::sendToUid($robot->user_id, json_encode($message));
  54. }
  55. }else{
  56. $update_data["is_login"] = 0;
  57. User::where("user_id", $robot->user_id)->update($update_data);
  58. }
  59. }
  60. }
  61. sleep(5);
  62. }
  63. }
  64. }