/// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- /// <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), points = from Points in Club.Elements(c_pointsElement) select new { point = from Point in Points.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) } }, harmonyPoints = from HarmonyPoints in Club.Elements(HarmonyPointsElement) select new { events = from HarmonyEvent in HarmonyPoints.Elements(EventElement) select new { size = (int)HarmonyEvent.Attribute(HarmonyTeamSizeAttribute), virtualPoint = (int)HarmonyEvent.Attribute(HarmonyVirtualAthletePointAttribute), date = (string)HarmonyEvent.Attribute(HarmonyEventDateAttribute), score = (int)HarmonyEvent.Attribute(HarmonyScoreAttribute), points = from Point in HarmonyEvent.Elements(c_eventPointsElement) select new { value = (int)Point.Attribute(HarmonyPointAttribute), key = (int)Point.Attribute(AthleteKeyAttribute) } } } }; foreach (var club in clubList) { ClubSeasonDetails clubDetails = new ClubSeasonDetails(club.name); foreach (var points in club.points) { foreach (var point in points.point) { DateType date = new DateType( point.date); CommonPoints readPoints = new CommonPoints( point.finishing, point.position, point.best, date); clubDetails.ClubCompetition.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 harmonyPoints in club.harmonyPoints) { foreach (var harmonyEvent in harmonyPoints.events) { List <ICommonHarmonyPoints> pointsList = new List <ICommonHarmonyPoints>(); DateType date = new DateType( harmonyEvent.date); foreach (var point in harmonyEvent.points) { CommonHarmonyPoints readPoints = new CommonHarmonyPoints( point.value, string.Empty, point.key, true, date); pointsList.Add(readPoints); } IHarmonyEvent readEvent = new HarmonyEvent( date, pointsList, harmonyEvent.size, harmonyEvent.score); readEvent.Complete( harmonyEvent.size, harmonyEvent.virtualPoint); clubDetails.HarmonyCompetition.AddEvent(readEvent); } } seasonDetails.Add(clubDetails); } } catch (Exception ex) { this.logger.WriteLog("Error reading athlete data: " + ex.ToString()); seasonDetails = new List <IClubSeasonDetails>(); } return(seasonDetails); }
/// <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); } }