forked from noxious/server
Rnamed Datetime > Date
This commit is contained in:
parent
bb9f62a9c8
commit
bfd941c091
68
src/managers/dateManager.ts
Normal file
68
src/managers/dateManager.ts
Normal file
@ -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<void> {
|
||||
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<void> {
|
||||
try {
|
||||
const dateString = await readJsonValue<string>(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<void> {
|
||||
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()
|
@ -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<void> {
|
||||
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<void> {
|
||||
try {
|
||||
const datetimeString = await readJsonValue<string>(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<void> {
|
||||
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()
|
@ -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()
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user