-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeMonitor.cs
More file actions
528 lines (462 loc) · 21.9 KB
/
TimeMonitor.cs
File metadata and controls
528 lines (462 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
using System;
using Il2CppScheduleOne.GameTime;
using MelonLoader;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using PaxDrops.MrStacks;
using Il2CppScheduleOne.Storage;
using Il2CppScheduleOne.Economy;
using PaxDrops.Configs;
namespace PaxDrops
{
/// <summary>
/// Enhanced time monitoring system with collection detection, expiry cleanup, and reminders.
/// Monitors game time changes and handles scheduled events.
/// </summary>
public static class TimeMonitor
{
private static bool _initialized = false;
public static void Init()
{
if (_initialized) return;
_initialized = true;
try
{
var timeManager = TimeManager.Instance;
if (timeManager != null)
{
HookTimeManagerEvents(timeManager);
}
else
{
Logger.Warn("⚠️ TimeManager not found during init, will retry later.", "TimeMonitor");
MelonCoroutines.Start(RetryTimeManagerHook());
}
Logger.Info("⏰ Time monitoring initialized.", "TimeMonitor");
}
catch (Exception ex)
{
Logger.Error("❌ Failed to initialize time monitoring.", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
private static IEnumerator RetryTimeManagerHook()
{
int retries = 0;
while (retries < 10 && TimeManager.Instance == null)
{
yield return new WaitForSeconds(5f);
retries++;
}
var timeManager = TimeManager.Instance;
if (timeManager != null)
{
HookTimeManagerEvents(timeManager);
Logger.Msg("✅ TimeManager hook established after retry.", "TimeMonitor");
}
else
{
Logger.Error("❌ Failed to hook TimeManager after retries.", "TimeMonitor");
}
}
private static void HookTimeManagerEvents(TimeManager timeManager)
{
// Hook into hour changes to check for 7:00 AM events and reminders
timeManager.onHourPass += new System.Action(OnHourPass);
// Hook into day changes for daily reset
timeManager.onDayPass += new System.Action(OnDayPass);
Logger.Info("🕐 Event hooks established.", "TimeMonitor");
}
/// <summary>
/// Handles hour changes - checks for scheduled drops, collection detection, expiry cleanup, and reminders.
/// </summary>
private static void OnHourPass()
{
try
{
var timeManager = TimeManager.Instance;
if (timeManager == null) return;
int currentHour = timeManager.CurrentTime;
int currentDay = timeManager.ElapsedDays;
Logger.Msg($"⏰ Hour changed to {currentHour} on day {currentDay}", "TimeMonitor");
// Check for scheduled drop deliveries at delivery time (7:30 AM = 730)
if (currentHour == 730)
{
Logger.Msg("🌅 Drop delivery time! Checking for scheduled deliveries...", "TimeMonitor");
ProcessScheduledDeliveries(currentDay, currentHour);
}
// Also check for missed delivery windows (if player skipped 7:30 AM)
else if (currentHour > 730 && currentHour < 1200) // Between 7:30 AM and noon
{
Logger.Msg($"🔍 Checking for missed deliveries (current time {currentHour} is past 7:30 AM)...", "TimeMonitor");
ProcessScheduledDeliveries(currentDay, currentHour);
}
// Collection detection and expiry cleanup (every hour)
CheckCollectionStatus(currentDay, currentHour);
CleanupExpiredDrops(currentDay, currentHour);
// Morning business hours and inactivity reminders (7:00 AM = 700)
if (currentHour == 700)
{
Logger.Msg("🌅 Morning business hours starting!", "TimeMonitor");
MrStacksNPC.OnNewDay();
}
// Evening reminder check (8:00 PM = 2000)
if (currentHour == 2000)
{
Logger.Msg("🌙 Evening reminder time!", "TimeMonitor");
SendEveningReminders(currentDay);
}
}
catch (Exception ex)
{
Logger.Error($"❌ Error handling hour change: {ex.Message}", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
/// <summary>
/// Handles day changes - resets daily flags and checks for new opportunities.
/// </summary>
private static void OnDayPass()
{
try
{
var timeManager = TimeManager.Instance;
if (timeManager == null) return;
int currentDay = timeManager.ElapsedDays;
Logger.Msg($"📅 Day changed to {currentDay}", "TimeMonitor");
// Notify MrStacks about day change
MrStacksNPC.OnDayChanged();
// Clean up old drops from database
CleanupOldDrops(currentDay);
}
catch (Exception ex)
{
Logger.Error($"❌ Error handling day change: {ex.Message}", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
/// <summary>
/// Process scheduled deliveries for the current day and time
/// </summary>
private static void ProcessScheduledDeliveries(int currentDay, int currentHour)
{
try
{
var allDrops = SaveFileJsonDataStore.GetAllDrops();
Logger.Msg($"🔍 Checking {allDrops.Count} total drops for delivery at day {currentDay}, hour {currentHour}", "TimeMonitor");
var deliveriesToProcess = new List<SaveFileJsonDataStore.DropRecord>();
foreach (var drop in allDrops)
{
Logger.Msg($"🔍 Examining drop: Day={drop.Day}, Hour={drop.DropHour}, Location='{drop.Location}', Available={DropConfig.IsDropAvailable(drop.Day, drop.DropHour, currentDay, currentHour)}", "TimeMonitor");
// Check if this drop should be delivered now
if (DropConfig.IsDropAvailable(drop.Day, drop.DropHour, currentDay, currentHour) &&
string.IsNullOrEmpty(drop.Location)) // Not yet spawned
{
deliveriesToProcess.Add(drop);
Logger.Msg($"✅ Drop scheduled for delivery: {drop.Org} (Day={drop.Day}, Hour={drop.DropHour})", "TimeMonitor");
}
else
{
string reason = !DropConfig.IsDropAvailable(drop.Day, drop.DropHour, currentDay, currentHour)
? "not available yet"
: "already spawned";
Logger.Msg($"⏭️ Skipping drop: {drop.Org} - {reason}", "TimeMonitor");
}
}
Logger.Msg($"📦 Found {deliveriesToProcess.Count} drops ready for delivery", "TimeMonitor");
int successCount = 0;
int failCount = 0;
var readyDrops = new List<(SaveFileJsonDataStore.DropRecord drop, string location)>();
foreach (var drop in deliveriesToProcess)
{
Logger.Msg($"📦 Processing delivery #{successCount + failCount + 1}: {drop.Org} scheduled for day {drop.Day} at {DropConfig.FormatGameTime(drop.DropHour)}", "TimeMonitor");
// Spawn the drop at a location
string? location = DeadDrop.SpawnImmediateDrop(drop);
if (!string.IsNullOrEmpty(location))
{
// Add to ready drops list for consolidated messaging
if (drop.Org.Contains("Mr. Stacks"))
{
readyDrops.Add((drop, location));
}
successCount++;
Logger.Msg($"✅ Delivery #{successCount} completed: {drop.Org} at {location}", "TimeMonitor");
}
else
{
failCount++;
Logger.Error($"❌ Delivery #{failCount} failed: {drop.Org}", "TimeMonitor");
}
}
// Send consolidated ready message for all Mr. Stacks drops
if (readyDrops.Count > 0)
{
SendConsolidatedReadyMessage(readyDrops);
}
if (deliveriesToProcess.Count > 0)
{
Logger.Msg($"📬 Processed {deliveriesToProcess.Count} deliveries: {successCount} success, {failCount} failed", "TimeMonitor");
}
else
{
Logger.Msg("📭 No deliveries processed at this time", "TimeMonitor");
}
}
catch (Exception ex)
{
Logger.Error($"❌ Error processing deliveries: {ex.Message}", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
/// <summary>
/// Send consolidated ready message for multiple drops delivered at the same time
/// </summary>
private static void SendConsolidatedReadyMessage(List<(SaveFileJsonDataStore.DropRecord drop, string location)> readyDrops)
{
try
{
var npc = MrStacksMessaging.FindMrStacksNPC();
if (npc == null) return;
if (readyDrops.Count == 1)
{
// Single drop - use original format
var drop = readyDrops[0].drop;
var location = readyDrops[0].location;
var (expiryDay, expiryHour) = DropConfig.ParseExpiryTime(drop.ExpiryTime);
string expiryText = expiryDay != -1 ? $" (Expires day {expiryDay} at {DropConfig.FormatGameTime(expiryHour)})" : "";
MrStacksMessaging.SendMessage(npc,
$"Package ready! Your {drop.Org} delivery is waiting at {location}. " +
$"Retrieve when safe. Quality guaranteed as always.{expiryText}");
}
else
{
// Multiple drops - consolidated format
var (expiryDay, expiryHour) = DropConfig.ParseExpiryTime(readyDrops[0].drop.ExpiryTime);
string expiryText = expiryDay != -1 ? $" (All expire day {expiryDay} at {DropConfig.FormatGameTime(expiryHour)})" : "";
var message = $"Multiple packages ready! Your {readyDrops.Count} deliveries:\n";
for (int i = 0; i < readyDrops.Count; i++)
{
var (drop, location) = readyDrops[i];
message += $"• {drop.Org} at {location}\n";
}
message += $"Retrieve when safe. Quality guaranteed as always.{expiryText}";
MrStacksMessaging.SendMessage(npc, message);
}
Logger.Msg($"📱 Consolidated ready message sent for {readyDrops.Count} drops", "TimeMonitor");
}
catch (Exception ex)
{
Logger.Error($"❌ Consolidated ready message failed: {ex.Message}", "TimeMonitor");
}
}
/// <summary>
/// Check collection status of active drops by monitoring storage
/// </summary>
private static void CheckCollectionStatus(int currentDay, int currentHour)
{
try
{
var pendingDrops = SaveFileJsonDataStore.GetAllDrops();
var deadDrops = UnityEngine.Object.FindObjectsOfType<Il2CppScheduleOne.Economy.DeadDrop>();
foreach (var drop in pendingDrops)
{
if (drop.IsCollected || string.IsNullOrEmpty(drop.Location)) continue;
// Only check drops that are available for pickup
if (!DropConfig.IsDropAvailable(drop.Day, drop.DropHour, currentDay, currentHour)) continue;
// Find the dead drop by name
Il2CppScheduleOne.Economy.DeadDrop? targetDeadDrop = null;
foreach (var deadDrop in deadDrops)
{
if (deadDrop.DeadDropName == drop.Location)
{
targetDeadDrop = deadDrop;
break;
}
}
if (targetDeadDrop?.Storage != null)
{
// Check if storage is significantly emptier than initial
int currentItemCount = targetDeadDrop.Storage.ItemCount;
// If storage has 50% or fewer items than initially placed, consider it collected
if (currentItemCount <= (drop.InitialItemCount * 0.5f))
{
Logger.Msg($"✅ Drop at {drop.Location} appears to have been collected ({currentItemCount}/{drop.InitialItemCount} items remaining)", "TimeMonitor");
SaveFileJsonDataStore.MarkSpecificDropCollected(drop.Day, drop.Location);
}
}
}
}
catch (Exception ex)
{
Logger.Error($"❌ Error checking collection status: {ex.Message}", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
/// <summary>
/// Clean up expired drops from their storage locations
/// </summary>
private static void CleanupExpiredDrops(int currentDay, int currentHour)
{
try
{
var allDrops = SaveFileJsonDataStore.GetAllDrops();
var expiredDrops = new List<SaveFileJsonDataStore.DropRecord>();
// Find expired drops using game time
foreach (var drop in allDrops)
{
if (!string.IsNullOrEmpty(drop.ExpiryTime) &&
DropConfig.IsDropExpired(drop.ExpiryTime, currentDay, currentHour))
{
expiredDrops.Add(drop);
}
}
if (expiredDrops.Count == 0) return;
var deadDrops = UnityEngine.Object.FindObjectsOfType<Il2CppScheduleOne.Economy.DeadDrop>();
foreach (var drop in expiredDrops)
{
var (expiryDay, expiryHour) = DropConfig.ParseExpiryTime(drop.ExpiryTime);
Logger.Msg($"🗑️ Cleaning up expired drop at {drop.Location} (expired day {expiryDay} at {DropConfig.FormatGameTime(expiryHour)})", "TimeMonitor");
// Find the dead drop and clear its contents
foreach (var deadDrop in deadDrops)
{
if (deadDrop.DeadDropName == drop.Location && deadDrop.Storage != null)
{
try
{
// Clear the storage contents
deadDrop.Storage.ClearContents();
Logger.Msg($"✅ Cleared expired drop contents from {drop.Location}", "TimeMonitor");
}
catch (Exception ex)
{
Logger.Error($"❌ Failed to clear contents from {drop.Location}: {ex.Message}", "TimeMonitor");
}
break;
}
}
// Remove from pending drops - use specific location removal
SaveFileJsonDataStore.RemoveSpecificDrop(drop.Day, drop.Location);
}
if (expiredDrops.Count > 0)
{
Logger.Msg($"🗑️ Cleaned up {expiredDrops.Count} expired drops", "TimeMonitor");
}
}
catch (Exception ex)
{
Logger.Error($"❌ Error cleaning up expired drops: {ex.Message}", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
/// <summary>
/// Send evening reminders for uncollected drops
/// </summary>
private static void SendEveningReminders(int currentDay)
{
try
{
var pendingDrops = SaveFileJsonDataStore.GetAllDrops();
var uncollectedDrops = new List<SaveFileJsonDataStore.DropRecord>();
foreach (var drop in pendingDrops)
{
// Check for drops that are available today but not collected
if (drop.Day == currentDay && !drop.IsCollected && !string.IsNullOrEmpty(drop.Location))
{
uncollectedDrops.Add(drop);
}
}
if (uncollectedDrops.Count > 0)
{
Logger.Msg($"📱 Sending evening reminder for {uncollectedDrops.Count} uncollected drops", "TimeMonitor");
var npc = MrStacksMessaging.FindMrStacksNPC();
if (npc != null)
{
if (uncollectedDrops.Count == 1)
{
// Single drop - use original format
var drop = uncollectedDrops[0];
var (expiryDay, expiryHour) = DropConfig.ParseExpiryTime(drop.ExpiryTime);
string expiryText = expiryDay != -1 ? $" (expires day {expiryDay} at {DropConfig.FormatGameTime(expiryHour)})" : "";
MrStacksMessaging.SendMessage(npc,
$"Evening reminder: Your {drop.Org} package is still waiting at {drop.Location}{expiryText}. " +
$"Don't forget to collect it when safe!");
}
else
{
// Multiple drops - consolidated format
var (expiryDay, expiryHour) = DropConfig.ParseExpiryTime(uncollectedDrops[0].ExpiryTime);
string expiryText = expiryDay != -1 ? $" (all expire day {expiryDay} at {DropConfig.FormatGameTime(expiryHour)})" : "";
var message = $"Evening reminder: You have {uncollectedDrops.Count} uncollected packages:\n";
for (int i = 0; i < uncollectedDrops.Count; i++)
{
var drop = uncollectedDrops[i];
message += $"• {drop.Org} at {drop.Location}\n";
}
message += $"Don't forget to collect them when safe!{expiryText}";
MrStacksMessaging.SendMessage(npc, message);
}
}
}
}
catch (Exception ex)
{
Logger.Error($"❌ Error sending evening reminders: {ex.Message}", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
/// <summary>
/// Removes old drops that are past their expiration
/// </summary>
private static void CleanupOldDrops(int currentDay)
{
try
{
int removedCount = 0;
var daysToRemove = new List<int>();
foreach (var kvp in SaveFileJsonDataStore.PendingDrops)
{
// Remove drops that are more than 2 days old (extra safety buffer)
if (kvp.Key < currentDay - 2)
{
removedCount += kvp.Value.Count;
daysToRemove.Add(kvp.Key);
}
}
foreach (var day in daysToRemove)
{
SaveFileJsonDataStore.PendingDrops.Remove(day);
}
if (removedCount > 0)
{
Logger.Msg($"🗑️ Cleaned up {removedCount} old drop records from {daysToRemove.Count} days", "TimeMonitor");
}
}
catch (Exception ex)
{
Logger.Error($"❌ Error cleaning up old drops: {ex.Message}", "TimeMonitor");
}
}
public static void Shutdown()
{
if (!_initialized) return;
try
{
var timeManager = TimeManager.Instance;
if (timeManager != null)
{
// Unhook events
timeManager.onHourPass -= new System.Action(OnHourPass);
timeManager.onDayPass -= new System.Action(OnDayPass);
}
_initialized = false;
Logger.Info("🔌 Time monitoring shutdown.", "TimeMonitor");
}
catch (Exception ex)
{
Logger.Error("❌ Error during shutdown.", "TimeMonitor");
Logger.Exception(ex, "TimeMonitor");
}
}
}
}