Example #1
0
        private void OnRoomFull(MatchRoom room)
        {
            BattleUserInfo roomInfo = room.GetBattleUserInfo();

            for (int i = 0; i < room.numUsers; ++i)
            {
                MatchRoomUser user = room.GetUserAt(i);
                room.RemoveUser(user);
            }
            this._rooms.Remove(room);
            MatchRoomPool.Push(room);
            this.eventHandler(MatchEvent.Type.MatchSuccess, null, roomInfo);
        }
Example #2
0
        internal bool Join(Protos.GC2CS_BeginMatch.Types.EMode mode, CSUser user)
        {
            if (this._userIDToRoomUser.ContainsKey(user.gcNID))
            {
                return(false);
            }

            byte mode2;

            switch (mode)
            {
            case Protos.GC2CS_BeginMatch.Types.EMode.T1P1:
                mode2 = (1 << 4) | 1;
                break;

            case Protos.GC2CS_BeginMatch.Types.EMode.T2P1:
                mode2 = (2 << 4) | 1;
                break;

            case Protos.GC2CS_BeginMatch.Types.EMode.T2P2:
                mode2 = (2 << 4) | 2;
                break;

            case Protos.GC2CS_BeginMatch.Types.EMode.T4P1:
                mode2 = (4 << 4) | 1;
                break;

            default:
                Logger.Warn($"not support mode:{mode}");
                return(false);
            }
            if (!this._matchingSystems.TryGetValue(mode2, out MatchSystem matchingSystem))
            {
                return(false);
            }

            MatchRoomUser roomUser = new MatchRoomUser(user.gcNID, user.rank);

            this._userIDToRoomUser[user.gcNID] = roomUser;
            if (!matchingSystem.Join(roomUser))
            {
                this._userIDToRoomUser.Remove(user.gcNID);
                return(false);
            }
            return(true);
        }
Example #3
0
        /// <summary>
        /// 玩家离开房间
        /// </summary>
        public bool Leave(MatchRoomUser user)
        {
            MatchRoom room = user.room;

            if (room == null)
            {
                return(false);
            }
            if (!room.RemoveUser(user))
            {
                return(false);
            }
            this.eventHandler(MatchEvent.Type.RemoveFromRoom, user, null);
            this.eventHandler(MatchEvent.Type.RoomInfo, null, room.GetBattleUserInfo());
            if (room.isEmpty)
            {
                this._rooms.Remove(room);
                MatchRoomPool.Push(room);
            }
            return(true);
        }
Example #4
0
        /// <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);
        }
Example #5
0
        private void OnEvent(MatchEvent.Type type, MatchRoomUser sender, BattleUserInfo battleUserInfo)
        {
            switch (type)
            {
            case MatchEvent.Type.AddToRoom:
            {
                Protos.CS2GC_AddToMatch response = ProtoCreator.Q_CS2GC_AddToMatch();
                CS.instance.userMgr.GetUser(sender.id).Send(response);
            }
            break;

            case MatchEvent.Type.RemoveFromRoom:
            {
                Protos.CS2GC_RemoveFromMatch response = ProtoCreator.Q_CS2GC_RemoveFromMatch();
                CS.instance.userMgr.GetUser(sender.id).Send(response);
            }
            break;

            case MatchEvent.Type.RoomInfo:
            {
                Protos.CS2GC_MatchState pMatchState = ProtoCreator.Q_CS2GC_MatchState();
                int c1 = battleUserInfo.tUsers.Length;
                for (int i = 0; i < c1; i++)
                {
                    BattleUser[] roomUsers = battleUserInfo.tUsers[i];
                    int          c2        = roomUsers.Length;
                    for (int j = 0; j < c2; j++)
                    {
                        BattleUser roomUser = roomUsers[j];
                        if (roomUser == null)
                        {
                            Protos.CS2GC_PlayerInfo playerInfo = new Protos.CS2GC_PlayerInfo
                            {
                                Vaild = false,
                            };
                            pMatchState.PlayerInfos.Add(playerInfo);
                        }
                        else
                        {
                            CSUser user = CS.instance.userMgr.GetUser(roomUser.id);
                            Protos.CS2GC_PlayerInfo playerInfo = new Protos.CS2GC_PlayerInfo
                            {
                                Vaild    = true,
                                GcNID    = user.gcNID,
                                Team     = i,
                                ActorID  = user.actorID,
                                Nickname = user.nickname,
                                Avatar   = user.avatar,
                                Gender   = user.gender,
                                Money    = user.money,
                                Diamoned = user.diamoned,
                                Rank     = user.rank
                            };
                            pMatchState.PlayerInfos.Add(playerInfo);
                        }
                    }
                }
                CS.instance.battleEntry.Broadcast(battleUserInfo.users, pMatchState);
            }
            break;

            case MatchEvent.Type.MatchSuccess:
                foreach (MatchRoomUser user in battleUserInfo.users)
                {
                    this._userIDToRoomUser.Remove(user.id);
                }
                CS.instance.battleEntry.BeginBattle(battleUserInfo.users, battleUserInfo.tUsers);
                break;
            }
        }
Example #6
0
 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;
             }
         }
     }
 }