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:
37
src/services/worldService.ts
Normal file
37
src/services/worldService.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import prisma from '../utilities/prisma'
|
||||
import { gameLogger } from '../utilities/logger'
|
||||
import { World } from '@prisma/client'
|
||||
import WorldRepository from '../repositories/worldRepository'
|
||||
|
||||
class WorldService {
|
||||
async update(worldData: Partial<World>): Promise<boolean> {
|
||||
try {
|
||||
const currentWorld = await WorldRepository.getFirst()
|
||||
if (!currentWorld) {
|
||||
// If no world exists, create first record
|
||||
await prisma.world.create({
|
||||
data: {
|
||||
...worldData,
|
||||
date: worldData.date || new Date()
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
// Update existing world using its date as unique identifier
|
||||
await prisma.world.update({
|
||||
where: {
|
||||
date: currentWorld.date
|
||||
},
|
||||
data: worldData
|
||||
})
|
||||
|
||||
return true
|
||||
} catch (error: any) {
|
||||
gameLogger.error(`Failed to update world: ${error instanceof Error ? error.message : String(error)}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new WorldService()
|
Reference in New Issue
Block a user