Esempio n. 1
0
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Service.GetEventInChannel(ctx.Channel.Id) is AnimalRace)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                    }
                    return;
                }

                var game = new AnimalRace(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-game-ar-start", AnimalRace.MaxParticipants);

                    await this.JoinAsync(ctx);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    if (game.ParticipantCount > 1)
                    {
                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await game.RunAsync(this.Localization);

                        if (game.WinnerIds is { })
                        {
                            await Task.WhenAll(game.WinnerIds.Select(w => gss.UpdateStatsAsync(w, s => s.AnimalRacesWon++)));
                        }
                    }
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                }

                DiscordDmChannel?dm = await ctx.Client.CreateDmChannelAsync(ctx.User.Id);

                if (dm is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-dm-create");
                }

                await dm.LocalizedEmbedAsync(this.Localization, Emojis.Question, this.ModuleColor, "q-game-hm");

                await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Question, "fmt-game-hm", ctx.User.Mention);

                string?word = null;

                for (int i = 0; i < 5; i++)
                {
                    DiscordMessage?reply = await ctx.WaitForDmReplyAsync(dm, ctx.User);

                    if (string.IsNullOrWhiteSpace(reply?.Content))
                    {
                        await ctx.FailAsync("cmd-err-game-hm");

                        return;
                    }
                    if (reply.Content.All(c => char.IsLetter(c)))
                    {
                        word = reply.Content;
                        break;
                    }
                    await dm.LocalizedEmbedAsync(this.Localization, Emojis.Question, this.ModuleColor, "cmd-err-game-hm-format");
                }

                if (word is null)
                {
                    await ctx.FailAsync("cmd-err-game-hm");

                    return;
                }

                await dm.LocalizedEmbedAsync(this.Localization, Emojis.Information, this.ModuleColor, "fmt-game-hm-ok", word);

                var game = new HangmanGame(ctx.Client.GetInteractivity(), ctx.Channel, word, ctx.User);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await game.RunAsync(this.Localization);

                    if (game.Winner is { })
                    {
                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await gss.UpdateStatsAsync(game.Winner.Id, s => s.HangmanWon++);
                    }
                } finally {
Esempio n. 3
0
            public async Task TopAsync(CommandContext ctx)
            {
                GameStatsService          gss      = ctx.Services.GetRequiredService <GameStatsService>();
                IReadOnlyList <GameStats> topStats = await gss.GetTopRussianRouletteStatsAsync();

                string top = await GameStatsExtensions.BuildStatsStringAsync(ctx.Client, topStats, s => s.BuildRussianRouletteStatsString());

                await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "fmt-game-rr-top", top);
            }
Esempio n. 4
0
        private async void GamesHistoryViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Game")
            {
                Plays = await GetGamePlaysAsync();

                GameStats   = new GameStatsService(Plays);
                PlayerStats = new PlayersStatsService(Plays);
            }
        }
Esempio n. 5
0
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Service.GetEventInChannel(ctx.Channel.Id) is RussianRouletteGame)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                    }
                    return;
                }

                var game = new RussianRouletteGame(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-game-rr-start", RussianRouletteGame.MaxParticipants);

                    await this.JoinAsync(ctx);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    if (game.ParticipantCount > 1)
                    {
                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await game.RunAsync(this.Localization);

                        if (game.Survivors.Any())
                        {
                            await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "fmt-winners", game.Survivors.Select(u => u.Mention).JoinWith(", "));

                            await Task.WhenAll(game.Survivors.Select(u => gss.UpdateStatsAsync(u.Id, s => s.RussianRoulettesWon++)));
                        }
                        else
                        {
                            await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Dead, "str-game-rr-alldead");
                        }
                    }
                    else
                    {
                        await ctx.ImpInfoAsync(this.ModuleColor, Emojis.AlarmClock, "str-game-rr-none");
                    }
                } finally {
                    this.Service.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }
Esempio n. 6
0
            public async Task ExecuteGroupAsync(CommandContext ctx,
                                                [Description("desc-game-movetime")] TimeSpan?moveTime = null)
            {
                if (moveTime?.TotalSeconds is < 2 or > 120)
                {
                    throw new InvalidCommandUsageException(ctx, "cmd-err-game-movetime", 2, 120);
                }

                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                }

                DiscordUser?opponent = await ctx.WaitForGameOpponentAsync();

                if (opponent is null)
                {
                    throw new CommandFailedException(ctx, "cmd-err-game-op-none", ctx.User.Mention);
                }

                var game = new Connect4Game(ctx.Client.GetInteractivity(), ctx.Channel, ctx.User, opponent, moveTime);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await game.RunAsync(this.Localization);

                    if (game.Winner is { })
                    {
                        if (game.IsTimeoutReached)
                        {
                            await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "str-game-timeout", game.Winner.Mention);
                        }
                        else
                        {
                            await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "fmt-winners", game.Winner.Mention);
                        }

                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await gss.UpdateStatsAsync(game.Winner.Id, s => s.Connect4Won++);

                        await gss.UpdateStatsAsync(game.Winner == ctx.User?opponent.Id : ctx.User.Id, s => s.Connect4Lost++);
                    }
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Service.GetEventInChannel(ctx.Channel.Id) is NumberRace)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                    }
                    return;
                }

                var game = new NumberRace(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-game-nr-start", NumberRace.MaxParticipants);

                    await this.JoinAsync(ctx);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    if (game.ParticipantCount > 1)
                    {
                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await game.RunAsync(this.Localization);

                        if (game.Winner is { })
                        {
                            if (game.IsTimeoutReached)
                            {
                                await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "cmd-err-game-timeout-w", game.Winner.Mention);
                            }
                            else
                            {
                                await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Trophy, "fmt-winners", game.Winner.Mention);
                            }
                            await gss.UpdateStatsAsync(game.Winner.Id, s => s.NumberRacesWon++);
                        }
Esempio n. 8
0
            public async Task StatsAsync(CommandContext ctx,
                                         [Description("desc-user")] DiscordUser?user = null)
            {
                user ??= ctx.User;
                GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();

                GameStats?stats = await gss.GetAsync(user.Id);

                await ctx.RespondWithLocalizedEmbedAsync(emb => {
                    emb.WithLocalizedTitle("fmt-game-stats", user.ToDiscriminatorString());
                    emb.WithColor(this.ModuleColor);
                    emb.WithThumbnail(user.AvatarUrl);
                    if (stats is null)
                    {
                        emb.WithLocalizedDescription("str-game-stats-none");
                    }
                    else
                    {
                        emb.WithDescription(stats.BuildRussianRouletteStatsString());
                    }
                });
            }
            private async Task HandleQuizResultsAsync(CommandContext ctx, IReadOnlyDictionary <DiscordUser, int> results)
            {
                if (results.Any())
                {
                    var ordered = results.OrderByDescending(kvp => kvp.Value).ToList();

                    if (results.Count > 0)
                    {
                        await ctx.RespondWithLocalizedEmbedAsync(emb => {
                            emb.WithLocalizedTitle("str-results");
                            emb.WithDescription(ordered.Take(10).Select(kvp => $"{kvp.Key.Mention} : {kvp.Value}").JoinWith());
                            emb.WithColor(this.ModuleColor);
                        });

                        if (results.Count > 1)
                        {
                            GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                            await gss.UpdateStatsAsync(ordered.First().Key.Id, s => s.QuizWon++);
                        }
                    }
                }
            }