示例#1
0
        public void Bench()
        {
            var random      = new Random();
            var contestants = Enumerable.Range(0, 40)
                              .Select(_ => new Contestant(MilitaryRank.Private, new FullName("А.", "Спортсмен", "Б.")))
                              .ToArray();

            _contestantsDb.OnNext(new ContestantsDb(contestants));
            var gameResults = Enumerable.Range(0, 10000)
                              .Select(_ =>
            {
                var firstContestantIndex  = random.Next(0, contestants.Length);
                var secondContestantIndex = random.Next(0, contestants.Length);
                while (firstContestantIndex == secondContestantIndex)
                {
                    secondContestantIndex = random.Next(0, contestants.Length);
                }
                return(new GameResult(
                           new ContestantScore(contestants[firstContestantIndex], 11),
                           new ContestantScore(contestants[secondContestantIndex], random.Next(0, 10))));
            })
                              .ToArray();
            var gamesDb = new GamesDb(gameResults);

            _gamesDb.OnNext(gamesDb);
        }
示例#2
0
        public void LoadOrNew()
        {
            var doesContestantsDbExist = File.Exists(ContestantsDbFilePath);

            if (doesContestantsDbExist)
            {
                LoadContestantsDb();
            }
            else
            {
                _contestantsDb.OnNext(new ContestantsDb());
            }

            if (File.Exists(GamesDbFilePath))
            {
                if (!doesContestantsDbExist)
                {
                    throw new Exception();
                }
                LoadGamesDb();
            }
            else
            {
                var gamesDb = new GamesDb();
                _gamesDb.OnNext(gamesDb);
            }
        }
示例#3
0
        /// <summary>
        /// Собирает и возвращает статистику для выбранного спортсмена.
        /// </summary>
        /// <param name="gamesDb">БД матчей.</param>
        /// <param name="contestantGuid">Guid спортсмена.</param>
        public static ContestantStatistics GetForContestant(GamesDb gamesDb, Guid contestantGuid)
        {
            var gamesWithContestant = gamesDb.GamesResults.Where(result => result.DidContestantTakePart(contestantGuid))
                                      .ToArray();
            var gamesTotal              = gamesWithContestant.Length;
            var wins                    = gamesWithContestant.Count(result => result.GetWinnerGuid() == contestantGuid);
            var losses                  = gamesWithContestant.Length - wins;
            var isCalibrated            = gamesTotal >= 10;
            var winTotalRatio           = isCalibrated ? 1.0 * wins / gamesTotal : 0;
            var winTotalRatioPercentage = winTotalRatio * 100;

            return(new ContestantStatistics(
                       contestantGuid,
                       gamesTotal,
                       wins,
                       losses,
                       isCalibrated,
                       winTotalRatio,
                       winTotalRatioPercentage));
        }