コード例 #1
0
        public async Task CreateQuickMatchActors()
        {
            var mayur = await Login();

            var matt = await Login("matt", "matt");

            using (var client = new HttpClient {
                BaseAddress = new Uri(ServerUrl)
            })
            {
                client.AcceptJson().AddSessionHeader(mayur.Id);

                var quickMatch = new QuickMatchActors
                {
                    Actors = new List <Guid>(new[] { mayur.Player.Id, matt.Player.Id })
                };

                var matchResponse = await client.PostAsJsonAsync("/api/matches/actors", quickMatch);

                Assert.Equal(HttpStatusCode.Created, matchResponse.StatusCode);

                var matchGet = await matchResponse.Content.ReadAsStringAsync();

                var match = JsonConvert.DeserializeObject <Match>(matchGet, Actor.JsonSerializerSettings());
                Assert.Equal(mayur.Player.Id, match.Tournament.OwnerId);
                Assert.False(match.IsFinished);
                Assert.False(match.IsDeleted);
            }
        }
コード例 #2
0
        public async Task CreateQuickMatchActorsInvalidActor()
        {
            var session = await Login();

            var invalidId = Guid.NewGuid();

            using (var client = new HttpClient {
                BaseAddress = new Uri(ServerUrl)
            })
            {
                client.AcceptJson().AddSessionHeader(session.Id);

                var quickMatch = new QuickMatchActors
                {
                    Actors = new List <Guid>(new[] { session.Player.Id, invalidId })
                };

                var matchResponse = await client.PostAsJsonAsync("/api/matches/actors", quickMatch);

                Assert.Equal(HttpStatusCode.NotFound, matchResponse.StatusCode);

                var content = await matchResponse.Content.ReadAsJsonAsync <ApiError>();

                Assert.Equal($"No Player with Id {invalidId} exists.", content.Error);
            }
        }
コード例 #3
0
        public async Task UpdateMatchRoundScore()
        {
            var mayur = await Login();

            var matt = await Login("matt", "matt");

            using (var client = new HttpClient {
                BaseAddress = new Uri(ServerUrl)
            })
            {
                client.AcceptJson().AddSessionHeader(mayur.Id);

                var quickMatch = new QuickMatchActors
                {
                    Actors = new List <Guid>(new[] { mayur.Player.Id, matt.Player.Id })
                };

                var matchResponse = await client.PostAsJsonAsync("/api/matches/actors", quickMatch);

                Assert.Equal(HttpStatusCode.Created, matchResponse.StatusCode);

                var matchReturn = await matchResponse.Content.ReadAsStringAsync();

                var match = JsonConvert.DeserializeObject <Match>(matchReturn, Actor.JsonSerializerSettings());
                Assert.Equal(mayur.Player.Id, match.Tournament.OwnerId);
                Assert.False(match.IsFinished);
                Assert.False(match.IsDeleted);

                // Update Match Round Score with Valid Id
                // Player 1
                var matchRoundForm = new MatchRoundForm {
                    ActorId = mayur.Player.Id, RoundNumber = 1, Score = 10
                };
                var matchRoundsResponse = await client.PutAsJsonAsync($"/api/matches/{match.Id}/rounds", matchRoundForm);

                Assert.Equal(HttpStatusCode.OK, matchRoundsResponse.StatusCode);

                var matchRoundScore = await matchRoundsResponse.Content.ReadAsJsonAsync <MatchRoundScoreResponse>();

                Assert.Equal(matchRoundForm.Score, matchRoundScore.Score);
                Assert.Equal(matchRoundForm.ActorId, matchRoundScore.ActorId);

                // Player 2
                var matchRoundForm2 = new MatchRoundForm {
                    ActorId = matt.Player.Id, RoundNumber = 1, Score = 5
                };
                matchRoundsResponse = await client.PutAsJsonAsync($"/api/matches/{match.Id}/rounds", matchRoundForm2);

                matchRoundScore = await matchRoundsResponse.Content.ReadAsJsonAsync <MatchRoundScoreResponse>();

                Assert.Equal(matchRoundForm2.Score, matchRoundScore.Score);
                Assert.Equal(matchRoundForm2.ActorId, matchRoundScore.ActorId);
            }
        }
