55 using System . Linq ;
66 using EssentialsPlugin . Settings ;
77 using EssentialsPlugin . Utility ;
8- using NLog ;
98 using Sandbox . ModAPI ;
109 using SEModAPI . API . Utility ;
1110
1211 class ProcessCleanup : ProcessHandlerBase
1312 {
1413 private SettingsCleanupTriggerItem _triggerdItem ;
15- private DateTime _start ;
16-
17- public ProcessCleanup ( )
18- {
19- _start = DateTime . Now ;
20- }
14+ private DateTime _start = DateTime . Now ;
15+ private readonly TimeSpan _oneSecond = new TimeSpan ( 0 , 0 , 1 ) ;
16+ private readonly TimeSpan _twentySeconds = new TimeSpan ( 0 , 0 , 20 ) ;
17+ private readonly TimeSpan _oneMinute = new TimeSpan ( 0 , 1 , 0 ) ;
18+ private readonly TimeSpan _fiveMinutes = new TimeSpan ( 0 , 5 , 0 ) ;
2119
2220 public override int GetUpdateResolution ( )
2321 {
@@ -54,18 +52,18 @@ private void ProcessTimedItem( SettingsCleanupTimedItem item )
5452 {
5553 _start = DateTime . Now ; // this needs to be updated for each run so multi-day runtimes are handled properly
5654
57- DateTime time = new DateTime ( _start . Year , _start . Month , _start . Day , item . Restart . Hour , item . Restart . Minute , 0 ) ;
58- if ( time - DateTime . Now < TimeSpan . FromSeconds ( - 20 ) )
55+ DateTime itemTime = new DateTime ( _start . Year , _start . Month , _start . Day , item . Restart . Hour , item . Restart . Minute , 0 ) ;
56+ if ( DateTime . Now - itemTime > _twentySeconds )
5957 {
60- time = new DateTime ( _start . Year , _start . Month , _start . Day + 1 , item . Restart . Hour , item . Restart . Minute , 0 ) ;
58+ itemTime = itemTime . AddDays ( 1 ) ;
6159 }
6260
63- if ( DateTime . Now - item . LastRan < TimeSpan . FromMinutes ( 1 ) )
61+ if ( DateTime . Now - item . LastRan < _oneMinute )
6462 return ;
6563
66- if ( time - DateTime . Now < TimeSpan . FromSeconds ( 1 ) && DateTime . Now - item . LastRan > TimeSpan . FromMinutes ( 1 ) )
64+ if ( itemTime - DateTime . Now < _oneSecond && DateTime . Now - item . LastRan > _oneMinute )
6765 {
68- string command = item . ScanCommand + " quiet";
66+ string command = string . Format ( "{0} quiet", item . ScanCommand ) ;
6967 HashSet < IMyEntity > entities = CubeGrids . ScanGrids ( 0 , CommandParser . GetCommandParts ( command ) . ToArray ( ) ) ;
7068 CubeGrids . DeleteGrids ( entities ) ;
7169 Communication . SendPublicInformation ( string . Format ( "[NOTICE]: Timed cleanup has run. {0} entities removed. Have a nice day." , entities . Count ) ) ;
@@ -79,11 +77,11 @@ private void ProcessTimedItem( SettingsCleanupTimedItem item )
7977 if ( item . NotificationItemsRan . Contains ( notifyItem ) )
8078 continue ;
8179
82- if ( time - DateTime . Now < TimeSpan . FromMinutes ( notifyItem . MinutesBeforeCleanup ) )
80+ if ( itemTime - DateTime . Now < TimeSpan . FromMinutes ( notifyItem . MinutesBeforeCleanup ) )
8381 {
8482 item . NotificationItemsRan . Add ( notifyItem ) ;
8583
86- if ( DateTime . Now - notifyItem . lastRan > TimeSpan . FromMinutes ( 1 ) )
84+ if ( DateTime . Now - notifyItem . lastRan > _oneMinute )
8785 {
8886 notifyItem . lastRan = DateTime . Now ;
8987 string notification = notifyItem . Message . Replace ( "%cleanup_reason%" , item . Reason ) ;
@@ -103,17 +101,16 @@ private void ProcessTriggerItem( SettingsCleanupTriggerItem item )
103101 if ( _triggerdItem == null )
104102 {
105103 // Increase to 5 minutes
106- if ( DateTime . Now - item . LastRan > TimeSpan . FromMinutes ( 5 ) )
104+ if ( DateTime . Now - item . LastRan > _fiveMinutes )
107105 {
108106 item . LastRan = DateTime . Now ;
109107 string command = item . ScanCommand + " quiet" ;
110- HashSet < IMyEntity > entities = CubeGrids . ScanGrids ( 0 , CommandParser . GetCommandParts ( command ) . ToArray ( ) ) ;
108+ HashSet < IMyEntity > entities = CubeGrids . ScanGrids ( 0 , CommandParser . GetCommandParts ( command ) . ToArray ( ) ) ;
111109 if ( entities . Count >= item . MaxCapacity )
112110 {
113111 Communication . SendPublicInformation ( string . Format ( "[NOTICE]: Cleanup triggered. ({0} of {1}) triggered grids found. Cleanup will run in {2} minutes. Reason: {3}" , entities . Count , item . MaxCapacity , item . MinutesAfterCapacity , item . Reason ) ) ;
114112 item . NotificationItemsRan . Clear ( ) ;
115113 _triggerdItem = item ;
116- return ;
117114 }
118115 }
119116 }
@@ -122,7 +119,7 @@ private void ProcessTriggerItem( SettingsCleanupTriggerItem item )
122119 if ( DateTime . Now - item . LastRan > TimeSpan . FromMinutes ( item . MinutesAfterCapacity ) )
123120 {
124121 string command = item . ScanCommand + " quiet" ;
125- HashSet < IMyEntity > entities = CubeGrids . ScanGrids ( 0 , CommandParser . GetCommandParts ( command ) . ToArray ( ) ) ;
122+ HashSet < IMyEntity > entities = CubeGrids . ScanGrids ( 0 , CommandParser . GetCommandParts ( command ) . ToArray ( ) ) ;
126123 CubeGrids . DeleteGrids ( entities ) ;
127124 Communication . SendPublicInformation ( string . Format ( "[NOTICE]: Triggered cleanup has run. {0} entities removed. Have a nice day." , entities . Count ) ) ;
128125 _triggerdItem = null ;
0 commit comments