Ejemplo n.º 1
0
        private async Task ConnectAsync(IUser playerTwo = null)
        {
            await Context.Message.DeleteAsync();

            if (playerTwo == null)
            {
                BotConfig conf  = BotConfig.Load();
                var       gconf = conf.GetConfig(Context.Guild.Id);
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Incorrect Command Usage", $"Correct Usage: `{gconf.Prefix}c4 <@user>`", false);

                return;
            }

            IUser playerOne = Context.User as IUser;

            if (playerTwo.IsBot || playerOne.Id == playerTwo.Id)
            {
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "You can not play against yourself or a bot.", false);

                return;
            }

            if (GameHandler.DoesGameExist(Context.Guild.Id, GameType.CONNECT))
            {
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "There is already a game in this guild.", false);

                return;
            }

            ulong[]     players = { playerOne.Id, playerTwo.Id };
            ConnectGame newGame = new ConnectGame(Context.Guild.Id, players);

            GameHandler.AddNewGame(newGame);

            string render        = newGame.RenderGame();
            IUser  currentPlayer = await Context.Guild.GetUserAsync(newGame.Players[newGame.Turn]);

            var msg = await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" +
                                                          $"Next Up: {currentPlayer.Mention}\n" +
                                                          $"`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 <column>` to take your turn\n`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 end` to end the game");

            newGame.RenderId = msg.Id;
        }
Ejemplo n.º 2
0
        private async Task ConnectTurnAsunc(int column = -1)
        {
            await Context.Message.DeleteAsync();

            if (column < 1 || column > 7)
            {
                BotConfig conf  = BotConfig.Load();
                var       gconf = conf.GetConfig(Context.Guild.Id);
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Incorrect Command Usage", $"Correct Usage: `{gconf.Prefix}c4 <column>` - Column must be 1 - 7", false);

                return;
            }

            if (!GameHandler.DoesGameExist(Context.Guild.Id, GameType.CONNECT))
            {
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "There is not a game in this guild.", false);

                return;
            }

            ConnectGame game = (ConnectGame)GameHandler.GetGame(Context.Guild.Id, GameType.CONNECT);

            if (game == null)
            {
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "The game could not be found.", false);

                return;
            }

            if (game.Players[0] != Context.User.Id && game.Players[1] != Context.User.Id)
            {
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "You are not part of this game...");

                return;
            }

            if (game.Players[game.Turn] != Context.User.Id)
            {
                await MessageUtil.SendErrorAsync((Context.Channel as ITextChannel), "Connect4 Error", "It is not your turn...");

                return;
            }

            GameHandler.TakeTurn(Context.Guild.Id, GameType.CONNECT, Context.User.Id, column);
            ulong winner = GameHandler.CheckForWinner(Context.Guild.Id, GameType.CONNECT);
            bool  draw   = GameHandler.CheckForDraw(Context.Guild.Id, GameType.CONNECT);

            IMessage oldMsg = await Context.Channel.GetMessageAsync(game.RenderId);

            await oldMsg.DeleteAsync();

            if (winner == 0L)
            {
                if (!draw)
                {
                    var nextUp = await Context.Guild.GetUserAsync(game.Players[game.Turn]);

                    string render = game.RenderGame();
                    var    msg    = await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" +
                                                                        $"Next Up: {nextUp.Mention}\n" +
                                                                        $"`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 <column>` to take your turn\n`{CommandHandler.GetPrefix(Context.Guild.Id)}c4 end` to end the game");

                    game.RenderId = msg.Id;
                }
                else
                {
                    string render = game.RenderGame();
                    await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" +
                                                        $"DRAW ({(await Context.Guild.GetUserAsync(game.Players[0])).Mention} v {(await Context.Guild.GetUserAsync(game.Players[1])).Mention})");

                    if (ProfileDatabase.GetUser(game.Players[0]) != null)
                    {
                        ProfileDatabase.AddCurrency(game.Players[0], 100);
                    }
                    if (ProfileDatabase.GetUser(game.Players[1]) != null)
                    {
                        ProfileDatabase.AddCurrency(game.Players[1], 100);
                    }
                    GameHandler.EndGame(game);
                }
            }
            else
            {
                string render = game.RenderGame();
                await Context.Channel.SendFileAsync(render, $"**Connect 4**\n" +
                                                    $"Game Won by " + (await Context.Guild.GetUserAsync(winner)).Mention);

                var winwin = ProfileDatabase.GetUser(winner);
                if (winwin != null)
                {
                    ProfileDatabase.AddCurrency(winner, 250);
                    await LeaderboardDatabase.CheckAsync(winner, winwin.Name, "Connect 4");

                    await LeaderboardDatabase.AddScoreAsync(winner, "Connect 4");
                }
                GameHandler.EndGame(game);
            }
        }