gamematch.php 9.9 KB

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