private void leave(UserToken token) { //取出用户唯一ID int userId = getUserId(token); Console.WriteLine("用户取消匹配" + userId); //判断用户是否有房间映射关系 if (!userRoom.ContainsKey(userId)) { return; } //获取用户所在房间ID int roomId = userRoom[userId]; //判断是否拥有此房间 if (roomMap.ContainsKey(roomId)) { MatchRoom room = roomMap[roomId]; //根据用户所在的队伍 进行移除 if (room.teamOne.Contains(userId)) { room.teamOne.Remove(userId); } else if (room.teamTwo.Contains(userId)) { room.teamTwo.Remove(userId); } //移除用户与房间之间的映射关系 userRoom.TryRemove(userId, out roomId); //如果当前用户为此房间最后一人 则移除房间 并丢进缓存队列 if (room.teamOne.Count + room.teamTwo.Count == 0) { roomMap.TryRemove(roomId, out room); cache.Push(room); } } }
private void leave(UserToken token) { User user = userBiz.get(token); if (user == null) { return; } if (!userMatch.ContainsKey(user.id)) { return; } int roomId = userMatch[user.id]; if (nowRoom.ContainsKey(roomId)) { MatchRoom room = nowRoom[roomId]; if (room.teamOne.Contains(user.id)) { room.teamOne.Remove(user.id); } else if (room.teamTwo.Contains(user.id)) { room.teamTwo.Remove(user.id); } int ri; userMatch.TryRemove(user.id, out ri); if (room.teamOne.Count + room.teamTwo.Count == 0) { MatchRoom r; nowRoom.TryRemove(room.id, out r); cache.Enqueue(room); } } }
private void enter(UserToken token) { User user = userBiz.get(token); if (!userMatch.ContainsKey(user.id)) { //判断当前 是否有已经在匹配的房间 MatchRoom room = null; if (nowRoom.Count > 0) { foreach (MatchRoom item in nowRoom.Values) { if (item.teamMax * 2 > item.teamOne.Count + item.teamTwo.Count) { room = item; if (room.teamOne.Count < item.teamMax) { room.teamOne.Add(user.id); } else { room.teamTwo.Add(user.id); } userMatch.TryAdd(user.id, room.id); break; } } if (room == null) { //没有的话 判断有没有历史房间 if (cache.Count > 0) { cache.TryDequeue(out room); room.teamOne.Add(user.id); nowRoom.TryAdd(room.id, room); userMatch.TryAdd(user.id, room.id); } else { //没有历史房间 果断创建新的 room = new MatchRoom(); room.id = roomId.getAndAdd(); room.teamOne.Add(user.id); nowRoom.TryAdd(room.id, room); userMatch.TryAdd(user.id, room.id); } } } else { //没有的话 判断有没有历史房间 if (cache.Count > 0) { cache.TryDequeue(out room); room.teamOne.Add(user.id); nowRoom.TryAdd(room.id, room); userMatch.TryAdd(user.id, room.id); } else { //没有历史房间 果断创建新的 room = new MatchRoom(); room.id = roomId.getAndAdd(); room.teamOne.Add(user.id); nowRoom.TryAdd(room.id, room); userMatch.TryAdd(user.id, room.id); } } //不管何种进入,满员就果断开之,并且将匹配对象放入缓存 if (room.teamOne.Count == room.teamTwo.Count && room.teamTwo.Count == room.teamMax) { EventUtil.Instance.selectStart(room.teamOne, room.teamTwo); foreach (int item in room.teamOne) { int i; userMatch.TryRemove(item, out i); } foreach (int item in room.teamTwo) { int i; userMatch.TryRemove(item, out i); } room.teamOne.Clear(); room.teamTwo.Clear(); MatchRoom r; nowRoom.TryRemove(room.id, out r); cache.Enqueue(room); } } }
private void enter(UserToken token) { int userId = getUserId(token); Console.WriteLine("用户开始匹配" + userId); //判断玩家当前是否正在匹配队列中 if (!userRoom.ContainsKey(userId)) { MatchRoom room = null; bool isenter = false; //当前是否有等待中的房间 if (roomMap.Count > 0) { //遍历当前所有等待中的房间 foreach (MatchRoom item in roomMap.Values) { //如果没满员 if (item.teamMax * 2 > item.teamOne.Count + item.teamTwo.Count) { room = item; //如果队伍1没有满员 则进入队伍1 否则进入队伍2 if (room.teamOne.Count < room.teamMax) { room.teamOne.Add(userId); } else { room.teamTwo.Add(userId); } //添加玩家与房间的映射关系 isenter = true; userRoom.TryAdd(userId, room.id); break; } } if (!isenter) { //走到这里 说明等待中的房间全部满员了 if (cache.Count > 0) { cache.TryPop(out room); room.teamOne.Add(userId); roomMap.TryAdd(room.id, room); userRoom.TryAdd(userId, room.id); } else { room = new MatchRoom(); room.id = index.GetAndAdd(); room.teamOne.Add(userId); roomMap.TryAdd(room.id, room); userRoom.TryAdd(userId, room.id); } } } else { //没有等待中的房间 //直接从缓存列表中找出可用房间 或者创建新房间 if (cache.Count > 0) { cache.TryPop(out room); room.teamOne.Add(userId); roomMap.TryAdd(room.id, room); userRoom.TryAdd(userId, room.id); } else { room = new MatchRoom(); room.id = index.GetAndAdd(); room.teamOne.Add(userId); roomMap.TryAdd(room.id, room); userRoom.TryAdd(userId, room.id); } } //不管什么方式进入房间 ,判断房间是否满员,满了就开始选人并将当前房间丢进缓存队列 if (room.teamOne.Count == room.teamTwo.Count && room.teamOne.Count == room.teamMax) { // 这里通知选人模块 开始选人了 EventUtil.createSelect(room.teamOne, room.teamTwo); writeToUsers(room.teamOne.ToArray(), GetType(), 0, MatchProtocol.ENTER_SELECT_BRO, null); writeToUsers(room.teamTwo.ToArray(), GetType(), 0, MatchProtocol.ENTER_SELECT_BRO, null); //移除玩家与房间映射 foreach (int item in room.teamOne) { int i; userRoom.TryRemove(item, out i); } foreach (int item in room.teamTwo) { int i; userRoom.TryRemove(item, out i); } //重置房间数据 供下次使用 room.teamOne.Clear(); room.teamTwo.Clear(); //将房间从等待房间表中移除 roomMap.TryRemove(room.id, out room); //将房间丢进缓存表 供下次使用 cache.Push(room); } } }