#174: Refactor character manager into zoneManager for better DX, major refactor of time and weather system (data is stored in DB now instead of JSON file), npm update, npm format, many other improvements

This commit is contained in:
2024-11-13 13:21:01 +01:00
parent 628b3bf1fa
commit d4e0cbe398
43 changed files with 465 additions and 461 deletions

View File

@ -1,7 +1,7 @@
import { Server } from 'socket.io'
import { appLogger } from '../utilities/logger'
import { getRootPath } from '../utilities/storage'
import { readJsonValue, setJsonValue } from '../utilities/json'
import prisma from '../utilities/prisma'
import worldService from '../services/worldService'
class DateManager {
private static readonly GAME_SPEED = 8 // 24 game hours / 3 real hours
@ -18,7 +18,6 @@ class DateManager {
appLogger.info('Date manager loaded')
}
// 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
@ -45,8 +44,13 @@ class DateManager {
private async loadDate(): Promise<void> {
try {
const dateString = await readJsonValue<string>(this.getWorldFilePath(), 'date')
this.currentDate = new Date(dateString)
const world = await prisma.world.findFirst({
orderBy: { date: 'desc' }
})
if (world) {
this.currentDate = world.date
}
} catch (error) {
appLogger.error(`Failed to load date: ${error instanceof Error ? error.message : String(error)}`)
this.currentDate = new Date() // Use current date as fallback
@ -57,7 +61,7 @@ class DateManager {
this.intervalId = setInterval(() => {
this.advanceGameTime()
this.emitDate()
this.saveDate()
void this.saveDate()
}, DateManager.UPDATE_INTERVAL)
}
@ -72,14 +76,22 @@ class DateManager {
private async saveDate(): Promise<void> {
try {
await setJsonValue(this.getWorldFilePath(), 'date', this.currentDate)
await worldService.update({
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')
public cleanup(): void {
if (this.intervalId) {
clearInterval(this.intervalId)
}
}
public getCurrentDate(): Date {
return this.currentDate
}
}