public async Task Withdraw(float cash) { using (var db = new SQLite.Models.DbContext()) { var userRepo = new UserRepository(db); var gangRepo = new GangRepository(db); var gang = await gangRepo.FetchGangAsync(Context.User.Id, Context.Guild.Id); var user = await userRepo.FetchUserAsync(Context.User.Id); if (cash < Config.MIN_WITHDRAW) { throw new Exception($"The minimum withdrawal is {Config.MIN_WITHDRAW.ToString("C2")}."); } if (cash > gang.Wealth * Config.WITHDRAW_CAP) { throw new Exception($"You may only withdraw {Config.WITHDRAW_CAP.ToString("P")} of your gang's wealth, " + $"that is {(gang.Wealth * Config.WITHDRAW_CAP).ToString("C2")}."); } await userRepo.ModifyAsync(x => { x.LastWithdraw = DateTime.Now.ToString(); return(Task.CompletedTask); }, Context.User.Id); await gangRepo.ModifyAsync(x => { x.Wealth -= cash; return(Task.CompletedTask); }, Context.User.Id, Context.Guild.Id); await userRepo.EditCashAsync(Context, +cash); await ReplyAsync($"{Context.User.Mention}, You have successfully withdrawn {cash.ToString("C2")}. " + $"{gang.Name}'s Wealth: {gang.Wealth.ToString("C2")}"); } }
public async Task Deposit(float cash) { using (var db = new SQLite.Models.DbContext()) { var userRepo = new UserRepository(db); var gangRepo = new GangRepository(db); var user = await userRepo.FetchUserAsync(Context.User.Id); if (cash < Config.MIN_DEPOSIT) { throw new Exception($"The lowest deposit is {Config.MIN_DEPOSIT.ToString("C2")}."); } if (user.Cash < cash) { throw new Exception($"You do not have enough money. Balance: {user.Cash.ToString("C2")}."); } await userRepo.EditCashAsync(Context, -cash); await gangRepo.ModifyAsync(x => { x.Wealth += cash; return(Task.CompletedTask); }, Context.User.Id, Context.Guild.Id); var gang = await gangRepo.FetchGangAsync(Context.User.Id, Context.Guild.Id); await ReplyAsync($"{Context.User.Mention}, You have successfully deposited {cash.ToString("C2")}. " + $"{gang.Name}'s Wealth: {gang.Wealth.ToString("C2")}"); } }
public async Task ChangeGangName([Summary("JERK EM OFF BOYS")][Remainder] string newName) { if (Context.Cash < Config.GANG_NAME_CHANGE_COST) { ReplyError($"You do not have {Config.GANG_NAME_CHANGE_COST.USD()}. Balance: {Context.Cash.USD()}."); } var gangs = await(await _gangRepo.Collection.FindAsync(y => y.GuildId == Context.Guild.Id)).ToListAsync(); if (gangs.Any(x => x.Name.ToLower() == newName.ToLower())) { ReplyError($"There is already a gang by the name {newName}."); } if (!Config.ALPHANUMERICAL.IsMatch(newName)) { ReplyError("Gang names may not contain any non alphanumeric characters."); } await _userRepo.EditCashAsync(Context, -Config.GANG_NAME_CHANGE_COST); await _gangRepo.ModifyAsync(Context.Gang, x => x.Name = newName); await ReplyAsync($"You have successfully changed your gang name to {newName} at the cost of {Config.GANG_NAME_CHANGE_COST.USD()}."); }
public async Task ChangeGangName([Remainder] string name) { var user = await UserRepository.FetchUserAsync(Context); if (user.Cash < Config.GANG_NAME_CHANGE_COST) { throw new Exception($"You do not have {Config.GANG_NAME_CHANGE_COST.ToString("C", Config.CI)}. Balance: {user.Cash.ToString("C", Config.CI)}."); } if ((await GangRepository.AllAsync(Context.Guild.Id)).Any(x => x.Name == name)) { throw new Exception($"There is already a gang by the name {name}."); } await UserRepository.EditCashAsync(Context, -Config.GANG_NAME_CHANGE_COST); await GangRepository.ModifyAsync(x => { x.Name = name; return(Task.CompletedTask); }, Context.User.Id, Context.Guild.Id); await ReplyAsync($"You have successfully changed your gang name to {name} at the cost of {Config.GANG_NAME_CHANGE_COST.ToString("C", Config.CI)}."); }
public async Task TransferLeadership(IGuildUser user) { if (user.Id == Context.User.Id) { throw new Exception("You are already the leader of this gang!"); } var gang = await GangRepository.FetchGangAsync(Context); if (!(await GangRepository.IsMemberOfAsync(Context.User.Id, Context.Guild.Id, user.Id))) { throw new Exception("This user is not a member of your gang!"); } await GangRepository.RemoveMemberAsync(Context.User.Id, Context.Guild.Id); await GangRepository.ModifyAsync(x => { x.LeaderId = user.Id; return(Task.CompletedTask); }, Context); await GangRepository.AddMemberAsync(Context.User.Id, Context.Guild.Id, Context.User.Id); await ReplyAsync($"{Context.User.Mention}, You have successfully transferred the leadership of {gang.Name} to {user.Mention}"); }
private void ApplyInterestRate(object stateObj) { Task.Run(async() => { Logger.Log(LogSeverity.Debug, $"Timers", "Interest Rate"); var updateBuilder = Builders <Gang> .Update; foreach (var gang in await _gangRepo.AllAsync()) { try { await _gangRepo.ModifyAsync(gang, x => x.Wealth += InterestRate.Calculate(x.Wealth) * x.Wealth); } catch (OverflowException) { // Ignored. } } }); }
public async Task ChangeGangName([Remainder] string name) { using (var db = new SQLite.Models.DbContext()) { var gangRepo = new GangRepository(db); var userRepo = new UserRepository(db); var user = await userRepo.FetchUserAsync(Context.User.Id); if (user.Cash < Config.GANG_NAME_CHANGE_COST) { throw new Exception($"You do not have {Config.GANG_NAME_CHANGE_COST.ToString("C2")}. Balance: {user.Cash.ToString("C2")}."); } if (await gangRepo.GetAll().AnyAsync(x => x.Name == name)) { throw new Exception($"There is already a gang by the name {name}."); } await userRepo.EditCashAsync(Context, -Config.GANG_NAME_CHANGE_COST); await gangRepo.ModifyAsync(x => { x.Name = name; return(Task.CompletedTask); }, Context.User.Id, Context.Guild.Id); await ReplyAsync($"You have successfully changed your gang name to {name} at the cost of {Config.GANG_NAME_CHANGE_COST.ToString("C2")}."); } }
public async Task Withdraw(double cash) { var gang = await GangRepository.FetchGangAsync(Context); var user = await UserRepository.FetchUserAsync(Context); if (cash < Config.MIN_WITHDRAW) { throw new Exception($"The minimum withdrawal is {Config.MIN_WITHDRAW.ToString("C", Config.CI)}."); } if (cash > gang.Wealth * Config.WITHDRAW_CAP) { throw new Exception($"You may only withdraw {Config.WITHDRAW_CAP.ToString("P")} of your gang's wealth, " + $"that is {(gang.Wealth * Config.WITHDRAW_CAP).ToString("C", Config.CI)}."); } await UserRepository.ModifyAsync(x => { x.Withdraw = DateTimeOffset.Now; return(Task.CompletedTask); }, Context); await GangRepository.ModifyAsync(x => { x.Wealth -= cash; return(Task.CompletedTask); }, Context.User.Id, Context.Guild.Id); await UserRepository.EditCashAsync(Context, +cash); await ReplyAsync($"{Context.User.Mention}, You have successfully withdrawn {cash.ToString("C", Config.CI)}. " + $"{gang.Name}'s Wealth: {gang.Wealth.ToString("C", Config.CI)}"); }