| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | <?phpnamespace App\Console\Commands;use Illuminate\Console\Command;use App\Model\Question;use App\Model\Option;use App\Model\User;use App\Model\RoomUser;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;use Workerman\Worker;use Workerman\Connection\AsyncTcpConnection;use Illuminate\Support\Facades\Redis;use GatewayWorker\Lib\Gateway;use Illuminate\Support\Facades\Log;class robot extends Command{    /**     * The name and signature of the console command.     *     * @var string     */    protected $signature = 'game:robot {user_id?}';    /**     * 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()    {        //获取机器人信息        $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();        }else{            // $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->where("is_login",0)->first();            $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->first();        }        if(!$user){            echo "未找到机器人";            die;        }        //定义请求客户端        $room_id = ''; //定义当前机器人加入的room_id        $client_id = ''; //定义当前机器人client_id        $worker = new Worker();        $worker->onWorkerStart = function($worker) use($user, $room_id, $client_id){            //定义基本信息            $http_addr = 'http://183.234.61.252:8090/';            // $http_addr = 'http://www.dt.com/';            $client = new Client([                'cookies' => true,                'verify' => false,            ]);            $response = $client->request('GET', $http_addr . 'Home/User/Info', [                'query' => [                    'user_id' => $user->user_id,                    'mt' => 1,                ]            ]);            // $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, $worker) {            };            $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr, $worker){                //注册地址                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_addr . 'Home/User/Bind', [                        'form_params' => [                            'client_id' => $client_id,                        ]                    ]);                    if($response->getStatusCode() != 200){                        echo "机器人绑定 socket失败, 结束进程\n";                        //结束进程                        $sig = SIGINT;                        $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;                        $master_pid && posix_kill($master_pid, $sig);                        return 0;                    }                    $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)){                        //结束进程                        $sig = SIGINT;                        $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";                    //启动匹配                    $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('GET', $http_addr . 'Home/Game/Join',[                        'form_params' => [                            'client_id' => $client_id,                        ]                    ]);                    var_dump($response->getBody()->getContents()) . "\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'){                    $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);                    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,                        ]                    ]);                }                //结算                if($json_data->type == 'round_end'){                    echo "回合结束,已结算";                }                //结算                if($json_data->type == 'game_end'){                    $update_data = [                        "is_login" => 0,                    ];                    //更新机器人登陆状态                    User::where("user_id", $user->user_id)->update($update_data);                    echo "已结算,机器人离开房间";                    //结束进程                    $sig = SIGINT;                    $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;                    $master_pid && posix_kill($master_pid, $sig);                    $master_is_alive = $master_pid && posix_kill($master_pid, 0);                    if ($master_is_alive) {                        Log::info("用户ID: {$user->user_id} \n 房间ID: {$room_id}\n 删除不了进程 {$master_pid}\n");                    }                }            };            $con->connect();        };        Worker::runAll();    }}
 |