Пример #1
0
        /// <summary>
        /// Complete the <see cref="Points"/> collection with dummy values with the <paramref name="pointsValue"/>
        /// as the points value of the remaining points up to the team size.
        /// </summary>
        /// <param name="teamSize">size of the team</param>
        /// <param name="pointsValue">value of remaining points</param>
        public void Complete(
            int teamSize,
            int pointsValue)
        {
            this.VirtualAthletePoints = pointsValue;

            if (this.Points.Count < teamSize)
            {
                while (this.Points.Count < teamSize)
                {
                    ICommonTeamTrophyPoints virtualPoint =
                        new CommonTeamTrophyPoints(
                            pointsValue,
                            string.Empty,
                            -1,
                            false,
                            this.Date);
                    this.Points.Add(virtualPoint);
                }
            }
            else
            {
                // TODO Trim long points totals?
            }
        }
Пример #2
0
        /// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
        /// <name>SaveAthleteSeasonData</name>
        /// <date>30/03/15</date>
        /// <summary>
        /// Reads the athlete season details xml from file and decodes it.
        /// </summary>
        /// <param name="fileName">name of xml file</param>
        /// <returns>decoded athlete's details</returns>
        /// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
        public List <IClubSeasonDetails> LoadClubSeasonData(string fileName)
        {
            List <IClubSeasonDetails> seasonDetails = new List <IClubSeasonDetails>();

            try
            {
                XDocument reader      = XDocument.Load(fileName);
                XElement  rootElement = reader.Root;

                var clubList = from Club in rootElement.Elements(c_clubElement)
                               select new
                {
                    name            = (string)Club.Attribute(nameAttribute),
                    mobTrophyPoints = from MobTrophyPoints in Club.Elements(MobTrophyPointsElement)
                                      select new
                    {
                        point = from Point in MobTrophyPoints.Elements(c_eventPointsElement)
                                select new
                        {
                            finishing = (int)Point.Attribute(finishingPointsAttribute),
                            position  = (int)Point.Attribute(positionPointsAttribute),
                            best      = (int)Point.Attribute(bestPointsAttribute),
                            date      = (string)Point.Attribute(eventDateAttribute)
                        }
                    },
                    teamTrophyPoints = from TeamTrophyPoints in Club.Elements(TeamTrophyPointsElement)
                                       select new
                    {
                        events = from TeamTrophyEvent in TeamTrophyPoints.Elements(EventElement)
                                 select new
                        {
                            size         = (int)TeamTrophyEvent.Attribute(TeamTrophyTeamSizeAttribute),
                            virtualPoint = (int)TeamTrophyEvent.Attribute(TeamTrophyVirtualAthletePointAttribute),
                            date         = (string)TeamTrophyEvent.Attribute(TeamTrophyEventDateAttribute),
                            score        = (int)TeamTrophyEvent.Attribute(TeamTrophyScoreAttribute),
                            points       = from Point in TeamTrophyEvent.Elements(c_eventPointsElement)
                                           select new
                            {
                                value = (int)Point.Attribute(TeamTrophyPointAttribute),
                                key   = (int)Point.Attribute(AthleteKeyAttribute)
                            }
                        }
                    }
                };

                foreach (var club in clubList)
                {
                    ClubSeasonDetails clubDetails = new ClubSeasonDetails(club.name);

                    foreach (var points in club.mobTrophyPoints)
                    {
                        foreach (var point in points.point)
                        {
                            DateType date =
                                new DateType(
                                    point.date);
                            CommonPoints readPoints =
                                new CommonPoints(
                                    point.finishing,
                                    point.position,
                                    point.best,
                                    date);


                            clubDetails.MobTrophy.AddNewEvent(readPoints);
                            // TODO, should probably check that there are the correct number read from the xml file.
                            // i.e. there is one for each event in the currently loaded season.
                        }
                    }

                    foreach (var teamTrophyPoints in club.teamTrophyPoints)
                    {
                        foreach (var teamTrophyEvent in teamTrophyPoints.events)
                        {
                            List <ICommonTeamTrophyPoints> pointsList = new List <ICommonTeamTrophyPoints>();
                            DateType date =
                                new DateType(
                                    teamTrophyEvent.date);

                            foreach (var point in teamTrophyEvent.points)
                            {
                                CommonTeamTrophyPoints readPoints =
                                    new CommonTeamTrophyPoints(
                                        point.value,
                                        string.Empty,
                                        point.key,
                                        true,
                                        date);

                                pointsList.Add(readPoints);
                            }

                            ITeamTrophyEvent readEvent =
                                new TeamTrophyEvent(
                                    date,
                                    pointsList,
                                    teamTrophyEvent.size,
                                    teamTrophyEvent.score);
                            readEvent.Complete(
                                teamTrophyEvent.size,
                                teamTrophyEvent.virtualPoint);

                            clubDetails.TeamTrophy.AddEvent(readEvent);
                        }
                    }


                    seasonDetails.Add(clubDetails);
                }
            }
            catch (Exception ex)
            {
                this.logger.WriteLog("Error reading athlete data: " + ex.ToString());

                seasonDetails = new List <IClubSeasonDetails>();
            }

            return(seasonDetails);
        }
