gamematch.php 9.4 KB

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