argument('user_id'); // $argv[0] = ''; // $argv[1] = "start"; // $argv[2] = ''; if(!$user_id){ $user = User::inRandomOrder()->select('user_id','name')->where("is_robot", 1)->where("is_login", 0)->first(); }else{ // $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->where("is_login",0)->first(); $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->first(); } if(!$user){ echo "未找到机器人"; die; } //定义请求客户端 $room_id = ''; //定义当前机器人加入的room_id $client_id = ''; //定义当前机器人client_id $worker = new Worker(); $worker->onWorkerStart = function($worker) use($user, $room_id, $client_id){ //定义基本信息 $http_addr = 'http://183.234.61.252:8090/'; // $http_addr = 'http://www.dt.com/'; $client = new Client([ 'cookies' => true, 'verify' => false, ]); $response = $client->request('GET', $http_addr . 'Home/User/Info', [ 'query' => [ 'user_id' => $user->user_id, 'mt' => 1, 'is_robot' => 1, ] ]); // $robot = $response->getBody()->getContents(); $con = new AsyncTcpConnection('ws://183.234.61.252:8282'); // $con = new AsyncTcpConnection('ws://127.0.0.1:8282'); $con->onConnect = function($con) use($client, $worker) { }; $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr, $worker){ //注册地址 Gateway::$registerAddress = '127.0.0.1:1238'; //格式化参数 $json_data = json_decode($data); //初始化 if($json_data->type == 'init'){ $client_id = $json_data->client_id; $response = $client->request('POST', $http_addr . 'Home/User/Bind', [ 'form_params' => [ 'client_id' => $client_id, ] ]); if($response->getStatusCode() != 200){ echo "机器人绑定 socket失败, 结束进程\n"; //结束进程 $sig = 9; $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0; $master_pid && posix_kill($master_pid, $sig); Log::info("用户ID: {$user->user_id} \n 绑定Socket失败退出进程\n"); return 0; } $update_data = [ "is_login" => 1, "client_id" => $client_id, ]; //更新机器人登陆状态 User::where("user_id", $user->user_id)->update($update_data); $result = json_decode($response->getBody()->getContents()); if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){ //结束进程 $sig = 9; $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0; $master_pid && posix_kill($master_pid, $sig); Log::info("用户ID: {$user->user_id} \n 正在游戏或者登录失败退出进程\n"); return 0; } echo "client: {$client_id} 成功绑定socket\n"; //启动匹配 $response = $client->request('GET', $http_addr . 'Home/Game/Join',[ 'form_params' => [ 'client_id' => $client_id, ] ]); echo "{$client_id} 成功加入匹配\n"; } //唤起匹配 if($json_data->type == 'gotomatch'){ $response = $client->request('GET', $http_addr . 'Home/Game/Join',[ 'form_params' => [ 'client_id' => $client_id, ] ]); var_dump($response->getBody()->getContents()) . "\n"; echo "{$client_id} 成功加入匹配\n"; } //匹配成功 if($json_data->type == 'match'){ $room_id = $json_data->info->room_id; echo "{$client_id} 成功加入 {$room_id} \n"; } //获得题目 if($json_data->type == 'question'){ $question_id = $json_data->info->question->question_id; $options = $json_data->info->options; $answer = rand(0, count($json_data->info->options) - 1); //随机几秒回答问题 $second = rand(0,6); sleep($second); echo "获得题目 {$json_data->info->question->question_id} : {$json_data->info->question->title}\n"; $response = $client->request('POST', $http_addr . 'Home/Game/Answer',[ 'form_params' => [ 'question_id' => $question_id, 'option_id' => $options[$answer]->option_id, ] ]); } //结算 if($json_data->type == 'round_end'){ echo "回合结束,已结算"; } //结算 if($json_data->type == 'game_end'){ $update_data['is_login'] = 0; //更新机器人登陆状态 User::where("user_id", $user->user_id)->update($update_data); echo "已结算,机器人离开房间"; //结束进程 $sig = 9; $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0; $master_pid && posix_kill($master_pid, $sig); $master_is_alive = $master_pid && posix_kill($master_pid, 0); if ($master_is_alive) { Log::info("用户ID: {$user->user_id} \n 房间ID: {$room_id}\n 删除不了进程 {$master_pid}\n"); } } }; $con->connect(); }; Worker::runAll(); } }