Пример #3
0
        /// <summary>
        /// Loop through the results and work out all the points for the Team Trophy.
        /// </summary>
        /// <param name="resultsTable">results table</param>
        /// <param name="currentDate">date of the event</param>
        private void CalculateTeamTrophyPoints(
            EventResults resultsTable,
            DateType currentDate)
        {
            // Next score is used to complete the Team Trophy by filling in any blank spots.
            // The position is used to assign points to an athlete in the Team Trophy.
            int teamTrophyCompetitionPosition = 0;
            int nextScore = 1;

            resultsTable.OrderByFinishingTime();
            Dictionary <string, ITeamTrophyEvent> eventDictionary = new Dictionary <string, ITeamTrophyEvent>();

            foreach (IClubSeasonDetails club in this.Model.CurrentSeason.Clubs)
            {
                ITeamTrophyEvent newEvent =
                    new TeamTrophyEvent(
                        currentDate,
                        this.resultsConfiguration.ResultsConfigurationDetails.NumberInTeamTrophyTeam);
                eventDictionary.Add(
                    club.Name,
                    newEvent);
            }

            foreach (ResultsTableEntry result in resultsTable.Entries)
            {
                IAthleteTeamTrophyPoints athletePoints;
                IAthleteSeasonDetails    athlete =
                    this.Model.CurrentSeason.Athletes.Find(
                        a => a.Key == result.Key);

                if (athlete == null)
                {
                    this.logger.WriteLog(
                        $"Calculate Results Manager - Can'f find athlete {result.Key}");
                    continue;
                }

                if (result.Club == string.Empty ||
                    result.FirstTimer)
                {
                    result.TeamTrophyPoints = TeamTrophyNoScore;

                    athletePoints =
                        new AthleteTeamTrophyPoints(
                            TeamTrophyNoScore,
                            currentDate);

                    athlete.TeamTrophyPoints.AddNewEvent(athletePoints);

                    // Not part of the Team Trophy, move onto the next loop.
                    continue;
                }

                ++teamTrophyCompetitionPosition;
                ITeamTrophyEvent clubEvent = eventDictionary[result.Club];

                ICommonTeamTrophyPoints clubPoint =
                    new CommonTeamTrophyPoints(
                        teamTrophyCompetitionPosition,
                        result.Name,
                        result.Key,
                        true,
                        currentDate);

                // Attempt to add point to the club. It will fail if the team is already full.
                bool success = clubEvent.AddPoint(clubPoint);

                if (success)
                {
                    nextScore = teamTrophyCompetitionPosition + 1;
                    result.TeamTrophyPoints = teamTrophyCompetitionPosition;
                }
                else
                {
                    // Add points failed, revert the Team Trophy position.
                    --teamTrophyCompetitionPosition;
                    result.TeamTrophyPoints = TeamTrophyNoScore;
                }

                athletePoints =
                    new AthleteTeamTrophyPoints(
                        result.TeamTrophyPoints,
                        currentDate);
                athlete.TeamTrophyPoints.AddNewEvent(athletePoints);
            }

            List <ITeamTrophyEvent> orderedEvent = new List <ITeamTrophyEvent>();

            foreach (KeyValuePair <string, ITeamTrophyEvent> entry in eventDictionary)
            {
                entry.Value.Complete(
                    this.resultsConfiguration.ResultsConfigurationDetails.NumberInTeamTrophyTeam,
                    nextScore);
                orderedEvent.Add(entry.Value);
            }

            // Apply the score for each team as defined by the configuration file.
            // To order the teams, they've needed to be pulled out from the dictionary into a list.
            orderedEvent = orderedEvent.OrderBy(e => e.TotalAthletePoints).ToList();

            int lastPoints       = -1;
            int lastScoringIndex = 0;

            for (int index = 0; index < orderedEvent.Count; ++index)
            {
                if (orderedEvent[index].NumberOfAthletes == 0)
                {
                    break;
                }

                if (orderedEvent[index].TotalAthletePoints == lastPoints)
                {
                    orderedEvent[index].Score =
                        this.resultsConfiguration.ResultsConfigurationDetails.TeamTrophyPoints[lastScoringIndex];
                }
                else if (index < this.resultsConfiguration.ResultsConfigurationDetails.TeamTrophyPoints.Count)
                {
                    orderedEvent[index].Score = this.resultsConfiguration.ResultsConfigurationDetails.TeamTrophyPoints[index];
                    lastScoringIndex          = index;
                }

                lastPoints = orderedEvent[index].TotalAthletePoints;
            }

            foreach (KeyValuePair <string, ITeamTrophyEvent> entry in eventDictionary)
            {
                this.Model.CurrentSeason.AddNewClubPoints(entry.Key, entry.Value);
            }
        }