1
0
forked from noxious/server

Moved models to simplify and improve prisma schemas , added logic to update characterTypes

This commit is contained in:
Dennis Postma 2024-11-17 17:53:25 +01:00
parent 27f8bc8784
commit ec3bf0f51e
14 changed files with 80 additions and 103 deletions

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE `CharacterType` ADD COLUMN `isEnabledForCharCreation` BOOLEAN NOT NULL DEFAULT false;

View File

@ -5,3 +5,37 @@ model World {
isFogEnabled Boolean @default(false) isFogEnabled Boolean @default(false)
fogDensity Int @default(0) fogDensity Int @default(0)
} }
model Chat {
id Int @id @default(autoincrement())
characterId Int
character Character @relation(fields: [characterId], references: [id], onDelete: Cascade)
zoneId Int
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
message String
createdAt DateTime @default(now())
}
model Sprite {
id String @id @default(uuid())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
spriteActions SpriteAction[]
characterTypes CharacterType[]
}
model SpriteAction {
id String @id @default(uuid())
spriteId String
sprite Sprite @relation(fields: [spriteId], references: [id], onDelete: Cascade)
action String
sprites Json?
originX Decimal @default(0)
originY Decimal @default(0)
isAnimated Boolean @default(false)
isLooping Boolean @default(false)
frameWidth Int @default(0)
frameHeight Int @default(0)
frameSpeed Int @default(0)
}

View File

@ -19,13 +19,3 @@ datasource db {
provider = "mysql" provider = "mysql"
url = env("DATABASE_URL") url = env("DATABASE_URL")
} }
model Chat {
id Int @id @default(autoincrement())
characterId Int
character Character @relation(fields: [characterId], references: [id], onDelete: Cascade)
zoneId Int
zone Zone @relation(fields: [zoneId], references: [id], onDelete: Cascade)
message String
createdAt DateTime @default(now())
}

View File

@ -1,23 +0,0 @@
model Sprite {
id String @id @default(uuid())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
spriteActions SpriteAction[]
characterTypes CharacterType[]
}
model SpriteAction {
id String @id @default(uuid())
spriteId String
sprite Sprite @relation(fields: [spriteId], references: [id], onDelete: Cascade)
action String
sprites Json?
originX Decimal @default(0)
originY Decimal @default(0)
isAnimated Boolean @default(false)
isLooping Boolean @default(false)
frameWidth Int @default(0)
frameHeight Int @default(0)
frameSpeed Int @default(0)
}

View File

