forked from noxious/server
Renamed utilities to files, added datetimeManager, npm update
This commit is contained in:
@ -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/utilities'
|
||||
import { getAppPath } from '../utilities/files'
|
||||
|
||||
class CommandManager {
|
||||
private commands: Map<string, any> = new Map()
|
||||
|
84
src/managers/datetimeManager.ts
Normal file
84
src/managers/datetimeManager.ts
Normal file
@ -0,0 +1,84 @@
|
||||
// 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'
|
||||
|
||||
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 io: Server | null = null
|
||||
private intervalId: NodeJS.Timeout | null = null
|
||||
|
||||
public async boot(io: Server): Promise<void> {
|
||||
this.io = io
|
||||
this.startDateTimeLoop()
|
||||
appLogger.info('Datetime manager loaded')
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
if (this.intervalId) {
|
||||
clearInterval(this.intervalId)
|
||||
this.intervalId = null
|
||||
}
|
||||
}
|
||||
|
||||
public async loadDateTime(): Promise<Date> {
|
||||
try {
|
||||
const datetimeFilePath = this.getDatetimeFilePath()
|
||||
const content = await fs.readFile(datetimeFilePath, 'utf-8')
|
||||
return new Date(content.trim())
|
||||
} catch (error) {
|
||||
appLogger.error(`Failed to load datetime: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return 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)
|
||||
}, DatetimeManager.UPDATE_INTERVAL)
|
||||
}
|
||||
|
||||
private advanceGameTime(currentDateTime: Date): void {
|
||||
const advanceTime = (DatetimeManager.GAME_SPEED * DatetimeManager.UPDATE_INTERVAL) / 1000 * 1000
|
||||
currentDateTime.setTime(currentDateTime.getTime() + advanceTime)
|
||||
}
|
||||
|
||||
private emitDateTime(currentDateTime: Date): void {
|
||||
this.io?.emit('datetime', this.formatDateTime(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> {
|
||||
try {
|
||||
const datetimeFilePath = this.getDatetimeFilePath()
|
||||
await fs.writeFile(datetimeFilePath, this.formatDateTime(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')
|
||||
}
|
||||
}
|
||||
|
||||
export default new DatetimeManager()
|
@ -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/utilities'
|
||||
import { getAppPath } from '../utilities/files'
|
||||
|
||||
class QueueManager {
|
||||
private connection!: IORedis
|
||||
|
0
src/managers/weatherManager.ts
Normal file
0
src/managers/weatherManager.ts
Normal file
Reference in New Issue
Block a user