Exemplo n.º 1
0
        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.PlayerCount >= 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);
        }
Exemplo n.º 2
0
        protected virtual void OnPlayerAdded(LobbyMember member)
        {
            // Notify others about the new user
            var msg = MessageHelper.Create((short)MsfMessageCodes.LobbyMemberJoined, member.GenerateDataPacket().ToBytes());

            // Don't send to the person who just joined
            Broadcast(msg, p => p != member.Extension.Peer);
        }
Exemplo n.º 3
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;
            }
        }
Exemplo n.º 4
0
        protected virtual void PickNewGameMaster(bool broadcastChange = true)
        {
            if (!Config.EnableGameMasters)
            {
                return;
            }

            GameMaster = membersList.Values.FirstOrDefault();
        }
Exemplo n.º 5
0
        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)MsfMessageCodes.LobbyMemberReadyStatusChange, packet.ToBytes()));
        }
Exemplo n.º 6
0
        protected virtual void OnPlayerRemoved(LobbyMember member)
        {
            // Destroy lobby if last member left
            if (Members.Count == 0)
            {
                Destroy();
            }

            // Notify others about the user who left
            Broadcast(MessageHelper.Create((short)MsfOpCodes.LobbyMemberLeft, member.Username));
        }
Exemplo n.º 7
0
        protected virtual void OnPlayerRemoved(LobbyMember member)
        {
            // Destroy lobby if last member left
            if (!Config.KeepAliveWithZeroPlayers && membersList.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)MsfMessageCodes.LobbyMemberLeft, member.Username));
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        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)MsfMessageCodes.LobbyMemberChangedTeam, packet.ToBytes());

            Broadcast(msg);
        }
Exemplo n.º 10
0
        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)MsfMessageCodes.LobbyMemberPropertyChanged, changesPacket.ToBytes()));
        }
Exemplo n.º 11
0
        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)MsfMessageCodes.LobbyChatMessage, packet.ToBytes());

            member.Extension.Peer.SendMessage(msg, DeliveryMethod.Reliable);
        }
Exemplo n.º 12
0
        public void HandleChatMessage(LobbyMember member, IIncommingMessage message)
        {
            var text = message.AsString();

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

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

            Broadcast(msg);
        }
Exemplo n.º 13
0
        public void SetReadyState(LobbyMember member, bool state)
        {
            if (!membersList.ContainsKey(member.Username))
            {
                return;
            }

            member.IsReady = state;

            OnPlayerReadyStatusChange(member);

            if (membersList.Values.All(m => m.IsReady))
            {
                OnAllPlayersReady();
            }
        }
Exemplo n.º 14
0
        public bool SetPlayerProperty(LobbyMember player, string key, string value)
        {
            // Invalid property
            if (key == null)
            {
                return(false);
            }

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

            player.SetProperty(key, value);

            OnPlayerPropertyChange(player, key);

            return(true);
        }
Exemplo n.º 15
0
 public virtual LobbyTeam PickTeamForPlayer(LobbyMember member)
 {
     return(teamsList.Values
            .Where(t => t.CanAddPlayer(member))
            .OrderBy(t => t.PlayerCount).FirstOrDefault());
 }
Exemplo n.º 16
0
 public virtual LobbyMemberData GenerateMemberData(LobbyMember member)
 {
     return(member.GenerateDataPacket());
 }
Exemplo n.º 17
0
 protected virtual bool IsPlayerPropertyChangeable(LobbyMember member, string key, string value)
 {
     return(true);
 }
Exemplo n.º 18
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(PlayerCount < MaxPlayers);
 }