コード例 #1
0
        private async Task ExecuteGameInputAsync(IMessagesGame game, SocketUserMessage message)
        {
            var gameMessage = await game.GetMessageAsync();

            log.Verbose(
                $"Input {message.Content} by {message.Author.FullName()} in {message.Channel.FullName()}",
                game.GameName);

            await game.InputAsync(message.Content, message.Author.Id);

            if (game is MultiplayerGame mGame)
            {
                while (mGame.BotTurn)
                {
                    await mGame.BotInputAsync();
                }
            }

            if (game.State != GameState.Active)
            {
                games.Remove(game);
            }

            if (gameMessage != null && message.Channel.BotCan(ChannelPermission.ManageMessages))
            {
                game.CancelRequests();
                try { await gameMessage.ModifyAsync(game.GetMessageUpdate(), game.GetRequestOptions()); }
                catch (OperationCanceledException) { }

                await message.DeleteAsync(PmBot.DefaultOptions);
            }
            else
            {
                game.CancelRequests();
                try
                {
                    var newMsg = await message.Channel.SendMessageAsync(game.GetContent(), false, game.GetEmbed()?.Build(), game.GetRequestOptions());

                    game.MessageId = newMsg.Id;
                }
                catch (OperationCanceledException) { }

                if (gameMessage != null)
                {
                    await gameMessage.DeleteAsync(PmBot.DefaultOptions);
                }
            }
        }
コード例 #2
0
        private void DeleteOldGames(object state)
        {
            var now   = DateTime.Now;
            int count = 0;
            var removedChannelGames = new List <IChannelGame>();

            foreach (var game in _games.AllGames.Where(g => now - g.LastPlayed > g.Expiry).ToArray())
            {
                count++;
                game.State = GameState.Cancelled;
                _games.Remove(game, false);
                if (game is IChannelGame cGame)
                {
                    removedChannelGames.Add(cGame);
                }
            }

            if (count > 0)
            {
                _log.Debug($"Removed {count} expired game{"s".If(count > 1)}");
            }

            if (removedChannelGames.Count is > 0 and < 10)
            {
                Task.Run(async() =>
                {
                    foreach (var game in removedChannelGames)
                    {
                        try { await game.UpdateMessageAsync(); }
                        catch { }
                    }
                });
            }
        }
コード例 #3
0
        private async Task ExecuteGameInputAsync(IMessagesGame game, IUserMessage message)
        {
            var gameMessage = await game.GetMessage();

            await logger.Log(LogSeverity.Verbose, game.GameName,
                             $"Input {message.Content} by {message.Author.FullName()} in {message.Channel.FullName()}");

            game.Input(message.Content, message.Author.Id);
            if (game is MultiplayerGame mGame)
            {
                while (mGame.BotTurn)
                {
                    mGame.BotInput();
                }
            }
            if (game.State != State.Active)
            {
                games.Remove(game);
            }

            game.CancelRequests();
            var requestOptions = game.GetRequestOptions();

            if (gameMessage != null && message.Channel.BotCan(ChannelPermission.ManageMessages))
            {
                await gameMessage.ModifyAsync(game.GetMessageUpdate(), requestOptions);

                await message.DeleteAsync(Bot.DefaultOptions);
            }
            else
            {
                var newMsg = await message.Channel.SendMessageAsync(game.GetContent(), false, game.GetEmbed()?.Build(), requestOptions);

                game.MessageId = newMsg.Id;
                if (gameMessage != null)
                {
                    await gameMessage.DeleteAsync(Bot.DefaultOptions);
                }
            }
        }
コード例 #4
0
        private async void DeleteOldGames(object state)
        {
            var now   = DateTime.Now;
            int count = 0;

            var removedChannelGames = new List <IChannelGame>();

            var expiredGames = games.AllGames.Where(g => now - g.LastPlayed > g.Expiry).ToArray();

            foreach (var game in expiredGames)
            {
                count++;
                game.State = GameState.Cancelled;
                games.Remove(game, false);

                if (game is IChannelGame cGame)
                {
                    removedChannelGames.Add(cGame);
                }
            }


            if (count > 0)
            {
                log.Info($"Removed {count} expired game{"s".If(count > 1)}", LogSource.Scheduling);
            }


            if (client?.LoginState == LoginState.LoggedIn)
            {
                foreach (var game in removedChannelGames)
                {
                    try
                    {
                        var gameMessage = await game.GetMessageAsync();

                        if (gameMessage != null)
                        {
                            await gameMessage.ModifyAsync(game.GetMessageUpdate(), PmBot.DefaultOptions);
                        }
                    }
                    catch (HttpException) { } // Something happened to the message, we can ignore it
                }
            }
        }