Ejemplo n.º 1
0
        public async Task <ChessChallenge> Challenge(ulong channel, IUser player1, IUser 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);
        }
Ejemplo n.º 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);
                }
            }
        }