Skip to content

Commit cf345bf

Browse files
authored
Поправить порядок в CharacterTreeBuilder (#2386)
1 parent 2d1f907 commit cf345bf

File tree

4 files changed

+21
-45
lines changed

4 files changed

+21
-45
lines changed

src/JoinRpg.Portal/Controllers/GameGroupsController.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private async Task<ActionResult> AllGroupsJson(int projectId, bool includeSpecia
192192
g.FirstCopy,
193193
Path = g.Path.Select(gr => gr.Name),
194194
PathIds = g.Path.Select(gr => gr.CharacterGroupId),
195-
Characters = g.Characters.Select(ConvertCharacterToJsonSlim),
195+
g.Characters,
196196
}),
197197
});
198198
}
@@ -228,17 +228,6 @@ private object ConvertCharacterToJson(CharacterViewModel ch)
228228
};
229229
}
230230

231-
private object ConvertCharacterToJsonSlim(CharacterLinkViewModel ch)
232-
{
233-
return new
234-
{
235-
ch.CharacterId,
236-
ch.IsAvailable,
237-
ch.IsFirstCopy,
238-
ch.CharacterName,
239-
};
240-
}
241-
242231
[HttpGet, Authorize]
243232
[MasterAuthorize(Permission.CanEditRoles)]
244233
public async Task<ActionResult> Edit(int projectId, int characterGroupId)

src/JoinRpg.Portal/wwwroot/Scripts/multicontrol.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,10 +1150,12 @@ MultiControl.App.prototype.processData = function (data) {
11501150
}
11511151
if (!!group.Characters) {
11521152
for (j = 0; j < group.Characters.length; j++) {
1153-
character = group.Characters[j];
1154-
if (character.IsFirstCopy) {
1153+
character = group.Characters[j];
1154+
let id = 'character^' + character.CharacterId;
1155+
character_model = this.CharacterList[id];
1156+
if (!character_model) {
11551157
character_option = {
1156-
id: 'character^' + character.CharacterId,
1158+
id: id,
11571159
name: character.CharacterName,
11581160
checkable: false,
11591161
checked: false,
@@ -1163,11 +1165,8 @@ MultiControl.App.prototype.processData = function (data) {
11631165
character_model = new MultiControl.Models.Character(character_option, this.ActionManager,
11641166
this.Settings.strategy);
11651167
this.CharacterList[character_model.getId()] = character_model;
1166-
}
1167-
else {
1168-
character_model = this.CharacterList['character^' + character.CharacterId];
1169-
}
1170-
character_model.addParent(group_model);
1168+
}
1169+
character_model.addParent(group_model);
11711170
}
11721171
}
11731172
}

src/JoinRpg.WebPortal.Models/Characters/CharacterLinkViewModel.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,8 @@ public class CharacterLinkViewModel : IEquatable<CharacterLinkViewModel>
77
public int CharacterId { get; }
88
public string CharacterName { get; }
99

10-
public bool IsFirstCopy { get; set; }
11-
1210
public bool IsAvailable { get; }
1311

14-
public bool IsPublic { get; }
15-
16-
public bool IsActive { get; }
17-
18-
public bool IsAcceptingClaims { get; set; }
19-
public int ProjectId { get; set; }
20-
2112
public bool Equals(CharacterLinkViewModel? other) => other != null && CharacterId == other.CharacterId;
2213

2314
public override bool Equals(object? obj) => Equals(obj as CharacterLinkViewModel);
@@ -31,10 +22,5 @@ public CharacterLinkViewModel(Character arg)
3122
CharacterId = arg.CharacterId;
3223
CharacterName = arg.CharacterName;
3324
IsAvailable = arg.IsAvailable;
34-
IsPublic = arg.IsPublic;
35-
IsActive = arg.IsActive;
36-
ProjectId = arg.ProjectId;
37-
IsAcceptingClaims = arg.IsAcceptingClaims;
38-
3925
}
4026
}

src/JoinRpg.WebPortal.Models/Characters/CharacterTreeBuilder.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using JetBrains.Annotations;
21
using JoinRpg.DataModel;
32
using JoinRpg.Domain;
43

@@ -8,7 +7,7 @@ public class CharacterTreeBuilder
87
{
98
private CharacterGroup Root { get; }
109

11-
private IList<int> AlreadyOutputedChars { get; } = new List<int>();
10+
private readonly Dictionary<int, CharacterLinkViewModel> alreadyOutputedChars = new();
1211

1312
private IList<CharacterTreeItem> Results { get; } = new List<CharacterTreeItem>();
1413

@@ -18,7 +17,6 @@ public CharacterTreeBuilder(CharacterGroup root, int? currentUserId)
1817
CurrentUserId = currentUserId;
1918
}
2019

21-
[MustUseReturnValue]
2220
public IList<CharacterTreeItem> Generate()
2321
{
2422
_ = GenerateFrom(Root, 0, new List<CharacterGroup>());
@@ -36,10 +34,15 @@ private CharacterTreeItem GenerateFrom(CharacterGroup characterGroup, int deepLe
3634
Characters = characterGroup.GetOrderedCharacters().Where(c => c.IsActive && c.IsVisible(CurrentUserId)).Select(GenerateCharacter).ToList(),
3735
Path = pathToTop.Select(cg => Results.First(item => item.CharacterGroupId == cg.CharacterGroupId)),
3836
IsSpecial = characterGroup.IsSpecial,
39-
ChildGroups = prevCopy?.ChildGroups ?? CreateChilds().ToList(),
37+
ChildGroups = prevCopy?.ChildGroups!, //Will be set later
4038
};
4139

42-
Results.Add(vm);
40+
Results.Add(vm); // Надо добавить в список перед тем, как добавлять детей, иначе клиент сходит с ума
41+
42+
if (vm.ChildGroups is null)
43+
{
44+
vm.ChildGroups = CreateChilds().ToList();
45+
}
4346

4447
return vm;
4548

@@ -57,14 +60,13 @@ IEnumerable<CharacterTreeItem> CreateChilds()
5760

5861
private CharacterLinkViewModel GenerateCharacter(Character arg)
5962
{
60-
var vm = new CharacterLinkViewModel(arg)
63+
if (alreadyOutputedChars.TryGetValue(arg.CharacterId, out var prevCopy))
6164
{
62-
IsFirstCopy = !AlreadyOutputedChars.Contains(arg.CharacterId),
63-
};
64-
if (vm.IsFirstCopy)
65-
{
66-
AlreadyOutputedChars.Add(vm.CharacterId);
65+
return prevCopy;
6766
}
67+
var vm = new CharacterLinkViewModel(arg);
68+
alreadyOutputedChars.Add(arg.CharacterId, vm);
69+
6870
return vm;
6971
}
7072
}

0 commit comments

Comments
 (0)