Beispiel #1
0
        public InteractionResponseProperties SerializeState()
        {
            bool hasWinner = TryFindWinner(out bool challengerIsWinner);

            var form = new DiscordComponentForm(Client);

            for (int i = 0; i < Grid.Length; i++)
            {
                List <ComponentFormInput> buttons = new List <ComponentFormInput>();

                for (int j = 0; j < Grid[i].Length; j++)
                {
                    int y = i;
                    int x = j;

                    var square = new ComponentFormButton(MessageButtonStyle.Secondary, SerializeSquare(Grid[y][x]))
                    {
                        Disabled = Grid[y][x] != SquareState.Neutral || hasWinner
                    };
                    square.OnClick += (s, e) =>
                    {
                        if (ValidMover(e.Member.User.Id))
                        {
                            Grid[y][x]     = ChallengerTurn ? SquareState.Challenger : SquareState.Challengee;
                            ChallengerTurn = !ChallengerTurn;
                            e.Respond(InteractionCallbackType.UpdateMessage, SerializeState());
                        }
                    };

                    buttons.Add(square);
                }

                form.Rows.Add(buttons);
            }

            var embed = new EmbedMaker()
            {
                Title = "Tic Tac Toe"
            }
            .AddField($"{SerializeSquare(SquareState.Challenger)} Player 1", Challenger.AsMessagable())
            .AddField($"{SerializeSquare(SquareState.Challengee)} Player 2", Challengee.AsMessagable());

            if (hasWinner)
            {
                embed.Description = $"{(challengerIsWinner ? Challenger.AsMessagable() : Challengee.AsMessagable())} won";
            }
            else if (!Grid.Any(row => row.Any(col => col == SquareState.Neutral)))
            {
                embed.Description = "The game resulted in a tie";
            }
            else
            {
                embed.Description = $"{(ChallengerTurn ? Challenger.AsMessagable() : Challengee.AsMessagable())}'s turn";
            }

            return(new InteractionResponseProperties()
            {
                Content = null, Components = form, Embed = embed
            });
        }
        public override InteractionResponseProperties Handle()
        {
            if (Target.Id == Caller.Id)
            {
                return new InteractionResponseProperties()
                       {
                           Content = "You cannot challenge yourself"
                       }
            }
            ;

            var game = new Game(Client, CallerMember.User, Target);

            var deny = new ComponentFormButton(MessageButtonStyle.Secondary, "Deny");

            deny.OnClick += (s, e) =>
            {
                if (e.Member.User.Id == Target.Id)
                {
                    e.Respond(InteractionCallbackType.UpdateMessage, new InteractionResponseProperties()
                    {
                        Components = new List <MessageComponent>(),
                        Content    = $"{Target.AsMessagable()} declined {CallerMember.User.AsMessagable()}'s challenge"
                    });
                }
            };

            var accept = new ComponentFormButton(MessageButtonStyle.Primary, "Accept");

            accept.OnClick += (s, e) =>
            {
                if (e.Member.User.Id == Target.Id)
                {
                    e.Respond(InteractionCallbackType.UpdateMessage, game.SerializeState());
                }
            };

            return(new InteractionResponseProperties()
            {
                Content = $"{CallerMember.User.AsMessagable()} is challenging you, {Target.AsMessagable()}",
                Components = new DiscordComponentForm(Client, new List <List <ComponentFormButton> >()
                {
                    new List <ComponentFormButton>()
                    {
                        deny,
                        accept
                    }
                })
            });
        }
    }