郑晓宇 6 vuotta sitten
vanhempi
commit
9f6b7e90b1

+ 10 - 0
app/Console/Commands/gamematch.php

@@ -48,9 +48,19 @@ class gamematch extends Command
             $data = Redis::lpop('match_list');
             if(!$data){
                 echo "未找到匹配\n";
+                if($match_times > 3){
+                    $robot = Redis::lpop('robot_list');
+                    $message = [
+                        "type" => 'gotomatch',
+                    ];
+                    Gateway::sendToClient($robot, $message);
+                    echo "启动机器人: {$robot}\n";
+                }
                 sleep(1);
+                $match_times++;
                 continue;
             }
+            $match_times = 0;
             $info = [];
             $questions = [];
             $data = json_decode($data, 1);

+ 127 - 0
app/Console/Commands/robot.php

@@ -0,0 +1,127 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Model\Question;
+use App\Model\Option;
+use GuzzleHttp\Client;
+use GuzzleHttp\Cookie\CookieJar;
+use Workerman\Worker;
+use Workerman\Connection\AsyncTcpConnection;
+use Illuminate\Support\Facades\Redis;
+
+class robot extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'game:robot {username?}';
+
+    /**
+     * 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()
+    {
+        $username = $this->argument('username');
+        if(!$username){
+            $users = array("晚桐","书蝶","北俞","时宛","花染");
+            $rand_user = rand(0,4);
+            $username = $users[$rand_user];
+        }
+        $worker = new Worker();
+        $worker->onWorkerStart = function($worker) use($username){
+            //定义请求客户端
+            $client = new Client([
+                'cookies' => true,
+                'verify' => false,
+            ]); 
+            $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Login',[
+                'form_params' => [
+                    'username' => $username,
+                ]
+            ]);
+            $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->onConnect = function($con) use($client) {
+            };
+            $con->onMessage = function($con, $data) use($client){
+                $json_data = json_decode($data);
+                //初始化
+                if($json_data->type == 'init'){
+                    $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Bind',[
+                        'form_params' => [
+                            'client_id' => $json_data->client_id,
+                        ]
+                    ]);
+
+                    //测试使用直接加入匹配
+                    // $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
+                    //     'form_params' => [
+                    //         'client_id' => $json_data->client_id,
+                    //     ]
+                    // ]);
+
+                    //记录当前在线机器人
+                    Redis::lpush('robot_list', $json_data->client_id);
+                }
+                //唤起匹配
+                if($json_data->type == 'gotomatch'){
+                    $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
+                        'form_params' => [
+                            'client_id' => $json_data->client_id,
+                        ]
+                    ]);
+                }
+                //获得题目
+                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);
+                    $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[
+                        'form_params' => [
+                            'question_id' => $question_id,
+                            'option_id' => $options[$answer],
+                        ]
+                    ]);
+                }
+                //获得题目
+                if($json_data->type == 'answer'){
+                    if($json_data->is_end == 1){
+                        //重新加入机器人队列
+                        Redis::lpush('robot_list', $json_data->client_id);
+                    }
+                }
+            };
+            $con->connect();
+        };
+
+        Worker::runAll();
+    }
+}

+ 33 - 3
app/Home/Controllers/IndexController.php

@@ -152,14 +152,44 @@ class IndexController extends Controller
      */
     public function robot()
     {
+
         $client = new Client([
-            'base_uri' => 'http://183.234.61.252:8090/Home/User/Login',
+            'base_uri' => 'http://183.234.61.252:8090',
             'timeout'  => 2.0,
             'cookies' => true,
             'verify' => false,
         ]); 
-        $client->request('POST');
-        dump($client);
+
+        $worker = new Worker('websocket://183.234.61.252:8282');
+        
+        $worker->onMessage = function($ws_connection, $message)
+        {
+            //$client->request('POST');
+            // 与远程task服务建立异步连接,ip为远程task服务的ip,如果是本机就是127.0.0.1,如果是集群就是lvs的ip
+            $task_connection = new AsyncTcpConnection('Text://127.0.0.1:12345');
+            // 任务及参数数据
+            $task_data = array(
+                'function' => 'send_mail',
+                'args'       => array('from'=>'xxx', 'to'=>'xxx', 'contents'=>'xxx'),
+            );
+            // 发送数据
+            $task_connection->send(json_encode($task_data));
+            // 异步获得结果
+            $task_connection->onMessage = function($task_connection, $task_result)use($ws_connection)
+            {
+                 // 结果
+                 var_dump($task_result);
+                 // 获得结果后记得关闭异步连接
+                 $task_connection->close();
+                 // 通知对应的websocket客户端任务完成
+                 $ws_connection->send('task complete');
+            };
+            // 执行异步连接
+            $task_connection->connect();
+        }
+
+        Worker::runAll();
+
     }
 
 }