Example #1
0
        public async Task <ChessChallenge> Challenge(ulong channel, ulong player1, ulong player2, Action <ChessChallenge> onTimeout = null)
        {
            if (await PlayerIsInGame(channel, player1))
            {
                throw new ChessException($"{player1.Mention()} is currently in a game.");
            }

            if (await PlayerIsInGame(channel, player2))
            {
                throw new ChessException($"{player2.Mention()} is currently in a game.");
            }

            if (_challenges.Any(x => x.Channel == channel && x.Challenged == player1 && x.Challenger == player2))
            {
                throw new ChessException($"{player1.Mention()} has already challenged {player2.Mention()}.");
            }

            var challenge = new ChessChallenge {
                ChallengeDate = DateTime.UtcNow, Channel = channel, Challenger = player1, Challenged = player2
            };

            _challenges.Add(challenge);

            RemoveChallenge(challenge, onTimeout);

            return(challenge);
        }
Example #2
0
        private async void RemoveChallenge(ChessChallenge challenge, Action <ChessChallenge> onTimeout)
        {
            await Task.Delay(_confirmationsTimeout);

            if (_challenges.Contains(challenge))
            {
                _challenges.Remove(challenge);
                if (onTimeout != null)
                {
                    onTimeout(challenge);
                }
            }
        }