gamematch.php 4.6 KB

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