From bfd941c091beb0b9b9a6dc921e728ca9da9a49cb Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Mon, 14 Oct 2024 20:11:08 +0200 Subject: [PATCH] Rnamed Datetime > Date --- src/managers/dateManager.ts | 68 ++++++++++++++++++++++++++++++ src/managers/datetimeManager.ts | 74 --------------------------------- src/server.ts | 6 +-- src/utilities/types.ts | 7 ++++ 4 files changed, 78 insertions(+), 77 deletions(-) create mode 100644 src/managers/dateManager.ts delete mode 100644 src/managers/datetimeManager.ts diff --git a/src/managers/dateManager.ts b/src/managers/dateManager.ts new file mode 100644 index 0000000..9a1ae1a --- /dev/null +++ b/src/managers/dateManager.ts @@ -0,0 +1,68 @@ +import { Server } from 'socket.io' +import { appLogger } from '../utilities/logger' +import { getRootPath } from '../utilities/storage' +import { readJsonValue, setJsonValue } from '../utilities/json' + +class DateManager { + private static readonly GAME_SPEED = 8 // 24 game hours / 3 real hours + private static readonly UPDATE_INTERVAL = 1000 // 1 second + + private io: Server | null = null + private intervalId: NodeJS.Timeout | null = null + private currentDate: Date = new Date() + + public async boot(io: Server): Promise { + this.io = io + await this.loadDate() + this.startDateLoop() + appLogger.info('Date manager loaded') + } + + public stop(): void { + if (this.intervalId) { + clearInterval(this.intervalId) + this.intervalId = null + } + } + + private async loadDate(): Promise { + try { + const dateString = await readJsonValue(this.getWorldFilePath(), 'date') + this.currentDate = new Date(dateString) + } catch (error) { + appLogger.error(`Failed to load date: ${error instanceof Error ? error.message : String(error)}`) + this.currentDate = new Date() // Use current date as fallback + } + } + + private startDateLoop(): void { + this.intervalId = setInterval(() => { + this.advanceGameTime() + this.emitDate() + this.saveDate() + }, DateManager.UPDATE_INTERVAL) + } + + private advanceGameTime(): void { + const advanceMilliseconds = DateManager.GAME_SPEED * DateManager.UPDATE_INTERVAL + this.currentDate = new Date(this.currentDate.getTime() + advanceMilliseconds) + } + + private emitDate(): void { + this.io?.emit('date', this.currentDate) + } + + private async saveDate(): Promise { + try { + await setJsonValue(this.getWorldFilePath(), 'date', this.currentDate) + } catch (error) { + appLogger.error(`Failed to save date: ${error instanceof Error ? error.message : String(error)}`) + } + } + + private getWorldFilePath(): string { + return getRootPath('data', 'world.json') + } +} + +export default new DateManager() \ No newline at end of file diff --git a/src/managers/datetimeManager.ts b/src/managers/datetimeManager.ts deleted file mode 100644 index cd511c7..0000000 --- a/src/managers/datetimeManager.ts +++ /dev/null @@ -1,74 +0,0 @@ -// src/managers/datetimeManager.ts - -import { Server } from 'socket.io' -import { appLogger } from '../utilities/logger' -import { getRootPath } from '../utilities/storage' -import { readJsonValue, setJsonValue } from '../utilities/json' - -class DatetimeManager { - private static readonly GAME_SPEED = 8 // 24 game hours / 3 real hours - private static readonly UPDATE_INTERVAL = 1000 // 1 second - - private io: Server | null = null - private intervalId: NodeJS.Timeout | null = null - private currentDateTime: Date = new Date() - - public async boot(io: Server): Promise { - this.io = io - await this.loadDateTime() - this.startDateTimeLoop() - appLogger.info('Datetime manager loaded') - } - - public stop(): void { - if (this.intervalId) { - clearInterval(this.intervalId) - this.intervalId = null - } - } - - private async loadDateTime(): Promise { - try { - const datetimeString = await readJsonValue(this.getWorldFilePath(), 'datetime') - this.currentDateTime = new Date(datetimeString) - } catch (error) { - appLogger.error(`Failed to load datetime: ${error instanceof Error ? error.message : String(error)}`) - this.currentDateTime = new Date() // Use current date as fallback - } - } - - private startDateTimeLoop(): void { - this.intervalId = setInterval(() => { - this.advanceGameTime() - this.emitDateTime() - this.saveDateTime() - }, DatetimeManager.UPDATE_INTERVAL) - } - - private advanceGameTime(): void { - const advanceMilliseconds = DatetimeManager.GAME_SPEED * DatetimeManager.UPDATE_INTERVAL - this.currentDateTime = new Date(this.currentDateTime.getTime() + advanceMilliseconds) - } - - private emitDateTime(): void { - this.io?.emit('datetime', this.formatDateTime(this.currentDateTime)) - } - - private formatDateTime(date: Date): string { - return date.toISOString().slice(0, 19).replace('T', ' ') - } - - private async saveDateTime(): Promise { - try { - await setJsonValue(this.getWorldFilePath(), 'datetime', this.formatDateTime(this.currentDateTime)) - } catch (error) { - appLogger.error(`Failed to save datetime: ${error instanceof Error ? error.message : String(error)}`) - } - } - - private getWorldFilePath(): string { - return getRootPath('data', 'world.json') - } -} - -export default new DatetimeManager() \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 5a3a271..3e2d3a8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -15,7 +15,7 @@ import UserManager from './managers/userManager' import CommandManager from './managers/commandManager' import CharacterManager from './managers/characterManager' import QueueManager from './managers/queueManager' -import DatetimeManager from './managers/datetimeManager' +import DateManager from './managers/dateManager' export class Server { private readonly app: Application @@ -67,8 +67,8 @@ export class Server { // Load user manager await UserManager.boot() - // Load datetime manager - await DatetimeManager.boot(this.io) + // Load date manager + await DateManager.boot(this.io) // Load zoneEditor manager await ZoneManager.boot() diff --git a/src/utilities/types.ts b/src/utilities/types.ts index 10d46c1..4aebba0 100644 --- a/src/utilities/types.ts +++ b/src/utilities/types.ts @@ -30,6 +30,13 @@ export type TAsset = { frameHeight?: number } +export type WorldSettings = { + date: Date + isRainEnabled: boolean + isFogEnabled: boolean + fogDensity: number +} + // export type TCharacter = Socket & { // user?: User // character?: Character