gamematch.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. use App\Model\User;
  9. use Ramsey\Uuid\Uuid;
  10. use App\Jobs\Distribute;
  11. class gamematch extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'game:match';
  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. //注册地址
  42. Gateway::$registerAddress = '127.0.0.1:1238';
  43. $match_times = 0;
  44. //循环匹配
  45. while (1) {
  46. $user_id = Redis::lpop('match_list');
  47. if(!$user_id){
  48. if($match_times > 5){
  49. //获取是否有房间未满人
  50. $room_id = $this->get_free_room();
  51. if(!$room_id){
  52. echo "未找到匹配房间\n";
  53. continue;
  54. }
  55. //获取离线机器人
  56. $user = User::inRandomOrder()->select('user_id','name')->where("is_robot",1)->where("is_login",0)->first();
  57. if(!$user){
  58. echo "没有离线机器人\n";
  59. sleep(1);
  60. continue;
  61. }
  62. //发布启动机器人
  63. Redis::publish("startRobot", $user->user_id);
  64. }
  65. sleep(1);
  66. $match_times++;
  67. continue;
  68. }
  69. $match_times = 0;
  70. $info = [];
  71. $room_id = $this->get_free_room();
  72. $user = User::select('user_id', 'avatar', 'name', 'win_rate')->where("user_id", $user_id)->first();
  73. $user_client_id = Gateway::getClientIdByUid($user_id);
  74. if($user->is_robot && !$user->is_login){
  75. if(Gateway::isUidOnline($user_id)){
  76. //更新用户登陆状态
  77. unset($update_data);
  78. $update_data = [
  79. "is_login" => 1,
  80. "client_id" => $user_client_id,
  81. ];
  82. User::where("user_id", $user_id)->update($update_data);
  83. }else{
  84. continue;
  85. }
  86. }
  87. if($room_id){
  88. //获取房间信息
  89. $room = Room::where("room_id", $room_id)->first();
  90. //把当前用户加入当前房间
  91. Gateway::joinGroup($user_client_id, $room_id);
  92. RoomUser::insert([
  93. 'user_id' => $user_id,
  94. 'room_id' => $room_id,
  95. 'state' => 1,
  96. ]);
  97. $info['user'] = [
  98. "user_id" => $user_id,
  99. "name" => $user->name,
  100. "avatar" => $user->avatar,
  101. "win_rate" => $user->win_rate,
  102. ];
  103. //获取房间玩家信息
  104. $group_users = Gateway::getUidListByGroup($room_id);
  105. if(count($group_users) > 0 && count($group_users) < $room->user_limit){
  106. //清理不在房间的用户
  107. RoomUser::whereNotIn('user_id', array_values($group_users))->delete();
  108. }
  109. $info['players'] = User::select('user_id', 'avatar', 'name', 'win_rate')->whereIn("user_id", array_values($group_users))->get();
  110. //发送可以开始消息
  111. $message = [
  112. "type" => 'player_join',
  113. "msg" => "玩家加入房间",
  114. "info" => $info
  115. ];
  116. Gateway::sendToGroup($room_id, json_encode($message));
  117. echo "玩家加入房间: {$user_id}\n";
  118. //判断对战是否开启标志
  119. if(count($group_users) == $room->user_limit){
  120. unset($update_data);
  121. $update_data['is_full'] = 1;
  122. $update_data['start_at'] = date('Y-m-d H:i:s');
  123. Room::where("room_id", $room_id)->update($update_data);
  124. }
  125. //执行发题
  126. Distribute::dispatch($room_id)->onQueue('distribute');
  127. //发送题目
  128. // $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->first();
  129. // $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
  130. // $info['question'] = $question;
  131. // $info['options'] = $options;
  132. // $info['questions_count'] = 1;
  133. // $questions[] = $info;
  134. // Redis::set($room['room_id'] . '_questions', json_encode($questions));
  135. // $message = [
  136. // "type" => 'question',
  137. // "msg" => "获取题目成功",
  138. // "info" => $info,
  139. // ];
  140. // Gateway::sendToGroup($room['room_id'], json_encode($message));
  141. // echo "发送题目成功: {$room['room_id']}\n";
  142. }else{
  143. //新建一个房间
  144. $room_id = Room::insertGetId([
  145. 'title' => 'room_' . $user->name . date('YmdHis'),
  146. 'is_full' => 0,
  147. 'is_end' => 0,
  148. 'start_at' => 0,
  149. 'user_limit' => 2,
  150. 'nickname' => $user->name,
  151. 'user_id' => $user_idc,
  152. 'created_at' => date('Y-m-d H:i:s'),
  153. 'updated_at' => date('Y-m-d H:i:s'),
  154. ]);
  155. RoomUser::insert([
  156. "user_id" => $user_id,
  157. "room_id" => $room_id,
  158. "state" => 1,
  159. ]);
  160. //把当前用户加入当前房间 保险点获取用户最新 client_id
  161. Gateway::joinGroup($user_client_id, $room_id);
  162. echo "玩家新建房间: {$user_id}\n";
  163. //当前用户信息
  164. $info['user'] = [
  165. "user_id" => $user_id,
  166. "name" => $user->name,
  167. "avatar" => $user->avatar,
  168. "win_rate" => $user->win_rate,
  169. ];
  170. //获取房间玩家信息
  171. $group_users = Gateway::getUidListByGroup($room_id);
  172. $info['players'] = User::select('user_id', 'avatar', 'name', 'win_rate')->whereIn("user_id", array_values($group_users))->get();
  173. //发送可以开始消息
  174. $message = [
  175. "type" => 'player_join',
  176. "msg" => "玩家加入房间",
  177. "info" => $info
  178. ];
  179. Gateway::sendToGroup($room_id, json_encode($message));
  180. }
  181. }
  182. }
  183. /**
  184. * 获取正在匹配的房间
  185. */
  186. public function get_free_room()
  187. {
  188. //注册地址
  189. Gateway::$registerAddress = '127.0.0.1:1238';
  190. while (1) {
  191. $room = Room::select("room_id")->where("is_full",0)->where("is_end","<>",1)->first();
  192. if($room){
  193. $users = RommUser::select("user_id")->where("room_id", $room->room_id)->where("state", 1)->get();
  194. foreach ($users as $user) {
  195. //检查当前room玩家是否还在房间中
  196. $is_online = Gateway::isUidOnline($user->user_id);
  197. if(!$is_online){
  198. //把当前房间关闭
  199. $update_data["is_end"] = 1;
  200. Room::where("room_id", $room->room_id)->update($update_data);
  201. unset($update_data);
  202. //取消用户在房间的状态
  203. $update_data['state'] = 2;
  204. RoomUser::where("room_id", $room->room_id)->update($update_data);
  205. continue;
  206. }
  207. }
  208. return $room->room_id;
  209. }else{
  210. return false;
  211. }
  212. }
  213. }
  214. }