robot.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class robot extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'game:robot {username?}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '对战机器人';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $username = $this->argument('username');
  42. if(!$username){
  43. $users = array("晚桐","书蝶","北俞","时宛","花染");
  44. $rand_user = rand(0,4);
  45. $username = $users[$rand_user];
  46. }
  47. $worker = new Worker();
  48. $worker->onWorkerStart = function($worker) use($username){
  49. //定义请求客户端
  50. $client = new Client([
  51. 'cookies' => true,
  52. 'verify' => false,
  53. ]);
  54. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Login',[
  55. 'form_params' => [
  56. 'username' => $username,
  57. ]
  58. ]);
  59. $response = $client->request('GET', 'http://183.234.61.252:8090/Home/User/Info');
  60. $robot = $response->getBody()->getContents();
  61. $con = new AsyncTcpConnection('ws://183.234.61.252:8282');
  62. $con->onConnect = function($con) use($client) {
  63. };
  64. $con->onMessage = function($con, $data) use($client){
  65. $json_data = json_decode($data);
  66. //初始化
  67. if($json_data->type == 'init'){
  68. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Bind',[
  69. 'form_params' => [
  70. 'client_id' => $json_data->client_id,
  71. ]
  72. ]);
  73. //测试使用直接加入匹配
  74. // $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  75. // 'form_params' => [
  76. // 'client_id' => $json_data->client_id,
  77. // ]
  78. // ]);
  79. //记录当前在线机器人
  80. Redis::lpush('robot_list', $json_data->client_id);
  81. }
  82. //唤起匹配
  83. if($json_data->type == 'gotomatch'){
  84. var_dump($json_data);
  85. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  86. 'form_params' => [
  87. 'client_id' => $json_data->client_id,
  88. ]
  89. ]);
  90. }
  91. //获得题目
  92. if($json_data->type == 'question'){
  93. $question_id = $json_data->info->question->question_id;
  94. $options = $json_data->info->options;
  95. $answer = rand(0, count($json_data->info->options) - 1);
  96. //随机几秒回答问题
  97. $second = rand(0,6);
  98. sleep($second);
  99. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[
  100. 'form_params' => [
  101. 'question_id' => $question_id,
  102. 'option_id' => $options[$answer],
  103. ]
  104. ]);
  105. }
  106. //获得题目
  107. if($json_data->type == 'answer'){
  108. if($json_data->is_end == 1){
  109. //重新加入机器人队列
  110. Redis::lpush('robot_list', $json_data->client_id);
  111. }
  112. }
  113. };
  114. $con->connect();
  115. };
  116. Worker::runAll();
  117. }
  118. }