Example #1
0
        /// <summary>
        /// Send mail to another <see cref="Player"/>.
        /// </summary>
        public void SendMail(ClientMailSend mailSend)
        {
            player.Session.EnqueueEvent(new TaskGenericEvent <Character>(CharacterDatabase.GetCharacterByName(mailSend.Name),
                                                                         targetCharacter =>
            {
                var items = new List <Item>();
                GenericError GetResult()
                {
                    if (targetCharacter == null)
                    {
                        return(GenericError.MailCannotFindPlayer);
                    }

                    if (targetCharacter.Id == player.CharacterId)
                    {
                        return(GenericError.MailCannotMailSelf);
                    }

                    // TODO: Check that the player is not blocked

                    if (mailSend.CreditsRequested > 0ul && mailSend.CreditsSent > 0ul)
                    {
                        return(GenericError.MailCanNotHaveCoDAndGift);
                    }

                    if (mailSend.CreditsRequested > 0ul && mailSend.Items.All(i => i == 0ul))
                    {
                        return(GenericError.MailFailedToCreate);
                    }

                    if (mailSend.Items.Any(i => i != 0ul))
                    {
                        if (!IsTargetMailBoxInRange(mailSend.UnitId))
                        {
                            return(GenericError.MailMailBoxOutOfRange);
                        }

                        foreach (ulong itemGuid in mailSend.Items.Where(i => i != 0ul))
                        {
                            Item item = player.Inventory.GetItem(itemGuid);
                            if (item == null)
                            {
                                return(GenericError.MailInvalidInventorySlot);
                            }

                            // TODO: Check the Item can be traded.
                            items.Add(item);
                        }
                    }

                    uint cost = CalculateMailCost(mailSend.DeliveryTime, items);
                    if (!player.CurrencyManager.CanAfford(CurrencyType.Credits, cost))
                    {
                        return(GenericError.MailInsufficientFunds);
                    }

                    if (!player.CurrencyManager.CanAfford(CurrencyType.Credits, mailSend.CreditsSent))
                    {
                        return(GenericError.MailInsufficientFunds);
                    }

                    return(GenericError.Ok);
                }

                GenericError result = GetResult();
                if (result == GenericError.Ok)
                {
                    var parameters = new MailParameters
                    {
                        RecipientCharacterId = targetCharacter.Id,
                        SenderCharacterId    = player.CharacterId,
                        MessageType          = SenderType.Player,
                        Subject      = mailSend.Subject,
                        Body         = mailSend.Message,
                        MoneyToGive  = mailSend.CreditsSent,
                        CodAmount    = mailSend.CreditsRequested,
                        DeliveryTime = mailSend.DeliveryTime
                    };

                    foreach (Item item in items)
                    {
                        player.Inventory.ItemRemove(item);
                    }

                    SendMail(parameters, items);

                    uint cost = CalculateMailCost(mailSend.DeliveryTime, items);
                    player.CurrencyManager.CurrencySubtractAmount(CurrencyType.Credits, cost);

                    if (mailSend.CreditsSent > 0ul)
                    {
                        player.CurrencyManager.CurrencySubtractAmount(CurrencyType.Credits, mailSend.CreditsSent);
                    }
                }

                player.Session.EnqueueMessageEncrypted(new ServerMailResult
                {
                    Action = 1,
                    MailId = 0,
                    Result = result
                });
            }));
        }