robot.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. //获取机器人信息
  46. $user_id = $this->argument('user_id');
  47. if(!$user_id){
  48. $user = User::inRandomOrder()->select('user_id','name')->where("is_robot", 1)->where("is_login", 0)->first();
  49. }else{
  50. // $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->where("is_login",0)->first();
  51. $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->first();
  52. }
  53. if(!$user){
  54. echo "未找到机器人";
  55. die;
  56. }
  57. $worker = new Worker();
  58. $worker->onWorkerStart = function($worker) use($user){
  59. //定义基本信息
  60. $http_addr = 'http://183.234.61.252:8090/';
  61. // $http_addr = 'http://www.dt.com/';
  62. //定义请求客户端
  63. $room_id = ''; //定义当前机器人加入的room_id
  64. $client_id = ''; //定义当前机器人client_id
  65. $client = new Client([
  66. 'cookies' => true,
  67. 'verify' => false,
  68. ]);
  69. $response = $client->request('GET', $http_addr . 'Home/User/Info', [
  70. 'query' => [
  71. 'user_id' => $user->user_id,
  72. 'mt' => 1,
  73. ]
  74. ]);
  75. // $robot = $response->getBody()->getContents();
  76. $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  77. // $con = new AsyncTcpConnection('ws://127.0.0.1:8282');
  78. $con->onConnect = function($con) use($client, $worker) {
  79. };
  80. $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr, $worker){
  81. //注册地址
  82. Gateway::$registerAddress = '127.0.0.1:1238';
  83. //格式化参数
  84. $json_data = json_decode($data);
  85. //初始化
  86. if($json_data->type == 'init'){
  87. $client_id = $json_data->client_id;
  88. $response = $client->request('POST', $http_addr . 'Home/User/Bind', [
  89. 'form_params' => [
  90. 'client_id' => $client_id,
  91. ]
  92. ]);
  93. if($response->getStatusCode() != 200){
  94. echo "机器人绑定 socket失败, 结束进程\n";
  95. //结束进程
  96. $sig = SIGINT;
  97. $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
  98. $master_pid && posix_kill($master_pid, $sig);
  99. return 0;
  100. }
  101. $update_data = [
  102. "is_login" => 1,
  103. "client_id" => $client_id,
  104. ];
  105. //更新机器人登陆状态
  106. User::where("user_id", $user->user_id)->update($update_data);
  107. $result = json_decode($response->getBody()->getContents());
  108. if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){
  109. //结束进程
  110. $sig = SIGINT;
  111. $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
  112. $master_pid && posix_kill($master_pid, $sig);
  113. return 0;
  114. }
  115. echo "client: {$client_id} 成功绑定socket\n";
  116. //启动匹配
  117. $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
  118. 'form_params' => [
  119. 'client_id' => $client_id,
  120. ]
  121. ]);
  122. echo "{$client_id} 成功加入匹配\n";
  123. }
  124. //唤起匹配
  125. if($json_data->type == 'gotomatch'){
  126. $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
  127. 'form_params' => [
  128. 'client_id' => $client_id,
  129. ]
  130. ]);
  131. var_dump($response->getBody()->getContents()) . "\n";
  132. echo "{$client_id} 成功加入匹配\n";
  133. }
  134. //匹配成功
  135. if($json_data->type == 'match'){
  136. $room_id = $json_data->info->room_id;
  137. echo "{$client_id} 成功加入 {$room_id} \n";
  138. }
  139. //获得题目
  140. if($json_data->type == 'question'){
  141. $question_id = $json_data->info->question->question_id;
  142. $options = $json_data->info->options;
  143. $answer = rand(0, count($json_data->info->options) - 1);
  144. //随机几秒回答问题
  145. $second = rand(0,6);
  146. sleep($second);
  147. echo "获得题目 {$json_data->info->question->question_id} : {$json_data->info->question->title}\n";
  148. $response = $client->request('POST', $http_addr . 'Home/Game/Answer',[
  149. 'form_params' => [
  150. 'question_id' => $question_id,
  151. 'option_id' => $options[$answer]->option_id,
  152. ]
  153. ]);
  154. }
  155. //结算
  156. if($json_data->type == 'round_end'){
  157. echo "回合结束,已结算";
  158. }
  159. //结算
  160. if($json_data->type == 'game_end'){
  161. $update_data = [
  162. "is_login" => 0,
  163. ];
  164. //更新机器人登陆状态
  165. User::where("user_id", $user->user_id)->update($update_data);
  166. echo "已结算,机器人离开房间";
  167. //结束进程
  168. $sig = SIGINT;
  169. $master_pid = is_file($worker::$pidFile) ? file_get_contents($worker::$pidFile) : 0;
  170. $master_pid && posix_kill($master_pid, $sig);
  171. Log::info("用户ID: {$user->user_id} \n 房间ID: {$room_id}\n 删除不了进程 {$master_pid}\n");
  172. }
  173. };
  174. $con->connect();
  175. };
  176. Worker::runAll();
  177. }
  178. }