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. var_dump($group_users);
  128. //判断对战是否开启标志
  129. // if(count($group_users) == $room->user_limit){
  130. unset($update_data);
  131. $update_data['is_full'] = 1;
  132. $update_data['start_at'] = date('Y-m-d H:i:s');
  133. Room::where("room_id", $room_id)->update($update_data);
  134. // }
  135. //执行发题
  136. Distribute::dispatch($room_id)->onQueue('distribute');
  137. //发送题目
  138. // $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->first();
  139. // $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);
  140. // $info['question'] = $question;
  141. // $info['options'] = $options;
  142. // $info['questions_count'] = 1;
  143. // $questions[] = $info;
  144. // Redis::set($room['room_id'] . '_questions', json_encode($questions));
  145. // $message = [
  146. // "type" => 'question',
  147. // "msg" => "获取题目成功",
  148. // "info" => $info,
  149. // ];
  150. // Gateway::sendToGroup($room['room_id'], json_encode($message));
  151. // echo "发送题目成功: {$room['room_id']}\n";
  152. }else{
  153. //新建一个房间
  154. $room_id = Room::insertGetId([
  155. 'title' => 'room_' . $user->name . date('YmdHis'),
  156. 'is_full' => 0,
  157. 'is_end' => 0,
  158. 'user_limit' => 2,
  159. 'nickname' => $user->name,
  160. 'user_id' => $user_id,
  161. 'created_at' => date('Y-m-d H:i:s'),
  162. 'updated_at' => date('Y-m-d H:i:s'),
  163. ]);
  164. RoomUser::insert([
  165. "user_id" => $user_id,
  166. "room_id" => $room_id,
  167. "state" => 1,
  168. 'created_at' => date('Y-m-d H:i:s'),
  169. 'updated_at' => date('Y-m-d H:i:s'),
  170. ]);
  171. //把当前用户加入当前房间 保险点获取用户最新 client_id
  172. Gateway::joinGroup($user->client_id, $room_id);
  173. echo "玩家新建房间: {$user_id}\n";
  174. if($user->win_count == 0 || ($user->win_count == 0 && $user->lose_count == 0)){
  175. $win_rate = 0;
  176. }else{
  177. $win_rate = $user->win_count / ($user->win_count + $user->lose_count);
  178. }
  179. //当前用户信息
  180. $info['user'] = [
  181. "user_id" => $user_id,
  182. "name" => $user->name,
  183. "avatar" => $user->avatar,
  184. "win_rate" => $win_rate,
  185. ];
  186. //获取房间玩家信息
  187. $group_users = Gateway::getUidListByGroup($room_id);
  188. $info['players'] = User::select('user_id', 'avatar', 'name', 'win_count', 'lose_count')->whereIn("user_id", array_values($group_users))->get();
  189. //发送可以开始消息
  190. $message = [
  191. "type" => 'player_join',
  192. "msg" => "玩家加入房间",
  193. "info" => $info
  194. ];
  195. Gateway::sendToGroup($room_id, json_encode($message));
  196. }
  197. }
  198. }
  199. /**
  200. * 获取正在匹配的房间
  201. */
  202. public function get_free_room()
  203. {
  204. //注册地址
  205. Gateway::$registerAddress = '127.0.0.1:1238';
  206. while (1) {
  207. $room = Room::select("room_id")->where("is_full",0)->where("is_end","<>",1)->first();
  208. if($room){
  209. $users = RoomUser::select("user_id")->where("room_id", $room->room_id)->where("state", 1)->get();
  210. foreach ($users as $user) {
  211. //检查当前room玩家是否还在房间中
  212. $is_online = Gateway::isUidOnline($user->user_id);
  213. if(!$is_online){
  214. //把当前房间关闭
  215. $update_data["is_end"] = 1;
  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. }