gamematch.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 = env("WS_REGEDIT_ADDR");
  45. $match_times = 0;
  46. //循环匹配
  47. while (1) {
  48. $user_id = Redis::lpop('match_list');
  49. if(!$user_id){
  50. if($match_times > 2){
  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','client_id')->where("is_robot",1)->where("is_login", 0)->first();
  60. if(!$user){
  61. echo "没有离线机器人\n";
  62. sleep(1);
  63. continue;
  64. }
  65. if(!Gateway::isUidOnline($user->user_id)){
  66. $update_data = [
  67. "is_login" => 0,
  68. ];
  69. //更新机器人登陆状态
  70. User::where("user_id", $user->user_id)->update($update_data);
  71. }
  72. //发布启动机器人
  73. Redis::publish("startRobot", $user->user_id);
  74. echo "启动机器人 {$user->user_id}\n";
  75. // $message['type'] = 'gotomatch';
  76. // Gateway::sendToUid($user->user_id, json_encode($message));
  77. }
  78. sleep(2);
  79. $match_times++;
  80. continue;
  81. }
  82. $match_times = 0;
  83. $info = [];
  84. $room_id = $this->get_free_room();
  85. $user = User::select('user_id', 'avatar', 'name', 'win_count', 'lose_count','client_id')->where("user_id", $user_id)->first();
  86. // if($user->is_robot && !$user->is_login){
  87. // if(Gateway::isUidOnline($user_id)){
  88. // //更新用户登陆状态
  89. // unset($update_data);
  90. // $update_data = [
  91. // "is_login" => 1,
  92. // "client_id" => $user->client_id,
  93. // ];
  94. // User::where("user_id", $user_id)->update($update_data);
  95. // }else{
  96. // continue;
  97. // }
  98. // }
  99. if($room_id){
  100. //获取房间信息
  101. $room = Room::where("room_id", $room_id)->first();
  102. //把当前用户加入当前房间
  103. Gateway::joinGroup($user->client_id, $room_id);
  104. RoomUser::insert([
  105. 'user_id' => $user_id,
  106. 'room_id' => $room_id,
  107. 'state' => 1,
  108. 'created_at' => date('Y-m-d H:i:s'),
  109. 'updated_at' => date('Y-m-d H:i:s'),
  110. ]);
  111. echo "玩家加入房间: {$user_id}\n";
  112. //获取房间玩家信息
  113. $room_users = RoomUser::where("room_id", $room_id)->pluck("user_id");
  114. //判断对战是否开启标志
  115. if(count($room_users) == $room->user_limit){
  116. unset($update_data);
  117. $update_data['is_full'] = 1;
  118. $update_data['start_at'] = date('Y-m-d H:i:s');
  119. Room::where("room_id", $room_id)->update($update_data);
  120. }
  121. if($user->win_count == 0 || ($user->win_count == 0 && $user->lose_count == 0)){
  122. $win_rate = 0;
  123. }else{
  124. $win_rate = $user->win_count / ($user->win_count + $user->lose_count);
  125. }
  126. $info['user'] = [
  127. "user_id" => $user_id,
  128. "name" => $user->name,
  129. "avatar" => $user->avatar,
  130. "win_rate" => round($win_rate, 2) * 100,
  131. ];
  132. $players = User::select('user_id', 'avatar', 'name', 'win_count', 'lose_count')->whereIn("user_id", $room_users)->get();
  133. foreach ($players as $k => $player) {
  134. if($player->win_count == 0 || ($player->win_count == 0 && $player->lose_count == 0)){
  135. $win_rate = 0;
  136. }else{
  137. $win_rate = $player->win_count / ($player->win_count + $player->lose_count);
  138. }
  139. $players[$k]->win_rate = round($win_rate,2) * 100;
  140. }
  141. $info['players'] = $players;
  142. //发送可以开始消息
  143. $message = [
  144. "type" => 'player_join',
  145. "msg" => "玩家加入房间",
  146. "info" => $info
  147. ];
  148. Gateway::sendToGroup($room_id, json_encode($message));
  149. //执行发题
  150. sleep(2);
  151. Distribute::dispatch($room_id)->onQueue('distribute');
  152. //发送题目
  153. // $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->first();
  154. // $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
  155. // $info['question'] = $question;
  156. // $info['options'] = $options;
  157. // $info['questions_count'] = 1;
  158. // $questions[] = $info;
  159. // Redis::set($room['room_id'] . '_questions', json_encode($questions));
  160. // $message = [
  161. // "type" => 'question',
  162. // "msg" => "获取题目成功",
  163. // "info" => $info,
  164. // ];
  165. // Gateway::sendToGroup($room['room_id'], json_encode($message));
  166. // echo "发送题目成功: {$room['room_id']}\n";
  167. }else{
  168. //新建一个房间
  169. $room_id = Room::insertGetId([
  170. 'title' => 'room_' . $user->name . date('YmdHis'),
  171. 'is_full' => 0,
  172. 'is_end' => 0,
  173. 'user_limit' => 2,
  174. 'nickname' => $user->name,
  175. 'user_id' => $user_id,
  176. 'created_at' => date('Y-m-d H:i:s'),
  177. 'updated_at' => date('Y-m-d H:i:s'),
  178. ]);
  179. RoomUser::insert([
  180. "user_id" => $user_id,
  181. "room_id" => $room_id,
  182. "state" => 1,
  183. 'created_at' => date('Y-m-d H:i:s'),
  184. 'updated_at' => date('Y-m-d H:i:s'),
  185. ]);
  186. //把当前用户加入当前房间
  187. Gateway::joinGroup($user->client_id, $room_id);
  188. echo "玩家新建房间: {$user_id}\n";
  189. /*
  190. if($user->win_count == 0 || ($user->win_count == 0 && $user->lose_count == 0)){
  191. $win_rate = 0;
  192. }else{
  193. $win_rate = $user->win_count / ($user->win_count + $user->lose_count);
  194. }
  195. //当前用户信息
  196. $info['user'] = [
  197. "user_id" => $user_id,
  198. "name" => $user->name,
  199. "avatar" => $user->avatar,
  200. "win_rate" => $win_rate,
  201. ];
  202. //获取房间玩家信息
  203. $group_users = Gateway::getUidListByGroup($room_id);
  204. $info['players'] = User::select('user_id', 'avatar', 'name', 'win_count', 'lose_count')->whereIn("user_id", array_values($group_users))->get();
  205. //发送可以开始消息
  206. $message = [
  207. "type" => 'player_join',
  208. "msg" => "玩家加入房间",
  209. "info" => $info
  210. ];
  211. Gateway::sendToGroup($room_id, json_encode($message));
  212. */
  213. }
  214. }
  215. }
  216. /**
  217. * 获取正在匹配的房间
  218. */
  219. public function get_free_room()
  220. {
  221. //注册地址
  222. Gateway::$registerAddress = env("WS_REGEDIT_ADDR");
  223. while (1) {
  224. $room = Room::select("room_id")->where("is_full",0)->where("is_end","<>",1)->first();
  225. if($room){
  226. $users = RoomUser::select("user_id")->where("room_id", $room->room_id)->where("state", 1)->get();
  227. foreach ($users as $user) {
  228. //检查当前room玩家是否还在房间中
  229. $is_online = Gateway::isUidOnline($user->user_id);
  230. if(!$is_online){
  231. //把当前房间关闭
  232. $update_data["is_end"] = 1;
  233. $update_data["end_at"] = date('Y-m-d H:i:s');
  234. Room::where("room_id", $room->room_id)->update($update_data);
  235. unset($update_data);
  236. //取消用户在房间的状态
  237. $update_data['state'] = 2;
  238. RoomUser::where([
  239. "room_id" => $room->room_id,
  240. "user_id" => $user->user_id,
  241. ])->update($update_data);
  242. continue;
  243. }
  244. }
  245. return $room->room_id;
  246. }else{
  247. return false;
  248. }
  249. }
  250. }
  251. }