@@ -42,19 +42,15 @@ public void CleanIdentities(int days)
4242 var count = 0 ;
4343 var idents = MySession . Static . Players . GetAllIdentities ( ) . ToList ( ) ;
4444 var cutoff = DateTime . Now - TimeSpan . FromDays ( days ) ;
45- foreach ( var identity in idents )
45+ var removeIds = new List < MyIdentity > ( ) ;
46+ foreach ( var identity in idents . Where ( identity => identity . LastLoginTime < cutoff ) )
4647 {
47- if ( identity . LastLoginTime < cutoff )
48- {
49- //MySession.Static.Factions.KickPlayerFromFaction(identity.IdentityId);
50- RemoveFromFaction_Internal ( identity ) ;
51- MySession . Static . Players . RemoveIdentity ( identity . IdentityId ) ;
52- count ++ ;
53- }
48+ count ++ ;
49+ removeIds . Add ( identity ) ;
5450 }
55-
51+
52+ FixGridOwnership ( new List < long > ( removeIds . Select ( x => x . IdentityId ) ) , false ) ;
5653 RemoveEmptyFactions ( ) ;
57- FixBlockOwnership ( ) ;
5854 Context . Respond ( $ "Removed { count } old identities") ;
5955 }
6056
@@ -64,30 +60,25 @@ public void PurgeIdentities(int days)
6460 {
6561 var count = 0 ;
6662 var count2 = 0 ;
67- var grids = MyEntities . GetEntities ( ) . OfType < MyCubeGrid > ( ) . ToList ( ) ;
63+ var removeIds = new List < MyIdentity > ( ) ;
6864 var idents = MySession . Static . Players . GetAllIdentities ( ) . ToList ( ) ;
6965 var cutoff = DateTime . Now - TimeSpan . FromDays ( days ) ;
70- foreach ( var identity in idents )
66+ foreach ( var identity in idents . Where ( identity => identity . LastLoginTime < cutoff ) )
7167 {
72- if ( identity . LastLoginTime < cutoff )
73- {
74- //MySession.Static.Factions.KickPlayerFromFaction(identity.IdentityId);
75- RemoveFromFaction_Internal ( identity ) ;
76- MySession . Static . Players . RemoveIdentity ( identity . IdentityId ) ;
77- count ++ ;
78- foreach ( var grid in grids )
79- {
80- if ( grid . BigOwners . Contains ( identity . IdentityId ) )
81- {
82- grid . Close ( ) ;
83- count2 ++ ;
84- }
85- }
86- }
68+ count ++ ;
69+ removeIds . Add ( identity ) ;
8770 }
8871
72+ if ( count == 0 )
73+ {
74+ Context . Respond ( $ "No old identity found past { days } ") ;
75+ return ;
76+ }
77+
78+ count2 = FixGridOwnership ( new List < long > ( removeIds . Select ( x => x . IdentityId ) ) ) ;
79+ RemoveFromFaction_Internal ( removeIds ) ;
80+
8981 RemoveEmptyFactions ( ) ;
90- FixBlockOwnership ( ) ;
9182 Context . Respond ( $ "Removed { count } old identities and { count2 } grids owned by them.") ;
9283 }
9384
@@ -96,23 +87,16 @@ public void PurgeIdentities(int days)
9687 public void PurgeIdentity ( string playername ) {
9788 var count2 = 0 ;
9889 var grids = MyEntities . GetEntities ( ) . OfType < MyCubeGrid > ( ) . ToList ( ) ;
99- var idents = MySession . Static . Players . GetAllIdentities ( ) . ToList ( ) ;
100- foreach ( var identity in idents ) {
101- if ( identity . DisplayName == playername ) {
102- //MySession.Static.Factions.KickPlayerFromFaction(identity.IdentityId);
103- RemoveFromFaction_Internal ( identity ) ;
104- MySession . Static . Players . RemoveIdentity ( identity . IdentityId ) ;
105- foreach ( var grid in grids ) {
106- if ( grid . BigOwners . Contains ( identity . IdentityId ) ) {
107- grid . Close ( ) ;
108- count2 ++ ;
109- }
110- }
111- }
90+ var id = Utilities . GetIdentityByNameOrIds ( playername ) ;
91+
92+ if ( id == null )
93+ {
94+ Context . Respond ( $ "No Identity found for { playername } . Try Again") ;
95+ return ;
11296 }
11397
98+ count2 = FixGridOwnership ( new List < long > { id . IdentityId } ) ;
11499 RemoveEmptyFactions ( ) ;
115- FixBlockOwnership ( ) ;
116100 Context . Respond ( $ "Removed identity and { count2 } grids owned by them.") ;
117101 }
118102
@@ -233,6 +217,15 @@ private static int CleanFaction_Internal(int memberCount = 1)
233217 return result ;
234218 }
235219
220+ private static void RemoveFromFaction_Internal ( List < MyIdentity > Ids )
221+ {
222+ foreach ( var identity in Ids )
223+ {
224+ RemoveFromFaction_Internal ( identity ) ;
225+ MySession . Static . Players . RemoveIdentity ( identity . IdentityId ) ;
226+ }
227+ }
228+
236229 private static bool RemoveFromFaction_Internal ( MyIdentity identity )
237230 {
238231 var fac = MySession . Static . Factions . GetPlayerFaction ( identity . IdentityId ) ;
@@ -270,6 +263,36 @@ private static void RemoveFaction(MyFaction faction)
270263 MyAPIGateway . Session . Factions . RemoveFaction ( faction . FactionId ) ; //Added to remove factions that got through the crack
271264 }
272265
266+ private static int FixGridOwnership ( List < long > Ids , bool deleteGrids = true )
267+ {
268+ if ( Ids . Count == 0 ) return 0 ;
269+ var grids = new List < MyCubeGrid > ( MyEntities . GetEntities ( ) . OfType < MyCubeGrid > ( ) ) ;
270+ int count = 0 ;
271+ foreach ( var id in Ids )
272+ {
273+ if ( id == 0 ) continue ;
274+ foreach ( var grid in grids . Where ( grid => grid . BigOwners . Contains ( id ) ) )
275+ {
276+ if ( grid . BigOwners . Count > 1 )
277+ {
278+ var newOwnerId = grid . BigOwners . FirstOrDefault ( x => x != id ) ;
279+ grid . TransferBlocksBuiltByID ( id , newOwnerId ) ;
280+ foreach ( var gridCubeBlock in grid . CubeBlocks . Where ( x=> x . OwnerId == id ) )
281+ {
282+ grid . ChangeOwner ( gridCubeBlock . FatBlock , id , newOwnerId ) ;
283+ }
284+ grid . RecalculateOwners ( ) ;
285+ continue ;
286+ }
287+ if ( deleteGrids ) grid . Close ( ) ;
288+ count ++ ;
289+ }
290+ }
291+
292+ return count ;
293+
294+ }
295+
273296 private static int FixBlockOwnership ( )
274297 {
275298 int count = 0 ;
@@ -326,7 +349,7 @@ public void CleanSandbox()
326349 //clean identities that don't own any blocks, or don't have a steam ID for whatever reason
327350 foreach ( var identity in MySession . Static . Players . GetAllIdentities ( ) . ToList ( ) )
328351 {
329- if ( MySession . Static . Players . IdentityIsNpc ( identity . IdentityId ) )
352+ if ( MySession . Static . Players . IdentityIsNpc ( identity . IdentityId ) || string . IsNullOrEmpty ( identity . DisplayName ) )
330353 {
331354 validIdentities . Add ( identity . IdentityId ) ;
332355 continue ;
0 commit comments