public Money Credit(PtcAdvert ad) { Money calculated = ad.MoneyToClaimAsCashLink; user.AddToMainBalance(calculated, "CashLink", BalanceLogType.CashLinkClick); user.IncreaseEarnings(calculated); //OK mark as watched and credit List <int> av = user.AdsViewed; av.Add(ad.Id); user.AdsViewed = av; user.LastActive = DateTime.Now; Money NewTotalEarned = (user.TotalEarned + calculated); user.TryToAddAchievements( Prem.PTC.Achievements.Achievement.GetProperAchievements( Prem.PTC.Achievements.AchievementType.AfterEarning, NewTotalEarned.GetRealTotals())); user.IncreaseUserStatClicks(1); user.IncreaseStatClicks(1); user.IncreaseCashLinksEarnings(calculated); user.Save(); DailyPoolManager.AddToPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn), DateTime.Now, calculated); AdPackProfitDistributionHistory.Add(calculated); return(calculated); }
/// <summary> /// Add to N next pools, amount/n, starting from date /// </summary> /// <param name="type"></param> /// <param name="date"></param> /// <param name="amount"></param> /// <param name="parts"></param> public static void AddToPool(int poolId, DateTime date, Money amount) { int n = 1; //default value if (poolId == PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn)) { n = AppSettings.RevShare.DailyPoolDurationTime; } Money divided = new Money(amount.ToDecimal() / (Decimal)n); Money distributed = Money.Zero; DateTime currentDate = date.Date; for (int i = 0; i < n; i++) { var pool = DailyPool.Get(poolId, currentDate.Date); if (i == n - 1) //Last pool rule { pool.SumAmount += (amount - distributed); } else { pool.SumAmount += divided; } pool.Save(); currentDate = currentDate.AddDays(1); distributed += divided; } }
public static void TryRepayDebt(Money amount, Member user, out Money repaidAmound) { repaidAmound = Money.Zero; var loan = GetUsersLoans(user.Id, false).FirstOrDefault(); if (loan != null) { if (amount <= Money.Zero) { throw new MsgException("Amount must be higher than 0"); } if (amount > user.PurchaseBalance) { throw new MsgException(U6012.CREDITLINEPURCHASEBALANCEERROR); } repaidAmound = loan.Loaned - loan.Repaid; if (amount > repaidAmound) { amount = repaidAmound; } else { repaidAmound = amount; } loan.Repaid += amount; loan.Save(); GlobalPoolManager.AddToPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.CreditLine), amount); user.SubtractFromPurchaseBalance(amount, "Credit Line", BalanceLogType.CreditLine); user.SaveBalances(); } }
public static void AddMoneyToRevenuePool(Money amount) { var poolId = PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn); var pool = TableHelper.GetListFromRawQuery <ProfitPoolDistribution>(string.Format("SELECT * FROM ProfitPoolDistribution WHERE Pool = {0}", poolId)).FirstOrDefault(); if (pool != null) { PoolDistributionManager.AddProfit(ProfitSource.Memberships, amount, pool); } }
public static void AddProfit(ProfitSource source, Money amount, ProfitPoolDistribution targetPool) { if (targetPool.Pool == PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn)) { //Daily pool DailyPoolManager.AddToPool(targetPool.Pool, DateTime.Now, amount); AdPackProfitDistributionHistory.Add(amount); } else { //Global pool GlobalPoolManager.AddToPool(targetPool.Pool, amount); } }
public void CreditWin(Money cashWon) { base.CreditMainBalance(cashWon, "PvpJackpot Win", BalanceLogType.PvpJackpotWin); User.Save(); if (TitanFeatures.IsNightwolf && PoolDistributionManager.GetGlobalPoolSumInMoney(PoolsHelper.GetBuiltInProfitPoolId(Pools.PvpJackpotGamePool)) < cashWon) { GlobalPoolManager.SubtractFromPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.AdministratorProfit), cashWon); } else { GlobalPoolManager.SubtractFromPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.PvpJackpotGamePool), cashWon); } }
public static Money GetMaxPossibleRequest(Member user) { var amountInPool = GlobalPool.Get(PoolsHelper.GetBuiltInProfitPoolId(Pools.CreditLine)).SumAmount; var pendingSum = GetPendingRequestsSumAmount(); var left = amountInPool - pendingSum; var amounts = new List <Money>(); amounts.Add(left); amounts.Add(user.Membership.MaxCreditLineRequest); if (TitanFeatures.IsCopiousassets) { amounts.Add(Money.MultiplyPercent(GetThisWeeksMarketplaceSpendings(user.Id), 10)); } return(amounts.Min()); }
public static ProfitPoolDistribution GetAdministratorProfitRule(ProfitSource source) { var query = string.Format("SELECT TOP 1 * FROM ProfitPoolDistribution WHERE Pool = {0} AND ProfitSource = {1}", PoolsHelper.GetBuiltInProfitPoolId(Pools.AdministratorProfit), (int)source); var adminRule = TableHelper.GetListFromRawQuery <ProfitPoolDistribution>(query).SingleOrDefault(); if (adminRule == null) { adminRule = new ProfitPoolDistribution() { ProfitPoolPercent = 100, ProfitSource = source, Pool = PoolsHelper.GetBuiltInProfitPoolId(Pools.AdministratorProfit) }; adminRule.Save(); } return(adminRule); }
public static void TryCheckSystemPoolsCash(int selectedStageId) { var CurrentStage = new JackpotPvpStage(selectedStageId); Money MoneyToWin = (CurrentStage.Cost / BattlesAmountPerStage) * Decimal.Parse(CurrentStage.WinPercent.ToString()) / 100; if (PoolDistributionManager.GetGlobalPoolSumInMoney(PoolsHelper.GetBuiltInProfitPoolId(Pools.PvpJackpotGamePool)) < MoneyToWin) { if (TitanFeatures.IsNightwolf) { if (PoolDistributionManager.GetGlobalPoolSumInMoney(PoolsHelper.GetBuiltInProfitPoolId(Pools.AdministratorProfit)) < MoneyToWin) { throw new MsgException(U6011.NOCREDITSINSYSTEM); } } else { throw new MsgException(U6011.NOCREDITSINSYSTEM); } } }
/// <summary> /// Distributes the money from the Profit Source properly /// </summary> /// <param name="source"></param> /// <param name="amount"></param> public static bool AddProfit(ProfitSource source, Money amount) { if (amount != Money.Zero) { var where = TableHelper.MakeDictionary(ProfitPoolDistribution.Columns.ProfitSource, (int)source); var targetPools = TableHelper.SelectRows <ProfitPoolDistribution>(where); Money distributed = new Money(0); for (int i = 0; i < targetPools.Count; i++) { var poolRule = targetPools[i]; Money toDistribute = new Money(0); //Last pool rule if (i == (targetPools.Count - 1)) { toDistribute = (amount - distributed); } else { toDistribute = Money.MultiplyPercent(amount, poolRule.ProfitPoolPercent); distributed += toDistribute; } if (poolRule.Pool == PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn)) { //Daily pool DailyPoolManager.AddToPool(poolRule.Pool, AppSettings.ServerTime, toDistribute); AdPackProfitDistributionHistory.Add(toDistribute); } else { //Global pool GlobalPoolManager.AddToPool(poolRule.Pool, toDistribute); } } return(distributed != Money.Zero); } return(false); }
public static void AcceptRequest(int creditLineRequestId) { Money maxLoan = GlobalPool.Get(PoolsHelper.GetBuiltInProfitPoolId(Pools.CreditLine)).SumAmount; CreditLineRequest request = new CreditLineRequest(creditLineRequestId); if (request.LoanRequested > maxLoan) { request.Status = CreditLineRequestStatus.Rejected; request.Save(); throw new MsgException("There is not enough money in the pool. The request has been rejected."); } request.Status = CreditLineRequestStatus.Accepted; request.Save(); Member user = new Member(request.UserId); DateTime now = AppSettings.ServerTime; CreditLineLoan loan = new CreditLineLoan(); loan.UserId = user.Id; loan.Loaned = request.LoanRequested; loan.FirstDeadline = now.AddDays(AppSettings.CreditLine.FirstDeadlineDays); loan.SecondDeadline = now.AddDays(AppSettings.CreditLine.SecondDeadlineDays); loan.FinalDeadline = now.AddDays(AppSettings.CreditLine.FinalDeadlineDays); loan.AmounBeforeFirstDeadline = Money.MultiplyPercent(request.LoanRequested, AppSettings.CreditLine.FirstRepayPercent); loan.AmounBeforeSecondDeadline = Money.MultiplyPercent(request.LoanRequested, AppSettings.CreditLine.SecondRepayPercent); loan.BorrowDate = now; loan.Repaid = Money.Zero; loan.Save(); var moneyWithoutFee = Money.MultiplyPercent(request.LoanRequested, 100 - AppSettings.CreditLine.Fee); GlobalPoolManager.SubtractFromPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.CreditLine), moneyWithoutFee); user.AddToPurchaseBalance(moneyWithoutFee, "Credit Line", BalanceLogType.CreditLine); user.SaveBalances(); Mailer.SendNewCreditLineMessage(user.Email, request.LoanRequested); }
public static void CRON() { int poolId = PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn); //Delete pools older then 2 days and move their money to current pools List <DailyPool> oldPools = TableHelper.GetListFromRawQuery <DailyPool>(String.Format( "SELECT * FROM DailyPools WHERE DateDay <= '{0}' AND DailyPoolType = {1}", AppSettings.ServerTime.AddDays(-2).ToDBString(), poolId)); Money left = Money.Zero; foreach (var oldPool in oldPools) { left += oldPool.SumAmount; } if (left > Money.Zero) { DailyPoolManager.AddToPool(poolId, AppSettings.ServerTime, left); } TableHelper.ExecuteRawCommandNonQuery(String.Format( "DELETE FROM DailyPools WHERE DateDay <= '{0}' AND DailyPoolType = {1}", AppSettings.ServerTime.AddDays(-2).ToDBString(), poolId)); }
public static void AutomaticRepay(Member user, DateTime now) { List <CreditLineLoan> loans = GetUsersLoans(user.Id, false); Money debt = GetUsersDebt(loans, now, user.Name, true); if (debt <= Money.Zero) { return; } var availableMoney = Money.Zero; var adBalance = user.PurchaseBalance; if (adBalance <= debt) { availableMoney = adBalance; } else { availableMoney = debt; } user.SubtractFromPurchaseBalance(availableMoney, "Credit Line", BalanceLogType.CreditLine); //Subtract rest of the debt from Main Balance, balance can go to negative var debtLeft = debt - availableMoney; if (debtLeft > Money.Zero) { user.SubtractFromMainBalance(debtLeft, "Credit Line", BalanceLogType.CreditLine); availableMoney += debtLeft; } user.SaveBalances(); Money moneyForPool = Money.Zero; foreach (var loan in loans) { if (availableMoney <= Money.Zero) { return; } var toPay = Money.Zero; if (loan.FinalDeadline < now) { toPay = loan.Loaned - loan.Repaid; } else if (loan.SecondDeadline < now) { toPay = loan.AmounBeforeSecondDeadline - loan.Repaid; } else if (loan.FirstDeadline < now) { toPay = loan.AmounBeforeFirstDeadline - loan.Repaid; } if (toPay > Money.Zero) { if (toPay > availableMoney) { loan.Repaid += availableMoney; moneyForPool += availableMoney; availableMoney -= availableMoney; } else { loan.Repaid += toPay; moneyForPool += toPay; availableMoney -= toPay; } } loan.Save(); } if (moneyForPool > Money.Zero) { GlobalPoolManager.AddToPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.CreditLine), moneyForPool); } }
private static void DailyDistribution(bool HourDistribution = false, bool FinalDistributionButHourly = false) { DailyPool Pool = DailyPool.GetYesterdayPool(PoolsHelper.GetBuiltInProfitPoolId(Pools.AdPackRevenueReturn)); String RaportMessage = String.Empty; Int32 ActiveAdPacks = 0; Money TotalDistributed = Money.Zero; Money PerUnit = Money.Zero; Money InThePool = Pool.SumAmount; using (var bridge = ParserPool.Acquire(Database.Client)) { DistributionSQLHelper DistributionHelper = new DistributionSQLHelper(bridge.Instance); DistributionHelper.SetStartingDistributionPriority(); ActiveAdPacks = DistributionHelper.GetSumOfActiveAdPacks(); try { if (AppSettings.RevShare.AdPack.GroupPolicy == GroupPolicy.Classic) { //Classic //Nothing to change } if (AppSettings.RevShare.AdPack.GroupPolicy == GroupPolicy.CustomGroups || AppSettings.RevShare.AdPack.GroupPolicy == GroupPolicy.AutomaticAndCustomGroups) { //CustomGrups if (AppSettings.RevShare.AdPack.CustomReturnOption == CustomReturnOption.Accelerate) { DistributionHelper.UpdatePrioritiesCustomGroups(); } } if (AppSettings.RevShare.AdPack.GroupPolicy == GroupPolicy.AutomaticGroups || AppSettings.RevShare.AdPack.GroupPolicy == GroupPolicy.AutomaticAndCustomGroups) { //AutomaticGroups DistributionHelper.UpdatePrioritiesAutomaticGroups(); } Decimal priorities = DistributionHelper.GetSumOfPriorities(); if (ActiveAdPacks == 0) { throw new MsgException("No active AdPacks with active members. " + GetNoDistributionMessage(HourDistribution)); } //Make the distribution var adPackTypes = AdPackTypeManager.GetAllTypes(); foreach (var adPackType in adPackTypes) { var returnedPercentage = 0.0m; if (AppSettings.RevShare.AdPack.DistributionPolicy == DistributionPolicy.Fixed) { PerUnit = GetMoneyPerUnit(GetMoneyPerUnitFixed(adPackType), HourDistribution, FinalDistributionButHourly, adPackType); returnedPercentage = adPackType.FixedDistributionValuePercent; } else if (AppSettings.RevShare.AdPack.DistributionPolicy == DistributionPolicy.Pools) { PerUnit = GetMoneyPerUnit(GetMoneyPerUnitPools(InThePool, priorities), HourDistribution, FinalDistributionButHourly, adPackType); returnedPercentage = PerUnit.ToDecimal() / adPackType.Price.ToDecimal(); } RaportMessage += "<b>" + adPackType.Name + "</b> for priority 1.00 (no acceleration): <b>" + PerUnit.ToClearString() + "</b>. <br/>"; if (PerUnit > Money.Zero) { TotalDistributed += DistributionHelper.DistributeUsingPriority(PerUnit, adPackType.Id); } RevShareManager.AddAdPackTypePercentageHistory(adPackType.Id, returnedPercentage); } if (TitanFeatures.isAri) { AriRevShareDistribution.CreditAriRevShareDistribution(); } } catch (MsgException ex) { RaportMessage += ex.Message; } catch (Exception ex) { ErrorLogger.Log(ex); } Pool.SumAmount -= TotalDistributed; if (HourDistribution == false) { if (AppSettings.RevShare.AdPack.DistributionPolicy == DistributionPolicy.Pools) { RaportMessage += "Money moved to the next day pool: " + Pool.SumAmount.ToClearString() + ". "; } Pool.MoveMoneyForTomorrow(); } Pool.Save(); Report.Add(ActiveAdPacks, InThePool, TotalDistributed, RaportMessage); CustomGroupManager.TrySetGroupsAsExpired(); } }