public static void TryInvest(Money amountToInvest, int kelly, Member user) { if (amountToInvest <= Money.Zero) { throw new MsgException(U4200.INVESTMENTTOOLOW); } if (amountToInvest > user.PurchaseBalance) { throw new MsgException(L1.NOTENOUGHFUNDS); } if (kelly <= 0 || kelly > AppSettings.DiceGame.MaxKellyLevelInt) { throw new MsgException(U4200.KELLYERROR + ": " + AppSettings.DiceGame.MaxKellyLevelInt); } user.SubtractFromPurchaseBalance(amountToInvest, "Dice Game Investment"); SiteInvestment Investment = new SiteInvestment(); Investment.UserId = user.Id; Investment.Amount = amountToInvest; Investment.KellyInt = kelly; Investment.OperationDate = DateTime.Now; Investment.Save(); user.SaveBalances(); //might be used in future //AppSettings.DiceGame.MaxBitCoinProfit = DiceGameManager.GetMaxProfit(); //AppSettings.Save(); }
public static void TryDivest(Money amountToDivest, int?kelly, Member user) { Money _amountToDivest; Money totalInvestment; if (amountToDivest == null || kelly == null) { _amountToDivest = totalInvestment = GetInvestments(user, null); } else { if (kelly <= 0 || kelly > AppSettings.DiceGame.MaxKellyLevel) { throw new MsgException(U4200.KELLYERROR + ' ' + AppSettings.DiceGame.MaxKellyLevel); } _amountToDivest = amountToDivest; totalInvestment = GetInvestments(user, kelly); } if (totalInvestment == Money.Zero || _amountToDivest > totalInvestment) { throw new MsgException(U4200.DIVESTTOOHIGH); } else if (_amountToDivest <= Money.Zero) { throw new MsgException(U4200.DIVESTTOOLOW); } user.AddToMainBalance(_amountToDivest, "Dice Game Divestment", BalanceLogType.Other); var siteInvestments = SiteInvestment.GetAll(user.Id); int i = 0; while (_amountToDivest > Money.Zero && i < siteInvestments.Count) { if (kelly == null || kelly == siteInvestments[i].KellyInt) { if (_amountToDivest > siteInvestments[i].Amount) { siteInvestments[i].Amount -= siteInvestments[i].Amount; _amountToDivest -= siteInvestments[i].Amount; } else { siteInvestments[i].Amount -= _amountToDivest; _amountToDivest = Money.Zero; } siteInvestments[i].OperationDate = DateTime.Now; siteInvestments[i].Save(); if (siteInvestments[i].Amount == Money.Zero) { siteInvestments[i].Delete(); } } i++; } user.SaveBalances(); }