郑晓宇 před 6 roky
rodič
revize
0dc0e3c606
2 změnil soubory, kde provedl 27 přidání a 15 odebrání
  1. 1 1
      app/Console/Commands/gamematch.php
  2. 26 14
      app/Console/Commands/robot.php

+ 1 - 1
app/Console/Commands/gamematch.php

@@ -52,7 +52,7 @@ class gamematch extends Command
                 if($match_times > 3){
                     $room = Redis::get('T1');
                     if($room){
-                        $robot = Redis::lpop('robot_list');
+                        $robot = Redis::spop('robot_list');
                         $message = [
                             "type" => 'gotomatch',
                             "client_id" => $robot,

+ 26 - 14
app/Console/Commands/robot.php

@@ -10,6 +10,7 @@ use GuzzleHttp\Cookie\CookieJar;
 use Workerman\Worker;
 use Workerman\Connection\AsyncTcpConnection;
 use Illuminate\Support\Facades\Redis;
+use GatewayWorker\Lib\Gateway;
 
 class robot extends Command
 {
@@ -53,6 +54,8 @@ class robot extends Command
         $worker = new Worker();
         $worker->onWorkerStart = function($worker) use($username){
             //定义请求客户端
+            $room_id = ''; //定义当前机器人加入的room_id
+            $client_id = ''; //定义当前机器人client_id
             $client = new Client([
                 'cookies' => true,
                 'verify' => false,
@@ -68,35 +71,38 @@ class robot extends Command
             $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
             $con->onConnect = function($con) use($client) {
             };
-            $con->onMessage = function($con, $data) use($client){
+            $con->onMessage = function($con, $data) use($client, $room_id, $client_id){
+                //注册地址
+                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://183.234.61.252:8090/Home/User/Bind',[
                         'form_params' => [
-                            'client_id' => $json_data->client_id,
+                            'client_id' => $client_id,
                         ]
                     ]);
-                    echo "成功绑定socket\n";
-                    //测试使用直接加入匹配
-                    // $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
-                    //     'form_params' => [
-                    //         'client_id' => $json_data->client_id,
-                    //     ]
-                    // ]);
+                    echo "client: {$client_id} 成功绑定socket\n";
 
                     //记录当前在线机器人
-                    Redis::lpush('robot_list', $json_data->client_id);
+                    Redis::sadd('robot_list', $client_id);
+                    Redis::lpush('online_robot_list', $client_id);
                 }
                 //唤起匹配
                 if($json_data->type == 'gotomatch'){
-                    var_dump($json_data);
                     $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
                         'form_params' => [
-                            'client_id' => $json_data->client_id,
+                            'client_id' => $client_id,
                         ]
                     ]);
-                    echo "成功加入匹配\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'){
@@ -118,9 +124,15 @@ class robot extends Command
                     if($json_data->is_end == 1){
                         echo "成功重回队列\n";
                         //重新加入机器人队列
-                        Redis::lpush('robot_list', $json_data->client_id);
+                        Redis::sadd('robot_list', $json_data->client_id);
                     }
                 }
+                //获得题目
+                if($json_data->type == 'checkrobot'){
+                    echo "{$client_id} 成功重回队列\n";
+                    //重新加入机器人队列
+                    Redis::sadd('robot_list', $json_data->client_id);
+                }
             };
             $con->connect();
         };