private async Task SpawnItems(IReadOnlyList <Item> items)
        {
            if (items.Count > MaxRequestCount)
            {
                var clamped = $"Users are limited to {MaxRequestCount} items per command. Please use this bot responsibly.";
                await ReplyAsync(clamped).ConfigureAwait(false);

                items = items.Take(MaxRequestCount).ToArray();
            }

            var bot    = Globals.Bot;
            var fi     = bot.FieldItemState;
            var cfg    = fi.Config;
            var height = cfg.GetSpawnHeight(items.Count);

            (int x, int y) = fi.GetNextInjectCoordinates(items.Count, height);
            string atCoords = $"at coordinates ({x},{y}) (count:{items.Count}, height:{Math.Min(items.Count, height)})";

            bool canInject = FieldItemDropper.CanFitDropped(x, y, items.Count, height, cfg.SpawnMinX, cfg.SpawnMaxX, cfg.SpawnMinY, cfg.SpawnMaxY);

            if (!canInject)
            {
                await ReplyAsync($"Unable to inject {atCoords}. Please confirm the bot is configured correctly, and contact the owner.").ConfigureAwait(false);

                return;
            }

            var column = FieldItemDropper.InjectItemsAsDropped(x, y, items, height);

            fi.Injections.Enqueue(column);
            var msg = $"Item spawn request{(items.Count > 1 ? "s" : string.Empty)} {atCoords} will be executed momentarily.";

            await ReplyAsync(msg).ConfigureAwait(false);
        }
        public void CoordinateTest()
        {
            var bc  = new BotConfig();
            var cfg = bc.FieldItemConfig;

            cfg.ValidateCoordinates().Should().Be(0);
            var bot = new Bot(bc);

            const int count = 5;

            (int x, int y) = bot.FieldItemState.GetNextInjectCoordinates(count, count);
            var canDrop = FieldItemDropper.CanFitDropped(x, y, count, count, cfg.SpawnMinX, cfg.SpawnMaxX, cfg.SpawnMinY, cfg.SpawnMaxY);

            canDrop.Should().BeTrue();
        }
        public void SpawnMany()
        {
            var bc  = new BotConfig();
            var cfg = bc.FieldItemConfig;

            cfg.ValidateCoordinates().Should().Be(0);
            var bot = new Bot(bc);

            for (int i = 0; i < 100; i++)
            {
                const int count = 5;
                (int x, int y) = bot.FieldItemState.GetNextInjectCoordinates(count, count);
                var canDrop = FieldItemDropper.CanFitDropped(x, y, count, count, cfg.SpawnMinX, cfg.SpawnMaxX, cfg.SpawnMinY, cfg.SpawnMaxY);
                canDrop.Should().BeTrue();
                Debug.WriteLine($"Dropped at {x},{y}.");
            }
        }
Esempio n. 4
0
        private async Task SpawnItems(IReadOnlyList <Item> items)
        {
            if (items.Count > MaxRequestCount)
            {
                var clamped = $"Users are limited to {MaxRequestCount} items per command. Please use this bot responsibly.";
                await ReplyAsync(clamped).ConfigureAwait(false);

                items = items.Take(MaxRequestCount).ToArray();
            }

            var bot    = Globals.Bot;
            var fi     = bot.FieldItemState;
            var cfg    = fi.Config;
            var height = cfg.GetSpawnHeight(items.Count);

            (int x, int y) = fi.GetNextInjectCoordinates(items.Count, height);
            string atCoords = $"at coordinates ({x},{y}) (count:{items.Count}, height:{Math.Min(items.Count, height)})";

            bool canInject = FieldItemDropper.CanFitDropped(x, y, items.Count, height, cfg.SpawnMinX, cfg.SpawnMaxX, cfg.SpawnMinY, cfg.SpawnMaxY);

            if (!canInject)
            {
                await ReplyAsync($"Unable to inject {atCoords}. Please confirm the bot is configured correctly, and contact the owner.").ConfigureAwait(false);

                return;
            }

            var column  = FieldItemDropper.InjectItemsAsDropped(x, y, items, height);
            var mention = Context.User.Mention;
            var request = new SpawnRequest(Context.User.Username, Context.User.Id, column, items)
            {
                OnFinish = success =>
                {
                    var reply = success
                        ? "Items have been spawned on the map. Please pick them up!"
                        : "Failed to spawn items. Please tell the bot owner to look at the logs!";
                    Task.Run(async() => await ReplyAsync($"{mention}: {reply}").ConfigureAwait(false));
                }
            };

            fi.Injections.Enqueue(request);
            var msg = $"{mention}: Item spawn request{(items.Count > 1 ? "s" : string.Empty)} {atCoords} have been added to the queue and will be spawned momentarily.";

            await ReplyAsync(msg).ConfigureAwait(false);
        }