robot.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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("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. $room_id = ''; //定义当前机器人加入的room_id
  59. $client_id = ''; //定义当前机器人client_id
  60. $client = new Client([
  61. 'cookies' => true,
  62. 'verify' => false,
  63. ]);
  64. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Login',[
  65. 'form_params' => [
  66. 'username' => $user->name,
  67. ]
  68. ]);
  69. $response = $client->request('GET', 'http://183.234.61.252:8090/Home/User/Info');
  70. // $robot = $response->getBody()->getContents();
  71. $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  72. $con->onConnect = function($con) use($client) {
  73. };
  74. $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id, $user){
  75. //注册地址
  76. Gateway::$registerAddress = '127.0.0.1:1238';
  77. //格式化参数
  78. $json_data = json_decode($data);
  79. //初始化
  80. if($json_data->type == 'init'){
  81. $client_id = $json_data->client_id;
  82. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Bind',[
  83. 'form_params' => [
  84. 'client_id' => $client_id,
  85. ]
  86. ]);
  87. echo "client: {$client_id} 成功绑定socket\n";
  88. $update_data = [
  89. "is_login" => 1,
  90. "client_id" => $client_id,
  91. ];
  92. //更新机器人登陆状态
  93. User::where("user_id", $user->user_id)->update($update_data);
  94. //记录当前在线机器人
  95. // Redis::sadd('robot_list', $client_id);
  96. // Redis::lpush('online_robot_list', $client_id);
  97. //启动匹配
  98. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  99. 'form_params' => [
  100. 'client_id' => $client_id,
  101. ]
  102. ]);
  103. echo "{$client_id} 成功加入匹配\n";
  104. }
  105. //唤起匹配
  106. if($json_data->type == 'gotomatch'){
  107. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  108. 'form_params' => [
  109. 'client_id' => $client_id,
  110. ]
  111. ]);
  112. echo "{$client_id} 成功加入匹配\n";
  113. }
  114. //匹配成功
  115. if($json_data->type == 'match'){
  116. $room_id = $json_data->info->room_id;
  117. echo "{$client_id} 成功加入 {$room_id} \n";
  118. }
  119. //获得题目
  120. if($json_data->type == 'question'){
  121. $question_id = $json_data->info->question->question_id;
  122. $options = $json_data->info->options;
  123. $answer = rand(0, count($json_data->info->options) - 1);
  124. //随机几秒回答问题
  125. $second = rand(0,6);
  126. sleep($second);
  127. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[
  128. 'form_params' => [
  129. 'question_id' => $question_id,
  130. 'option_id' => $options[$answer],
  131. ]
  132. ]);
  133. }
  134. //结算
  135. if($json_data->type == 'settlement'){
  136. $update_data = [
  137. "is_login" => 0,
  138. "client_id" => '',
  139. ];
  140. //更新机器人登陆状态
  141. User::where("user_id", $user->user_id)->update($update_data);
  142. unset($update_data);
  143. $update_data['state'] = 2;
  144. User::where("user_id", $user->user_id)->where("state", 1)->update($update_data);
  145. echo "已结算,关闭机器人";
  146. die;
  147. }
  148. };
  149. $con->connect();
  150. };
  151. Worker::runAll();
  152. }
  153. }