robot.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. echo "成功绑定socket\n";
  74. //测试使用直接加入匹配
  75. // $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  76. // 'form_params' => [
  77. // 'client_id' => $json_data->client_id,
  78. // ]
  79. // ]);
  80. //记录当前在线机器人
  81. Redis::lpush('robot_list', $json_data->client_id);
  82. }
  83. //唤起匹配
  84. if($json_data->type == 'gotomatch'){
  85. var_dump($json_data);
  86. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[
  87. 'form_params' => [
  88. 'client_id' => $json_data->client_id,
  89. ]
  90. ]);
  91. echo "成功加入匹配\n";
  92. }
  93. //获得题目
  94. if($json_data->type == 'question'){
  95. $question_id = $json_data->info->question->question_id;
  96. $options = $json_data->info->options;
  97. $answer = rand(0, count($json_data->info->options) - 1);
  98. //随机几秒回答问题
  99. $second = rand(0,6);
  100. sleep($second);
  101. $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[
  102. 'form_params' => [
  103. 'question_id' => $question_id,
  104. 'option_id' => $options[$answer],
  105. ]
  106. ]);
  107. }
  108. //获得题目
  109. if($json_data->type == 'answer'){
  110. if($json_data->is_end == 1){
  111. echo "成功重回队列\n";
  112. //重新加入机器人队列
  113. Redis::lpush('robot_list', $json_data->client_id);
  114. }
  115. }
  116. };
  117. $con->connect();
  118. };
  119. Worker::runAll();
  120. }
  121. }