示例#1
0
        public void OfferSelected(User user, ReceivedMessage message)
        {
            var splitted = message.Text.Split(' ');

            if (splitted.Length < 2)
            {
                goto SomethingWrong;
            }

            var action     = splitted[0];
            var itemName   = string.Join(" ", splitted.Skip(1));
            var itemManger = RoomBase.GetAllItems();
            var item       = itemManger.Keys()
                             .Select(id => itemManger.Get(id))
                             .SingleOrDefault(i => i != null && i.Name == itemName);

            if (item == null)
            {
                goto SomethingWrong;
            }

            var offers = Offers.Load();

            if (!offers.TryGetValue(item.Identifier, out var offer))
            {
                goto SomethingWrong;
            }

            var state = new BuySellState
            {
                SelectedItemId = item.Identifier
            };

            switch (action)
            {
            case "Продать":
            {
                state.Selling = true;
                break;
            }

            case "Купить":
            {
                state.Selling = false;
                break;
            }

            default:
                goto SomethingWrong;
            }

            Room.GetRoomVariables(user).Set("state", StateContainer.Serialize(state));
            Room.SwitchAction <AuctionRoom.ItemOffersAction>(user);
            Room.GetAction <AuctionRoom.ItemOffersAction>().Enter(user);

            return;

SomethingWrong:
            Enter(user);
        }
示例#2
0
        public void Enter(User user)
        {
            var offers  = Offers.Load();
            var message = new StringBuilder();
            var buttons = new List <string[]>();

            foreach (var itemInfo in user.ItemManager.Items)
            {
                if (itemInfo.Item.Group.Intersects(new StructFlag <BuyGroup>(BuyGroup.NotSellable)))
                {
                    continue;
                }

                var itemId = itemInfo.Identifier;

                if (!offers.TryGetValue(itemId, out var itemOffers))
                {
                    itemOffers = new ItemOffer(new List <Offer>(), new List <Offer>());
                }

                Offer Filter(IEnumerable <Offer> offs) => offs.SingleOrDefault(offer => offer.UserId.Equals(user.Info.UserId));

                var myBuy  = Filter(itemOffers.BuyOffers);
                var mySell = Filter(itemOffers.SellOffers);
                var btnRow = new List <string>
                {
                    myBuy != null ? $"Не покупать {itemInfo.Item.Name}" : $"Купить {itemInfo.Item.Name}",
                    mySell != null ? $"Не продавать {itemInfo.Item.Name}" : $"Продать {itemInfo.Item.Name}"
                };
                message.AppendLine($"{itemInfo.Item.Name} x {itemInfo.Count}");
                buttons.Add(btnRow.ToArray());
            }

            Room.SendMessage(user, message.ToString(), buttons.Concat(new[] { new[] { "Назад" } }).ToArray());
        }
示例#3
0
        public void OfferSelected(User user, ReceivedMessage message)
        {
            var state = StateContainer.Deserialize <BuySellState>(
                Room.GetRoomVariables(user).Get <VariableContainer>("state")
                );
            var offers = Offers.Load();

            var(itemOffers, _) = GetOffers(offers, state);
            if (state.SelectedItemCount == null)
            {
                // Selecting price1
                if (!decimal.TryParse(message.Text, out var price))
                {
                    Enter(user);
                    return;
                }

                var matchingOffers = itemOffers.Where(o => o.Price == price).ToList();
                state.SelectedItemCount = matchingOffers.Select(o => o.Count).Sum();
                state.SelectedItemPrice = price;
                Room.GetRoomVariables(user).Set("state", StateContainer.Serialize(state));
                Room.SendMessage(user,
                                 $"Введите количество предметов (от 0 до {state.SelectedItemCount})",
                                 new[] { new[] { "0" } });
            }
            else
            {
                // Selecting count
                if (!int.TryParse(message.Text, out var count) || count < 0 || count > state.SelectedItemCount)
                {
                    Room.SendMessage(user,
                                     $"Введите количество предметов (от 0 до {state.SelectedItemCount})",
                                     new[] { new[] { "0" } });
                    return;
                }

                if (count == 0)
                {
                    // Looks like cancel
                    Back(user, message);
                    return;
                }

                var matchingOffers = itemOffers
                                     .Where(o => o.Price == state.SelectedItemPrice)
                                     .OrderBy(o => o.Created)
                                     .ToList();

                var buyedCount = Buy(matchingOffers, user, count);
                Room.SendMessage(user, $"Вы купили {buyedCount} штук!");

                offers.Save();
                Back(user, message);
            }

            Room.GetRoomVariables(user).Set("state", StateContainer.Serialize(state));
        }
