|
@@ -50,9 +50,9 @@ class robot extends Command
|
|
|
//获取机器人信息
|
|
|
$user_id = $this->argument('user_id');
|
|
|
if(!$user_id){
|
|
|
- $user = User::inRandomOrder()->select('user_id','name')->where("is_robot",1)->where("is_login",0)->first();
|
|
|
+ $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("is_login",0)->first();
|
|
|
+ $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->where("is_login",0)->first();
|
|
|
}
|
|
|
if(!$user){
|
|
|
echo "未找到机器人";
|
|
@@ -60,6 +60,9 @@ class robot extends Command
|
|
|
}
|
|
|
$worker = new Worker();
|
|
|
$worker->onWorkerStart = function($worker) use($user){
|
|
|
+ //定义基本信息
|
|
|
+ $http_addr = 'http://183.234.61.252:8090/';
|
|
|
+ $http_addr = 'http://www.dt.com/';
|
|
|
//定义请求客户端
|
|
|
$room_id = ''; //定义当前机器人加入的room_id
|
|
|
$client_id = ''; //定义当前机器人client_id
|
|
@@ -67,30 +70,37 @@ class robot extends Command
|
|
|
'cookies' => true,
|
|
|
'verify' => false,
|
|
|
]);
|
|
|
- $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Login',[
|
|
|
- 'form_params' => [
|
|
|
- 'username' => $user->name,
|
|
|
+ $response = $client->request('GET', $http_addr . 'Home/User/Info', [
|
|
|
+ 'query' => [
|
|
|
+ 'user_id' => $user->user_id,
|
|
|
+ 'mt' => 1,
|
|
|
]
|
|
|
]);
|
|
|
- $response = $client->request('GET', 'http://183.234.61.252:8090/Home/User/Info');
|
|
|
// $robot = $response->getBody()->getContents();
|
|
|
|
|
|
- $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
|
|
|
+ // $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
|
|
|
+ $con = new AsyncTcpConnection('ws://127.0.0.1:8282');
|
|
|
$con->onConnect = function($con) use($client) {
|
|
|
};
|
|
|
- $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user){
|
|
|
+ $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr){
|
|
|
//注册地址
|
|
|
Gateway::$registerAddress = '127.0.0.1:1238';
|
|
|
//格式化参数
|
|
|
+ var_dump($data);
|
|
|
$json_data = json_decode($data);
|
|
|
//初始化
|
|
|
if($json_data->type == 'init'){
|
|
|
$client_id = $json_data->client_id;
|
|
|
- $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Bind',[
|
|
|
+ $response = $client->request('POST', $http_addr . 'Home/User/Bind', [
|
|
|
'form_params' => [
|
|
|
'client_id' => $client_id,
|
|
|
]
|
|
|
]);
|
|
|
+ $result = json_decode($response->getBody()->getContents());
|
|
|
+ echo $result->msg;
|
|
|
+ if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
echo "client: {$client_id} 成功绑定socket\n";
|
|
|
$update_data = [
|
|
|
"is_login" => 1,
|
|
@@ -98,22 +108,17 @@ class robot extends Command
|
|
|
];
|
|
|
//更新机器人登陆状态
|
|
|
User::where("user_id", $user->user_id)->update($update_data);
|
|
|
- //记录当前在线机器人
|
|
|
- // Redis::sadd('robot_list', $client_id);
|
|
|
- // Redis::lpush('online_robot_list', $client_id);
|
|
|
-
|
|
|
//启动匹配
|
|
|
- $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
|
|
|
+ $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('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
|
|
|
+ $response = $client->request('POST', $http_addr . 'Home/Game/Join',[
|
|
|
'form_params' => [
|
|
|
'client_id' => $client_id,
|
|
|
]
|
|
@@ -134,16 +139,17 @@ class robot extends Command
|
|
|
//随机几秒回答问题
|
|
|
$second = rand(0,6);
|
|
|
sleep($second);
|
|
|
- $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[
|
|
|
+ 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' => $options[$answer]->option_id,
|
|
|
]
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
//结算
|
|
|
- if($json_data->type == 'settlement'){
|
|
|
+ if($json_data->type == 'game_end'){
|
|
|
$update_data = [
|
|
|
"is_login" => 0,
|
|
|
"client_id" => '',
|
|
@@ -152,7 +158,7 @@ class robot extends Command
|
|
|
User::where("user_id", $user->user_id)->update($update_data);
|
|
|
unset($update_data);
|
|
|
$update_data['state'] = 2;
|
|
|
- User::where("user_id", $user->user_id)->where("state", 1)->update($update_data);
|
|
|
+ RoomUser::where("user_id", $user->user_id)->where("state", 1)->update($update_data);
|
|
|
echo "已结算,关闭机器人";
|
|
|
die;
|
|
|
}
|