robot.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  51. if(!$user){
  52. echo "未找到机器人";
  53. die;
  54. }
  55. $worker = new Worker();
  56. $worker->onWorkerStart = function($worker) use($user){
  57. //定义基本信息
  58. $http_addr = 'http://183.234.61.252:8090/';
  59. $http_addr = 'http://www.dt.com/';
  60. //定义请求客户端
  61. $room_id = ''; //定义当前机器人加入的room_id
  62. $client_id = ''; //定义当前机器人client_id
  63. $client = new Client([
  64. 'cookies' => true,
  65. 'verify' => false,
  66. ]);
  67. $response = $client->request('GET', $http_addr . 'Home/User/Info', [
  68. 'query' => [
  69. 'user_id' => $user->user_id,
  70. 'mt' => 1,
  71. ]
  72. ]);
  73. // $robot = $response->getBody()->getContents();
  74. // $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  75. $con = new AsyncTcpConnection('ws://127.0.0.1:8282');
  76. $con->onConnect = function($con) use($client) {
  77. };
  78. $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user, $http_addr){
  79. //注册地址
  80. Gateway::$registerAddress = '127.0.0.1:1238';
  81. //格式化参数
  82. var_dump($data);
  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. echo $result->msg;
  94. if($result->code == 400 || ($result->code == 0 && $result->info->in_game == 1)){
  95. return 0;
  96. }
  97. echo "client: {$client_id} 成功绑定socket\n";
  98. $update_data = [
  99. "is_login" => 1,
  100. "client_id" => $client_id,
  101. ];
  102. //更新机器人登陆状态
  103. User::where("user_id", $user->user_id)->update($update_data);
  104. //启动匹配
  105. $response = $client->request('GET', $http_addr . 'Home/Game/Join',[
  106. 'form_params' => [
  107. 'client_id' => $client_id,
  108. ]
  109. ]);
  110. echo "{$client_id} 成功加入匹配\n";
  111. }
  112. //唤起匹配
  113. if($json_data->type == 'gotomatch'){
  114. $response = $client->request('POST', $http_addr . 'Home/Game/Join',[
  115. 'form_params' => [
  116. 'client_id' => $client_id,
  117. ]
  118. ]);
  119. echo "{$client_id} 成功加入匹配\n";
  120. }
  121. //匹配成功
  122. if($json_data->type == 'match'){
  123. $room_id = $json_data->info->room_id;
  124. echo "{$client_id} 成功加入 {$room_id} \n";
  125. }
  126. //获得题目
  127. if($json_data->type == 'question'){
  128. $question_id = $json_data->info->question->question_id;
  129. $options = $json_data->info->options;
  130. $answer = rand(0, count($json_data->info->options) - 1);
  131. //随机几秒回答问题
  132. $second = rand(0,6);
  133. sleep($second);
  134. echo "获得题目 {$json_data->info->question->question_id} : {$json_data->info->question->title}\n";
  135. $response = $client->request('POST', $http_addr . 'Home/Game/Answer',[
  136. 'form_params' => [
  137. 'question_id' => $question_id,
  138. 'option_id' => $options[$answer]->option_id,
  139. ]
  140. ]);
  141. }
  142. //结算
  143. if($json_data->type == 'game_end'){
  144. $update_data = [
  145. "is_login" => 0,
  146. "client_id" => '',
  147. ];
  148. //更新机器人登陆状态
  149. User::where("user_id", $user->user_id)->update($update_data);
  150. unset($update_data);
  151. $update_data['state'] = 2;
  152. RoomUser::where("user_id", $user->user_id)->where("state", 1)->update($update_data);
  153. echo "已结算,关闭机器人";
  154. die;
  155. }
  156. };
  157. $con->connect();
  158. };
  159. Worker::runAll();
  160. }
  161. }