@ -1,11 +1,11 @@
model User { model User {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
username String @unique username String @unique
email String @unique email String @unique
password String password String
online Boolean @default(false) online Boolean @default(false)
characters Character[] characters Character[]
passwordResetTokens PasswordResetToken[] passwordResetTokens PasswordResetToken[]
} }
model PasswordResetToken { model PasswordResetToken {
@ -30,15 +30,16 @@ enum CharacterRace {
} }
model CharacterType { model CharacterType {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
name String name String
gender CharacterGender gender CharacterGender
race CharacterRace race CharacterRace
characters Character[] isEnabledForCharCreation Boolean @default(false)
spriteId String? characters Character[]
sprite Sprite? @relation(fields: [spriteId], references: [id], onDelete: Cascade) spriteId String?
createdAt DateTime @default(now()) sprite Sprite? @relation(fields: [spriteId], references: [id], onDelete: Cascade)
updatedAt DateTime @updatedAt createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
} }
model Character { model Character {
@ -71,4 +72,4 @@ model CharacterItem {
itemId String itemId String
item Item @relation(fields: [itemId], references: [id], onDelete: Cascade) item Item @relation(fields: [itemId], references: [id], onDelete: Cascade)
quantity Int quantity Int
} }

View File

@ -28,26 +28,23 @@ export default class AlertCommandEvent {
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!) const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!)
if (!character) { if (!character) {
gameLogger.error('chat:alert_command error', 'Character not found') gameLogger.error('chat:alert_command error', 'Character not found')
callback(false) return callback(false)
return
} }
// Check if the user is the GM // Check if the user is the GM
if (character.role !== 'gm') { if (character.role !== 'gm') {
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`) gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
callback(false) return callback(false)
return
} }
const args = getArgs('alert', data.message) const args = getArgs('alert', data.message)
if (!args) { if (!args) {
callback(false) return callback(false)
return
} }
this.io.emit('notification', { title: 'Message from GM', message: args.join(' ') }) this.io.emit('notification', { title: 'Message from GM', message: args.join(' ') })
callback(true) return callback(true)
} catch (error: any) { } catch (error: any) {
gameLogger.error('chat:alert_command error', error.message) gameLogger.error('chat:alert_command error', error.message)
callback(false) callback(false)

View File

@ -29,14 +29,12 @@ export default class SetTimeCommand {
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!) const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!)
if (!character) { if (!character) {
gameLogger.error('chat:alert_command error', 'Character not found') gameLogger.error('chat:alert_command error', 'Character not found')
callback(false)
return return
} }
// Check if the user is the GM // Check if the user is the GM
if (character.role !== 'gm') { if (character.role !== 'gm') {
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`) gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
callback(false)
return return
} }
@ -44,20 +42,16 @@ export default class SetTimeCommand {
const args = getArgs('time', data.message) const args = getArgs('time', data.message)
if (!args) { if (!args) {
callback(false)
return return
} }
const time = args[0] // 24h time, e.g. 17:34 const time = args[0] // 24h time, e.g. 17:34
if (!time) { if (!time) {
callback(false)
return return
} }
await DateManager.setTime(time) await DateManager.setTime(time)
callback(true)
} catch (error: any) { } catch (error: any) {
gameLogger.error('command error', error.message) gameLogger.error('command error', error.message)
callback(false) callback(false)

View File

@ -26,7 +26,6 @@ export default class TeleportCommandEvent {
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!) const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
if (!zoneCharacter) { if (!zoneCharacter) {
gameLogger.error('chat:message error', 'Character not found') gameLogger.error('chat:message error', 'Character not found')
callback(false)
return return
} }
@ -35,7 +34,6 @@ export default class TeleportCommandEvent {
// Check if the user is the GM // Check if the user is the GM
if (character.role !== 'gm') { if (character.role !== 'gm') {
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`) gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
callback(false)
return return
} }
@ -86,8 +84,6 @@ export default class TeleportCommandEvent {
this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` }) this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` })
gameMasterLogger.info('teleport', `Character ${character.id} teleported to zone ${zone.id}`) gameMasterLogger.info('teleport', `Character ${character.id} teleported to zone ${zone.id}`)
callback(true)
} catch (error: any) { } catch (error: any) {
gameMasterLogger.error(`Error in teleport command: ${error.message}`) gameMasterLogger.error(`Error in teleport command: ${error.message}`)
this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' }) this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' })

View File

@ -29,20 +29,16 @@ export default class ToggleFogCommand {
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!) const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!)
if (!character) { if (!character) {
gameLogger.error('chat:alert_command error', 'Character not found') gameLogger.error('chat:alert_command error', 'Character not found')
callback(false)
return return
} }
// Check if the user is the GM // Check if the user is the GM
if (character.role !== 'gm') { if (character.role !== 'gm') {
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`) gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
callback(false)
return return
} }
await WeatherManager.toggleFog() await WeatherManager.toggleFog()
callback(true)
} catch (error: any) { } catch (error: any) {
gameLogger.error('command error', error.message) gameLogger.error('command error', error.message)
callback(false) callback(false)

View File

@ -29,20 +29,16 @@ export default class ToggleRainCommand {
const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!) const character = await CharacterRepository.getByUserAndId(this.socket.userId!, this.socket.characterId!)
if (!character) { if (!character) {
gameLogger.error('chat:alert_command error', 'Character not found') gameLogger.error('chat:alert_command error', 'Character not found')
callback(false)
return return
} }
// Check if the user is the GM // Check if the user is the GM
if (character.role !== 'gm') { if (character.role !== 'gm') {
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`) gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
callback(false)
return return
} }
await WeatherManager.toggleRain() await WeatherManager.toggleRain()
callback(true)
} catch (error: any) { } catch (error: any) {
gameLogger.error('command error', error.message) gameLogger.error('command error', error.message)
callback(false) callback(false)

View File

@ -11,10 +11,10 @@ export default class CharacterTypeCreateEvent {
) {} ) {}
public listen(): void { public listen(): void {
this.socket.on('gm:characterType:create', this.handleCharacterTypeCreate.bind(this)) this.socket.on('gm:characterType:create', this.handleEvent.bind(this))
} }
private async handleCharacterTypeCreate(data: undefined, callback: (response: boolean, characterType?: any) => void): Promise<void> { private async handleEvent(data: undefined, callback: (response: boolean, characterType?: any) => void): Promise<void> {
try { try {
const character = await characterRepository.getById(this.socket.characterId as number) const character = await characterRepository.getById(this.socket.characterId as number)
if (!character) return callback(false) if (!character) return callback(false)

View File

@ -10,17 +10,17 @@ interface IPayload {
object: string object: string
} }
export default class ObjectRemoveEvent { export default class CharacterTypeDeleteEvent {
constructor( constructor(
private readonly io: Server, private readonly io: Server,
private readonly socket: TSocket private readonly socket: TSocket
) {} ) {}
public listen(): void { public listen(): void {
this.socket.on('gm:object:remove', this.handleObjectRemove.bind(this)) this.socket.on('gm:characterType:remove', this.handleEvent.bind(this))
} }
private async handleObjectRemove(data: IPayload, callback: (response: boolean) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: boolean) => void): Promise<void> {
const character = await characterRepository.getById(this.socket.characterId as number) const character = await characterRepository.getById(this.socket.characterId as number)
if (!character) return callback(false) if (!character) return callback(false)

View File

@ -14,10 +14,10 @@ export default class CharacterTypeListEvent {
) {} ) {}
public listen(): void { public listen(): void {
this.socket.on('gm:characterType:list', this.handleCharacterTypeList.bind(this)) this.socket.on('gm:characterType:list', this.handleEvent.bind(this))
} }
private async handleCharacterTypeList(data: IPayload, callback: (response: CharacterType[]) => void): Promise<void> { private async handleEvent(data: IPayload, callback: (response: CharacterType[]) => void): Promise<void> {
const character = await characterRepository.getById(this.socket.characterId as number) const character = await characterRepository.getById(this.socket.characterId as number)
if (!character) { if (!character) {
gameMasterLogger.error('gm:characterType:list error', 'Character not found') gameMasterLogger.error('gm:characterType:list error', 'Character not found')

View File

@ -2,27 +2,25 @@ import { Server } from 'socket.io'
import { TSocket } from '../../../../utilities/types' import { TSocket } from '../../../../utilities/types'
import prisma from '../../../../utilities/prisma' import prisma from '../../../../utilities/prisma'
import characterRepository from '../../../../repositories/characterRepository' import characterRepository from '../../../../repositories/characterRepository'
import { CharacterGender, CharacterRace } from '@prisma/client'
type Payload = { type Payload = {
id: string id: number
name: string name: string
tags: string[] gender: CharacterGender
originX: number race: CharacterRace
originY: number isEnabledForCharCreation: boolean
isAnimated: boolean spriteId: string
frameSpeed: number
frameWidth: number
frameHeight: number
} }
export default class ObjectUpdateEvent { export default class CharacterTypeUpdateEvent {
constructor( constructor(
private readonly io: Server, private readonly io: Server,
private readonly socket: TSocket private readonly socket: TSocket
) {} ) {}
public listen(): void { public listen(): void {
this.socket.on('gm:object:update', this.handleObjectUpdate.bind(this)) this.socket.on('gm:characterType:update', this.handleObjectUpdate.bind(this))
} }
private async handleObjectUpdate(data: Payload, callback: (success: boolean) => void): Promise<void> { private async handleObjectUpdate(data: Payload, callback: (success: boolean) => void): Promise<void> {
@ -34,21 +32,17 @@ export default class ObjectUpdateEvent {
} }
try { try {
await prisma.object.update({ await prisma.characterType.update({
where: { where: { id: data.id },
id: data.id
},
data: { data: {
name: data.name, name: data.name,
tags: data.tags, gender: data.gender,
originX: data.originX, race: data.race,
originY: data.originY, isEnabledForCharCreation: data.isEnabledForCharCreation,
isAnimated: data.isAnimated, spriteId: data.spriteId
frameSpeed: data.frameSpeed,
frameWidth: data.frameWidth,
frameHeight: data.frameHeight
} }
}) })
callback(true) callback(true)
} catch (error) { } catch (error) {
console.error(error) console.error(error)