public void HandleGMCommand(string sender, string command)
        {
            if (string.IsNullOrEmpty(command))
            {
                return;
            }

            string[] splited    = command.Split(' ');
            string   commandKey = splited[0];
            string   receiver;
            BasePlayerCharacterEntity playerCharacter;

            if (GMCommands.IsSplitedLengthValid(commandKey, splited.Length))
            {
                if (commandKey.Equals(GMCommands.Level))
                {
                    receiver = sender;
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        short.TryParse(splited[1], out amount) &&
                        amount > 0)
                    {
                        playerCharacter.Level = amount;
                    }
                }
                if (commandKey.Equals(GMCommands.StatPoint))
                {
                    receiver = sender;
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        short.TryParse(splited[1], out amount) &&
                        amount >= 0)
                    {
                        playerCharacter.StatPoint = amount;
                    }
                }
                if (commandKey.Equals(GMCommands.SkillPoint))
                {
                    receiver = sender;
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        short.TryParse(splited[1], out amount) &&
                        amount >= 0)
                    {
                        playerCharacter.SkillPoint = amount;
                    }
                }
                if (commandKey.Equals(GMCommands.Gold))
                {
                    receiver = sender;
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        short.TryParse(splited[1], out amount) &&
                        amount >= 0)
                    {
                        playerCharacter.Gold = amount;
                    }
                }
                if (commandKey.Equals(GMCommands.AddItem))
                {
                    receiver = sender;
                    Item  item;
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        GameInstance.Items.TryGetValue(splited[1].GenerateHashId(), out item) &&
                        short.TryParse(splited[2], out amount))
                    {
                        if (amount > item.maxStack)
                        {
                            amount = item.maxStack;
                        }
                        CharacterItem newItem = CharacterItem.Create(item, 1, amount);
                        playerCharacter.NonEquipItems.Add(newItem);
                    }
                }
                if (commandKey.Equals(GMCommands.GiveGold))
                {
                    receiver = splited[1];
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        short.TryParse(splited[2], out amount))
                    {
                        playerCharacter.Gold += amount;
                    }
                }
                if (commandKey.Equals(GMCommands.GiveItem))
                {
                    receiver = splited[1];
                    Item  item;
                    short amount;
                    if (TryGetPlayerCharacterByName(receiver, out playerCharacter) &&
                        GameInstance.Items.TryGetValue(splited[2].GenerateHashId(), out item) &&
                        short.TryParse(splited[3], out amount))
                    {
                        if (amount > item.maxStack)
                        {
                            amount = item.maxStack;
                        }
                        CharacterItem newItem = CharacterItem.Create(item, 1, amount);
                        playerCharacter.NonEquipItems.Add(newItem);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        protected virtual void ReadChatMessage(ChatMessage message)
        {
            long senderConnectionId;
            long receiverConnectionId;
            BasePlayerCharacterEntity playerCharacter;

            switch (message.channel)
            {
            case ChatChannel.Local:
                if (!string.IsNullOrEmpty(message.sender) &&
                    TryGetPlayerCharacterByName(message.sender, out playerCharacter))
                {
                    // TODO: Don't use fixed user level
                    if (GMCommands.IsGMCommand(message.message) && playerCharacter.UserLevel > 0)
                    {
                        // If it's gm command and sender's user level > 0, handle gm commands
                        HandleGMCommand(message.sender, message.message);
                    }
                    else
                    {
                        // Send messages to nearby characters
                        List <BasePlayerCharacterEntity> receivers = playerCharacter.FindCharacters <BasePlayerCharacterEntity>(gameInstance.localChatDistance, false, true, true, true);
                        foreach (BasePlayerCharacterEntity receiver in receivers)
                        {
                            ServerSendPacket(receiver.ConnectionId, DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                        }
                        // Send messages to sender
                        ServerSendPacket(playerCharacter.ConnectionId, DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                    }
                }
                break;

            case ChatChannel.Global:
                if (!string.IsNullOrEmpty(message.sender))
                {
                    // Send message to all clients
                    ServerSendPacketToAllConnections(DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                }
                break;

            case ChatChannel.Whisper:
                if (!string.IsNullOrEmpty(message.sender) &&
                    connectionIdsByCharacterName.TryGetValue(message.sender, out senderConnectionId))
                {
                    // If found sender send whisper message to sender
                    ServerSendPacket(senderConnectionId, DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                }
                if (!string.IsNullOrEmpty(message.receiver) &&
                    connectionIdsByCharacterName.TryGetValue(message.receiver, out receiverConnectionId))
                {
                    // If found receiver send whisper message to receiver
                    ServerSendPacket(receiverConnectionId, DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                }
                break;

            case ChatChannel.Party:
                PartyData party;
                if (parties.TryGetValue(message.channelId, out party))
                {
                    foreach (string memberId in party.GetMemberIds())
                    {
                        if (TryGetPlayerCharacterById(memberId, out playerCharacter) &&
                            ContainsConnectionId(playerCharacter.ConnectionId))
                        {
                            // If party member is online, send party message to the member
                            ServerSendPacket(playerCharacter.ConnectionId, DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                        }
                    }
                }
                break;

            case ChatChannel.Guild:
                GuildData guild;
                if (guilds.TryGetValue(message.channelId, out guild))
                {
                    foreach (string memberId in guild.GetMemberIds())
                    {
                        if (TryGetPlayerCharacterById(memberId, out playerCharacter) &&
                            ContainsConnectionId(playerCharacter.ConnectionId))
                        {
                            // If guild member is online, send guild message to the member
                            ServerSendPacket(playerCharacter.ConnectionId, DeliveryMethod.ReliableOrdered, MsgTypes.Chat, message);
                        }
                    }
                }
                break;
            }
        }