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