public async Task <MatchIds> FindMatchAsync() { int max = await Context.Ranks.MaxAsync(a => a.Elo).ConfigureAwait(false); int min = await Context.Ranks.MinAsync(a => a.Elo).ConfigureAwait(false); Random seed = new Random(); IList <Rank> superSet = null; int currentMax = max; while (currentMax != 0 && (superSet == null || superSet.Count < 2)) { currentMax = (superSet == null) ? max : max / 2; FindBoundaries(min, currentMax, seed, out int lowerBound, out int higherBound); superSet = Context.Ranks.OrderBy(e => e.Elo).Where(e => e.Elo <higherBound && e.Elo> lowerBound).ToList(); } if (superSet == null || superSet.Count < 2) { throw new ServiceException("Couldn't find a match", ResponseCode.MatchDispersion); } var ids = new MatchIds() { Left = 0, Right = 0 }; ids.Left = seed.Next(0, superSet.Count); ids.Right = (ids.Left == 0) ? 1 : ids.Left - 1; return(ids); }
public async Task ShouldFindMatch_WhenCatsAreAvailable() { using (var dbContext = DbContextFixture.GetDbContext()) { IMatchService service = new MatchService(dbContext, new RankingService(RankingValues.Limit, RankingValues.EvolutionCoef)); ICatService catService = new CatService(new HttpService(), dbContext, IConfigMock.GetCatsMock()); await catService.InjectCats().ConfigureAwait(false); // Prepare cats MatchIds matchIds = await service.FindMatchAsync().ConfigureAwait(false); Assert.NotEqual(0, matchIds.Left); Assert.NotEqual(0, matchIds.Right); } }
public async Task <IActionResult> GetMatch() { MatchIds matchIds = await MatchService.FindMatchAsync().ConfigureAwait(false); var options = new RequestOptions() { IncludeMatchHistory = false }; Cat left = await CatService.Get(matchIds.Left, options).ConfigureAwait(false); Cat right = await CatService.Get(matchIds.Right, options).ConfigureAwait(false); MatchFoundResponse matchFound = new MatchFoundResponse() { Left = left.ToApiModel(), Right = right.ToApiModel() }; var response = new CommonResponse <MatchFoundResponse>(ResponseCode.Success, "Ok", matchFound); return(Ok(response)); }
public Matches(MatchListBson bson) { MatchIds.AddRange(bson.Matches); Id = bson.Id; }