示例#4
0
        public void Fallback(User user, ReceivedMessage message)
        {
            if (int.TryParse(message.Text, out var quantity))
            {
                var state = StateContainer.Deserialize <AddOfferState>(
                    Room.GetRoomVariables(user).Get <VariableContainer>("state")
                    );
                if (quantity <= state.MaxQuantityAvailable && quantity >= 0)
                {
                    state.QuantitySelected = quantity;
                    Room.GetRoomVariables(user).Set("state", StateContainer.Serialize(state));

                    if (state.QuantitySelected != 0)
                    {
                        var offers = Offers.Load();
                        if (!offers.TryGetValue(state.SelectedItemId, out var itemOffers))
                        {
                            itemOffers = new ItemOffer(new List <Offer>(), new List <Offer>());
                            offers[state.SelectedItemId] = itemOffers;
                        }

                        var offer = new Offer(
                            user.Info.UserId,
                            state.SelectedItemPrice,
                            state.QuantitySelected,
                            DateTimeOffset.Now,
                            state.SelectedItemId
                            );

                        if (state.Selling)
                        {
                            if (user.ItemManager.Remove(new ItemInfo(offer.ItemId, offer.Count)))
                            {
                                itemOffers.SellOffers.Add(offer);
                            }
                        }
                        else
                        {
                            if (user.Info.TryDecreaseGold(state.SelectedItemPrice * state.QuantitySelected))
                            {
                                itemOffers.BuyOffers.Add(offer);
                            }
                        }

                        offers.Save();
                    }

                    Room.SwitchAction <AuctionRoom.AddOfferAction>(user);
                    Room.GetAction <AuctionRoom.AddOfferAction>().Enter(user);
                    return;
                }
            }

            // Something wrong
            Enter(user);
        }
示例#5
0
        public static Offers Deserialize(VariableContainer items)
        {
            var result = new Offers();

            foreach (var key in items.Keys())
            {
                var offer = items.Get <VariableContainer>(key);
                result[key] = ItemOffer.Deserialize(offer);
            }

            return(result);
        }
示例#6
0
        public void Enter(User user)
        {
            var offers      = Offers.Load();
            var message     = new StringBuilder();
            var itemManager = RoomBase.GetAllItems();
            var buttons     = new List <string[]>(offers.Count);

            foreach (var kv in offers)
            {
                var item       = itemManager.Get(kv.Key);
                var sellPrices = FindMinMax(kv.Value.SellOffers);
                var buyPrices  = FindMinMax(kv.Value.BuyOffers);
                if (sellPrices == null && buyPrices == null ||
                    item == null)
                {
                    continue;
                }

                message.Append($"{item.Name}");
                var buttonRow = new List <string>(2);
                if (sellPrices != null)
                {
                    message.Append(sellPrices.Item1 != sellPrices.Item2
                        ? $"; продают от {sellPrices.Item1.Format()} до {sellPrices.Item2.Format()}"
                        : $"; продают за {sellPrices.Item1.Format()}");
                    buttonRow.Add($"Купить {item.Name}");
                }

                if (buyPrices != null)
                {
                    message.Append(buyPrices.Item1 != buyPrices.Item2
                        ? $"; покупают от {buyPrices.Item1.Format()} до {buyPrices.Item2.Format()}"
                        : $"; покупают за {buyPrices.Item1.Format()}");
                    buttonRow.Add($"Продать {item.Name}");
                }

                buttons.Add(buttonRow.ToArray());

                message.AppendLine();
            }

            user.MessageManager.SendMessage(new SentMessage
            {
                Text    = message.ToString(),
                Buttons = buttons
                          .Concat(new[] { new[] { "Назад" } })
                          .ToArray()
            });
        }
