1
0
forked from noxious/server

#184: Allow GMs to set time

This commit is contained in:
2024-11-05 23:07:23 +01:00
parent ae0241fecb
commit 26dbaa45a7
2 changed files with 88 additions and 4 deletions

View File

@ -18,10 +18,28 @@ class DateManager {
appLogger.info('Date manager loaded')
}
public stop(): void {
if (this.intervalId) {
clearInterval(this.intervalId)
this.intervalId = null
// When a GM sets the time, update the current date and update the world file
public async setTime(time: string): Promise<void> {
try {
let newDate: Date;
// Check if it's just a time (HH:mm or HH:mm:ss format)
if (/^\d{1,2}:\d{2}(:\d{2})?$/.test(time)) {
const [hours, minutes] = time.split(':').map(Number);
newDate = new Date(this.currentDate); // Clone current date
newDate.setHours(hours, minutes);
} else {
// Treat as full datetime string
newDate = new Date(time);
if (isNaN(newDate.getTime())) return;
}
this.currentDate = newDate;
this.emitDate();
await this.saveDate();
} catch (error) {
appLogger.error(`Failed to set time: ${error instanceof Error ? error.message : String(error)}`);
throw error;
}
}