gamematch.php 4.2 KB

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