| | | 1 | | using Chronicis.Client.Models; |
| | | 2 | | using static Chronicis.Client.Services.RenderDefinitionHelpers; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Builds RenderSection lists from classified fields and prefix groups. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class SectionBuilder |
| | | 10 | | { |
| | | 11 | | public static List<RenderSection> Build(List<FieldInfo> remaining, List<PrefixGroup> prefixGroups) |
| | | 12 | | { |
| | 20 | 13 | | var groupedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 60 | 14 | | foreach (var group in prefixGroups) |
| | 98 | 15 | | foreach (var fi in group.Fields) |
| | 39 | 16 | | groupedNames.Add(fi.Name); |
| | | 17 | | |
| | 20 | 18 | | var ungrouped = remaining.Where(f => !groupedNames.Contains(f.Name)).ToList(); |
| | 20 | 19 | | var sections = new List<RenderSection>(); |
| | | 20 | | |
| | 20 | 21 | | AddOverviewSection(sections, ungrouped); |
| | 20 | 22 | | AddGroupedSections(sections, prefixGroups); |
| | 20 | 23 | | AddComplexFieldsSection(sections, ungrouped); |
| | | 24 | | |
| | 20 | 25 | | return sections; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | private static void AddOverviewSection(List<RenderSection> sections, List<FieldInfo> ungrouped) |
| | | 29 | | { |
| | 20 | 30 | | var overviewFields = ungrouped |
| | 20 | 31 | | .Where(f => !f.IsComplex) |
| | 20 | 32 | | .OrderBy(f => IsDescriptionField(f.Name) ? 1 : 0) |
| | 20 | 33 | | .ToList(); |
| | | 34 | | |
| | 20 | 35 | | if (overviewFields.Count > 0) |
| | | 36 | | { |
| | 10 | 37 | | sections.Add(new RenderSection |
| | 10 | 38 | | { |
| | 10 | 39 | | Label = "Overview", |
| | 10 | 40 | | Render = "fields", |
| | 10 | 41 | | Fields = overviewFields.Select(f => new RenderField |
| | 10 | 42 | | { |
| | 10 | 43 | | Path = f.Name, |
| | 10 | 44 | | Label = FormatFieldName(f.Name), |
| | 10 | 45 | | Render = IsDescriptionField(f.Name) ? "richtext" : "text" |
| | 10 | 46 | | }).ToList() |
| | 10 | 47 | | }); |
| | | 48 | | } |
| | 20 | 49 | | } |
| | | 50 | | |
| | | 51 | | private static void AddGroupedSections(List<RenderSection> sections, List<PrefixGroup> prefixGroups) |
| | | 52 | | { |
| | 60 | 53 | | foreach (var group in prefixGroups.OrderBy(g => g.Label)) |
| | | 54 | | { |
| | 10 | 55 | | if (PrefixGroupDetector.IsAbilityScoreGroup(group)) |
| | | 56 | | { |
| | 2 | 57 | | sections.Add(BuildAbilityScoreSection(group)); |
| | | 58 | | } |
| | | 59 | | else |
| | | 60 | | { |
| | 8 | 61 | | var mostlyNull = group.Fields.Count(f => f.IsNull) > group.Fields.Count / 2; |
| | 8 | 62 | | sections.Add(new RenderSection |
| | 8 | 63 | | { |
| | 8 | 64 | | Label = FormatGroupLabel(group.Prefix), |
| | 8 | 65 | | Render = "fields", |
| | 8 | 66 | | Collapsed = mostlyNull, |
| | 8 | 67 | | Fields = group.Fields |
| | 8 | 68 | | .OrderBy(f => f.IsNull ? 1 : 0) |
| | 8 | 69 | | .Select(f => new RenderField |
| | 8 | 70 | | { |
| | 8 | 71 | | Path = f.Name, |
| | 8 | 72 | | Label = FormatFieldName(StripPrefix(f.Name, group.Prefix)) |
| | 8 | 73 | | }).ToList() |
| | 8 | 74 | | }); |
| | | 75 | | } |
| | | 76 | | } |
| | 20 | 77 | | } |
| | | 78 | | |
| | | 79 | | private static RenderSection BuildAbilityScoreSection(PrefixGroup group) |
| | | 80 | | { |
| | 2 | 81 | | return new RenderSection |
| | 2 | 82 | | { |
| | 2 | 83 | | Label = "Ability Scores", |
| | 2 | 84 | | Render = "stat-row", |
| | 2 | 85 | | Fields = AbilitySuffixes.Select((suffix, i) => |
| | 2 | 86 | | { |
| | 2 | 87 | | var match = group.Fields.FirstOrDefault(f => |
| | 2 | 88 | | f.Name.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)); |
| | 2 | 89 | | return new RenderField |
| | 2 | 90 | | { |
| | 2 | 91 | | Path = match!.Name, |
| | 2 | 92 | | Label = AbilityLabels[i] |
| | 2 | 93 | | }; |
| | 2 | 94 | | }).ToList() |
| | 2 | 95 | | }; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | private static void AddComplexFieldsSection(List<RenderSection> sections, List<FieldInfo> ungrouped) |
| | | 99 | | { |
| | 20 | 100 | | var complexFields = ungrouped.Where(f => f.IsComplex).ToList(); |
| | 20 | 101 | | if (complexFields.Count > 0) |
| | | 102 | | { |
| | 2 | 103 | | sections.Add(new RenderSection |
| | 2 | 104 | | { |
| | 2 | 105 | | Label = "Additional Data", |
| | 2 | 106 | | Render = "fields", |
| | 2 | 107 | | Collapsed = true, |
| | 2 | 108 | | Fields = complexFields.Select(f => new RenderField |
| | 2 | 109 | | { |
| | 2 | 110 | | Path = f.Name, |
| | 2 | 111 | | Label = FormatFieldName(f.Name) |
| | 2 | 112 | | }).ToList() |
| | 2 | 113 | | }); |
| | | 114 | | } |
| | 20 | 115 | | } |
| | | 116 | | } |