forked from noxious/server
#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:
@ -1,49 +0,0 @@
|
||||
import * as fs from 'fs/promises'
|
||||
import { appLogger } from './logger'
|
||||
|
||||
export async function readJsonFile<T>(filePath: string): Promise<T> {
|
||||
try {
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8')
|
||||
return JSON.parse(fileContent) as T
|
||||
} catch (error) {
|
||||
appLogger.error(`Error reading JSON file: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeJsonFile<T>(filePath: string, data: T): Promise<void> {
|
||||
try {
|
||||
const jsonString = JSON.stringify(data, null, 2)
|
||||
await fs.writeFile(filePath, jsonString, 'utf-8')
|
||||
} catch (error) {
|
||||
appLogger.error(`Error writing JSON file: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function readJsonValue<T>(filePath: string, paramPath: string): Promise<T> {
|
||||
try {
|
||||
const jsonContent = await readJsonFile<any>(filePath)
|
||||
const paramValue = paramPath.split('.').reduce((obj, key) => obj && obj[key], jsonContent)
|
||||
|
||||
if (paramValue === undefined) {
|
||||
throw new Error(`Parameter ${paramPath} not found in the JSON file`)
|
||||
}
|
||||
|
||||
return paramValue as T
|
||||
} catch (error) {
|
||||
appLogger.error(`Error reading JSON parameter: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function setJsonValue<T>(filePath: string, key: string, value: any): Promise<void> {
|
||||
try {
|
||||
const data = await readJsonFile<T>(filePath)
|
||||
const updatedData = { ...data, [key]: value }
|
||||
await writeJsonFile(filePath, updatedData)
|
||||
} catch (error) {
|
||||
appLogger.error(`Error setting JSON value: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
}
|
@ -32,13 +32,31 @@ const watchLogs = () => {
|
||||
LOG_TYPES.forEach((type) => {
|
||||
const logFile = getRootPath('logs', `${type}.log`)
|
||||
|
||||
fs.watchFile(logFile, (curr, prev) => {
|
||||
if (curr.size > prev.size) {
|
||||
const stream = fs.createReadStream(logFile, { start: prev.size, end: curr.size })
|
||||
stream.on('data', (chunk) => {
|
||||
console.log(`[${type}]\n${chunk.toString()}`)
|
||||
})
|
||||
// Get initial file size
|
||||
const stats = fs.statSync(logFile)
|
||||
let lastPosition = stats.size
|
||||
|
||||
fs.watch(logFile, (eventType) => {
|
||||
if (eventType !== 'change') {
|
||||
return
|
||||
}
|
||||
|
||||
fs.stat(logFile, (err, stats) => {
|
||||
if (err) return
|
||||
|
||||
if (stats.size > lastPosition) {
|
||||
const stream = fs.createReadStream(logFile, {
|
||||
start: lastPosition,
|
||||
end: stats.size
|
||||
})
|
||||
|
||||
stream.on('data', (chunk) => {
|
||||
console.log(`[${type}]\n${chunk.toString()}`)
|
||||
})
|
||||
|
||||
lastPosition = stats.size
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { Socket } from 'socket.io'
|
||||
import { Character, User } from '@prisma/client'
|
||||
import { Character, User, ZoneEventTile, ZoneEventTileTeleport } from '@prisma/client'
|
||||
|
||||
export type TSocket = Socket & {
|
||||
user?: User
|
||||
userId?: number
|
||||
characterId?: number
|
||||
handshake?: {
|
||||
query?: {
|
||||
@ -18,7 +18,11 @@ export type TSocket = Socket & {
|
||||
|
||||
export type ExtendedCharacter = Character & {
|
||||
isMoving?: boolean
|
||||
resetMovement: boolean
|
||||
resetMovement?: boolean
|
||||
}
|
||||
|
||||
export type ZoneEventTileWithTeleport = ZoneEventTile & {
|
||||
teleport: ZoneEventTileTeleport
|
||||
}
|
||||
|
||||
export type AssetData = {
|
||||
|
Reference in New Issue
Block a user