示例#7
0
        private (List <Offer>, string) GetOffers(Offers offers, BuySellState state)
        {
            var item = RoomBase.GetAllItems().Get(state.SelectedItemId);

            if (item == null)
            {
                return(null, null);
            }

            if (!offers.TryGetValue(item.Identifier, out var itemOffer))
            {
                return(null, item.Name);
            }

            return(state.Selling ? itemOffer.BuyOffers : itemOffer.SellOffers, item.Name);
        }
        public void Enter(User user)
        {
            var buyOffers = Offers.Load()
                            .SelectMany(kv => kv.Value.BuyOffers)
                            .Where(offer => offer.UserId.Equals(user.Info.UserId));
            var sellOffers = Offers.Load()
                             .SelectMany(kv => kv.Value.SellOffers)
                             .Where(offer => offer.UserId.Equals(user.Info.UserId));

            var message = new StringBuilder();
            var buttons = new List <string>();
            var itemMgr = RoomBase.GetAllItems();

            foreach (var offer in buyOffers)
            {
                var item = itemMgr.Get(offer.ItemId);
                if (item == null)
                {
                    continue;
                }

                message.AppendLine($"{item.Name}: беру {offer.Count} шт за {offer.Price.Format()} каждая");
                buttons.Add($"Покупаю {item.Name}");
            }

            foreach (var offer in sellOffers)
            {
                var item = itemMgr.Get(offer.ItemId);
                if (item == null)
                {
                    continue;
                }

                message.AppendLine($"{item.Name}: продаю {offer.Count} шт за {offer.Price.Format()} каждая");
                buttons.Add($"Продаю {item.Name}");
            }

            Room.SendMessage(user, message.ToString(), buttons
                             .Concat(new[] { "Назад" })
                             .Select(msg => new[] { msg })
                             .ToArray()
                             );
        }
示例#9
0
        public void Enter(User user)
        {
            var state = StateContainer.Deserialize <BuySellState>(
                Room.GetRoomVariables(user).Get <VariableContainer>("state")
                );

            var(itemOffers, itemName) = GetOffers(Offers.Load(), state);
            if (itemOffers == null || itemName == null)
            {
                Back(user, null);
                return;
            }

            var prices = new SortedDictionary <decimal, List <int> >();

            foreach (var offer in itemOffers.OrderBy(o => o.Created))
            {
                if (prices.TryGetValue(offer.Price, out var price))
                {
                    price.Add(offer.Count);
                }
                else
                {
                    prices[offer.Price] = new List <int> {
                        offer.Count
                    };
                }
            }

            var messageOffers = string.Join("\n", prices.Select(
                                                kv => $"{kv.Key} золота: {string.Join("+", kv.Value.OrderBy(i => i))} шт."
                                                ));
            var message = $"<b>{itemName}</b>\n{messageOffers}";

            var buttons = prices
                          .Select(kv => new[] { kv.Key.Format() })
                          .Concat(new[] { new[] { "Назад" } })
                          .ToArray();

            Room.SendMessage(user, message, buttons);
        }
