robot.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Model\Question;
  5. use App\Model\Option;
  6. use App\Model\User;
  7. use App\Model\RoomUser;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Cookie\CookieJar;
  10. use Workerman\Worker;
  11. use Workerman\Connection\AsyncTcpConnection;
  12. use Illuminate\Support\Facades\Redis;
  13. use GatewayWorker\Lib\Gateway;
  14. use Illuminate\Support\Facades\Log;
  15. class robot extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'game:robot {user_id?}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '对战机器人';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return mixed
  42. */
  43. public function handle()
  44. {
  45. global $argv;
  46. //获取机器人信息
  47. $user_id = $this->argument('user_id');
  48. // $argv[0] = '';
  49. // $argv[1] = "start";
  50. // $argv[2] = '';
  51. if(!$user_id){
  52. $user = User::inRandomOrder()->select('user_id','name')->where("is_robot", 1)->where("is_login", 0)->first();
  53. }else{
  54. // $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->where("is_login",0)->first();
  55. $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->first();
  56. }
  57. if(!$user){
  58. echo "未找到机器人";
  59. die;
  60. }
  61. //定义请求客户端
  62. $room_id = ''; //定义当前机器人加入的room_id
  63. $client_id = ''; //定义当前机器人client_id
  64. $worker = new Worker();
  65. $worker->onWorkerStart = function($worker) use($user, $room_id, $client_id){
  66. //定义基本信息
  67. $http_addr = 'http://183.234.61.252:8090/';
  68. // $http_addr = 'http://www.dt.com/';
  69. $client = new Client([
  70. 'cookies' => true,
  71. 'verify' => false,
  72. ]);
  73. $response = $client->request('GET', $http_addr . 'Home/User/Info', [
  74. 'query' => [
  75. 'user_id' => $user->user_id,
  76. 'mt' => 1,
  77. 'is_robot' => 1,
  78. ]
  79. ]);
  80. // $robot = $response->getBody()->getContents();
  81. $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  82. // $con = new AsyncTcpConnection('ws://127.0.0.1:8282');
  83. $con->onConnect = function($con) use($client, $worker) {
  84. };
  85. $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr, $worker){
  86. //注册地址
  87. Gateway::$registerAddress = '127.0.0.1:1238';
  88. //格式化参数
  89. $json_data = json_decode($data);
  90. //初始化
  91. if($json_data->type == 'init'){
  92. $client_id = $json_data->client_id;
  93. $response = $client->request('POST', $http_addr . 'Home/User/Bind', [
  94. 'form_params' => [
  95. 'client_id' => $client_id,
  96. ]
  97. ]);
  98. if($response->getStatusCode() != 200){
  99. echo "机器人绑定 socket失败, 结束进程\n";
  100. //结束进程
  101. $sig = 9;
  102. $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
  103. $master_pid && posix_kill($master_pid, $sig);
  104. Log::info("用户ID: {$user->user_id} \n 绑定Socket失败退出进程\n");
  105. return 0;
  106. }
  107. $update_data = [
  108. "is_login" => 1,
  109. "client_id" => $client_id,
  110. ];
  111. //更新机器人登陆状态
  112. User::where("user_id", $user->user_id)->update($update_data);
  113. $result = json_decode($response->getBody()->getContents());
  114. if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){
  115. //结束进程
  116. $sig = 9;
  117. $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
  118. $master_pid && posix_kill($master_pid, $sig);
  119. Log::info("用户ID: {$user->user_id} \n 正在游戏或者登录失败退出进程\n");
  120. return 0;
  121. }
  122. echo "client: {$client_id} 成功绑定socket\n";
  123. //启动匹配
  124. $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
  125. 'form_params' => [
  126. 'client_id' => $client_id,
  127. ]
  128. ]);
  129. echo "{$client_id} 成功加入匹配\n";
  130. }
  131. //唤起匹配
  132. if($json_data->type == 'gotomatch'){
  133. $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
  134. 'form_params' => [
  135. 'client_id' => $client_id,
  136. ]
  137. ]);
  138. var_dump($response->getBody()->getContents()) . "\n";
  139. echo "{$client_id} 成功加入匹配\n";
  140. }
  141. //匹配成功
  142. if($json_data->type == 'match'){
  143. $room_id = $json_data->info->room_id;
  144. echo "{$client_id} 成功加入 {$room_id} \n";
  145. }
  146. //获得题目
  147. if($json_data->type == 'question'){
  148. $question_id = $json_data->info->question->question_id;
  149. $options = $json_data->info->options;
  150. $answer = rand(0, count($json_data->info->options) - 1);
  151. //随机几秒回答问题
  152. $second = rand(0,6);
  153. sleep($second);
  154. echo "获得题目 {$json_data->info->question->question_id} : {$json_data->info->question->title}\n";
  155. $response = $client->request('POST', $http_addr . 'Home/Game/Answer',[
  156. 'form_params' => [
  157. 'question_id' => $question_id,
  158. 'option_id' => $options[$answer]->option_id,
  159. ]
  160. ]);
  161. }
  162. //结算
  163. if($json_data->type == 'round_end'){
  164. echo "回合结束,已结算";
  165. }
  166. //结算
  167. if($json_data->type == 'game_end'){
  168. $update_data['is_login'] = 0;
  169. //更新机器人登陆状态
  170. User::where("user_id", $user->user_id)->update($update_data);
  171. echo "已结算,机器人离开房间";
  172. //结束进程
  173. $sig = 9;
  174. $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
  175. $master_pid && posix_kill($master_pid, $sig);
  176. $master_is_alive = $master_pid && posix_kill($master_pid, 0);
  177. if ($master_is_alive) {
  178. Log::info("用户ID: {$user->user_id} \n 房间ID: {$room_id}\n 删除不了进程 {$master_pid}\n");
  179. }
  180. }
  181. };
  182. $con->connect();
  183. };
  184. Worker::runAll();
  185. }
  186. }