/// <summary> /// Save the Team Trophy current event table. /// </summary> /// <param name="model">junior handicap model</param> /// <param name="folderPath">path of the folder to save the table to</param> /// <param name="logger">application logger</param> /// <returns>success flag</returns> private static bool WriteCurrentEventTable( IModel model, string folderPath, IJHcLogger logger) { bool success = true; try { string outPath = $"{folderPath}{ResultsPaths.teamTrophyPointsTableCurrentEvent}{ResultsPaths.csvExtension}"; using ( StreamWriter writer = new StreamWriter( outPath)) { string titleString = "Club" + ResultsPaths.separator + "Score" + ResultsPaths.separator + "Points"; writer.WriteLine(titleString); foreach (ClubSeasonDetails club in model.CurrentSeason.Clubs) { if (club.MobTrophy.TotalPoints > 0) { ITeamTrophyEvent foundEvent = club.TeamTrophy.Events.Find( e => e.Date == model.CurrentEvent.Date); if (foundEvent == null) { continue; } string entryString = $"{club.Name}{ResultsPaths.separator}{foundEvent.Score}{ResultsPaths.separator}{foundEvent.TotalAthletePoints}"; foreach (ICommonTeamTrophyPoints commonPoints in foundEvent.Points) { entryString = entryString + ResultsPaths.separator + commonPoints.Point; } writer.WriteLine(entryString); } } } } catch (Exception ex) { logger.WriteLog("Error, failed to print club points table: " + ex.ToString()); Messenger.Default.Send( new HandicapErrorMessage( "Failed to print club points table")); success = false; } return(success); }
/// <summary> /// Add a new events results for this season. /// </summary> /// <remarks> /// If there is already an event on the <paramref name="date"/> then it should be overwritten /// instead of adding a new event. /// </remarks> /// <param name="newEvent">new event</param> public void AddEvent( ITeamTrophyEvent newEvent) { if (this.Events.Count > 0) { for (int index = 0; index < this.Events.Count; ++index) { if (this.Events[index].Date == newEvent.Date) { this.Events[index] = newEvent; return; } } } this.Events.Add(newEvent); }
/// <summary> /// Add points to the named club /// </summary> /// <param name="clubName">name of club</param> /// <param name="points">Team Trophy points to add</param> public void AddNewClubPoints( string clubName, ITeamTrophyEvent points) { IClubSeasonDetails clubDetails = Clubs.Find(club => club.Name.CompareTo(clubName) == 0); if (clubDetails != null) { clubDetails.AddNewEvent(points); } else { ClubSeasonDetails newClubDetails = new ClubSeasonDetails(clubName); newClubDetails.AddNewEvent(points); Clubs.Add(newClubDetails); } this.ClubsChangedEvent?.Invoke(this, new EventArgs()); }
/// <summary> /// Save the Team Trophy to the file. /// </summary> /// <param name="model">junior handicap model</param> /// <param name="folderPath">path of the folder to save the table to</param> /// <param name="eventData">event data wrapper</param> /// <param name="logger">application logger</param> /// <returns>success flag</returns> private static bool WriteOverallSeasonTable( IModel model, string folderPath, IEventData eventData, IJHcLogger logger) { bool success = true; List <DateType> eventDates = new List <DateType>(); try { string outPath = $"{folderPath}{ResultsPaths.teamTrophyPointsTable}{ResultsPaths.csvExtension}"; using ( StreamWriter writer = new StreamWriter( outPath)) { string titleString = "Club" + ResultsPaths.separator + "TotalPoints"; foreach (string eventName in model.CurrentSeason.Events) { titleString = titleString + ResultsPaths.separator + eventName; eventDates.Add(eventData.LoadEventData(model.CurrentSeason.Name, eventName).EventDate); } writer.WriteLine(titleString); foreach (ClubSeasonDetails club in model.CurrentSeason.Clubs) { int totalScore = TeamTrophyTableWriter.CalculateTotalScore( club.TeamTrophy.Events); if (totalScore > 0) { string entryString = $"{club.Name}{ResultsPaths.separator}{totalScore}"; foreach (DateType eventDate in eventDates) { if (club.MobTrophy.Points.Exists(points => points.Date == eventDate)) { ITeamTrophyEvent foundEvent = club.TeamTrophy.Events.Find( points => points.Date == eventDate); int eventPoints = foundEvent != null ? foundEvent.Score : 0; entryString = entryString + ResultsPaths.separator + eventPoints; } else { entryString += ResultsPaths.separator; } } writer.WriteLine(entryString); } } } } catch (Exception ex) { logger.WriteLog("Error, failed to print club points table: " + ex.ToString()); Messenger.Default.Send( new HandicapErrorMessage( "Failed to print club points table")); success = false; } return(success); }
/// <summary> /// Add a new event. /// </summary> /// <param name="newPoints">Team Trophy points received</param> public void AddNewEvent( ITeamTrophyEvent newEvent) { this.TeamTrophy.AddEvent(newEvent); }
/// <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); } }