コード例 #4
0
        public async Task UpdateMatchRoundScoreFinishedMatch()
        {
            var mayur = await Login();

            var matt = await Login("matt", "matt");

            using (var client = new HttpClient {
                BaseAddress = new Uri(ServerUrl)
            })
            {
                client.AcceptJson().AddSessionHeader(mayur.Id);

                var quickMatch = new QuickMatchActors
                {
                    Actors = new List <Guid>(new[] { mayur.Player.Id, matt.Player.Id })
                };

                var matchResponse = await client.PostAsJsonAsync("/api/matches/actors", quickMatch);

                Assert.Equal(HttpStatusCode.Created, matchResponse.StatusCode);

                var matchReturn = await matchResponse.Content.ReadAsStringAsync();

                var match = JsonConvert.DeserializeObject <Match>(matchReturn, Actor.JsonSerializerSettings());
                Assert.Equal(mayur.Player.Id, match.Tournament.OwnerId);
                Assert.False(match.IsFinished);
                Assert.False(match.IsDeleted);

                // Finish Match with Valid Id
                var response = await client.DeleteAsync($"/api/matches/{match.Id}");

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var finishedMatch = await response.Content.ReadAsJsonAsync <Match>();

                Assert.True(finishedMatch.IsFinished);

                // Update Finished Match Round Score with Valid Id
                var matchRoundForm = new MatchRoundForm {
                    ActorId = mayur.Player.Id, RoundNumber = 1, Score = 10
                };
                var matchRoundsResponse = await client.PutAsJsonAsync($"/api/matches/{match.Id}/rounds", matchRoundForm);

                Assert.Equal(HttpStatusCode.BadRequest, matchRoundsResponse.StatusCode);

                var content = await matchRoundsResponse.Content.ReadAsJsonAsync <ApiError>();

                Assert.Equal($"This match is already finished.", content.Error);
            }
        }
コード例 #5
0
        public async Task UpdateMatchRoundScoreInvalidRound()
        {
            var mayur = await Login();

            var matt = await Login("matt", "matt");

            using (var client = new HttpClient {
                BaseAddress = new Uri(ServerUrl)
            })
            {
                client.AcceptJson().AddSessionHeader(mayur.Id);

                var quickMatch = new QuickMatchActors
                {
                    Actors = new List <Guid>(new[] { mayur.Player.Id, matt.Player.Id })
                };

                var matchResponse = await client.PostAsJsonAsync("/api/matches/actors", quickMatch);

                Assert.Equal(HttpStatusCode.Created, matchResponse.StatusCode);

                var matchReturn = await matchResponse.Content.ReadAsStringAsync();

                var match = JsonConvert.DeserializeObject <Match>(matchReturn, Actor.JsonSerializerSettings());
                Assert.Equal(mayur.Player.Id, match.Tournament.OwnerId);
                Assert.False(match.IsFinished);
                Assert.False(match.IsDeleted);

                // Update Match Round Score with Invalid Round
                var invalidActorId = Guid.NewGuid();
                var matchRoundForm = new MatchRoundForm {
                    ActorId = invalidActorId, RoundNumber = 1, Score = 10
                };
                var matchRoundsResponse = await client.PutAsJsonAsync($"/api/matches/{match.Id}/rounds", matchRoundForm);

                Assert.Equal(HttpStatusCode.NotFound, matchRoundsResponse.StatusCode);

                var content = await matchRoundsResponse.Content.ReadAsJsonAsync <ApiError>();

                Assert.Equal($"No Actor {invalidActorId} found for this Match.", content.Error);
            }
        }
コード例 #6
0
        public async Task CreateQuickMatchActorsLessThan2Actors()
        {
            var session = await Login();

            using (var client = new HttpClient {
                BaseAddress = new Uri(ServerUrl)
            })
            {
                client.AcceptJson().AddSessionHeader(session.Id);

                var quickMatch = new QuickMatchActors {
                    Actors = new List <Guid>(new[] { session.Player.Id })
                };

                var matchResponse = await client.PostAsJsonAsync("/api/matches/actors", quickMatch);

                Assert.Equal(HttpStatusCode.BadRequest, matchResponse.StatusCode);

                var content = await matchResponse.Content.ReadAsJsonAsync <ApiError>();

                Assert.Equal("Minimum 2 Actors are required for a Match", content.Error);
            }
        }
コード例 #7
0
        public async Task <IActionResult> CreateQuickMatch([FromBody] QuickMatchActors quickMatch)
        {
            if (session?.Player == null)
            {
                return(HttpResponseHelper.NotFound("No Session/Player found."));
            }

            if (!ModelState.IsValid)
            {
                return(HttpResponseHelper.BadRequest(ModelState));
            }

            if (quickMatch.Actors.Count < 2)
            {
                return(HttpResponseHelper.BadRequest("Minimum 2 Actors are required for a Match"));
            }

            QuickMatchResult result;

            IList <Player> players = new List <Player>();
            IList <Group>  groups  = new List <Group>();

            if (quickMatch.Type == MatchType.Player)
            {
                foreach (var actorId in quickMatch.Actors)
                {
                    var player = await _context.Players.FindAsync(actorId);

                    if (player == null)
                    {
                        return(HttpResponseHelper.NotFound($"No Player with Id {actorId} exists."));
                    }

                    players.Add(player);
                }

                result = await QuickMatch(quickMatch, players);
            }
            else
            {
                foreach (var actorId in quickMatch.Actors)
                {
                    var group = await _context.Groups.FindAsync(actorId);

                    if (group == null)
                    {
                        return(HttpResponseHelper.NotFound($"No Group with Id {actorId} exists."));
                    }

                    groups.Add(group);
                }

                result = await QuickMatch(quickMatch, groups);
            }

            if (result.error != null)
            {
                return(result.error);
            }

            return(CreatedAtRoute("GetMatchDetailed", new { id = result.match.Id }, result.match));
        }