private CharacterGroupListItemViewModel?GenerateFrom(CharacterGroup characterGroup, int deepLevel, IList <CharacterGroup> pathToTop) { if (!characterGroup.IsVisible(CurrentUserId)) { return(null); } var prevCopy = Results.FirstOrDefault(cg => cg.FirstCopy && cg.CharacterGroupId == characterGroup.CharacterGroupId); var vm = new CharacterGroupListItemViewModel { CharacterGroupId = characterGroup.CharacterGroupId, DeepLevel = deepLevel, Name = characterGroup.CharacterGroupName, FirstCopy = prevCopy == null, AvaiableDirectSlots = characterGroup.HaveDirectSlots ? characterGroup.AvaiableDirectSlots : 0, IsAcceptingClaims = characterGroup.IsAcceptingClaims(), ActiveCharacters = prevCopy?.ActiveCharacters ?? GenerateCharacters(characterGroup) .ToList(), Description = characterGroup.Description.ToHtmlString(), ActiveClaimsCount = characterGroup.Claims.Count(c => c.ClaimStatus.IsActive()), Path = pathToTop.Select(cg => Results.First(item => item.CharacterGroupId == cg.CharacterGroupId)), IsPublic = characterGroup.IsPublic, IsSpecial = characterGroup.IsSpecial, IsRootGroup = characterGroup.IsRoot, ProjectId = characterGroup.ProjectId, RootGroupId = Root.CharacterGroupId, }; if (Root == characterGroup) { vm.First = true; vm.Last = true; } if (vm.IsSpecial) { var variant = characterGroup.GetBoundFieldDropdownValueOrDefault(); if (variant != null) { vm.BoundExpression = $"{variant.ProjectField.FieldName} = {variant.Label}"; } } Results.Add(vm); if (prevCopy != null) { vm.ChildGroups = prevCopy.ChildGroups; return(vm); } var childGroups = characterGroup.GetOrderedChildGroups().OrderBy(g => g.IsSpecial).Where(g => g.IsActive && g.IsVisible(CurrentUserId)).ToList(); var pathForChildren = pathToTop.Union(new[] { characterGroup }).ToList(); vm.ChildGroups = childGroups.Select(childGroup => GenerateFrom(childGroup, deepLevel + 1, pathForChildren)).ToList().MarkFirstAndLast(); return(vm); }
private CharacterGroupListItemViewModel GenerateFrom(CharacterGroup characterGroup, int deepLevel, IList <CharacterGroup> pathToTop, IReadOnlyList <CharacterGroup> siblings) { if (!characterGroup.IsVisible(CurrentUserId)) { return(null); } var prevCopy = Results.FirstOrDefault(cg => cg.FirstCopy && cg.CharacterGroupId == characterGroup.CharacterGroupId); var vm = new CharacterGroupListItemViewModel { CharacterGroupId = characterGroup.CharacterGroupId, DeepLevel = deepLevel, Name = characterGroup.CharacterGroupName, FirstCopy = prevCopy == null, AvaiableDirectSlots = characterGroup.HaveDirectSlots ? characterGroup.AvaiableDirectSlots : 0, IsAcceptingClaims = characterGroup.HaveDirectSlots && characterGroup.Project.IsAcceptingClaims && characterGroup.AvaiableDirectSlots != 0, ActiveCharacters = prevCopy?.ActiveCharacters ?? GenerateCharacters(characterGroup) .ToList(), Description = characterGroup.Description.ToHtmlString(), ActiveClaimsCount = characterGroup.Claims.Count(c => c.IsActive), Path = pathToTop.Select(cg => Results.First(item => item.CharacterGroupId == cg.CharacterGroupId)), IsPublic = characterGroup.IsPublic, IsSpecial = characterGroup.IsSpecial, IsActive = characterGroup.IsActive, IsRootGroup = characterGroup.IsRoot, FirstInGroup = siblings.First() == characterGroup, LastInGroup = siblings.Last() == characterGroup, ProjectId = characterGroup.ProjectId, RootGroupId = Root.CharacterGroupId, }; if (vm.IsSpecial) { var variant = characterGroup.Project.ProjectFields.SelectMany(pf => pf.DropdownValues) .SingleOrDefault(pfv => pfv.CharacterGroup == characterGroup); if (variant != null) { vm.BoundExpression = $"{variant.ProjectField.FieldName} = {variant.Label}"; } } Results.Add(vm); if (prevCopy != null) { vm.ChildGroups = prevCopy.ChildGroups; vm.TotalSlots = prevCopy.TotalSlots; vm.TotalCharacters = prevCopy.TotalCharacters; vm.TotalCharactersWithPlayers = prevCopy.TotalCharactersWithPlayers; vm.TotalDiscussedClaims = prevCopy.TotalDiscussedClaims; vm.TotalActiveClaims = prevCopy.TotalActiveClaims; vm.TotalNpcCharacters = prevCopy.TotalNpcCharacters; vm.Unlimited = prevCopy.Unlimited; vm.TotalAcceptedClaims = prevCopy.TotalAcceptedClaims; return(vm); } var childGroups = characterGroup.GetOrderedChildGroups().Where(g => g.IsActive && g.IsVisible(CurrentUserId)).ToList(); var pathForChildren = pathToTop.Union(new[] { characterGroup }).ToList(); vm.ChildGroups = childGroups.Select(childGroup => GenerateFrom(childGroup, deepLevel + 1, pathForChildren, childGroups)).ToList(); var flatChilds = vm.FlatTree(model => model.ChildGroups).Distinct().ToList(); var flatCharacters = flatChilds.SelectMany(c => c.ActiveCharacters).Distinct().ToList(); vm.TotalSlots = flatChilds.Sum(c => c.AvaiableDirectSlots == -1 ? 0 : c.AvaiableDirectSlots) + flatCharacters.Count(c => c.IsAvailable); vm.TotalCharacters = flatCharacters.Count + flatChilds.Sum(c => c.AvaiableDirectSlots == -1 ? 0 : c.AvaiableDirectSlots); vm.TotalNpcCharacters = flatCharacters.Count(c => !c.IsAcceptingClaims); vm.TotalCharactersWithPlayers = flatCharacters.Count(c => c.Player != null); vm.TotalDiscussedClaims = flatCharacters.Where(c => c.Player == null).Sum(c => c.ActiveClaimsCount) + flatChilds.Sum(c => c.ActiveClaimsCount); vm.TotalActiveClaims = flatCharacters.Sum(c => c.ActiveClaimsCount) + flatChilds.Sum(c => c.ActiveClaimsCount); vm.TotalAcceptedClaims = flatCharacters.Count(c => c.Player != null); vm.Unlimited = vm.AvaiableDirectSlots == -1 || flatChilds.Any(c => c.AvaiableDirectSlots == -1); return(vm); }