recylerobots.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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::isOnline("7f0000010b5700000cb6");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::isUidOnline($robot->user_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. $update_data["client_id"] = '';
  58. User::where("user_id", $robot->user_id)->update($update_data);
  59. }
  60. }
  61. }
  62. sleep(10);
  63. }
  64. }
  65. }