1
0
forked from noxious/server

Renamed files to storage, re-worked datetimeManager, added json help utilities

This commit is contained in:
2024-10-14 19:47:52 +02:00
parent 049b9de2b3
commit bb9f62a9c8
17 changed files with 86 additions and 47 deletions

View File

@ -3,7 +3,7 @@ import * as fs from 'fs'
import * as path from 'path'
import { Server } from 'socket.io'
import { commandLogger } from '../utilities/logger'
import { getAppPath } from '../utilities/files'
import { getAppPath } from '../utilities/storage'
class CommandManager {
private commands: Map<string, any> = new Map()

View File

@ -1,19 +1,21 @@
// src/managers/datetimeManager.ts
import fs from 'fs/promises'
import { Server } from 'socket.io'
import { appLogger } from '../utilities/logger'
import { createDir, doesPathExist, getRootPath } from '../utilities/files'
import { getRootPath } from '../utilities/storage'
import { readJsonValue, setJsonValue } from '../utilities/json'
class DatetimeManager {
private static readonly GAME_SPEED = 24 / 3 // 24 hours / 3 hours = 8x speed
private static readonly UPDATE_INTERVAL = 1000 // Update every second for smooth second transitions
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')
}
@ -25,59 +27,47 @@ class DatetimeManager {
}
}
public async loadDateTime(): Promise<Date> {
private async loadDateTime(): Promise<void> {
try {
const datetimeFilePath = this.getDatetimeFilePath()
const content = await fs.readFile(datetimeFilePath, 'utf-8')
return new Date(content.trim())
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)}`)
return new Date() // Use current date as fallback
this.currentDateTime = new Date() // Use current date as fallback
}
}
private startDateTimeLoop(): void {
this.intervalId = setInterval(async () => {
const currentDateTime = await this.loadDateTime()
this.advanceGameTime(currentDateTime)
this.emitDateTime(currentDateTime)
this.saveDateTimeIfNeeded(currentDateTime)
this.intervalId = setInterval(() => {
this.advanceGameTime()
this.emitDateTime()
this.saveDateTime()
}, DatetimeManager.UPDATE_INTERVAL)
}
private advanceGameTime(currentDateTime: Date): void {
const advanceTime = (DatetimeManager.GAME_SPEED * DatetimeManager.UPDATE_INTERVAL) / 1000 * 1000
currentDateTime.setTime(currentDateTime.getTime() + advanceTime)
private advanceGameTime(): void {
const advanceMilliseconds = DatetimeManager.GAME_SPEED * DatetimeManager.UPDATE_INTERVAL
this.currentDateTime = new Date(this.currentDateTime.getTime() + advanceMilliseconds)
}
private emitDateTime(currentDateTime: Date): void {
this.io?.emit('datetime', this.formatDateTime(currentDateTime))
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 saveDateTimeIfNeeded(currentDateTime: Date): void {
if (currentDateTime.getMilliseconds() < DatetimeManager.UPDATE_INTERVAL) {
this.saveDateTime(currentDateTime)
}
}
private async saveDateTime(currentDateTime: Date): Promise<void> {
private async saveDateTime(): Promise<void> {
try {
const datetimeFilePath = this.getDatetimeFilePath()
await fs.writeFile(datetimeFilePath, this.formatDateTime(currentDateTime))
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 getDatetimeFilePath(): string {
if (!doesPathExist(getRootPath('data'))) {
createDir(getRootPath('data'))
}
return getRootPath('data', 'datetime.txt')
private getWorldFilePath(): string {
return getRootPath('data', 'world.json')
}
}

View File

@ -5,7 +5,7 @@ import { Server as SocketServer } from 'socket.io'
import { TSocket } from '../utilities/types'
import { queueLogger } from '../utilities/logger'
import fs from 'fs'
import { getAppPath } from '../utilities/files'
import { getAppPath } from '../utilities/storage'
class QueueManager {
private connection!: IORedis