gamematch.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GatewayWorker\Lib\Gateway;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Model\Question;
  7. use App\Model\Option;
  8. class gamematch extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'game:match';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '启动游戏匹配';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. //注册地址
  39. Gateway::$registerAddress = '127.0.0.1:1238';
  40. $match_times = 0;
  41. //循环匹配
  42. while (1) {
  43. $data = Redis::lpop('match_list');
  44. if(!$data){
  45. echo "未找到匹配\n";
  46. if($match_times > 3){
  47. $robot = Redis::lpop('robot_list');
  48. $message = [
  49. "type" => 'gotomatch',
  50. ];
  51. Gateway::sendToClient($robot, $message);
  52. echo "启动机器人: {$robot}\n";
  53. }
  54. sleep(1);
  55. $match_times++;
  56. continue;
  57. }
  58. $match_times = 0;
  59. $info = [];
  60. $questions = [];
  61. $data = json_decode($data, 1);
  62. $room = Redis::get($data['level']);
  63. if($room){
  64. Redis::del($data['level']);
  65. $room = json_decode($room, 1);
  66. //获取玩家ClientID
  67. if($room['client_id'] == $data['client_id'] || $room['user_id'] == $data['user_id']){
  68. continue;
  69. }
  70. // $player_1_ = Gateway::getClientIdByUid($room['uid']);
  71. Gateway::joinGroup($room['client_id'], $room['room_id']);
  72. // $player_2 = Gateway::getClientIdByUid($data['uid']);
  73. Gateway::joinGroup($data['client_id'], $room['room_id']);
  74. //组合玩家信息
  75. $players = [
  76. $data,
  77. $room
  78. ];
  79. //发送可以开始消息
  80. $message = [
  81. "type" => 'match',
  82. "msg" => "匹配成功",
  83. "info" => ['room_id'=>$room['room_id'],"players"=>$players],
  84. ];
  85. //清理房间数据
  86. Redis::del($room['room_id'] . '_info');
  87. Redis::del($room['room_id'] . '_questions');
  88. Gateway::sendToGroup($room['room_id'], json_encode($message));
  89. echo "清理数据 {$room['room_id']}\n";
  90. echo "匹配成功: {$data['client_id']} 、 {$room['client_id']}\n";
  91. //设置session
  92. Gateway::setSession($room['client_id'], ['room_id'=>$room['room_id']]);
  93. Gateway::setSession($data['client_id'], ['room_id'=>$room['room_id']]);
  94. //发送题目
  95. $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->first();
  96. $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
  97. $info['question'] = $question;
  98. $info['options'] = $options;
  99. $info['questions_count'] = 1;
  100. $questions[] = $info;
  101. Redis::set($room['room_id'] . '_questions', json_encode($questions));
  102. $message = [
  103. "type" => 'question',
  104. "msg" => "获取题目成功",
  105. "info" => $info,
  106. ];
  107. Gateway::sendToGroup($room['room_id'], json_encode($message));
  108. echo "发送题目成功: {$room['room_id']}\n";
  109. }else{
  110. //新建一个房间
  111. $room_id = $this->get_room_id();
  112. $room = ['room_id'=>$room_id, "user_id"=>$data['user_id'], "avatar"=>$data['avatar'], "username"=>$data['username'],'client_id'=>$data['client_id']];
  113. Redis::set($data['level'], json_encode($room));
  114. }
  115. }
  116. }
  117. public function get_room_id()
  118. {
  119. while (1) {
  120. $room_id = 'room_' . rand(10000,99999);
  121. $count = Gateway::getUidCountByGroup($room_id);
  122. if(!$count){
  123. return $room_id;
  124. }
  125. }
  126. }
  127. }