Exemple #1
0
        public void ComputeRatingNbMatchAbove30()
        {
            var catA = new Cat
            {
                CatId  = "a",
                Looses = 15,
                Wins   = 16,
                Rating = 1220
            };

            var catB = new Cat
            {
                CatId  = "b",
                Looses = 0,
                Wins   = 0,
                Rating = 1200
            };

            var expectedRatingA = 1230;
            var expectedRatingB = 1180;

            var elo = new EloRating(catA.Rating, catB.Rating, true, false, catA.TotalMatches, catB.TotalMatches);

            var(newRatingA, newRatingB) = elo.GetNewResults();

            Assert.AreEqual(expectedRatingA, newRatingA);
            Assert.AreEqual(expectedRatingB, newRatingB);
        }
Exemple #2
0
        public async Task <IActionResult> Result([FromRoute] int matchId, [FromBody] ResultModel model)
        {
            try
            {
                var match = _context.Matches
                            .Include(x => x.CatA)
                            .Include(x => x.CatB)
                            .FirstOrDefault(x => x.MatchId == matchId);

                if (match != null)
                {
                    var catA = match.CatA;
                    var catB = match.CatB;

                    var rating = new EloRating(catA.Rating,
                                               catB.Rating,
                                               model.Result == MatchResult.PLAYER_A_WIN,
                                               model.Result == MatchResult.PLAYER_B_WIN,
                                               catA.TotalMatches,
                                               catB.TotalMatches);

                    var(newRatingA, newRatingB) = rating.GetNewResults();

                    var now = DateTime.UtcNow;

                    catA.Rating = newRatingA;
                    catA.Wins   = model.Result == MatchResult.PLAYER_A_WIN ? catA.Wins + 1 : catA.Wins;
                    catA.Looses = model.Result == MatchResult.PLAYER_B_WIN ? catA.Looses + 1 : catA.Looses;
                    catA.Histories.Add(new History
                    {
                        Date    = now,
                        Rating  = newRatingA,
                        MatchId = matchId
                    });

                    catB.Rating = newRatingB;
                    catB.Wins   = model.Result == MatchResult.PLAYER_B_WIN ? catB.Wins + 1 : catB.Wins;
                    catB.Looses = model.Result == MatchResult.PLAYER_A_WIN ? catB.Looses + 1 : catB.Looses;
                    catB.Histories.Add(new History
                    {
                        Date    = now,
                        Rating  = newRatingB,
                        MatchId = matchId
                    });

                    match.Result = (int)model.Result;

                    await _context.SaveChangesAsync();

                    var winnerId = model.Result == MatchResult.PLAYER_A_WIN ? catA.CatId : catB.CatId;

                    return(Ok(winnerId));
                }

                return(BadRequest());
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }