private void UpdateForCardsYouGet(TradeBot bot, Trade trade, IEnumerable<MagicCard> youGet) { using (IConnection connection = Connection.NewConnection()) { connection.BeginTransaction(); foreach (var mCard in youGet) { var magicCard = mCard; var card = _cardRepository.Get(magicCard); Logger.Trace("You get - " + magicCard + " OwnedAmount: " + card.OwnedAmount); card.OwnedAmount += magicCard.CopiesOfCard; card.Save(connection); var botCard = _cardRepository.GetCardForBot(bot.Id, magicCard.Id); if (botCard == null) { botCard = new BotCard {BotId = bot.Id, CardId = magicCard.Id}; botCard.Save(connection); botCard.OwnedAmount = 0; } botCard.OwnedAmount += magicCard.CopiesOfCard; botCard.Save(connection); var detail = new TradeDetail { Buying = true, CardId = magicCard.Id, NumberOfCard = magicCard.CopiesOfCard, Price = magicCard.BuyPrice, TradeId = trade.TradeId }; detail.Save(connection); } connection.CommitTransaction(); } }
private void UpdateForCardsYouGive(TradeBot bot, IEnumerable<MagicCard> youGive, Trade trade) { using (IConnection connection = Connection.NewConnection()) { connection.BeginTransaction(); foreach (var mCard in youGive) { var magicCard = mCard; Logger.Trace("You give - " + magicCard + " OwnedAmount: " + magicCard.OwnedAmount); magicCard.OwnedAmount -= magicCard.CopiesOfCard; magicCard.Save(connection); var botCard = _cardRepository.GetCardForBot(bot.Id, magicCard.Id); botCard.OwnedAmount -= magicCard.CopiesOfCard; botCard.Save(connection); var detail = new TradeDetail { Buying = false, CardId = magicCard.Id, NumberOfCard = magicCard.CopiesOfCard, Price = magicCard.SellPrice }; detail.Save(connection); } connection.CommitTransaction(); } }
public void SaveCompletedTradeLog(string botName, string userName, List<MagicCard> youGet, List<MagicCard> youGive, decimal creditBeforeTrade, decimal creditAfterTrade) { Logger.TraceFormat("Botname: {0}, CreditBeforeTrade: {1}, CreditAfterTrade: {2}", botName, creditBeforeTrade, creditAfterTrade); var bot = _botRepository.GetBot(botName); var tradee = GetTradee(userName); var trade = new Trade { BotId = bot.Id, CreditBeforeTrade = creditBeforeTrade, TimeOfTrade = DateTime.Now, TradeeId = tradee.Id }; UpdateForCardsYouGive(bot, youGive, trade); UpdateForCardsYouGet(bot, trade, youGet); var tradeCredit = tradee.Credits.First(p => p.BotGroupId == bot.GroupId); tradeCredit.CreditAmount = creditAfterTrade; tradeCredit.Save(); }