Exemple #1
0
        public async void GetHexbot_Should_Deserialize_SendAsync_Response()
        {
            Func <HttpResponseMessage, Task <HexbotResponse> > responseMapper = null;
            await _apiClient.SendAsync(Arg.Any <HttpRequestMessage>(), Arg.Do <Func <HttpResponseMessage, Task <HexbotResponse> > >(a => responseMapper = a));

            await _client.GetHexbot(null, null, null, null);

            var content = new HexbotResponse
            {
                Colors = new []
                {
                    new HexbotResponseColor
                    {
                        Value       = "value1",
                        Coordinates = new HexbotResponseCoordinates
                        {
                            X = 123,
                            Y = 456
                        }
                    }
                }
            };

            var stringContent = JsonConvert.SerializeObject(content);
            var response      = new HttpResponseMessage
            {
                Content = new StringContent(stringContent)
            };

            var actual = await responseMapper(response);

            actual.Should().BeEquivalentTo(content);
        }
Exemple #2
0
        public async void GetHexbot_Should_Return_Correctly()
        {
            var response = new HexbotResponse();

            _apiClient.SendAsync(Arg.Any <HttpRequestMessage>(), Arg.Any <Func <HttpResponseMessage, Task <HexbotResponse> > >()).Returns(response);

            var actual = await _client.GetHexbot(null, null, null, null);

            actual.Should().Be(response);
        }
Exemple #3
0
        private async Task <Image <Rgb24> > AddHexbotAnimation(Image <Rgb24> image, int count, int width, int height, string seed)
        {
            _logger.LogDebug($"Adding the responses from hexbot API as an animation in the image...");

            var            frameCount = 0;
            HexbotResponse hexbot     = null;

            foreach (var frame in image.Frames)
            {
                var needsAnimation = (frameCount++ % Constants.FRAME_PER_HEXBOT == 0);
                hexbot = needsAnimation ? null : hexbot;
                if (needsAnimation)
                {
                    _logger.LogDebug($"Calling hexbot API once every {Constants.FRAME_PER_HEXBOT} frame(s)...");
                }

                hexbot = hexbot ?? await GetHexbot(count, width, height, seed);

                if (hexbot == null)
                {
                    _logger.LogDebug($"Received invalid hexbot response. Skipping Hexbotification of this frame...");
                }

                foreach (var color in hexbot?.Colors ?? new HexbotResponseColor[0])
                {
                    if (color.Coordinates?.X == null || color.Coordinates?.Y == null)
                    {
                        _logger.LogTrace($"Missing/incomplete coordinates (x={color.Coordinates?.X.ToString() ?? "null"}, y={color.Coordinates?.Y.ToString() ?? "null"}) received for item ({color.Value}). Skipping...");
                        continue;
                    }

                    var hex = color.Value.TrimStart('#');
                    var r   = Convert.ToByte(hex.Substring(0, 2), 16);
                    var g   = Convert.ToByte(hex.Substring(2, 2), 16);
                    var b   = Convert.ToByte(hex.Substring(4, 2), 16);

                    _logger.LogTrace($"Updating pixel (x={color.Coordinates.X}, y={color.Coordinates.Y}) color to {color.Value} (r={r}, g={g}, b={b})...");
                    frame[color.Coordinates.X, color.Coordinates.Y] = new Rgb24(r, g, b);
                }
            }

            return(image);
        }