11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4- using System . Runtime . CompilerServices ;
54using Sandbox . Definitions ;
65using Sandbox . Game . Entities ;
76using Sandbox . Game . Entities . Cube ;
8- using Sandbox . ModAPI ;
9- using Torch . API . Managers ;
7+ using Sandbox . Game . World ;
108using Torch . Collections ;
119using Torch . Server . ViewModels . Blocks ;
1210using VRage . Game ;
@@ -50,18 +48,19 @@ public MtObservableSortedDictionary<MyCubeBlockDefinition, MtObservableSortedDic
5048 Blocks { get ; } =
5149 new MtObservableSortedDictionary < MyCubeBlockDefinition , MtObservableSortedDictionary < long , BlockViewModel > > (
5250 CubeBlockDefinitionComparer . Default ) ;
53-
51+
5452 public GridViewModel ( )
5553 {
54+
5655 }
57-
56+
5857 public GridViewModel ( MyCubeGrid grid , EntityTreeViewModel tree ) : base ( grid , tree )
5958 {
6059 //DescriptiveName = $"{grid.DisplayName} ({grid.BlocksCount} blocks)";
6160 Blocks . Add ( _fillerDefinition , new MtObservableSortedDictionary < long , BlockViewModel > ( ) ) ;
6261 }
6362
64- private void Grid_OnBlockRemoved ( Sandbox . Game . Entities . Cube . MySlimBlock obj )
63+ private void Grid_OnBlockRemoved ( MySlimBlock obj )
6564 {
6665 if ( obj . FatBlock != null )
6766 RemoveBlock ( obj . FatBlock ) ;
@@ -75,16 +74,62 @@ private void RemoveBlock(MyCubeBlock block)
7574 return ;
7675 if ( group . Remove ( block . EntityId ) && group . Count == 0 && Blocks . Count > 1 )
7776 Blocks . Remove ( block . BlockDefinition ) ;
77+
78+ long ownerId = block . OwnerId ;
79+ if ( block . OwnerId == 0 )
80+ ownerId = block . IDModule . Owner ;
81+
82+ if ( ownerId != 0 )
83+ {
84+ var playerIdent = MySession . Static . Players . TryGetIdentity ( ownerId ) ;
85+ if ( playerIdent != null )
86+ {
87+ var name = playerIdent . DisplayName ;
88+ if ( Owners . ContainsKey ( name ) )
89+ Owners [ name ] -- ;
90+ if ( Owners [ name ] == 0 )
91+ Owners . Remove ( name ) ;
92+ }
93+ }
7894 }
7995
8096 private void AddBlock ( MyTerminalBlock block )
8197 {
8298 if ( ! Blocks . TryGetValue ( block . BlockDefinition , out var group ) )
8399 group = Blocks [ block . BlockDefinition ] = new MtObservableSortedDictionary < long , BlockViewModel > ( ) ;
84100 group . Add ( block . EntityId , new BlockViewModel ( block , Tree ) ) ;
101+
102+ long ownerId = block . OwnerId ;
103+ if ( block . OwnerId == 0 )
104+ ownerId = block . IDModule . Owner ;
105+
106+ if ( ownerId != 0 )
107+ {
108+ var playerIdent = MySession . Static . Players . TryGetIdentity ( ownerId ) ;
109+ if ( playerIdent != null )
110+ {
111+ var name = playerIdent . DisplayName ;
112+ if ( ! Owners . TryGetValue ( name , out int count ) )
113+ Owners [ name ] = 1 ;
114+ else
115+ Owners [ name ] = count + 1 ;
116+ }
117+ }
118+ }
119+
120+ public int BlockCount
121+ {
122+ get
123+ {
124+ if ( Grid != null )
125+ return Grid . BlocksCount ;
126+ return 0 ;
127+ }
85128 }
86129
87- private void Grid_OnBlockAdded ( Sandbox . Game . Entities . Cube . MySlimBlock obj )
130+ public MtObservableSortedDictionary < string , int > Owners { get ; } = new MtObservableSortedDictionary < string , int > ( ) ;
131+
132+ private void Grid_OnBlockAdded ( MySlimBlock obj )
88133 {
89134 var block = obj . FatBlock as MyTerminalBlock ;
90135 if ( block != null )
0 commit comments