浏览代码

更新机器人跟匹配

郑晓宇 6 年之前
父节点
当前提交
cf5ceb2519
共有 2 个文件被更改,包括 35 次插入18 次删除
  1. 6 5
      app/Console/Commands/gamematch.php
  2. 29 13
      app/Console/Commands/robot.php

+ 6 - 5
app/Console/Commands/gamematch.php

@@ -61,10 +61,10 @@ class gamematch extends Command
                         echo "未找到匹配房间\n";
                         continue;
                     }
-                    //获取线机器人
-                    $user = User::inRandomOrder()->select('user_id','name','client_id')->where("is_robot",1)->where("is_login", 1)->first();
+                    //获取线机器人
+                    $user = User::inRandomOrder()->select('user_id','name','client_id')->where("is_robot",1)->where("is_login", 0)->first();
                     if(!$user){
-                        echo "没有线机器人\n";
+                        echo "没有线机器人\n";
                         sleep(1);
                         continue;
                     }
@@ -78,8 +78,9 @@ class gamematch extends Command
                     }
 
                     //发布启动机器人
-                    $message['type'] = 'gotomatch';
-                    Gateway::sendToUid($user->user_id, json_encode($message));
+                    Redis::publish("startRobot", $user->user_id);
+                    // $message['type'] = 'gotomatch';
+                    // Gateway::sendToUid($user->user_id, json_encode($message));
                 }
                 sleep(2);
                 $match_times++;

+ 29 - 13
app/Console/Commands/robot.php

@@ -78,10 +78,9 @@ class robot extends Command
                 ]
             ]);
             // $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) {
+            $con->onConnect = function($con) use($client, $worker) {
             };
             $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr, $worker){
                 //注册地址
@@ -96,25 +95,32 @@ class robot extends Command
                             'client_id' => $client_id,
                         ]
                     ]);
-                    $result = json_decode($response->getBody()->getContents());
-                    if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){
-                        echo "断线重连中 ...";
+                    if($response->getStatusCode() != 200){
+                        echo "机器人绑定 socket失败, 结束进程\n";
+                        //结束进程
+                        $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
+                        $master_pid && posix_kill($master_pid, $sig);
                         return 0;
                     }
-                    echo "client: {$client_id} 成功绑定socket\n";
                     $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)){
+                        echo "断线重连中 ...";
+                        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";
+                    $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
+                        'form_params' => [
+                            'client_id' => $client_id,
+                        ]
+                    ]);
+                    echo "{$client_id} 成功加入匹配\n";
                 }
                 //唤起匹配
                 if($json_data->type == 'gotomatch'){
@@ -151,16 +157,26 @@ class robot extends Command
 
                 //结算
                 if($json_data->type == 'round_end'){
+                    file_get_contents($worker::$pidFile);
                     echo "回合结束,已结算";
                 }
 
                 //结算
                 if($json_data->type == 'game_end'){
+                    $update_data = [
+                        "is_login" => 0,
+                        "client_id" => '',
+                    ];
+                    //更新机器人登陆状态
+                    User::where("user_id", $user->user_id)->update($update_data);
                     unset($update_data);
                     $update_data['state'] = 2;
                     RoomUser::where("user_id", $user->user_id)->where("state", 1)->update($update_data);
-                    echo "已结算,机器人离开房间";
                     Gateway::leaveGroup($client_id, $room_id);
+                    echo "已结算,机器人离开房间";
+                    //结束进程
+                    $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
+                    $master_pid && posix_kill($master_pid, $sig);
                 }
 
             };