Beispiel #1
0
        void BuyFromTravelingMerchant(string service, string channel, string username, Player player, Item item, int quantity, double discount)
        {
            User user = context.GetModule <UserModule>().GetExistingUser(service, username);

            int price = (int)(item.Value * (travelingmerchant.Price - discount));

            if (price > player.Gold)
            {
                context.GetModule <StreamModule>().SendMessage(service, channel, username, $"Sadly you don't have enough gold. You would need {price} gold to buy {item.GetCountName(quantity)}");
                return;
            }

            AddInventoryItemResult result = context.GetModule <InventoryModule>().AddItem(player.UserID, item.ID, quantity);

            switch (result)
            {
            case AddInventoryItemResult.Success:
            case AddInventoryItemResult.SuccessFull:
                Aggregate agg = Aggregate.Max(Constant.Create(0), EntityField.Create <Player>(pl => pl.Gold));
                context.Database.Update <Player>().Set(p => p.Gold == agg.Int - price).Where(p => p.UserID == player.UserID).Execute();


                RPGMessageBuilder message = context.GetModule <RPGMessageModule>().Create().User(user).Text(" bought ").Item(item, quantity).Text(" and spent ").Gold(price);

                if (result == AddInventoryItemResult.SuccessFull)
                {
                    message.Text(" and is now encumbered.");
                }
                else
                {
                    message.Text(".");
                }

                message.Send();
                break;

            case AddInventoryItemResult.InventoryFull:
                context.GetModule <StreamModule>().SendMessage(service, channel, username, $"You don't have room in your inventory to buy {quantity} {item.Name}");
                break;
            }
        }
        void ExecuteChest(Player player)
        {
            double chance = 0.8;

            List <FoundItem> items = new List <FoundItem>();

            while (RNG.XORShift64.NextDouble() < chance)
            {
                Item item = context.GetModule <ItemModule>().SelectItem(player.Level, player.Luck * 2);
                if (item == null)
                {
                    return;
                }

                FoundItem found = items.FirstOrDefault(i => i.Item.ID == item.ID);
                if (found == null)
                {
                    found = new FoundItem {
                        Item = item
                    };
                    items.Add(found);
                }

                if (item.Type == ItemType.Gold)
                {
                    found.Quantity += 1 + player.Luck * 30 + RNG.XORShift64.NextInt((int)Math.Max(1, player.Level * player.Luck * 0.75));
                }
                else
                {
                    ++found.Quantity;
                }

                chance *= 0.8;
            }

            User user = context.GetModule <UserModule>().GetUser(player.UserID);

            if (items.Count == 0)
            {
                context.GetModule <RPGMessageModule>().Create().User(user).Text(" has found a chest ... which was empty.").Emotion(EmotionType.FuckYou).Send();
                return;
            }

            List <FoundItem>       dropped   = new List <FoundItem>();
            AddInventoryItemResult allresult = AddInventoryItemResult.Success;

            for (int i = items.Count - 1; i >= 0; --i)
            {
                FoundItem item = items[i];

                if (item.Item.Type == ItemType.Gold)
                {
                    context.GetModule <PlayerModule>().UpdateGold(player.UserID, item.Quantity);
                    continue;
                }

                AddInventoryItemResult result = context.GetModule <InventoryModule>().AddItem(player.UserID, item.Item.ID, item.Quantity);
                if (result == AddInventoryItemResult.InvalidItem)
                {
                    continue;
                }

                if (result > allresult)
                {
                    allresult = result;
                }

                if (result == AddInventoryItemResult.InventoryFull)
                {
                    context.GetModule <ShopModule>().AddItem(item.Item.ID, item.Quantity);
                    dropped.Add(item);
                    items.RemoveAt(i);
                }
            }

            RPGMessageBuilder message = context.GetModule <RPGMessageModule>().Create();

            message.User(user).Text(" opened a chest ");


            if (items.Count > 0)
            {
                if (dropped.Count == 0 && allresult <= AddInventoryItemResult.Success)
                {
                    message.Text(" and found");
                }
                else
                {
                    message.Text(" found");
                }

                for (int i = 0; i < items.Count; ++i)
                {
                    if (i == 0)
                    {
                        message.Text(" ");
                    }
                    else if (i == items.Count - 1)
                    {
                        message.Text(" and ");
                    }
                    else
                    {
                        message.Text(", ");
                    }

                    FoundItem item = items[i];

                    message.Item(item.Item, item.Quantity);
                }
            }

            if (dropped.Count > 0)
            {
                if (allresult > AddInventoryItemResult.Success)
                {
                    message.Text(", dropped");
                }
                else
                {
                    message.Text(" and dropped");
                }

                for (int i = 0; i < dropped.Count; ++i)
                {
                    if (i == 0)
                    {
                        message.Text(" ");
                    }
                    else if (i == items.Count - 1)
                    {
                        message.Text(" and ");
                    }
                    else
                    {
                        message.Text(", ");
                    }

                    FoundItem item = dropped[i];

                    message.Item(item.Item, item.Quantity);
                }
            }

            if (allresult > AddInventoryItemResult.Success)
            {
                message.Text(" and is now encumbered.");
            }
            else
            {
                message.Text(".");
            }

            message.Send();

            foreach (FoundItem item in items)
            {
                context.GetModule <AdventureModule>().TriggerItemFound(player.UserID, item.Item.ID, item.Quantity);
            }
        }
