public static PointBank GetPointBank(string bank) { PointBank pointBank = null; var doc = DatabaseUtils.FindMongoDocument("_id", bank, "pointBanks"); if (doc != null) { pointBank = BsonSerializer.Deserialize <PointBank>(doc); } return(pointBank); }
public async Task PayLoan(int amount, [Remainder] string bank) { var user = Context.User; UserInfo userInfo = ObjectUtils.GetUserInformation(user.Id); PointBank pointBank = ObjectUtils.GetPointBank(bank.ToLower()); var loans = Util.CheckIfUserHasLoan(Context.User.Id); foreach (var loan in loans) { if (bank.Equals(loan.Key)) { if (userInfo.points < amount) { await ReplyAsync($"{user.Username} has {userInfo.points}.\nYou cannot pay {amount}."); return; } else if (amount > loan.Value) { await ReplyAsync($"Loan is {loan.Value}.\nYou cannot pay {amount}."); return; } else if (amount < 1) { await ReplyAsync($"You cannot pay less than 1 point."); return; } else if (amount.Equals(loan.Value)) { DatabaseUtils.DecrementDocument(user.Id, "points", amount); pointBank.currentLoans.Remove(user.Id.ToString()); Util.UpdateArray("_id", pointBank._id, "currentLoans", user.Id.ToString(), pointBank, "pointBanks", false); await ReplyAsync($"Loan paid back in full to {pointBank._id}"); return; } else { DatabaseUtils.DecrementDocument(user.Id, "points", amount); pointBank.currentLoans[user.Id.ToString()] -= amount; Util.UpdateArray("_id", pointBank._id, "currentLoans", user.Id.ToString(), pointBank, "pointBanks", false); await ReplyAsync($"Payment on loan made.\nRemaining balance {pointBank.currentLoans[user.Id.ToString()]}"); return; } } } await ReplyAsync("You do not have a loan out for this bank."); }
public async Task GetBankInfo([Remainder] string bank) { EmbedBuilder embed = new EmbedBuilder(); PointBank pointBank = ObjectUtils.GetPointBank(bank.ToLower()); if (pointBank.Equals(null)) { await ReplyAsync($"{bank} does not exist"); } else { embed.Title = pointBank._id; embed.AddField("Money in vault", pointBank.currentCredits, true); embed.AddField("Interest Rate", $"{pointBank.interestRate * 100}%", true); embed.Color = Color.Purple; } await ReplyAsync(embed : embed.Build()); }
public static Dictionary <string, int> CheckIfUserHasLoan(ulong user) { DatabaseUtils.ChangeCollection("pointBanks"); Dictionary <string, int> currentLoans = new Dictionary <string, int>(); using (var cursor = DatabaseUtils.MyMongoCollection.Find(new BsonDocument()).ToCursor()) { while (cursor.MoveNext()) { foreach (var doc in cursor.Current) { var docArray = doc.FirstOrDefault(x => x.Name == "currentLoans").Value.AsBsonDocument; if (docArray.Contains(user.ToString())) { PointBank pointBank = BsonSerializer.Deserialize <PointBank>(doc); currentLoans.Add(pointBank._id, pointBank.currentLoans.Values.First()); } } } } return(currentLoans); }
public async Task TakeLoan(int amount, [Remainder] string bank) { var user = Context.User; UserInfo userInfo = ObjectUtils.GetUserInformation(user.Id); PointBank pointBank = ObjectUtils.GetPointBank(bank.ToLower()); EmbedBuilder embed = new EmbedBuilder(); if (Util.CheckIfUserHasLoan(user.Id) != null) { await ReplyAsync("You already have a loan out for this bank!"); return; } if (amount < pointBank.minWithdrawal) { await ReplyAsync($"Cannot take out a loan less than {pointBank.minWithdrawal} points!"); } else if (amount > pointBank.currentCredits) { await ReplyAsync($"Cannot take out a loan which exceeds the amount of money in the vault!\nCurrent money in the vault is {pointBank.currentCredits}"); } else { int loanBalance = (int)(amount * (1 + pointBank.interestRate)); pointBank.currentLoans.Add(user.Id.ToString(), loanBalance); var dict = new BsonDocument { { user.Id.ToString(), loanBalance } }; Util.UpdateArray("_id", pointBank._id, "currentLoans", dict, pointBank, "pointBanks"); DatabaseUtils.IncrementDocument(Context.User.Id, "points", amount); DatabaseUtils.DecrementDocument(pointBank._id, "currentCredits", amount, "pointBanks"); embed.Title = "Succesful loan approval!"; embed.Color = Color.Green; embed.AddField($"{Context.User.ToString()} points", userInfo.points + amount); embed.AddField($"{pointBank._id}'s vault value", pointBank.currentCredits - amount); await ReplyAsync(embed : embed.Build()); } }
public static void UpdateArray(string filterField, string filterCriteria, string arrayField, object arrayCriteria, PointBank bank, string collectionName = "userData", bool newLoan = true) { var doc = DatabaseUtils.FindMongoDocument(filterField, filterCriteria, collectionName); if (doc != null) { var docArray = doc.FirstOrDefault(x => x.Name == arrayField).Value.AsBsonDocument; if (newLoan) { if (docArray.Contains(arrayCriteria.ToString())) { Logger.Log(new LogMessage(LogSeverity.Warning, MethodBase.GetCurrentMethod().DeclaringType.FullName + ".UpdateArray", $"Array already contains {arrayCriteria}")); return; } } var filter = DatabaseUtils.FilterMongoDocument(filterField, filterCriteria, collectionName); var update = Builders <BsonDocument> .Update.Set("currentLoans", bank.currentLoans); DatabaseUtils.MyMongoCollection.FindOneAndUpdateAsync(filter, update); } }