11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
5- using System . Threading . Tasks ;
63using System . Windows . Controls ;
74using Sandbox . Game . Entities ;
85using Sandbox . Game . Entities . Character ;
96using Torch . Server . ViewModels . Entities ;
10- using VRage . Game . ModAPI ;
11- using VRage . ModAPI ;
127using System . Windows . Threading ;
138using 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 ;
1414using Torch . Collections ;
15- using Torch . Server . Views . Entities ;
15+ using VRage . Game . ModAPI ;
16+ using PlayerViewModel = Torch . Server . ViewModels . Entities . PlayerViewModel ;
1617
1718namespace 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