AccountWarningConfigCore.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace molilian.core
  4. {
  5. public static class AccountWarningConfigCore
  6. {
  7. private static readonly char[] Separators = new[] { '\r', '\n', ',', ',', ';', ';' };
  8. public static Dictionary<string, List<int>> FilterStats(
  9. string platform,
  10. Dictionary<string, List<int>> stats,
  11. string? ignoredGroups)
  12. {
  13. if (stats == null || stats.Count == 0) return stats ?? new Dictionary<string, List<int>>();
  14. HashSet<string> rules = ParseIgnoredGroups(ignoredGroups);
  15. if (rules.Count == 0) return stats;
  16. string normalizedPlatform = NormalizeToken(platform);
  17. return stats
  18. .Where(item => !ShouldIgnore(rules, normalizedPlatform, item.Key))
  19. .ToDictionary(item => item.Key, item => item.Value);
  20. }
  21. private static bool ShouldIgnore(HashSet<string> rules, string platform, string groupKey)
  22. {
  23. string normalizedGroup = NormalizeToken(groupKey);
  24. if (string.IsNullOrEmpty(normalizedGroup)) return false;
  25. return rules.Contains(normalizedGroup) || rules.Contains($"{platform}:{normalizedGroup}");
  26. }
  27. private static HashSet<string> ParseIgnoredGroups(string? ignoredGroups)
  28. {
  29. if (string.IsNullOrWhiteSpace(ignoredGroups))
  30. {
  31. return new HashSet<string>(System.StringComparer.OrdinalIgnoreCase);
  32. }
  33. return ignoredGroups
  34. .Split(Separators, System.StringSplitOptions.RemoveEmptyEntries | System.StringSplitOptions.TrimEntries)
  35. .Select(NormalizeToken)
  36. .Where(item => !string.IsNullOrEmpty(item) && !item.StartsWith("#"))
  37. .ToHashSet(System.StringComparer.OrdinalIgnoreCase);
  38. }
  39. private static string NormalizeToken(string? value)
  40. {
  41. return string.IsNullOrWhiteSpace(value)
  42. ? string.Empty
  43. : value.Trim().ToLowerInvariant();
  44. }
  45. }
  46. }