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); }
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() }); }
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() ); }
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); }