Skip to content

Commit a8fcee5

Browse files
committed
Playerview and Factionview added.
Playerview adds a list of all players, online of offline. This is different from the Character view as that is only online players. Factinview added with basic utilites to promote/demote members and remove factions.
1 parent e325066 commit a8fcee5

12 files changed

Lines changed: 640 additions & 42 deletions

‎Torch.Server/Torch.Server.csproj‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@
277277
<Compile Include="ViewModels\Entities\CharacterViewModel.cs" />
278278
<Compile Include="ViewModels\ConfigDedicatedViewModel.cs" />
279279
<Compile Include="ViewModels\Entities\EntityControlViewModel.cs" />
280+
<Compile Include="ViewModels\Entities\FactionViewModel.cs" />
280281
<Compile Include="ViewModels\Entities\IEntityModel.cs" />
282+
<Compile Include="ViewModels\Entities\PlayerViewModel.cs" />
281283
<Compile Include="ViewModels\ModItemInfo.cs" />
282284
<Compile Include="ViewModels\SessionSettingsViewModel.cs" />
283285
<Compile Include="ViewModels\WorldConfigurationViewModel.cs" />
@@ -291,9 +293,15 @@
291293
<Compile Include="Views\Entities\CharacterView.xaml.cs">
292294
<DependentUpon>CharacterView.xaml</DependentUpon>
293295
</Compile>
296+
<Compile Include="Views\Entities\FactionView.xaml.cs">
297+
<DependentUpon>FactionView.xaml</DependentUpon>
298+
</Compile>
294299
<Compile Include="Views\Entities\FloatingObjectsView.xaml.cs">
295300
<DependentUpon>FloatingObjectsView.xaml</DependentUpon>
296301
</Compile>
302+
<Compile Include="Views\Entities\PlayerView.xaml.cs">
303+
<DependentUpon>PlayerView.xaml</DependentUpon>
304+
</Compile>
297305
<Compile Include="Views\Extensions.cs" />
298306
<Compile Include="Views\LogEventViewer.xaml.cs">
299307
<DependentUpon>LogEventViewer.xaml</DependentUpon>
@@ -470,11 +478,13 @@
470478
<Generator>MSBuild:Compile</Generator>
471479
<SubType>Designer</SubType>
472480
</Page>
481+
<Page Include="Views\Entities\FactionView.xaml" />
473482
<Page Include="Views\Entities\FloatingObjectsView.xaml" />
474483
<Page Include="Views\Entities\GridView.xaml">
475484
<SubType>Designer</SubType>
476485
<Generator>MSBuild:Compile</Generator>
477486
</Page>
487+
<Page Include="Views\Entities\PlayerView.xaml" />
478488
<Page Include="Views\LogEventViewer.xaml" />
479489
<Page Include="Views\LogMessageWindow.xaml" />
480490
<Page Include="Views\ModListControl.xaml">
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.ObjectModel;
2+
using Sandbox.Game.World;
3+
using VRage.Game;
4+
5+
namespace Torch.Server.ViewModels.Entities
6+
{
7+
public class FactionViewModel : ViewModel
8+
{
9+
public FactionViewModel()
10+
{ }
11+
12+
public MyFaction Faction { get; }
13+
14+
public FactionViewModel(MyFaction faction)
15+
{
16+
Faction = faction;
17+
GenerateMembers();
18+
}
19+
20+
public string Name => Faction.Name;
21+
public string Description => Faction.Description;
22+
public string Tag => Faction.Tag;
23+
public long ID => Faction.FactionId;
24+
25+
public MemberData Founder { get; private set; }
26+
public ObservableCollection<MemberData> Leaders { get; } = new ObservableCollection<MemberData>();
27+
public ObservableCollection<MemberData> Members { get; } = new ObservableCollection<MemberData>();
28+
29+
public void GenerateMembers()
30+
{
31+
Leaders.Clear();
32+
Members.Clear();
33+
foreach (MyFactionMember factionMember in Faction.Members.Values)
34+
{
35+
var playerIdent = MySession.Static.Players.TryGetIdentity(factionMember.PlayerId);
36+
MySession.Static.Players.TryGetPlayerId(factionMember.PlayerId, out MyPlayer.PlayerId playerId);
37+
MemberData md = new MemberData
38+
{
39+
PlayerIdent = playerIdent,
40+
PlayerId = playerId
41+
};
42+
43+
if (factionMember.IsFounder)
44+
{
45+
Founder = md;
46+
continue;
47+
}
48+
49+
if (factionMember.IsLeader)
50+
{
51+
Leaders.Add(md);
52+
continue;
53+
}
54+
55+
Members.Add(md);
56+
}
57+
}
58+
}
59+
60+
public sealed class MemberData
61+
{
62+
public MyIdentity PlayerIdent { get; set; }
63+
public MyPlayer.PlayerId PlayerId { get; set; }
64+
}
65+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Sandbox.Game.World;
4+
5+
namespace Torch.Server.ViewModels.Entities
6+
{
7+
public class PlayerViewModel : ViewModel
8+
{
9+
private readonly MyIdentity _backing;
10+
private readonly MyPlayer.PlayerId _backingPlayer;
11+
12+
public PlayerViewModel()
13+
{ }
14+
15+
public MyIdentity Player => _backing;
16+
17+
public PlayerViewModel(MyIdentity player, MyPlayer.PlayerId playerId)
18+
{
19+
_backing = player;
20+
_backingPlayer = playerId;
21+
}
22+
23+
public string Name => Player.DisplayName;
24+
public long ID => Player.IdentityId;
25+
public ulong SteamID => _backingPlayer.SteamId;
26+
27+
public string FactionTag
28+
{
29+
get
30+
{
31+
var faction = MySession.Static.Factions.GetPlayerFaction(ID);
32+
return faction is null ? string.Empty : faction.Tag;
33+
}
34+
}
35+
36+
public string FactionName
37+
{
38+
get
39+
{
40+
var faction = MySession.Static.Factions.GetPlayerFaction(ID);
41+
return faction is null ? string.Empty : faction.Name;
42+
}
43+
}
44+
public DateTime LastLogin => Player.LastLoginTime;
45+
public DateTime LastLogout => Player.LastLogoutTime;
46+
public string LastDeathLocation => Player.LastDeathPosition.ToString();
47+
public int BlocksBuilt => Player.BlockLimits.BlocksBuilt;
48+
public int PCU => Player.BlockLimits.PCUBuilt;
49+
public bool OverLimits => Player.BlockLimits.IsOverLimits;
50+
}
51+
}

‎Torch.Server/ViewModels/EntityTreeViewModel.cs‎

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63
using System.Windows.Controls;
74
using Sandbox.Game.Entities;
85
using Sandbox.Game.Entities.Character;
96
using Torch.Server.ViewModels.Entities;
10-
using VRage.Game.ModAPI;
11-
using VRage.ModAPI;
127
using System.Windows.Threading;
138
using NLog;
9+
using Sandbox.Game.Multiplayer;
10+
using Sandbox.Game.World;
11+
using Torch.API;
12+
using Torch.API.Managers;
13+
using Torch.API.Session;
1414
using Torch.Collections;
15-
using Torch.Server.Views.Entities;
15+
using VRage.Game.ModAPI;
16+
using PlayerViewModel = Torch.Server.ViewModels.Entities.PlayerViewModel;
1617

1718
namespace Torch.Server.ViewModels
1819
{
@@ -34,13 +35,17 @@ public enum SortEnum
3435
public MtObservableSortedDictionary<long, CharacterViewModel> Characters { get; set; } = new MtObservableSortedDictionary<long, CharacterViewModel>();
3536
public MtObservableSortedDictionary<long, FloatingObjectViewModel> FloatingObjects { get; set; } = new MtObservableSortedDictionary<long, FloatingObjectViewModel>();
3637
public MtObservableSortedDictionary<long, VoxelMapViewModel> VoxelMaps { get; set; } = new MtObservableSortedDictionary<long, VoxelMapViewModel>();
38+
public MtObservableSortedDictionary<long, PlayerViewModel> Players { get; set; } = new MtObservableSortedDictionary<long, PlayerViewModel>();
39+
public MtObservableSortedDictionary<long, FactionViewModel> Factions { get; set; } = new MtObservableSortedDictionary<long, FactionViewModel>();
3740
public Dispatcher ControlDispatcher => _control.Dispatcher;
3841

3942
public SortedView<GridViewModel> SortedGrids { get; }
4043
public SortedView<GridViewModel> FilteredSortedGrids { get; }
4144
public SortedView<CharacterViewModel> SortedCharacters { get; }
4245
public SortedView<FloatingObjectViewModel> SortedFloatingObjects { get; }
4346
public SortedView<VoxelMapViewModel> SortedVoxelMaps { get; }
47+
public SortedView<PlayerViewModel> SortedPlayers { get; }
48+
public SortedView<FactionViewModel> SortedFactions { get; }
4449

4550
private EntityViewModel _currentEntity;
4651
private SortEnum _currentSort;
@@ -58,20 +63,105 @@ public SortEnum CurrentSort
5863
set => SetValue(ref _currentSort, value);
5964
}
6065

61-
// I hate you today WPF
62-
public EntityTreeViewModel() : this(null)
66+
// Westin miller still hates you today WPF
67+
public EntityTreeViewModel() : this(null, null)
6368
{
6469
}
6570

66-
public EntityTreeViewModel(UserControl control)
71+
public EntityTreeViewModel(UserControl control, ITorchServer server)
6772
{
6873
_control = control;
69-
var comparer = new EntityViewModel.Comparer(_currentSort);
70-
SortedGrids = new SortedView<GridViewModel>(Grids.Values, comparer);
71-
FilteredSortedGrids = new SortedView<GridViewModel>(Grids.Values, comparer);
72-
SortedCharacters = new SortedView<CharacterViewModel>(Characters.Values, comparer);
73-
SortedFloatingObjects = new SortedView<FloatingObjectViewModel>(FloatingObjects.Values, comparer);
74-
SortedVoxelMaps = new SortedView<VoxelMapViewModel>(VoxelMaps.Values, comparer);
74+
var entityComparer = new EntityViewModel.Comparer(_currentSort);
75+
SortedGrids = new SortedView<GridViewModel>(Grids.Values, entityComparer);
76+
FilteredSortedGrids = new SortedView<GridViewModel>(Grids.Values, entityComparer);
77+
SortedCharacters = new SortedView<CharacterViewModel>(Characters.Values, entityComparer);
78+
SortedFloatingObjects = new SortedView<FloatingObjectViewModel>(FloatingObjects.Values, entityComparer);
79+
SortedVoxelMaps = new SortedView<VoxelMapViewModel>(VoxelMaps.Values, entityComparer);
80+
SortedPlayers = new SortedView<PlayerViewModel>(Players.Values, Comparer<PlayerViewModel>
81+
.Create((x, y) =>
82+
string.Compare(x?.Name, y?.Name, StringComparison.InvariantCultureIgnoreCase))
83+
);
84+
SortedFactions = new SortedView<FactionViewModel>(Factions.Values, Comparer<FactionViewModel>
85+
.Create((x, y) =>
86+
string.Compare(x?.Name, y?.Name, StringComparison.InvariantCultureIgnoreCase))
87+
);
88+
89+
if (server != null)
90+
{
91+
var sessionManager = server.Managers.GetManager<ITorchSessionManager>();
92+
sessionManager.SessionStateChanged += RegisterLiveNonEntities;
93+
}
94+
}
95+
96+
private void RegisterLiveNonEntities(ITorchSession session, TorchSessionState newState)
97+
{
98+
switch (newState)
99+
{
100+
case TorchSessionState.Loaded:
101+
foreach (var identity in MySession.Static.Players.GetAllPlayers())
102+
{
103+
if (identity.SteamId == 0) continue;
104+
var player = MySession.Static.Players.TryGetPlayerIdentity(identity.SteamId);
105+
if (player is null) continue;
106+
Players.Add(new KeyValuePair<long, PlayerViewModel>(player.IdentityId, new PlayerViewModel(player, identity)));
107+
}
108+
109+
foreach (MyFaction faction in MySession.Static.Factions.GetAllFactions())
110+
{
111+
Factions.Add(new KeyValuePair<long, FactionViewModel>(faction.FactionId, new FactionViewModel(faction)));
112+
}
113+
114+
Sync.Players.RealPlayerIdentityCreated += NewPlayerCreated;
115+
MySession.Static.Factions.FactionCreated += NewFactionCreated;
116+
MySession.Static.Factions.FactionStateChanged += FactionChanged;
117+
break;
118+
119+
case TorchSessionState.Unloading:
120+
Sync.Players.RealPlayerIdentityCreated -= NewPlayerCreated;
121+
MySession.Static.Factions.FactionCreated -= NewFactionCreated;
122+
Players.Clear();
123+
break;
124+
}
125+
}
126+
127+
// These might be off, but I only need the reason and main faction id.
128+
private void FactionChanged(MyFactionStateChange reason, long FactionId, long ToFactionId, long PlayerId, long SenderId)
129+
{
130+
switch (reason)
131+
{
132+
case MyFactionStateChange.RemoveFaction:
133+
ControlDispatcher.Invoke(() => { Factions.Remove(FactionId);});
134+
135+
break;
136+
case MyFactionStateChange.FactionMemberAcceptJoin:
137+
case MyFactionStateChange.FactionMemberPromote:
138+
case MyFactionStateChange.FactionMemberKick:
139+
case MyFactionStateChange.FactionMemberDemote:
140+
case MyFactionStateChange.FactionMemberLeave:
141+
ControlDispatcher.Invoke(() =>
142+
{
143+
if (Factions.TryGetValue(FactionId, out FactionViewModel faction))
144+
faction.GenerateMembers();
145+
});
146+
break;
147+
}
148+
}
149+
150+
private void NewFactionCreated(long id)
151+
{
152+
ControlDispatcher.Invoke(() =>
153+
{
154+
var faction = MySession.Static.Factions.GetPlayerFaction(id);
155+
if (faction is null) return;
156+
Factions.Add(new KeyValuePair<long, FactionViewModel>(faction.FactionId, new FactionViewModel(faction)));
157+
});
158+
}
159+
160+
private void NewPlayerCreated(long identityId)
161+
{
162+
var player = MySession.Static.Players.TryGetPlayer(identityId);
163+
if (player is null) return;
164+
Players.Add(new KeyValuePair<long, PlayerViewModel>(player.Identity.IdentityId, new PlayerViewModel(player.Identity, new MyPlayer.PlayerId())));
75165
}
76166

77167
public void Init()

0 commit comments

Comments
 (0)