robot.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. class robot extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'game:robot {user_id?}';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = '对战机器人';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. //获取机器人信息
  45. $user_id = $this->argument('user_id');
  46. if(!$user_id){
  47. $user = User::inRandomOrder()->select('user_id','name')->where("is_robot", 1)->where("is_login", 0)->first();
  48. }else{
  49. // $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->where("is_login",0)->first();
  50. $user = User::select('user_id','name')->where("is_robot",1)->where("user_id", $user_id)->first();
  51. }
  52. if(!$user){
  53. echo "未找到机器人";
  54. die;
  55. }
  56. $worker = new Worker();
  57. $worker->onWorkerStart = function($worker) use($user){
  58. //定义基本信息
  59. $http_addr = 'http://183.234.61.252:8090/';
  60. // $http_addr = 'http://www.dt.com/';
  61. //定义请求客户端
  62. $room_id = ''; //定义当前机器人加入的room_id
  63. $client_id = ''; //定义当前机器人client_id
  64. $client = new Client([
  65. 'cookies' => true,
  66. 'verify' => false,
  67. ]);
  68. $response = $client->request('GET', $http_addr . 'Home/User/Info', [
  69. 'query' => [
  70. 'user_id' => $user->user_id,
  71. 'mt' => 1,
  72. ]
  73. ]);
  74. // $robot = $response->getBody()->getContents();
  75. $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  76. // $con = new AsyncTcpConnection('ws://127.0.0.1:8282');
  77. $con->onConnect = function($con) use($client) {
  78. };
  79. $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr){
  80. //注册地址
  81. Gateway::$registerAddress = '127.0.0.1:1238';
  82. //格式化参数
  83. $json_data = json_decode($data);
  84. //初始化
  85. if($json_data->type == 'init'){
  86. $client_id = $json_data->client_id;
  87. $response = $client->request('POST', $http_addr . 'Home/User/Bind', [
  88. 'form_params' => [
  89. 'client_id' => $client_id,
  90. ]
  91. ]);
  92. $result = json_decode($response->getBody()->getContents());
  93. if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){
  94. return 0;
  95. }
  96. echo "client: {$client_id} 成功绑定socket\n";
  97. $update_data = [
  98. "is_login" => 1,
  99. "client_id" => $client_id,
  100. ];
  101. //更新机器人登陆状态
  102. User::where("user_id", $user->user_id)->update($update_data);
  103. //启动匹配
  104. $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
  105. 'form_params' => [
  106. 'client_id' => $client_id,
  107. ]
  108. ]);
  109. echo "{$client_id} 成功加入匹配\n";
  110. }
  111. //唤起匹配
  112. if($json_data->type == 'gotomatch'){
  113. $response = $client->request('POST', $http_addr . 'Home/Game/Join',[
  114. 'form_params' => [
  115. 'client_id' => $client_id,
  116. ]
  117. ]);
  118. echo "{$client_id} 成功加入匹配\n";
  119. }
  120. //匹配成功
  121. if($json_data->type == 'match'){
  122. $room_id = $json_data->info->room_id;
  123. echo "{$client_id} 成功加入 {$room_id} \n";
  124. }
  125. //获得题目
  126. if($json_data->type == 'question'){
  127. $question_id = $json_data->info->question->question_id;
  128. $options = $json_data->info->options;
  129. $answer = rand(0, count($json_data->info->options) - 1);
  130. //随机几秒回答问题
  131. $second = rand(0,6);
  132. sleep($second);
  133. echo "获得题目 {$json_data->info->question->question_id} : {$json_data->info->question->title}\n";
  134. $response = $client->request('POST', $http_addr . 'Home/Game/Answer',[
  135. 'form_params' => [
  136. 'question_id' => $question_id,
  137. 'option_id' => $options[$answer]->option_id,
  138. ]
  139. ]);
  140. }
  141. //结算
  142. if($json_data->type == 'round_end'){
  143. echo "回合结束,已结算";
  144. }
  145. //结算
  146. if($json_data->type == 'game_end'){
  147. $update_data = [
  148. "is_login" => 0,
  149. "client_id" => '',
  150. ];
  151. //更新机器人登陆状态
  152. User::where("user_id", $user->user_id)->update($update_data);
  153. unset($update_data);
  154. $update_data['state'] = 2;
  155. RoomUser::where("user_id", $user->user_id)->where("state", 1)->update($update_data);
  156. echo "已结算,关闭机器人";
  157. Gateway::leaveGroup($client_id, $room_id);
  158. Gateway::unbindUid($client_id, $user->user_id);
  159. Gateway::closeClient($client_id);
  160. Worker::stopAll();
  161. }
  162. };
  163. $con->connect();
  164. };
  165. Worker::runAll();
  166. }
  167. }