123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?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;
- use GatewayWorker\Lib\Gateway;
- 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){
- //定义请求客户端
- $room_id = ''; //定义当前机器人加入的room_id
- $client_id = ''; //定义当前机器人client_id
- $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, &$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' => $client_id,
- ]
- ]);
- echo "client: {$client_id} 成功绑定socket\n";
- //记录当前在线机器人
- Redis::sadd('robot_list', $client_id);
- Redis::lpush('online_robot_list', $client_id);
- }
- //唤起匹配
- if($json_data->type == 'gotomatch'){
- $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
- 'form_params' => [
- 'client_id' => $client_id,
- ]
- ]);
- 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);
- $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){
- $room_id = '';
- echo "{$client_id} 回答问题成功重回队列\n";
- //重新加入机器人队列
- Redis::sadd('robot_list', $client_id);
- }
- }
- //获得题目
- if($json_data->type == 'checkrobot'){
- if($room_id && $client_id){
- $clients = Gateway::getClientIdListByGroup($room_id);
- if(count($clients) > 0 && count($clients) < 2){
- Gateway::leaveGroup($client_id, $room_id);
- //重新加入机器人队列
- Redis::sadd('robot_list', $client_id);
- $room_id = '';
- echo "{$client_id} 成功重回队列\n";
- }
- }
- }
- };
- $con->connect();
- };
- Worker::runAll();
- }
- }
|