Esempio n. 1
0
        private Team GetTeam(Round currentRound, string teamName)
        {
            if (teamName.StartsWith("*Random"))
            {
                GenerateRandomRound(currentRound, teamName.Remove(teamName.Length - 1).Remove(0, 8));
                return(null);
            }

            if (teamName.StartsWith("*BestThird"))
            {
                var index = Convert.ToInt32(teamName.Remove(teamName.Length - 1).Remove(0, 11)) - 1;
                GenerateBestThirdRound(currentRound);
                return(_bestThirds[index]);
            }

            var   teamInfos = teamName.Remove(0, 1).Split('-');
            Round targetRound;
            var   key = teamInfos[0];

            if (!_rounds.TryGetValue(key, out targetRound))
            {
                targetRound = RoundFactory.GetRound("", key, _competition,
                                                    _results.ContainsKey(key) ? _results[key] : new List <CompetitionResult>(), _games[key]);
                _rounds[key] = targetRound;
                SimulateRound(targetRound);
            }

            var newTeam = targetRound.GetTeam(int.Parse(teamInfos[1]));

            return(newTeam.RealTeam.Value ? newTeam : GetTeam(targetRound, newTeam));
        }
Esempio n. 2
0
        private SimulationResult SimulateCompetition()
        {
            _rounds.Clear();
            _bestThirds = null;
            foreach (var game in _games)
            {
                if (_rounds.ContainsKey(game.Key))
                {
                    continue;
                }

                var round = RoundFactory.GetRound("", game.Key, _competition, _results.ContainsKey(game.Key) ? _results[game.Key] : new List <CompetitionResult>(), game.Value);
                _rounds.Add(game.Key, round);
                SimulateRound(round);
            }

            var results = new Dictionary <int, int>(_prizes.Count);

            foreach (var prize in _prizes)
            {
                Team team;
                if (!prize.RoundKey.StartsWith("*"))
                {
                    var teamName = prize.RoundKey.Trim();
                    team = Teams.Values.First(x => x.Name == teamName);
                }
                else
                {
                    team = GetTeam(null, prize.RoundKey);
                }

                results[team.Id] = prize.Value;
            }

            return(new SimulationResult(results));
        }
Esempio n. 3
0
        private void GenerateRandomRound(Round round, string parameters)
        {
            var param                = parameters.Split(',');
            var roundToGenerate      = int.Parse(param[0]);
            var teamPerRound         = int.Parse(param[1]);
            var originalGroups       = param[2].Split('-');
            var canBeFromSameGroup   = bool.Parse(param[3]);
            var canBeFromSameCountry = bool.Parse(param[4]);

            var selectionPerGroup = (roundToGenerate * teamPerRound) / originalGroups.Count();
            var teams             = new List <List <Team> >();

            for (int i = 1; i <= selectionPerGroup; i++)
            {
                var hat = originalGroups.Select(originalGroup => GetTeam(round, string.Format("*{0}-{1}", originalGroup, i))).ToList();
                teams.Add(hat);
            }

            var permutations = new List <List <int> >();
            var initialList  = Enumerable.Range(0, originalGroups.Count()).ToList();

            initialList.Shuffle();
            permutations.Add(initialList);
            for (int i = 1; i < selectionPerGroup; i++)
            {
                permutations.Add(initialList.Permute(canBeFromSameGroup));
            }

            int count            = 0;
            int originalRoundKey = int.Parse(round.RoundKey);

            var originalTeams = round.Teams;

            originalTeams.Clear();
            while (originalTeams.Count < teamPerRound)
            {
                for (int i = 0; i < selectionPerGroup; i++)
                {
                    originalTeams.Add(teams[i][permutations[i][count]]);
                }
                count++;
            }

            for (int i = 1; i < roundToGenerate; i++)
            {
                originalRoundKey++;
                originalTeams = new List <Team>();
                while (originalTeams.Count < teamPerRound)
                {
                    for (int j = 0; j < selectionPerGroup; j++)
                    {
                        originalTeams.Add(teams[j][permutations[j][count]]);
                    }
                    count++;
                }

                var newRound = RoundFactory.GetRound("", originalRoundKey.ToString(), round.HomeAndAway, _competition,
                                                     new List <CompetitionResult>(), originalTeams);
                _rounds[originalRoundKey.ToString()] = newRound;
                SimulateRound(newRound);
            }
        }