12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use GatewayWorker\Lib\Gateway;
- use Illuminate\Support\Facades\Redis;
- use App\Model\User;
- use App\Model\Room;
- use App\Model\RoomUser;
- class recylerobots extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'game:recylerobots';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '机器人资源回收';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- //注册地址
- Gateway::$registerAddress = '127.0.0.1:1238';
- while(1){
- //获取所有机器人
- $robots = User::where("is_login", 1)->where("is_robot", 1)->get();
- if($robots){
- foreach ($robots as $robot) {
- //检测是否在线
- if(Gateway::isOnline($robot->client_id)){
- //检测游戏是否结束
- $has_game = RoomUser::where("user_id", $robot->user_id)->where("state", 1)->count();
- if(!$has_game){
- $message['type'] = "game_end";
- Gateway::sendToUid($robot->user_id, json_encode($message));
- }
- }else{
- //检测游戏是否结束
- $room_id = RoomUser::where("user_id", $robot->user_id)->where("state", 1)->value("room_id");
- if($room_id){
- //房间是否结束
- $is_end = Room::where("room_id", $room_id)->where("is_end", 1)->count();
- if($is_end){
- unset($update_data);
- $update_data['state'] = 0;
- RoomUser::where("user_id", $robot->user_id)->where("state", 1)->update($update_data);
- }
- }
- $update_data["is_login"] = 0;
- User::where("user_id", $robot->user_id)->update($update_data);
- }
- }
- }
- sleep(5);
- }
- }
- }
|