예제 #1
0
        public JsonResult AddLapResultInMilliseconds([FromBody] RequestObject.LapResultMilliseconds lapResultMilliseconds)
        {
            _logger.LogInformation("POST /AddLapResultInMilliseconds \n\tBODY: ipAddress:" + lapResultMilliseconds.ipAddress + " | lapTime: " + lapResultMilliseconds.lapTime);
            try
            {
                IPAddress ipAddress = IPAddress.Parse(lapResultMilliseconds.ipAddress);
                TimeSpan  lapTime   = TimeSpan.FromMilliseconds(lapResultMilliseconds.lapTime);
                _raceManager.AddLapResult(ipAddress, lapTime);
                return(Json(new ResponseObject {
                    responseMessage = "success"
                }));
            }
            catch (Exception error)
            {
                _logger.LogError("RaceTimer/AddLapResult Exception: " + error.Message);

                if (error is KeyNotFoundException)
                {
                    return(Json(new ResponseObject
                    {
                        responseMessage = "The given IP address '" + lapResultMilliseconds.ipAddress + "' is not registered."
                    }));
                }

                ResponseObject response = new ResponseObject
                {
                    responseMessage = error.Message,
                };

                return(new JsonResult(response)
                {
                    StatusCode = (int)HttpStatusCode.BadRequest
                });
            }
        }
예제 #2
0
 public StatusCodeResult AnnounceLapTime([FromBody] RequestObject.LapResultMilliseconds lapTimeMilliseconds)
 {
     try
     {
         _lapTimeAnnouncer.Announce(lapTimeMilliseconds.lapTime / 1000);
         return(new StatusCodeResult((int)HttpStatusCode.OK));
     }
     catch (Exception error)
     {
         _logger.LogError("RaceTimer/AnnounceLapTime Exception: " + error);
         return(new StatusCodeResult((int)HttpStatusCode.InternalServerError));
     }
 }
예제 #3
0
        public async Task AnnounceLapTime()
        {
            var lapResult = new RequestObject.LapResultMilliseconds
            {
                lapTime = 1100
            };
            var jsonString = JsonSerializer.Serialize(lapResult);
            var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(prefix + "/AnnounceLapTime", lapContent);

            response.EnsureSuccessStatusCode();
        }
예제 #4
0
        public async Task AddLapResult_Success()
        {
            string ipAddress = "10.1.1.1";

            var registerResponse = await _httpClient.GetAsync(prefix + "/Register/" + ipAddress);

            registerResponse.EnsureSuccessStatusCode();

            var lapResult = new RequestObject.LapResult
            {
                ipAddress = ipAddress,
                lapTime   = "0:0:1:12"
            };
            var jsonString = JsonSerializer.Serialize(lapResult);
            var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(prefix + "/AddLapResultAsString", lapContent);

            var responseObject = JsonSerializer.Deserialize <ResponseObject>(
                await response.Content.ReadAsStringAsync());

            Assert.Equal("success", responseObject.responseMessage);

            var lapResultMillis = new RequestObject.LapResultMilliseconds
            {
                ipAddress = ipAddress,
                lapTime   = 123456
            };
            var jsonString2 = JsonSerializer.Serialize(lapResultMillis);
            var lapContent2 = new StringContent(jsonString2, Encoding.UTF8, "application/json");

            var response2 = await _httpClient.PostAsync(prefix + "/AddLapResultInMilliseconds", lapContent2);

            var responseObject2 = JsonSerializer.Deserialize <ResponseObject>(
                await response2.Content.ReadAsStringAsync());

            Assert.Equal("success", responseObject2.responseMessage);
        }
예제 #5
0
        public async Task GetLastRaceResultById_MultipleIDs()
        {
            string ip1      = "1.1.1.1";
            string ip2      = "2.2.2.2";
            string ip3      = "3.3.3.3";
            var    response = await _httpClient.GetAsync(prefix + "/SetMaxParticipants/" + 3);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/Register/" + ip1);

            response.EnsureSuccessStatusCode();
            response = await _httpClient.GetAsync(prefix + "/Register/" + ip2);

            response.EnsureSuccessStatusCode();
            var ip2response = JsonSerializer.Deserialize <ResponseObject.Register>(
                await response.Content.ReadAsStringAsync());
            int id2 = ip2response.id;

            response = await _httpClient.GetAsync(prefix + "/Register/" + ip3);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/SetRaceStartCountdownDuration/" + 0);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/StartRace");

            response.EnsureSuccessStatusCode();

            // this test is dependent on the default number of laps being 10 (haven't implement a way to change it yet)
            for (int lap = 1; lap <= 10; lap++)
            {
                var lapResult = new RequestObject.LapResultMilliseconds
                {
                    lapTime = 61010
                };

                if (lap == 3)
                {
                    lapResult.lapTime = 59015; // fastest lap
                }

                foreach (string ipAddress in new List <string> {
                    ip3, ip2, ip1
                })
                {
                    lapResult.ipAddress = ipAddress;
                    var jsonString = JsonSerializer.Serialize(lapResult);
                    var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    await _httpClient.PostAsync(prefix + "/AddLapResultInMilliseconds", lapContent);
                }
            }

            response = await _httpClient.GetAsync(prefix + "/GetLastRaceResultById/" + id2);

            response.EnsureSuccessStatusCode();
            var raceResult = JsonSerializer.Deserialize <ResponseObject.RaceResultByID>(
                await response.Content.ReadAsStringAsync());

            Assert.Equal("success", raceResult.responseMessage);
            Assert.Equal(id2, raceResult.id);
            Assert.Equal(2, raceResult.place);
            Assert.Equal("00:10:08.1050000", raceResult.overallTime);
            Assert.Equal(608105, raceResult.overallTimeMilliseconds);
            Assert.Equal("00:00:59.0150000", raceResult.fastestLap);
            Assert.Equal(59015, raceResult.fastestLapMilliseconds);
            Assert.Equal(3, raceResult.fastestLapNumber);
        }