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); }
public void BuildCommandHandler() { var commandHandlerBuilder = new CommandHandlerBuilder(ApplicationNameSpace); foreach (var domainClass in DomainTree.Classes) { var codeNamespace = commandHandlerBuilder.Build(domainClass); TestUtils.SnapshotTest(codeNamespace); } }
public static CommandHandlerBuilder <TCommand> Validate <TCommand>( this CommandHandlerBuilder <TCommand> builder, IValidator <TCommand> validator) { return(builder.Pipe( next => (envelope, token) => { validator.ValidateAndThrow(envelope.Command); return next(envelope, token); })); }
public static CommandHandlerBuilder <TCommand> ValidateAsync <TCommand>( this CommandHandlerBuilder <TCommand> builder, IValidator <TCommand> validator) { return(builder.Pipe( next => async(envelope, token) => { await validator.ValidateAndThrowAsync(envelope.Command); await next(envelope, token); })); }
public async Task BuyMarriageSlotAsync(EventContext e) { using (var context = new MikiContext()) { User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); int limit = 10; bool isDonator = await user.IsDonatorAsync(context); if (isDonator) { limit += 5; } IDiscordEmbed embed = new RuntimeEmbed(new EmbedBuilder()); if (user.MarriageSlots >= limit) { embed.Description = $"For now, **{limit} slots** is the max. sorry :("; if (limit == 10 && !isDonator) { embed.AddField("Pro tip!", "Donators get 5 more slots!") .SetFooter("Check `>donate` for more information!", ""); } embed.Color = new Common.Color(1f, 0.6f, 0.4f); embed.QueueToChannel(e.Channel); return; } int costForUpgrade = (user.MarriageSlots - 4) * 2500; embed.Description = $"Do you want to buy a Marriage slot for **{costForUpgrade}**?\n\nType `yes` to confirm."; embed.Color = new Miki.Common.Color(0.4f, 0.6f, 1f); embed.QueueToChannel(e.Channel); CommandHandler c = new CommandHandlerBuilder(EventSystem.Instance) .AddPrefix("") .DisposeInSeconds(20) .SetOwner(e.message) .AddCommand( new RuntimeCommandEvent("yes") .Default(async(cont) => { await ConfirmBuyMarriageSlot(cont, costForUpgrade); })) .Build(); EventSystem.Instance.AddPrivateCommandHandler(e.message, c); } }
static async Task Main(string[] args) { Users users = new Users(); ICommandHandler handler = new CommandHandlerBuilder() //.Configure(options => //{ // options.Debug = true; //}) .UseDefault( outputStream: Console.OpenStandardOutput(), encoding: Console.OutputEncoding); while (true) { string input = System.Console.ReadLine(); if (input == "q") { break; } if (input.StartsWith("/")) { string command = input.TrimStart('/'); bool res = await handler.HandleCommandAsync(users.CurrentUser, command); if (res) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully executed command"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Failed to execute command"); } Console.ResetColor(); Console.WriteLine(); } } }
public async Task ValidateBet(EventContext e, Func <EventContext, int, Task> callback = null, int maxBet = 1000000) { if (!string.IsNullOrEmpty(e.arguments)) { int bet; const int noAskLimit = 10000; using (MikiContext context = new MikiContext()) { User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (user == null) { // TODO: add user null error return; } string checkArg = e.arguments.Split(' ')[0]; if (int.TryParse(checkArg, out bet)) { } else if (checkArg.ToLower() == "all" || e.arguments == "*") { bet = user.Currency; } else { await e.ErrorEmbed(e.GetResource("miki_error_gambling_parse_error")) .SendToChannel(e.Channel); return; } if (bet < 1) { await e.ErrorEmbed(e.GetResource("miki_error_gambling_zero_or_less")) .SendToChannel(e.Channel); } else if (bet > user.Currency) { await e.ErrorEmbed(e.GetResource("miki_mekos_insufficient")) .SendToChannel(e.Channel); } else if (bet >= maxBet) { await e.ErrorEmbed($"you cannot bet more than {maxBet} mekos!") .SendToChannel(e.Channel); return; } else if (bet >= noAskLimit) { IDiscordEmbed embed = Utils.Embed; embed.Description = $"Are you sure you want to bet **{bet}**? You currently have `{user.Currency}` mekos.\n\nType `yes` to confirm."; embed.Color = new IA.SDK.Color(0.4f, 0.6f, 1f); await embed.SendToChannel(e.Channel); CommandHandler confirmCommand = new CommandHandlerBuilder(Bot.instance.Events) .AddPrefix("") .DisposeInSeconds(20) .SetOwner(e.message) .AddCommand( new RuntimeCommandEvent("yes") .Default(async(ec) => { await ec.commandHandler.RequestDisposeAsync(); await ec.message.DeleteAsync(); if (callback != null) { await callback(e, bet); } })).Build(); Bot.instance.Events.AddPrivateCommandHandler(e.message, confirmCommand); } else { if (callback != null) { await callback(e, bet); } } } } else { await Utils.ErrorEmbed(e.Channel.GetLocale(), e.GetResource("miki_error_gambling_no_arg")) .SendToChannel(e.Channel); } }
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); } }