Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Torch.Server/Torch.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@
<Compile Include="ViewModels\Entities\CharacterViewModel.cs" />
<Compile Include="ViewModels\ConfigDedicatedViewModel.cs" />
<Compile Include="ViewModels\Entities\EntityControlViewModel.cs" />
<Compile Include="ViewModels\Entities\FactionViewModel.cs" />
<Compile Include="ViewModels\Entities\IEntityModel.cs" />
<Compile Include="ViewModels\Entities\PlayerViewModel.cs" />
<Compile Include="ViewModels\ModItemInfo.cs" />
<Compile Include="ViewModels\SessionSettingsViewModel.cs" />
<Compile Include="ViewModels\WorldConfigurationViewModel.cs" />
Expand All @@ -291,9 +293,15 @@
<Compile Include="Views\Entities\CharacterView.xaml.cs">
<DependentUpon>CharacterView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Entities\FactionView.xaml.cs">
<DependentUpon>FactionView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Entities\FloatingObjectsView.xaml.cs">
<DependentUpon>FloatingObjectsView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Entities\PlayerView.xaml.cs">
<DependentUpon>PlayerView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Extensions.cs" />
<Compile Include="Views\LogEventViewer.xaml.cs">
<DependentUpon>LogEventViewer.xaml</DependentUpon>
Expand Down Expand Up @@ -470,11 +478,13 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Entities\FactionView.xaml" />
<Page Include="Views\Entities\FloatingObjectsView.xaml" />
<Page Include="Views\Entities\GridView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Entities\PlayerView.xaml" />
<Page Include="Views\LogEventViewer.xaml" />
<Page Include="Views\LogMessageWindow.xaml" />
<Page Include="Views\ModListControl.xaml">
Expand Down
65 changes: 65 additions & 0 deletions Torch.Server/ViewModels/Entities/FactionViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.ObjectModel;
using Sandbox.Game.World;
using VRage.Game;

namespace Torch.Server.ViewModels.Entities
{
public class FactionViewModel : ViewModel
{
public FactionViewModel()
{ }

public MyFaction Faction { get; }

public FactionViewModel(MyFaction faction)
{
Faction = faction;
GenerateMembers();
}

public string Name => Faction.Name;
public string Description => Faction.Description;
public string Tag => Faction.Tag;
public long ID => Faction.FactionId;

public MemberData Founder { get; private set; }
public ObservableCollection<MemberData> Leaders { get; } = new ObservableCollection<MemberData>();
public ObservableCollection<MemberData> Members { get; } = new ObservableCollection<MemberData>();

public void GenerateMembers()
{
Leaders.Clear();
Members.Clear();
foreach (MyFactionMember factionMember in Faction.Members.Values)
{
var playerIdent = MySession.Static.Players.TryGetIdentity(factionMember.PlayerId);
MySession.Static.Players.TryGetPlayerId(factionMember.PlayerId, out MyPlayer.PlayerId playerId);
MemberData md = new MemberData
{
PlayerIdent = playerIdent,
PlayerId = playerId
};

if (factionMember.IsFounder)
{
Founder = md;
continue;
}

if (factionMember.IsLeader)
{
Leaders.Add(md);
continue;
}

Members.Add(md);
}
}
}

public sealed class MemberData
{
public MyIdentity PlayerIdent { get; set; }
public MyPlayer.PlayerId PlayerId { get; set; }
}
}
51 changes: 51 additions & 0 deletions Torch.Server/ViewModels/Entities/PlayerViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using Sandbox.Game.World;

