robot.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Model\Question;
  5. use App\Model\Option;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Cookie\CookieJar;
  8. use Workerman\Worker;
  9. use Workerman\Connection\AsyncTcpConnection;
  10. use Illuminate\Support\Facades\Redis;
  11. use GatewayWorker\Lib\Gateway;
  12. class robot extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'game:robot {username?}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '对战机器人';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. $username = $this->argument('username');
  43. if(!$username){
  44. $users = array("晚桐","书蝶","北俞","时宛","花染");
  45. $rand_user = rand(0,4);
  46. $username = $users[$rand_user];
  47. }
  48. $worker = new Worker();
  49. $worker->onWorkerStart = function($worker) use($username){
  50. //定义请求客户端
  51. $room_id = ''; //定义当前机器人加入的room_id
  52. $client_id = ''; //定义当前机器人client_id
  53. $client = new Client([
  54. 'cookies' => true,
  55. 'verify' => false,
  56. ]);
  57. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Login',[
  58. 'form_params' => [
  59. 'username' => $username,
  60. ]
  61. ]);
  62. $response = $client->request('GET', 'http://183.234.61.252:8090/Home/User/Info');
  63. $robot = $response->getBody()->getContents();
  64. $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  65. $con->onConnect = function($con) use($client) {
  66. };
  67. $con->onMessage = function($con, $data) use($client, &$room_id, &$client_id){
  68. //注册地址
  69. Gateway::$registerAddress = '127.0.0.1:1238';
  70. //格式化参数
  71. $json_data = json_decode($data);
  72. //初始化
  73. if($json_data->type == 'init'){
  74. $client_id = $json_data->client_id;
  75. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Bind',[
  76. 'form_params' => [
  77. 'client_id' => $client_id,
  78. ]
  79. ]);
  80. echo "client: {$client_id} 成功绑定socket\n";
  81. //记录当前在线机器人
  82. Redis::sadd('robot_list', $client_id);
  83. Redis::lpush('online_robot_list', $client_id);
  84. }
  85. //唤起匹配
  86. if($json_data->type == 'gotomatch'){
  87. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  88. 'form_params' => [
  89. 'client_id' => $client_id,
  90. ]
  91. ]);
  92. echo "{$client_id} 成功加入匹配\n";
  93. }
  94. //匹配成功
  95. if($json_data->type == 'match'){
  96. $room_id = $json_data->info->room_id;
  97. echo "{$client_id} 成功加入 {$room_id} \n";
  98. }
  99. //获得题目
  100. if($json_data->type == 'question'){
  101. $question_id = $json_data->info->question->question_id;
  102. $options = $json_data->info->options;
  103. $answer = rand(0, count($json_data->info->options) - 1);
  104. //随机几秒回答问题
  105. $second = rand(0,6);
  106. sleep($second);
  107. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[
  108. 'form_params' => [
  109. 'question_id' => $question_id,
  110. 'option_id' => $options[$answer],
  111. ]
  112. ]);
  113. }
  114. //获得题目
  115. if($json_data->type == 'answer'){
  116. if($json_data->is_end == 1){
  117. $room_id = '';
  118. echo "{$client_id} 回答问题成功重回队列\n";
  119. //重新加入机器人队列
  120. Redis::sadd('robot_list', $client_id);
  121. }
  122. }
  123. //获得题目
  124. if($json_data->type == 'checkrobot'){
  125. if($room_id && $client_id){
  126. $clients = Gateway::getClientIdListByGroup($room_id);
  127. if(count($clients) > 0 && count($clients) < 2){
  128. Gateway::leaveGroup($client_id, $room_id);
  129. //重新加入机器人队列
  130. Redis::sadd('robot_list', $client_id);
  131. $room_id = '';
  132. echo "{$client_id} 成功重回队列\n";
  133. }
  134. }
  135. }
  136. };
  137. $con->connect();
  138. };
  139. Worker::runAll();
  140. }
  141. }