示例#10
0
        public void ItemSelected(User user, ReceivedMessage message)
        {
            var splitted = message.Text.Split(' ');

            var    action = splitted[0];
            string itemName;

            if (action == "Не")
            {
                action  += " " + splitted[1];
                itemName = string.Join(" ", splitted.Skip(2));
            }
            else
            {
                itemName = string.Join(" ", splitted.Skip(1));
            }

            var itemMgr = RoomBase.GetAllItems();
            var item    = itemMgr.Keys().Select(id => itemMgr.Get(id)).SingleOrDefault(i => i?.Name == itemName);

            if (item == null)
            {
                goto SomethingWrong;
            }

            var offers = Offers.Load();

            offers.TryGetValue(item.Identifier, out var itemOffers);

            var sellOffer = itemOffers?.SellOffers.SingleOrDefault(o => o.UserId.Equals(user.Info.UserId));
            var buyOffer  = itemOffers?.SellOffers.SingleOrDefault(o => o.UserId.Equals(user.Info.UserId));

            switch (action)
            {
            case "Не покупать":
            {
                if (buyOffer == null)
                {
                    goto SomethingWrong;
                }

                user.Info.Gold += buyOffer.Price * buyOffer.Count;
                itemOffers.BuyOffers.Remove(buyOffer);
                break;
            }

            case "Не продавать":
            {
                if (sellOffer == null)
                {
                    goto SomethingWrong;
                }

                user.ItemManager.Add(new ItemInfo(sellOffer.ItemId, sellOffer.Count));
                itemOffers.SellOffers.Remove(sellOffer);
                break;
            }

            case "Купить":
            {
                if (buyOffer != null)
                {
                    goto SomethingWrong;
                }

                var state = new AddOfferState
                {
                    Selling              = false,
                    SelectedItemId       = item.Identifier,
                    MaxQuantityAvailable = int.MaxValue
                };

                Room.GetRoomVariables(user).Set("state", StateContainer.Serialize(state));

                Room.SwitchAction <AuctionRoom.PriceGroupSelectionAction>(user);
                Room.GetAction <AuctionRoom.PriceGroupSelectionAction>().Enter(user);
                break;
            }

            case "Продать":
            {
                if (sellOffer != null)
                {
                    goto SomethingWrong;
                }

                var state = new AddOfferState
                {
                    Selling              = true,
                    SelectedItemId       = item.Identifier,
                    MaxQuantityAvailable = user.ItemManager.Get(item.Identifier)?.Count ?? 0
                };
                Room.GetRoomVariables(user).Set("state", StateContainer.Serialize(state));

                Room.SwitchAction <AuctionRoom.PriceGroupSelectionAction>(user);
                Room.GetAction <AuctionRoom.PriceGroupSelectionAction>().Enter(user);
                break;
            }

            default:
            {
                goto SomethingWrong;
            }
            }

            return;

SomethingWrong:
            Logger.Warn("Something went wrong when adding offer");
            Enter(user);
        }
        public void OfferSelected(User user, ReceivedMessage message)
        {
            var splitted = message.Text.Split(' ');

            if (splitted.Length < 2)
            {
                Enter(user);
                return;
            }

            var action   = splitted[0];
            var itemName = string.Join(" ", splitted.Skip(1));
            var itemMgr  = RoomBase.GetAllItems();
            var item     = itemMgr.Keys()
                           .Select(key => itemMgr.Get(key))
                           .SingleOrDefault(i => i?.Name == itemName);

            if (item == null)
            {
                goto SomethingWrong;
            }

            var offers = Offers.Load();

            if (!offers.TryGetValue(item.Identifier, out var itemOffers))
            {
                // No such item, so no offers to remove
                goto SomethingWrong;
            }

            Offer neededOffer;

            Offer Filter(IEnumerable <Offer> offr) => offr
            .SingleOrDefault(offer =>
                             offer.UserId.Equals(user.Info.UserId) &&
                             offer.ItemId == item.Identifier
                             );

            var selling = false;

            switch (action)
            {
            case "Покупаю":
            {
                neededOffer = Filter(itemOffers.BuyOffers);
                break;
            }

            case "Продаю":
            {
                neededOffer = Filter(itemOffers.SellOffers);
                selling     = true;
                break;
            }

            default:
                goto SomethingWrong;
            }

            if (neededOffer == null)
            {
                goto SomethingWrong;
            }

            if (selling)
            {
                user.ItemManager.Add(new ItemInfo(neededOffer.ItemId, neededOffer.Count));
            }
            else
            {
                user.Info.Gold += neededOffer.Count * neededOffer.Price;
            }

            neededOffer.Count = 0;
            offers.Save();

SomethingWrong:
            Enter(user);
        }