namespace Torch.Server.ViewModels.Entities
{
public class PlayerViewModel : ViewModel
{
private readonly MyIdentity _backing;
private readonly MyPlayer.PlayerId _backingPlayer;

public PlayerViewModel()
{ }

public MyIdentity Player => _backing;

public PlayerViewModel(MyIdentity player, MyPlayer.PlayerId playerId)
{
_backing = player;
_backingPlayer = playerId;
}

public string Name => Player.DisplayName;
public long ID => Player.IdentityId;
public ulong SteamID => _backingPlayer.SteamId;

public string FactionTag
{
get
{
var faction = MySession.Static.Factions.GetPlayerFaction(ID);
return faction is null ? string.Empty : faction.Tag;
}
}

public string FactionName
{
get
{
var faction = MySession.Static.Factions.GetPlayerFaction(ID);
return faction is null ? string.Empty : faction.Name;
}
}
public DateTime LastLogin => Player.LastLoginTime;
public DateTime LastLogout => Player.LastLogoutTime;
public string LastDeathLocation => Player.LastDeathPosition.ToString();
public int BlocksBuilt => Player.BlockLimits.BlocksBuilt;
public int PCU => Player.BlockLimits.PCUBuilt;
public bool OverLimits => Player.BlockLimits.IsOverLimits;
}
}
120 changes: 105 additions & 15 deletions Torch.Server/ViewModels/EntityTreeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Character;
using Torch.Server.ViewModels.Entities;
using VRage.Game.ModAPI;
using VRage.ModAPI;
using System.Windows.Threading;
using NLog;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.World;
using Torch.API;
using Torch.API.Managers;
using Torch.API.Session;
using Torch.Collections;
using Torch.Server.Views.Entities;
using VRage.Game.ModAPI;
using PlayerViewModel = Torch.Server.ViewModels.Entities.PlayerViewModel;

namespace Torch.Server.ViewModels
{
Expand All @@ -34,13 +35,17 @@ public enum SortEnum
public MtObservableSortedDictionary<long, CharacterViewModel> Characters { get; set; } = new MtObservableSortedDictionary<long, CharacterViewModel>();
public MtObservableSortedDictionary<long, FloatingObjectViewModel> FloatingObjects { get; set; } = new MtObservableSortedDictionary<long, FloatingObjectViewModel>();
public MtObservableSortedDictionary<long, VoxelMapViewModel> VoxelMaps { get; set; } = new MtObservableSortedDictionary<long, VoxelMapViewModel>();
public MtObservableSortedDictionary<long, PlayerViewModel> Players { get; set; } = new MtObservableSortedDictionary<long, PlayerViewModel>();
public MtObservableSortedDictionary<long, FactionViewModel> Factions { get; set; } = new MtObservableSortedDictionary<long, FactionViewModel>();
public Dispatcher ControlDispatcher => _control.Dispatcher;

public SortedView<GridViewModel> SortedGrids { get; }
public SortedView<GridViewModel> FilteredSortedGrids { get; }
public SortedView<CharacterViewModel> SortedCharacters { get; }
public SortedView<FloatingObjectViewModel> SortedFloatingObjects { get; }
public SortedView<VoxelMapViewModel> SortedVoxelMaps { get; }
public SortedView<PlayerViewModel> SortedPlayers { get; }
public SortedView<FactionViewModel> SortedFactions { get; }

private EntityViewModel _currentEntity;
private SortEnum _currentSort;
Expand All @@ -58,20 +63,105 @@ public SortEnum CurrentSort
set => SetValue(ref _currentSort, value);
}

// I hate you today WPF
public EntityTreeViewModel() : this(null)
// Westin miller still hates you today WPF
public EntityTreeViewModel() : this(null, null)
{
}

