private async Task OnBlackjackWin(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet) { e.EventSystem.GetCommandHandler <SessionBasedCommandHandler>() .RemoveSession(e.Author.Id, e.Channel.Id); User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(bet * 2, e.Channel); await context.SaveChangesAsync(); } } await instanceMessage.EditAsync(new EditMessageArgs { embed = bm.CreateEmbed(e) .SetAuthor(e.GetResource("miki_blackjack_win_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot") .SetDescription(e.GetResource("miki_blackjack_win_description", bet * 2) + "\n" + e.GetResource("miki_blackjack_new_balance", user.Currency)) .ToEmbed() }); }
private async Task OnBlackjackWin(EventContext e, BlackjackManager bm) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); var api = (IApiClient)e.Services.GetService(typeof(IApiClient)); User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(bm.Bet * 2, e.Channel); await context.SaveChangesAsync(); } } await api.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e) .SetAuthor(e.Locale.GetString("miki_blackjack_win_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot") .SetDescription(e.Locale.GetString("miki_blackjack_win_description", bm.Bet * 2) + "\n" + e.Locale.GetString("miki_blackjack_new_balance", user.Currency)) .ToEmbed() }); await cache.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}"); }
public async Task StartBlackjack(EventContext e, int bet) { using (var context = new MikiContext()) { User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); await user.RemoveCurrencyAsync(context, null, bet); } BlackjackManager bm = new BlackjackManager(); IDiscordMessage message = await bm.CreateEmbed(e).SendToChannel(e.Channel); CommandHandler c = new CommandHandlerBuilder(Bot.instance.Events) .AddPrefix("") .SetOwner(e.message) .AddCommand( new RuntimeCommandEvent("hit") .Default(async(ec) => await OnBlackjackHit(ec, bm, message, bet))) .AddCommand( new RuntimeCommandEvent("stand") .SetAliases("knock", "stay", "stop") .Default(async(ec) => await OnBlackjackHold(ec, bm, message, bet)) ).Build(); Bot.instance.Events.AddPrivateCommandHandler(e.message, c); }
private async Task OnBlackjackDraw(EventContext e, BlackjackManager bm) { User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(bm.Bet, e.Channel); await context.SaveChangesAsync(); } } await Global.Client.Client._apiClient.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e) .SetAuthor( e.Locale.GetString("miki_blackjack_draw_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot" ) .SetDescription( e.Locale.GetString("blackjack_draw_description") + "\n" + e.Locale.GetString("miki_blackjack_current_balance", user.Currency) ).ToEmbed() } ); await Global.RedisClient.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}"); }
public async Task StartBlackjack(EventContext e, int bet) { using (var context = new MikiContext()) { User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); await user.AddCurrencyAsync(-bet, e.Channel); await context.SaveChangesAsync(); } BlackjackManager bm = new BlackjackManager(); IDiscordMessage message = await bm.CreateEmbed(e) .ToEmbed() .SendToChannel(e.Channel); Framework.Events.CommandMap map = new Framework.Events.CommandMap(); SimpleCommandHandler c = new SimpleCommandHandler(map); c.AddPrefix(""); c.AddCommand(new CommandEvent("hit") .Default(async(ec) => await OnBlackjackHit(ec, bm, message, bet))); c.AddCommand(new CommandEvent("stand") .SetAliases("knock", "stay", "stop") .Default(async(ec) => await OnBlackjackHold(ec, bm, message, bet))); e.EventSystem.GetCommandHandler <SessionBasedCommandHandler>().AddSession( new CommandSession() { UserId = e.Author.Id, ChannelId = e.Channel.Id }, c, new TimeSpan(1, 0, 0)); }
private async Task OnBlackjackDraw(CommandContext e, BlackjackManager bm) { var cache = e.GetService <ICacheClient>(); var api = e.GetService <IApiClient>(); User user; var context = e.GetService <MikiDbContext>(); user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(bm.Bet, e.Channel); await context.SaveChangesAsync(); } await api.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e) .SetAuthor( e.Locale.GetString("blackjack_draw_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot" ) .SetDescription( e.Locale.GetString("blackjack_draw_description") + "\n" + e.Locale.GetString("miki_blackjack_current_balance", user.Currency) ).ToEmbed() } ); await cache.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}"); }
private async Task OnBlackjackDraw(EventContext e, BlackjackManager bm, IUserMessage instanceMessage, int bet) { e.EventSystem.GetCommandHandler <SessionBasedCommandHandler>() .RemoveSession(e.Author.Id, e.Channel.Id); User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(bet, e.Channel); await context.SaveChangesAsync(); } } await instanceMessage.ModifyAsync(x => { x.Embed = bm.CreateEmbed(e).ToEmbedBuilder() .SetAuthor(e.GetResource("miki_blackjack_draw_title") + " | " + e.Author.Username, e.Author.GetAvatarUrl(), "https://patreon.com/mikibot") .WithDescription(e.GetResource("blackjack_draw_description") + "\n" + e.GetResource("miki_blackjack_current_balance", user.Currency)).Build(); }); }
private async Task OnBlackjackDraw(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet) { await e.commandHandler.RequestDisposeAsync(); User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(bet, e.Channel); await context.SaveChangesAsync(); } } await bm.CreateEmbed(e) .SetAuthor(e.GetResource("miki_blackjack_draw_title") + " | " + e.Author.Username, e.Author.AvatarUrl, "https://patreon.com/mikibot") .SetDescription(e.GetResource("blackjack_draw_description") + "\n" + e.GetResource("miki_blackjack_current_balance", user.Currency)) .ModifyMessage(instanceMessage); }
private async Task OnBlackjackHit(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet) { await e.message.DeleteAsync(); bm.player.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.player) > 21) { await OnBlackjackDead(e, bm, instanceMessage, bet); } else { if (bm.player.Hand.Count == 5) { await OnBlackjackHold(e, bm, instanceMessage, bet, true); return; } else if (bm.Worth(bm.player) == 21 && bm.Worth(bm.dealer) != 21) { await OnBlackjackWin(e, bm, instanceMessage, bet); return; } else if (bm.Worth(bm.dealer) == 21 && bm.Worth(bm.player) != 21) { await OnBlackjackDead(e, bm, instanceMessage, bet); return; } await bm.CreateEmbed(e).ModifyMessage(instanceMessage); } }
private async Task OnBlackjackHold(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet, bool charlie = false) { bm.dealer.Hand.ForEach(x => x.isPublic = true); if (!charlie) { IDiscordGuildUser me = await e.Guild.GetSelfAsync(); //if (me.GetPermissions(e.Channel as IDiscordGuildUser).Has(ChannelPermission.ManageMessages)) //{ // await e.message.DeleteAsync(); //} } while (true) { if (bm.Worth(bm.dealer) >= Math.Max(bm.Worth(bm.player), 17)) { if (charlie) { if (bm.dealer.Hand.Count == 5) { if (bm.Worth(bm.dealer) == bm.Worth(bm.player)) { await OnBlackjackDraw(e, bm, instanceMessage, bet); return; } await OnBlackjackDead(e, bm, instanceMessage, bet); return; } } else { if (bm.Worth(bm.dealer) == bm.Worth(bm.player)) { await OnBlackjackDraw(e, bm, instanceMessage, bet); return; } await OnBlackjackDead(e, bm, instanceMessage, bet); return; } } bm.dealer.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.dealer) > 21) { await OnBlackjackWin(e, bm, instanceMessage, bet); return; } } }
private async Task OnBlackjackHold(EventContext e, bool charlie = false) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); BlackjackManager bm = await BlackjackManager.FromCacheClientAsync(cache, e.Channel.Id, e.Author.Id); CardHand Player = bm.GetPlayer(e.Author.Id); CardHand Dealer = bm.GetPlayer(0); Dealer.Hand.ForEach(x => x.isPublic = true); while (true) { if (bm.Worth(Dealer) >= Math.Max(bm.Worth(Player), 17)) { if (charlie) { if (Dealer.Hand.Count == 5) { if (bm.Worth(Dealer) == bm.Worth(Player)) { await OnBlackjackDraw(e, bm); return; } await OnBlackjackDead(e, bm); return; } } else { if (bm.Worth(Dealer) == bm.Worth(Player)) { await OnBlackjackDraw(e, bm); return; } await OnBlackjackDead(e, bm); return; } } bm.DealTo(Dealer); if (bm.Worth(Dealer) > 21) { await OnBlackjackWin(e, bm); return; } } }
private async Task OnBlackjackHold(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet, bool charlie = false) { bm.dealer.Hand.ForEach(x => x.isPublic = true); if (!charlie) { await e.message.DeleteAsync(); } while (true) { if (bm.Worth(bm.dealer) >= bm.Worth(bm.player)) { if (charlie) { if (bm.dealer.Hand.Count == 5) { if (bm.Worth(bm.dealer) == bm.Worth(bm.player)) { await OnBlackjackDraw(e, bm, instanceMessage, bet); return; } await OnBlackjackDead(e, bm, instanceMessage, bet); return; } } else { if (bm.Worth(bm.dealer) == bm.Worth(bm.player)) { await OnBlackjackDraw(e, bm, instanceMessage, bet); return; } await OnBlackjackDead(e, bm, instanceMessage, bet); return; } } bm.dealer.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.dealer) > 21) { await OnBlackjackWin(e, bm, instanceMessage, bet); return; } } }
private async Task OnBlackjackDead(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet) { await e.commandHandler.RequestDisposeAsync(); User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); } await bm.CreateEmbed(e) .SetTitle(e.GetResource("miki_blackjack_lose_title")) .SetDescription(e.GetResource("miki_blackjack_lose_description") + "\n" + e.GetResource("miki_blackjack_new_balance", user.Currency)) .ModifyMessage(instanceMessage); }
public async Task OnBlackjackNew(EventContext e) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); int?bet = await ValidateBetAsync(e); if (bet.HasValue) { if (await cache.ExistsAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}")) { await e.ErrorEmbedResource(new LanguageResource("blackjack_session_exists")) .ToEmbed().QueueToChannelAsync(e.Channel); return; } using (var context = new MikiContext()) { var user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user == null) { return; } user.RemoveCurrency(bet.Value); await context.SaveChangesAsync(); } BlackjackManager manager = new BlackjackManager(bet.Value); CardHand dealer = manager.AddPlayer(0); CardHand player = manager.AddPlayer(e.Author.Id); manager.DealAll(); manager.DealAll(); dealer.Hand[1].isPublic = false; IDiscordMessage message = await manager.CreateEmbed(e) .ToEmbed().SendToChannel(e.Channel); manager.MessageId = message.Id; await cache.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", manager.ToContext(), TimeSpan.FromHours(24)); } }
public async Task OnBlackjackNew(EventContext e, ArgObject args) { int?bet = await ValidateBetAsync(e, args); if (bet.HasValue) { using (var context = new MikiContext()) { var user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user == null) { return; } user.Currency -= bet.Value; await context.SaveChangesAsync(); } if (await Global.RedisClient.ExistsAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}")) { e.ErrorEmbed("You still have a blackjack game running here, please either stop it by using `>blackjack stay` or finish playing it. This game will expire in 24 hours.") .ToEmbed().QueueToChannel(e.Channel); return; } BlackjackManager manager = new BlackjackManager(bet.Value); CardHand dealer = manager.AddPlayer(0); CardHand player = manager.AddPlayer(e.Author.Id); manager.DealAll(); manager.DealAll(); dealer.Hand[1].isPublic = false; IDiscordMessage message = await manager.CreateEmbed(e) .ToEmbed() .SendToChannel(e.Channel); manager.MessageId = message.Id; await Global.RedisClient.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", manager.ToContext(), TimeSpan.FromHours(24)); } }
private async Task OnBlackjackHit(EventContext e) { var cache = (ICacheClient)e.Services.GetService(typeof(ICacheClient)); var api = (IApiClient)e.Services.GetService(typeof(IApiClient)); BlackjackManager bm = await BlackjackManager.FromCacheClientAsync(cache, e.Channel.Id, e.Author.Id); CardHand Player = bm.GetPlayer(e.Author.Id); CardHand Dealer = bm.GetPlayer(0); bm.DealTo(Player); if (bm.Worth(Player) > 21) { await OnBlackjackDead(e, bm); } else { if (Player.Hand.Count == 5) { await OnBlackjackHold(e, true); return; } else if (bm.Worth(Player) == 21 && bm.Worth(Dealer) != 21) { await OnBlackjackWin(e, bm); return; } else if (bm.Worth(Dealer) == 21 && bm.Worth(Player) != 21) { await OnBlackjackDead(e, bm); return; } await api.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e).ToEmbed() }); await cache.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", bm.ToContext(), TimeSpan.FromHours(24)); } }
private async Task OnBlackjackHit(EventContext e) { BlackjackManager bm = await BlackjackManager.FromCacheClientAsync(Global.RedisClient, e.Channel.Id, e.Author.Id); CardHand Player = bm.GetPlayer(e.Author.Id); CardHand Dealer = bm.GetPlayer(0); bm.DealTo(Player); if (bm.Worth(Player) > 21) { await OnBlackjackDead(e, bm); } else { if (Player.Hand.Count == 5) { await OnBlackjackHold(e, true); return; } else if (bm.Worth(Player) == 21 && bm.Worth(Dealer) != 21) { await OnBlackjackWin(e, bm); return; } else if (bm.Worth(Dealer) == 21 && bm.Worth(Player) != 21) { await OnBlackjackDead(e, bm); return; } await Global.Client.ChatClient._apiClient.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e).ToEmbed() }); await Global.RedisClient.UpsertAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}", bm.ToContext(), TimeSpan.FromHours(24)); } }
private async Task OnBlackjackHit(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet) { IDiscordGuildUser me = await e.Guild.GetSelfAsync(); //if (me.GetPermissions(e.Channel as IDiscordGuildChannel).Has(ChannelPermission.ManageMessages)) //{ // await e.message.DeleteAsync(); //} bm.player.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.player) > 21) { await OnBlackjackDead(e, bm, instanceMessage, bet); } else { if (bm.player.Hand.Count == 5) { await OnBlackjackHold(e, bm, instanceMessage, bet, true); return; } else if (bm.Worth(bm.player) == 21 && bm.Worth(bm.dealer) != 21) { await OnBlackjackWin(e, bm, instanceMessage, bet); return; } else if (bm.Worth(bm.dealer) == 21 && bm.Worth(bm.player) != 21) { await OnBlackjackDead(e, bm, instanceMessage, bet); return; } await instanceMessage.EditAsync(new EditMessageArgs { embed = bm.CreateEmbed(e).ToEmbed() }); } }
private async Task OnBlackjackHit(EventContext e, BlackjackManager bm, IUserMessage instanceMessage, int bet) { IGuildUser me = await e.Guild.GetCurrentUserAsync(); if (me.GetPermissions(e.Channel as IGuildChannel).Has(ChannelPermission.ManageMessages)) { await e.message.DeleteAsync(); } bm.player.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.player) > 21) { await OnBlackjackDead(e, bm, instanceMessage, bet); } else { if (bm.player.Hand.Count == 5) { await OnBlackjackHold(e, bm, instanceMessage, bet, true); return; } else if (bm.Worth(bm.player) == 21 && bm.Worth(bm.dealer) != 21) { await OnBlackjackWin(e, bm, instanceMessage, bet); return; } else if (bm.Worth(bm.dealer) == 21 && bm.Worth(bm.player) != 21) { await OnBlackjackDead(e, bm, instanceMessage, bet); return; } await instanceMessage.ModifyAsync(x => { x.Embed = bm.CreateEmbed(e); }); } }
private async Task OnBlackjackDead(EventContext e, BlackjackManager bm) { User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); } await Global.Client.Client._apiClient.EditMessageAsync(e.Channel.Id, bm.MessageId, new EditMessageArgs { embed = bm.CreateEmbed(e) .SetAuthor( e.Locale.GetString("miki_blackjack_lose_title") + " | " + e.Author.Username, (await e.Guild.GetSelfAsync()).GetAvatarUrl(), "https://patreon.com/mikibot" ) .SetDescription(e.Locale.GetString("miki_blackjack_lose_description") + "\n" + e.Locale.GetString("miki_blackjack_new_balance", user.Currency) ).ToEmbed() }); await Global.RedisClient.RemoveAsync($"miki:blackjack:{e.Channel.Id}:{e.Author.Id}"); }
private async Task OnBlackjackWin(EventContext e, BlackjackManager bm, IDiscordMessage instanceMessage, int bet) { await e.commandHandler.RequestDisposeAsync(); User user; using (var context = new MikiContext()) { user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user != null) { await user.AddCurrencyAsync(e.Channel, null, bet * 2); await context.SaveChangesAsync(); } } await bm.CreateEmbed(e) .SetTitle(e.GetResource("miki_blackjack_win_title")) .SetDescription(e.GetResource("miki_blackjack_win_description", bet * 2) + "\n" + e.GetResource("miki_blackjack_new_balance", user.Currency)) .ModifyMessage(instanceMessage); }
public async Task BlackjackAsync(EventContext e) { Locale locale = e.Channel.GetLocale(); if (Bot.instance.Events.PrivateCommandHandlerExist(e.Author.Id, e.Channel.Id)) { await e.ErrorEmbed(e.GetResource("blackjack_error_instance_exists")) .SendToChannel(e.Channel); return; } if (int.TryParse(e.arguments, out int bet)) { if (bet < 1) { return; } using (var context = new MikiContext()) { User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user.Currency >= bet) { await user.RemoveCurrencyAsync(context, null, bet); } else { await e.ErrorEmbed(e.GetResource("miki_mekos_insufficient")) .SendToChannel(e.Channel); return; } } BlackjackManager bm = new BlackjackManager(); IDiscordMessage message = await bm.CreateEmbed(e).SendToChannel(e.Channel); CommandHandler c = new CommandHandlerBuilder(Bot.instance.Events) .AddPrefix("") .SetOwner(e.message) .AddCommand( new RuntimeCommandEvent("hit") .Default(async(ec) => { await ec.message.DeleteAsync(); bm.player.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.player) > 21) { await OnBlackjackDead(ec, bm, message, bet); } else { await bm.CreateEmbed(e).ModifyMessage(message); } })) .AddCommand( new RuntimeCommandEvent("stand") .SetAliases("knock", "stay", "stop") .Default(async(ec) => { bm.dealer.Hand.ForEach(x => x.isPublic = true); await ec.message.DeleteAsync(); bool dealerQuit = false; while (!dealerQuit) { if (bm.Worth(bm.dealer) >= bm.Worth(bm.player)) { if (bm.Worth(bm.dealer) == bm.Worth(bm.player)) { await OnBlackjackDraw(ec, bm, message, bet); } else { await OnBlackjackDead(ec, bm, message, bet); } dealerQuit = true; } else { bm.dealer.AddToHand(bm.deck.DrawRandom()); if (bm.Worth(bm.dealer) > 21) { await OnBlackjackWin(ec, bm, message, bet); dealerQuit = true; } } await Task.Delay(500); } })).Build(); Bot.instance.Events.AddPrivateCommandHandler(e.message, c); } else { await Utils.ErrorEmbed(e.Channel.GetLocale(), e.GetResource("miki_blackjack_no_arg")) .SendToChannel(e.Channel); } }