/// <summary> /// 玩家加入房间 /// </summary> public bool Join(MatchRoomUser user) { MatchRoom room = null; //搜索房间 int count = this._rooms.Count; for (int i = 0; i < count; i++) { MatchRoom r = this._rooms[i]; if (user.rank >= r.from && user.rank < r.to) { room = r; break; } } if (room == null) { //创建房间 room = MatchRoomPool.Pop(this); room.from = user.rank - this.extendRange; room.to = user.rank + this.extendRange; this._rooms.Add(room); } if (!room.AddUser(user)) { return(false); } this.eventHandler(MatchEvent.Type.AddToRoom, user, null); this.eventHandler(MatchEvent.Type.RoomInfo, null, room.GetBattleUserInfo()); if (room.isFull) { this.OnRoomFull(room); } return(true); }
public void CheckRoom() { //检查每个房间是否有可合并的玩家 for (int i = 0; i < this._rooms.Count - 1; i++) { MatchRoom cur = this._rooms[i]; for (int j = i + 1; j < this._rooms.Count; j++) { MatchRoom nxt = this._rooms[j]; //先检查两个房间是否有重叠的分数区域 //按from的大小排序 MatchRoom r0, r1; if (cur.from < nxt.from) { r0 = cur; r1 = nxt; } else { r0 = nxt; r1 = cur; } //判断重叠,注意to是闭区间 if (r0.to <= r1.@from) { continue; } //重叠 //搜索房间玩家,符合范围的会被抢到当前房间 for (int k = 0; k < nxt.numUsers; ++k) { MatchRoomUser user = nxt.GetUserAt(k); if (user == null) { continue; } //检查玩家分数是否在范围内 if (user.rank >= cur.@from && user.rank < cur.to) { System.Diagnostics.Debug.Assert(nxt.RemoveUser(user)); System.Diagnostics.Debug.Assert(cur.AddUser(user)); this.eventHandler(MatchEvent.Type.RoomInfo, null, cur.GetBattleUserInfo()); if (nxt.isEmpty || cur.isFull) { break; } } } if (nxt.isEmpty) { this._rooms.RemoveAt(j); MatchRoomPool.Push(nxt); --j; } //如果满员则不用继续往下搜索了 if (cur.isFull) { this.OnRoomFull(cur); --i; break; } } } }