private int GetStatisticsCount( List <string> tokenNames, bool filterOnCurrentCommander, JournalEntry journalEntry, List <PropertyInfo> journalEntryProperties, CurrentData currentData, List <PropertyInfo> currentDataProperties) { var statisticsData = new EventStatisticsData { Commander = filterOnCurrentCommander ? _informationManager.CurrentCommander .Commander : null, Event = journalEntry.Event }; var statisticsDataProperties = statisticsData.GetType().GetProperties().ToList(); // Remove properties that are not allowed to be overridden statisticsDataProperties.RemoveWhere(prop => prop.Name == nameof(EventStatisticsData.Commander)); statisticsDataProperties.RemoveWhere(prop => prop.Name == nameof(EventStatisticsData.Event)); statisticsDataProperties.RemoveWhere(prop => prop.Name == nameof(EventStatisticsData.Count)); foreach (var statisticsDataProperty in statisticsDataProperties) { if (!tokenNames.Any( tokenName => tokenName.Equals(statisticsDataProperty.Name, StringComparison.OrdinalIgnoreCase))) { continue; } var journalEntryProperty = journalEntryProperties.FirstOrDefault(prop => prop.Name.Equals(statisticsDataProperty.Name)); if (journalEntryProperty != null) { statisticsDataProperty.SetValue(statisticsData, journalEntryProperty.GetValue(journalEntry)); continue; } var currentDataProperty = currentDataProperties.FirstOrDefault(prop => prop.Name.Equals(statisticsDataProperty.Name)); if (currentDataProperty != null) { statisticsDataProperty.SetValue(statisticsData, currentDataProperty.GetValue(currentData)); } } var count = _informationManager.GetEventStatisticsSum(statisticsData); return(count); }
private string GetMissionList(List <string> tokenNames, JournalEntry journalEntry, CurrentData currentData) { var starSystemProperty = journalEntry.GetType().GetProperty(nameof(CurrentData.StarSystem)); var starSystem = starSystemProperty?.GetValue(journalEntry) as string; starSystem = !string.IsNullOrWhiteSpace(starSystem) ? starSystem : currentData.StarSystem; var stationNameProperty = journalEntry.GetType().GetProperty(nameof(CurrentData.StationName)); var stationName = stationNameProperty?.GetValue(journalEntry) as string; stationName = !string.IsNullOrWhiteSpace(stationName) ? stationName : currentData.StationName; var filterOnStarSystem = tokenNames.Any( tokenName => tokenName.Equals(nameof(CurrentData.StarSystem), StringComparison.OrdinalIgnoreCase)); var filterOnStationName = tokenNames.Any( tokenName => tokenName.Equals(nameof(CurrentData.StationName), StringComparison.OrdinalIgnoreCase)); var missions = _informationManager.CurrentMissions; if (filterOnStarSystem) { missions = missions?.Where( mission => !string.IsNullOrWhiteSpace(mission.DestinationSystem) && mission.DestinationSystem.Equals( starSystem, StringComparison.OrdinalIgnoreCase)).ToArray(); } if (filterOnStationName) { missions = missions?.Where( mission => !string.IsNullOrWhiteSpace(mission.DestinationStation) && mission.DestinationStation.Equals( stationName, StringComparison.OrdinalIgnoreCase)).ToArray(); } if (missions == null || missions.Length == 0) { return("None"); } var missionListBuilder = new StringBuilder(); if (!filterOnStarSystem && !filterOnStationName) { foreach (var systemGroup in missions.GroupBy(mission => mission.DestinationSystem) .OrderBy(group => group.Key)) { var destinationSystem = systemGroup.First().DestinationSystem; if (string.IsNullOrWhiteSpace(destinationSystem)) { destinationSystem = "(Unknown Star System)"; } missionListBuilder.AppendLine($"{destinationSystem} ({systemGroup.Count()})"); } } else if (filterOnStarSystem) { foreach (var stationGroup in missions.GroupBy(mission => mission.DestinationStation) .OrderBy(group => group.Key)) { var destinationStation = stationGroup.First().DestinationStation; if (string.IsNullOrWhiteSpace(destinationStation)) { destinationStation = "(Unknown Station)"; } missionListBuilder.AppendLine($"{destinationStation} ({stationGroup.Count()})"); } } else { missionListBuilder.AppendLine($"{missions.Length} mission(s) at this station)"); } return(missionListBuilder.ToString()); }
private IEnumerable <SiouxMessagePart> GetMessageParts( [CanBeNull] JournalEntry journalEntry, bool filterOnCurrentCommander, string format) { var currentData = new CurrentData { Commander = _informationManager.CurrentCommander.Commander, Ship = _informationManager.CurrentShip.Ship, StarSystem = _informationManager.CurrentLocation.StarSystem, StationName = _informationManager.CurrentLocation.StationName, Body = _informationManager.CurrentLocation.Body, BodyType = _informationManager.CurrentLocation.BodyType }; var currentDataProperties = currentData.GetType().GetProperties().ToList(); var tokenRegex = new Regex( @"(?<token>\{(?<tokenName>[A-Za-z]*)(?<colorToken>:(?<colorType>[A-Za-z]*))?\})", RegexOptions.Compiled); var tokens = tokenRegex.Matches(format).Cast <Match>().Distinct(new TokenMatchComparer()).ToList(); var tokenNames = tokens.Select(token => token.Groups["tokenName"].Value.ToLowerInvariant()).ToList(); var journalEntryProperties = journalEntry?.GetType().GetProperties().ToList(); var count = -1; if (tokenNames.Any(tokenName => tokenName.Equals(CountValue) || tokenName.Equals(OrdinalCountValue))) { count = GetStatisticsCount( tokenNames, filterOnCurrentCommander, journalEntry, journalEntryProperties, currentData, currentDataProperties); } var missionList = string.Empty; if (tokenNames.Any(tokenName => tokenName.Equals(MissionListValue))) { missionList = GetMissionList(tokenNames, journalEntry, currentData); } var defaultBrush = ColorManager.Brushes[_siouxData.DefaultTextColorType]; GameStatistics gameStatistics = null; var messageParts = new List <SiouxMessagePart>(); var index = 0; foreach (var token in tokens) { var tokenName = token.Groups["tokenName"].Value.ToLowerInvariant(); var brush = GetBrushFromToken(token); if (token.Index > index) { messageParts.Add( new SiouxMessagePart { Text = format.Substring(index, token.Index - index), Foreground = defaultBrush }); } string value = null; //// Alternative solution if the ordinal indicator should not be colored //// ////if (tokenName.Equals(CountValue) || tokenName.Equals(OrdinalCountValue)) ////{ //// value = count >= 0 ? count.ToString() : string.Empty; //// messageParts.Add(new SiouxMessagePart { Text = value, Foreground = Brushes.ForestGreen }); //// if (tokenName.Equals(OrdinalCountValue)) //// { //// messageParts.Add(new SiouxMessagePart { Text = GetOrdinalIndicator(count) }); //// } //// index = token.Index + token.Length; //// continue; ////} if (tokenName.Equals(CountValue) || tokenName.Equals(OrdinalCountValue)) { var countString = string.Empty; if (count >= 0) { countString = tokenName.Equals(CountValue) ? count.ToString() : $"{count}{GetOrdinalIndicator(count)}"; } messageParts.Add(new SiouxMessagePart { Text = countString, Foreground = brush }); index = token.Index + token.Length; continue; } if (tokenName.Equals(MissionListValue)) { messageParts.Add(new SiouxMessagePart { Text = missionList, Foreground = brush }); index = token.Index + token.Length; continue; } if (_gameStatisticsNames.Contains(tokenName)) { gameStatistics = gameStatistics ?? _informationManager.GetGamePlayedStatistics(); string text; switch (tokenName) { case "sessionsplayed": text = gameStatistics.SessionsPlayed.ToString(); break; case "totaltimeplayed": text = $"{gameStatistics.TotalTimePlayed.Days} days {gameStatistics.TotalTimePlayed.Hours} hours {gameStatistics.TotalTimePlayed.Minutes} minutes"; break; case "currentsessionplayed": text = $"{gameStatistics.CurrentSessionPlayed?.Days} days {gameStatistics.CurrentSessionPlayed?.Hours} hours {gameStatistics.CurrentSessionPlayed?.Minutes} minutes"; break; default: // TODO: Handle by sending feedback text = "ERROR"; break; } messageParts.Add(new SiouxMessagePart { Text = text, Foreground = brush }); index = token.Index + token.Length; continue; } if (journalEntryProperties != null) { var journalProperty = journalEntryProperties.FirstOrDefault( prop => prop.Name.Equals(tokenName, StringComparison.OrdinalIgnoreCase)); value = GetPropertyValue(journalEntry, journalProperty); if (!string.IsNullOrEmpty(value)) { messageParts.Add(new SiouxMessagePart { Text = value, Foreground = brush }); index = token.Index + token.Length; continue; } } var currentDataProperty = currentDataProperties.FirstOrDefault( prop => prop.Name.Equals(tokenName, StringComparison.OrdinalIgnoreCase)); if (currentDataProperty != null) { value = GetPropertyValue(currentData, currentDataProperty); messageParts.Add(new SiouxMessagePart { Text = value, Foreground = brush }); index = token.Index + token.Length; continue; } if (value == null) { messageParts.Add( new SiouxMessagePart { Text = $"(no value found for {tokenName})", Foreground = Brushes.OrangeRed }); } index = token.Index + token.Length; } if (index < format.Length) { messageParts.Add(new SiouxMessagePart { Text = format.Substring(index), Foreground = defaultBrush }); } return(messageParts); }