Beispiel #3
0
        public void Buy(string service, string channel, string username, string[] arguments)
        {
            int argumentindex = 0;
            int quantity      = arguments.RecognizeQuantity(ref argumentindex);

            if (arguments.Length <= argumentindex)
            {
                context.GetModule <StreamModule>().SendMessage(service, channel, username, "You have to specify the name of the item to buy.");
                return;
            }

            Item item = context.GetModule <ItemModule>().RecognizeItem(arguments, ref argumentindex);

            if (item == null)
            {
                context.GetModule <StreamModule>().SendMessage(service, channel, username, "I don't understand what exactly you want to buy.");
                return;
            }

            if (quantity == -1)
            {
                quantity = 1;
            }

            if (quantity <= 0)
            {
                context.GetModule <StreamModule>().SendMessage(service, channel, username, $"It wouldn't make sense to buy {item.GetCountName(quantity)}.");
                return;
            }

            bool insulted = messageevaluator.HasInsult(arguments, argumentindex);

            User   user   = context.GetModule <UserModule>().GetExistingUser(service, username);
            Player player = context.GetModule <PlayerModule>().GetPlayer(service, username);

            double discount = GetDiscount(context.GetModule <SkillModule>().GetSkillLevel(player.UserID, SkillType.Haggler));

            if (insulted)
            {
                discount -= 0.2;
            }

            int price;

            if (item.Type == ItemType.Special)
            {
                price = GetSpecialPriceToBuy(item.Value, discount);
            }
            else
            {
                ShopItem shopitem = context.Database.LoadEntities <ShopItem>().Where(i => i.ItemID == item.ID).Execute().FirstOrDefault();
                if (shopitem == null || shopitem.Quantity < quantity)
                {
                    if (travelingmerchant != null && travelingmerchant.Type == item.Type)
                    {
                        if (item.LevelRequirement > player.Level && !insulted)
                        {
                            context.GetModule <StreamModule>().SendMessage(service, channel, username, $"The merchant denies you access to the item as you wouldn't be able to use it. (Level {item.LevelRequirement})");
                            return;
                        }

                        BuyFromTravelingMerchant(service, channel, username, player, item, quantity, discount);
                        return;
                    }

                    if (shopitem == null || shopitem.Quantity == 0)
                    {
                        context.GetModule <StreamModule>().SendMessage(service, channel, username, "The shopkeeper tells you that he has nothing on stock.");
                    }
                    else
                    {
                        context.GetModule <StreamModule>().SendMessage(service, channel, username, $"The shopkeeper tells you that he only has {item.GetCountName(shopitem.Quantity)}.");
                    }
                    return;
                }

                if (!insulted)
                {
                    if (Mood < 0.5)
                    {
                        context.GetModule <StreamModule>().SendMessage(service, channel, username, "The shopkeeper does not see a point in doing anything anymore.");
                        return;
                    }

                    if (context.Database.Load <ShopQuirk>(DBFunction.Count).Where(q => q.Type == ShopQuirkType.Phobia && q.ID == item.ID).ExecuteScalar <int>() > 0)
                    {
                        context.GetModule <RPGMessageModule>().Create().ShopKeeper().Text(" tells ").User(user).Text(" that no one should mess with ").ItemMultiple(item).Text(" since it is devil's work.").Send();
                        return;
                    }

                    if (context.Database.Load <ShopQuirk>(DBFunction.Count).Where(q => q.Type == ShopQuirkType.Nerd && q.ID == item.ID).ExecuteScalar <int>() > 0)
                    {
                        context.GetModule <RPGMessageModule>().Create().ShopKeeper().Text(" does not want to share his beloved ").ItemMultiple(item).Text(".").Send();
                        return;
                    }

                    if (context.Database.Load <ShopQuirk>(DBFunction.Count).Where(q => q.Type == ShopQuirkType.Grudge && q.ID == player.UserID).ExecuteScalar <int>() > 0)
                    {
                        context.GetModule <RPGMessageModule>().Create().ShopKeeper().Text(" casually ignores ").User(user).Text(".").Send();
                        return;
                    }
                }

                if (item.LevelRequirement > player.Level && !insulted)
                {
                    context.GetModule <StreamModule>().SendMessage(service, channel, username, $"The shopkeeper denies you access to the item as you wouldn't be able to use it. (Level {item.LevelRequirement})");
                    return;
                }

                price = (int)(item.Value * quantity * (1.0 + GetQuantityFactor(shopitem.Quantity) * 2 - discount) * shopitem.Discount);
            }

            if (price > player.Gold)
            {
                context.GetModule <StreamModule>().SendMessage(service, channel, username, $"Sadly you don't have enough gold. You would need {price} gold to buy {item.GetCountName(quantity)}");
                return;
            }

            AddInventoryItemResult result = context.GetModule <InventoryModule>().AddItem(player.UserID, item.ID, quantity);

            switch (result)
            {
            case AddInventoryItemResult.Success:
            case AddInventoryItemResult.SuccessFull:
                Aggregate agg = Aggregate.Max(Constant.Create(0), EntityField.Create <Player>(pl => pl.Gold));
                context.Database.Update <Player>().Set(p => p.Gold == agg.Int - price).Where(p => p.UserID == player.UserID).Execute();

                if (item.Type != ItemType.Special)
                {
                    context.Database.Update <ShopItem>().Set(i => i.Quantity == i.Quantity - quantity).Where(i => i.ItemID == item.ID).Execute();
                }

                RPGMessageBuilder message = context.GetModule <RPGMessageModule>().Create();
                message.User(user).Text(" has bought ").Item(item, quantity).Text(" from ").ShopKeeper().Text(" for ").Gold(price).Text(".");

                if (result == AddInventoryItemResult.SuccessFull)
                {
                    message.User(user).Text(" is now encumbered.");
                }

                message.Send();
                Mood += 0.05 * (1.0 - discount);
                break;

            case AddInventoryItemResult.InventoryFull:
                context.GetModule <RPGMessageModule>().Create().User(user).Text(" carries to much to buy ").Item(item, quantity).Text(".").Send();
                break;
            }
        }