Esempio n. 1
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 <AthleteSeasonDetails> LoadAthleteSeasonData(
            string fileName,
            IResultsConfigMngr resultsConfigurationManager)
        {
            List <AthleteSeasonDetails> seasonDetails = new List <AthleteSeasonDetails>();

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

                var athleteList = from Athlete in rootElement.Elements(c_athleteElement)
                                  select new
                {
                    key            = (int)Athlete.Attribute(c_keyAttribute),
                    name           = (string)Athlete.Attribute(c_nameAttribute),
                    runningNumbers = from RunningNumbers in Athlete.Elements(c_runningNumbersElement)
                                     select new
                    {
                        numbers = from Numbers in RunningNumbers.Elements(c_numberElement)
                                  select new
                        {
                            number = (string)Numbers.Attribute(c_numberAttribute)
                        }
                    },
                    eventTimes = from EventTimes in Athlete.Elements(c_timesElement)
                                 select new
                    {
                        events = from Times in EventTimes.Elements(c_eventTimeElement)
                                 select new
                        {
                            time = (string)Times.Attribute(c_eventTimeAttribute),
                            date = (string)Times.Attribute(c_eventDateAttribute)
                        }
                    },
                    points = from Points in Athlete.Elements(c_pointsElement)
                             select new
                    {
                        point = from Point in Points.Elements(c_eventPointsElement)
                                select new
                        {
                            finishing = (int)Point.Attribute(c_finishingPoints),
                            position  = (int)Point.Attribute(c_positionPoints),
                            best      = (int)Point.Attribute(c_bestPoints),
                            date      = (string)Point.Attribute(c_eventDateAttribute)
                        }
                    },
                    harmonyPoints = from Points in Athlete.Elements(HarmonyPointsElement)
                                    select new
                    {
                        point = from Point in Points.Elements(c_eventPointsElement)
                                select new
                        {
                            harmonyPoint = (int)Point.Attribute(HarmonyPointsAttribute),
                            date         = (string)Point.Attribute(c_eventDateAttribute)
                        }
                    }
                };

                foreach (var athlete in athleteList)
                {
                    AthleteSeasonDetails athleteDetails =
                        new AthleteSeasonDetails(
                            athlete.key,
                            athlete.name,
                            resultsConfigurationManager);

                    foreach (var eventTms in athlete.eventTimes)
                    {
                        foreach (var times in eventTms.events)
                        {
                            athleteDetails.AddNewTime(new Appearances(new RaceTimeType(times.time),
                                                                      new DateType(times.date)));
                        }
                    }

                    foreach (var points in athlete.points)
                    {
                        foreach (var point in points.point)
                        {
                            DateType eventDate =
                                new DateType(
                                    point.date);

                            CommonPoints commonPoints =
                                new CommonPoints(
                                    point.finishing,
                                    point.position,
                                    point.best,
                                    eventDate);

                            athleteDetails.Points.AddNewEvent(commonPoints);
                            // 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.
                            // Will want to change it to proper serialisation at some point.
                        }
                    }

                    foreach (var points in athlete.harmonyPoints)
                    {
                        foreach (var point in points.point)
                        {
                            DateType date = new DateType(point.date);
                            IAthleteHarmonyPoints newEvent =
                                new AthleteHarmonyPoints(
                                    point.harmonyPoint,
                                    date);

                            athleteDetails.HarmonyPoints.AddNewEvent(newEvent);
                        }
                    }

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

                seasonDetails = new List <AthleteSeasonDetails>();
            }

            return(seasonDetails);
        }
Esempio n. 2
0
        /// <summary>
        /// Loop through the results and work out all the points for the harmony competition.
        /// </summary>
        /// <param name="resultsTable">results table</param>
        /// <param name="currentDate">date of the event</param>
        private void CalculateTeamHarmonyPoints(
            EventResults resultsTable,
            DateType currentDate)
        {
            // Next score is used to complete the harmony competion by filling in any blank spots.
            // The position is used to assign points to an athlete in the harmony competion.
            int harmonyCompetitionPosition = 0;
            int nextScore = 1;

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

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

            foreach (ResultsTableEntry result in resultsTable.Entries)
            {
                IAthleteHarmonyPoints athletePoints;
                AthleteSeasonDetails  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.HarmonyPoints = HarmonyNoScore;

                    athletePoints =
                        new AthleteHarmonyPoints(
                            HarmonyNoScore,
                            currentDate);

                    athlete.HarmonyPoints.AddNewEvent(athletePoints);

                    // Not part of the harmony competition, move onto the next loop.
                    continue;
                }

                ++harmonyCompetitionPosition;
                IHarmonyEvent clubEvent = eventDictionary[result.Club];

                ICommonHarmonyPoints clubPoint =
                    new CommonHarmonyPoints(
                        harmonyCompetitionPosition,
                        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            = harmonyCompetitionPosition + 1;
                    result.HarmonyPoints = harmonyCompetitionPosition;
                }
                else
                {
                    // Add points failed, rever the harmony competition position.
                    --harmonyCompetitionPosition;
                    result.HarmonyPoints = HarmonyNoScore;
                }

                athletePoints =
                    new AthleteHarmonyPoints(
                        result.HarmonyPoints,
                        currentDate);
                athlete.HarmonyPoints.AddNewEvent(athletePoints);
            }

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

            foreach (KeyValuePair <string, IHarmonyEvent> entry in eventDictionary)
            {
                entry.Value.Complete(
                    this.resultsConfiguration.ResultsConfigurationDetails.NumberInHarmonyTeam,
                    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();
            for (int index = 0; index < orderedEvent.Count; ++index)
            {
                if (index < this.resultsConfiguration.ResultsConfigurationDetails.HarmonyPoints.Count)
                {
                    orderedEvent[index].Score = this.resultsConfiguration.ResultsConfigurationDetails.HarmonyPoints[index];
                }
            }

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