Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="teamName"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool TryJoinTeam(string teamName, LobbyMember member)
        {
            if (!Config.EnableTeamSwitching)
            {
                return(false);
            }

            var currentTeam = member.Team;
            var newTeam     = teamsList[teamName];

            // Ignore, if any of the teams is invalid
            if (currentTeam == null || newTeam == null)
            {
                return(false);
            }

            if (newTeam.PlayersCount >= newTeam.MaxPlayers)
            {
                SendChatMessage(member, "Team is full", true);
                return(false);
            }

            // Try to add the member
            if (!newTeam.AddMember(member))
            {
                return(false);
            }

            // Remove member from previous team
            currentTeam.RemoveMember(member);

            OnPlayerTeamChanged(member, newTeam);

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        protected virtual void OnPlayerAdded(LobbyMember member)
        {
            // Notify others about the new user
            var msg = MessageHelper.Create((short)MstMessageCodes.LobbyMemberJoined, member.GenerateDataPacket().ToBytes());

            // Don't send to the person who just joined
            Broadcast(msg, p => p != member.Extension.Peer);
        }
Esempio n. 3
0
        protected virtual void PickNewGameMaster(bool broadcastChange = true)
        {
            if (!Config.EnableGameMasters)
            {
                return;
            }

            GameMaster = membersByUsernameList.Values.FirstOrDefault();
        }
Esempio n. 4
0
        /// <summary>
        /// Removes a member from the lobby
        /// </summary>
        /// <param name="member"></param>
        public void RemoveMember(LobbyMember member)
        {
            Members.Remove(member.Username);

            if (member.Team == this)
            {
                member.Team = null;
            }
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        protected virtual void OnPlayerReadyStatusChange(LobbyMember member)
        {
            // Broadcast the new status
            var packet = new StringPairPacket()
            {
                A = member.Username,
                B = member.IsReady.ToString()
            };

            Broadcast(MessageHelper.Create((short)MstMessageCodes.LobbyMemberReadyStatusChange, packet.ToBytes()));
        }
Esempio n. 6
0
        /// <summary>
        /// Adds a member to the lobby
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool AddMember(LobbyMember member)
        {
            if (Members.ContainsKey(member.Username))
            {
                return(false);
            }

            Members.Add(member.Username, member);
            member.Team = this;

            return(true);
        }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        protected virtual void OnPlayerRemoved(LobbyMember member)
        {
            // Destroy lobby if last member left
            if (!Config.KeepAliveWithZeroPlayers && membersByUsernameList.Count == 0)
            {
                Destroy();
                Logger.Log(LogLevel.Info, string.Format("Lobby \"{0}\" destroyed due to last player leaving.", Name));
            }

            // Notify others about the user who left
            Broadcast(MessageHelper.Create((short)MstMessageCodes.LobbyMemberLeft, member.Username));
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        /// <param name="newTeam"></param>
        protected virtual void OnPlayerTeamChanged(LobbyMember member, LobbyTeam newTeam)
        {
            var packet = new StringPairPacket()
            {
                A = member.Username,
                B = newTeam.Name
            };

            // Broadcast the change
            var msg = MessageHelper.Create((short)MstMessageCodes.LobbyMemberChangedTeam, packet.ToBytes());

            Broadcast(msg);
        }
Esempio n. 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        /// <param name="propertyKey"></param>
        protected virtual void OnPlayerPropertyChange(LobbyMember member, string propertyKey)
        {
            // Broadcast the changes
            var changesPacket = new LobbyMemberPropChangePacket()
            {
                LobbyId  = Id,
                Username = member.Username,
                Property = propertyKey,
                Value    = member.GetProperty(propertyKey)
            };

            Broadcast(MessageHelper.Create((short)MstMessageCodes.LobbyMemberPropertyChanged, changesPacket.ToBytes()));
        }
Esempio n. 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        /// <param name="message"></param>
        /// <param name="isError"></param>
        /// <param name="sender"></param>
        public void SendChatMessage(LobbyMember member, string message, bool isError = false, string sender = "System")
        {
            var packet = new LobbyChatPacket()
            {
                Message = message,
                Sender  = sender,
                IsError = isError
            };

            var msg = MessageHelper.Create((short)MstMessageCodes.LobbyChatMessage, packet.ToBytes());

            member.Extension.Peer.SendMessage(msg, DeliveryMethod.Reliable);
        }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="member"></param>
        /// <param name="message"></param>
        public void ChatMessageHandler(LobbyMember member, IIncomingMessage message)
        {
            var text = message.AsString();

            var messagePacket = new LobbyChatPacket()
            {
                Message = text,
                Sender  = member.Username
            };

            var msg = MessageHelper.Create((short)MstMessageCodes.LobbyChatMessage, messagePacket.ToBytes());

            Broadcast(msg);
        }
Esempio n. 12
0
        /// <summary>
        /// Set the lobby member state as ready
        /// </summary>
        /// <param name="member"></param>
        /// <param name="state"></param>
        public void SetReadyState(LobbyMember member, bool state)
        {
            if (!membersByUsernameList.ContainsKey(member.Username))
            {
                return;
            }

            member.IsReady = state;

            OnPlayerReadyStatusChange(member);

            if (membersByUsernameList.Values.All(m => m.IsReady))
            {
                OnAllPlayersReady();
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Set a lobby player property
        /// </summary>
        /// <param name="member"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetPlayerProperty(LobbyMember member, string key, string value)
        {
            // Invalid property
            if (key == null)
            {
                return(false);
            }

            // Check if player is allowed to change this property
            if (!IsPlayerPropertyChangeable(member, key, value))
            {
                return(false);
            }

            member.SetProperty(key, value);

            OnPlayerPropertyChange(member, key);

            return(true);
        }
Esempio n. 14
0
 protected virtual bool IsPlayerPropertyChangeable(LobbyMember member, string key, string value)
 {
     return(true);
 }
Esempio n. 15
0
 public virtual LobbyTeam PickTeamForPlayer(LobbyMember member)
 {
     return(teamsList.Values
            .Where(t => t.CanAddPlayer(member))
            .OrderBy(t => t.PlayersCount).FirstOrDefault());
 }
Esempio n. 16
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="member"></param>
 /// <returns></returns>
 public virtual LobbyMemberData GenerateMemberData(LobbyMember member)
 {
     return(member.GenerateDataPacket());
 }
Esempio n. 17
0
 /// <summary>
 /// Checks if a specific member can be added to the lobby
 /// </summary>
 /// <param name="member"></param>
 /// <returns></returns>
 public virtual bool CanAddPlayer(LobbyMember member)
 {
     return(PlayersCount < MaxPlayers);
 }