public EntityTreeViewModel(UserControl control)
public EntityTreeViewModel(UserControl control, ITorchServer server)
{
_control = control;
var comparer = new EntityViewModel.Comparer(_currentSort);
SortedGrids = new SortedView<GridViewModel>(Grids.Values, comparer);
FilteredSortedGrids = new SortedView<GridViewModel>(Grids.Values, comparer);
SortedCharacters = new SortedView<CharacterViewModel>(Characters.Values, comparer);
SortedFloatingObjects = new SortedView<FloatingObjectViewModel>(FloatingObjects.Values, comparer);
SortedVoxelMaps = new SortedView<VoxelMapViewModel>(VoxelMaps.Values, comparer);
var entityComparer = new EntityViewModel.Comparer(_currentSort);
SortedGrids = new SortedView<GridViewModel>(Grids.Values, entityComparer);
FilteredSortedGrids = new SortedView<GridViewModel>(Grids.Values, entityComparer);
SortedCharacters = new SortedView<CharacterViewModel>(Characters.Values, entityComparer);
SortedFloatingObjects = new SortedView<FloatingObjectViewModel>(FloatingObjects.Values, entityComparer);
SortedVoxelMaps = new SortedView<VoxelMapViewModel>(VoxelMaps.Values, entityComparer);
SortedPlayers = new SortedView<PlayerViewModel>(Players.Values, Comparer<PlayerViewModel>
.Create((x, y) =>
string.Compare(x?.Name, y?.Name, StringComparison.InvariantCultureIgnoreCase))
);
SortedFactions = new SortedView<FactionViewModel>(Factions.Values, Comparer<FactionViewModel>
.Create((x, y) =>
string.Compare(x?.Name, y?.Name, StringComparison.InvariantCultureIgnoreCase))
);

if (server != null)
{
var sessionManager = server.Managers.GetManager<ITorchSessionManager>();
sessionManager.SessionStateChanged += RegisterLiveNonEntities;
}
}

private void RegisterLiveNonEntities(ITorchSession session, TorchSessionState newState)
{
switch (newState)
{
case TorchSessionState.Loaded:
foreach (var identity in MySession.Static.Players.GetAllPlayers())
{
if (identity.SteamId == 0) continue;
var player = MySession.Static.Players.TryGetPlayerIdentity(identity.SteamId);
if (player is null) continue;
Players.Add(new KeyValuePair<long, PlayerViewModel>(player.IdentityId, new PlayerViewModel(player, identity)));
}

foreach (MyFaction faction in MySession.Static.Factions.GetAllFactions())
{
Factions.Add(new KeyValuePair<long, FactionViewModel>(faction.FactionId, new FactionViewModel(faction)));
}

Sync.Players.RealPlayerIdentityCreated += NewPlayerCreated;
MySession.Static.Factions.FactionCreated += NewFactionCreated;
MySession.Static.Factions.FactionStateChanged += FactionChanged;
break;

case TorchSessionState.Unloading:
Sync.Players.RealPlayerIdentityCreated -= NewPlayerCreated;
MySession.Static.Factions.FactionCreated -= NewFactionCreated;
Players.Clear();
break;
}
}

// These might be off, but I only need the reason and main faction id.
private void FactionChanged(MyFactionStateChange reason, long FactionId, long ToFactionId, long PlayerId, long SenderId)
{
switch (reason)
{
case MyFactionStateChange.RemoveFaction:
ControlDispatcher.Invoke(() => { Factions.Remove(FactionId);});

break;
case MyFactionStateChange.FactionMemberAcceptJoin:
case MyFactionStateChange.FactionMemberPromote:
case MyFactionStateChange.FactionMemberKick:
case MyFactionStateChange.FactionMemberDemote:
case MyFactionStateChange.FactionMemberLeave:
ControlDispatcher.Invoke(() =>
{
if (Factions.TryGetValue(FactionId, out FactionViewModel faction))
faction.GenerateMembers();
});
break;
}
}

private void NewFactionCreated(long id)
{
ControlDispatcher.Invoke(() =>
{
var faction = MySession.Static.Factions.GetPlayerFaction(id);
if (faction is null) return;
Factions.Add(new KeyValuePair<long, FactionViewModel>(faction.FactionId, new FactionViewModel(faction)));
});
}

private void NewPlayerCreated(long identityId)
{
var player = MySession.Static.Players.TryGetPlayer(identityId);
if (player is null) return;
Players.Add(new KeyValuePair<long, PlayerViewModel>(player.Identity.IdentityId, new PlayerViewModel(player.Identity, new MyPlayer.PlayerId())));
}

public void Init()
Expand Down
Loading