Пример #1
0
        public LobbyMember GetMember(LobbyUserPeerExtension playerExt)
        {
            LobbyMember member;

            membersByPeerIdList.TryGetValue(playerExt.Peer.Id, out member);

            return(member);
        }
Пример #2
0
        public virtual bool StartGameManually(LobbyUserPeerExtension user)
        {
            var member = GetMember(user);

            if (!Config.EnableManualStart)
            {
                SendChatMessage(member, "You cannot start the game manually", true);
                return(false);
            }

            // If not game maester
            if (GameMaster != member)
            {
                SendChatMessage(member, "You're not the master of this game", true);
                return(false);
            }

            if (State != LobbyState.Preparations)
            {
                SendChatMessage(member, "Invalid lobby state", true);
                return(false);
            }

            if (IsDestroyed)
            {
                SendChatMessage(member, "Lobby is destroyed", true);
                return(false);
            }

            if (membersList.Values.Any(m => !m.IsReady && m != _gameMaster))
            {
                SendChatMessage(member, "Not all players are ready", true);
                return(false);
            }

            if (membersList.Count < MinPlayers)
            {
                SendChatMessage(
                    member,
                    string.Format("Not enough players. Need {0} more ", (MinPlayers - membersList.Count)),
                    true);
                return(false);
            }

            var lackingTeam = teamsList.Values.FirstOrDefault(t => t.MinPlayers > t.PlayerCount);

            if (lackingTeam != null)
            {
                var msg = string.Format("Team {0} does not have enough players", lackingTeam.Name);
                SendChatMessage(member, msg, true);
                return(false);
            }

            return(StartGame());
        }
Пример #3
0
        protected virtual LobbyUserPeerExtension GetOrCreateLobbiesExtension(IPeer peer)
        {
            var extension = peer.GetExtension <LobbyUserPeerExtension>();

            if (extension == null)
            {
                extension = new LobbyUserPeerExtension(peer);
                peer.AddExtension(extension);
            }

            return(extension);
        }
Пример #4
0
        public void RemovePlayer(LobbyUserPeerExtension playerExt)
        {
            var username = TryGetUsername(playerExt.Peer);

            LobbyMember member;

            membersList.TryGetValue(username, out member);

            // If this player was never in the lobby
            if (member == null)
            {
                return;
            }

            membersList.Remove(username);
            membersByPeerIdList.Remove(playerExt.Peer.Id);

            if (playerExt.CurrentLobby == this)
            {
                playerExt.CurrentLobby = null;
            }

            // Remove member from it's current team
            if (member.Team != null)
            {
                member.Team.RemoveMember(member);
            }

            // Change the game master
            if (GameMaster == member)
            {
                PickNewGameMaster();
            }


            // Unsubscribe
            playerExt.Peer.OnPeerDisconnectedEvent -= OnPeerDisconnected;
            Unsubscribe(playerExt.Peer);

            // Notify player himself that he's removed
            playerExt.Peer.SendMessage((short)MsfMessageCodes.LeftLobby, Id);

            OnPlayerRemoved(member);

            if (OnPlayerRemovedEvent != null)
            {
                OnPlayerRemovedEvent.Invoke(member);
            }
        }
Пример #5
0
        public virtual bool SetProperty(LobbyUserPeerExtension setter, string key, string value)
        {
            if (!Config.AllowPlayersChangeLobbyProperties)
            {
                return(false);
            }

            if (Config.EnableGameMasters)
            {
                LobbyMember member;
                membersByPeerIdList.TryGetValue(setter.Peer.Id, out member);

                if (GameMaster != member)
                {
                    return(false);
                }
            }

            return(SetProperty(key, value));
        }
Пример #6
0
        public LobbyDataPacket GenerateLobbyData(LobbyUserPeerExtension user)
        {
            var info = new LobbyDataPacket
            {
                LobbyType       = Type ?? "",
                GameMaster      = GameMaster != null ? GameMaster.Username : "",
                LobbyName       = Name,
                LobbyId         = Id,
                LobbyProperties = propertiesList,
                Players         = membersList.Values
                                  .ToDictionary(m => m.Username, GenerateMemberData),
                Teams               = teamsList.Values.ToDictionary(t => t.Name, t => t.GenerateData()),
                Controls            = Controls,
                LobbyState          = State,
                MaxPlayers          = MaxPlayers,
                EnableTeamSwitching = Config.EnableTeamSwitching,
                EnableReadySystem   = Config.EnableReadySystem,
                EnableManualStart   = Config.EnableManualStart,
                CurrentUserUsername = TryGetUsername(user.Peer)
            };

            return(info);
        }
Пример #7
0
 public LobbyMember(string username, LobbyUserPeerExtension ext)
 {
     Username   = username;
     Extension  = ext;
     Properties = new Dictionary <string, string>();
 }
Пример #8
0
 /// <summary>
 /// This will be called before adding a player to lobby.
 /// Override it to add custom checks for bans and etc.
 /// </summary>
 protected virtual bool IsPlayerAllowed(string username, LobbyUserPeerExtension user)
 {
     return(true);
 }
Пример #9
0
 protected virtual LobbyMember CreateMember(string username, LobbyUserPeerExtension extension)
 {
     return(new LobbyMember(username, extension));
 }
Пример #10
0
        public bool AddPlayer(LobbyUserPeerExtension playerExt, out string error)
        {
            error = null;

            if (playerExt.CurrentLobby != null)
            {
                error = "You're already in a lobby";
                return(false);
            }

            var username = TryGetUsername(playerExt.Peer);

            if (username == null)
            {
                error = "Invalid username";
                return(false);
            }

            if (membersList.ContainsKey(username))
            {
                error = "Already in the lobby";
                return(false);
            }

            if (IsDestroyed)
            {
                error = "Lobby is destroyed";
                return(false);
            }

            if (!IsPlayerAllowed(username, playerExt))
            {
                error = "You're not allowed";
                return(false);
            }

            if (membersList.Values.Count >= MaxPlayers)
            {
                error = "Lobby is full";
                return(false);
            }

            if (!Config.AllowJoiningWhenGameIsLive && State != LobbyState.Preparations)
            {
                error = "Game is already in progress";
                return(false);
            }

            // Create an "instance" of the member
            var member = CreateMember(username, playerExt);

            // Add it to a team
            var team = PickTeamForPlayer(member);

            if (team == null)
            {
                error = "Invalid lobby team";
                return(false);
            }

            if (!team.AddMember(member))
            {
                error = "Not allowed to join a team";
                return(false);
            }

            membersList[member.Username]           = member;
            membersByPeerIdList[playerExt.Peer.Id] = member;

            // Set this lobby as player's current lobby
            playerExt.CurrentLobby = this;

            if (GameMaster == null)
            {
                PickNewGameMaster(false);
            }

            Subscribe(playerExt.Peer);

            playerExt.Peer.OnPeerDisconnectedEvent += OnPeerDisconnected;

            OnPlayerAdded(member);

            if (OnPlayerAddedEvent != null)
            {
                OnPlayerAddedEvent.Invoke(member);
            }

            return(true);
        }