gamematch.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $room = Redis::get('T1');
  48. if($room){
  49. $robot = Redis::spop('robot_list');
  50. $message = [
  51. "type" => 'gotomatch',
  52. "client_id" => $robot,
  53. ];
  54. if($robot && Gateway::isOnline($robot)){
  55. Gateway::sendToClient($robot, json_encode($message));
  56. echo "启动机器人: {$robot}\n";
  57. }
  58. }
  59. }
  60. sleep(1);
  61. $match_times++;
  62. continue;
  63. }
  64. $match_times = 0;
  65. $info = [];
  66. $questions = [];
  67. $data = json_decode($data, 1);
  68. $room = Redis::get($data['level']);
  69. if($room){
  70. Redis::del($data['level']);
  71. $room = json_decode($room, 1);
  72. //获取玩家ClientID
  73. if($room['client_id'] == $data['client_id'] || $room['user_id'] == $data['user_id']){
  74. continue;
  75. }
  76. // $player_1_ = Gateway::getClientIdByUid($room['uid']);
  77. Gateway::joinGroup($room['client_id'], $room['room_id']);
  78. // $player_2 = Gateway::getClientIdByUid($data['uid']);
  79. Gateway::joinGroup($data['client_id'], $room['room_id']);
  80. //组合玩家信息
  81. $players = [
  82. $data,
  83. $room
  84. ];
  85. //发送可以开始消息
  86. $message = [
  87. "type" => 'match',
  88. "msg" => "匹配成功",
  89. "info" => ['room_id'=>$room['room_id'],"players"=>$players],
  90. ];
  91. //清理房间数据
  92. Redis::del($room['room_id'] . '_info');
  93. Redis::del($room['room_id'] . '_questions');
  94. Gateway::sendToGroup($room['room_id'], json_encode($message));
  95. echo "清理数据 {$room['room_id']}\n";
  96. echo "匹配成功: {$data['client_id']} 、 {$room['client_id']}\n";
  97. //设置session
  98. Gateway::setSession($room['client_id'], ['room_id'=>$room['room_id']]);
  99. Gateway::setSession($data['client_id'], ['room_id'=>$room['room_id']]);
  100. //发送题目
  101. $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->first();
  102. $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
  103. $info['question'] = $question;
  104. $info['options'] = $options;
  105. $info['questions_count'] = 1;
  106. $questions[] = $info;
  107. Redis::set($room['room_id'] . '_questions', json_encode($questions));
  108. $message = [
  109. "type" => 'question',
  110. "msg" => "获取题目成功",
  111. "info" => $info,
  112. ];
  113. Gateway::sendToGroup($room['room_id'], json_encode($message));
  114. echo "发送题目成功: {$room['room_id']}\n";
  115. }else{
  116. //新建一个房间
  117. $room_id = $this->get_room_id();
  118. $room = ['room_id'=>$room_id, "user_id"=>$data['user_id'], "avatar"=>$data['avatar'], "username"=>$data['username'],'client_id'=>$data['client_id']];
  119. Redis::set($data['level'], json_encode($room));
  120. }
  121. }
  122. }
  123. public function get_room_id()
  124. {
  125. while (1) {
  126. $room_id = 'room_' . rand(10000,99999);
  127. $count = Gateway::getUidCountByGroup($room_id);
  128. if(!$count){
  129. return $room_id;
  130. }
  131. }
  132. }
  133. }