forked from noxious/server
Worked on sprite logic
This commit is contained in:
parent
bc626c57ad
commit
71f5330b9b
@ -24,6 +24,7 @@ CREATE TABLE `SpriteAction` (
|
|||||||
`id` VARCHAR(191) NOT NULL,
|
`id` VARCHAR(191) NOT NULL,
|
||||||
`spriteId` VARCHAR(191) NOT NULL,
|
`spriteId` VARCHAR(191) NOT NULL,
|
||||||
`action` VARCHAR(191) NOT NULL,
|
`action` VARCHAR(191) NOT NULL,
|
||||||
|
`sprites` JSON NULL,
|
||||||
`origin_x` DECIMAL(65, 30) NOT NULL DEFAULT 0,
|
`origin_x` DECIMAL(65, 30) NOT NULL DEFAULT 0,
|
||||||
`origin_y` DECIMAL(65, 30) NOT NULL DEFAULT 0,
|
`origin_y` DECIMAL(65, 30) NOT NULL DEFAULT 0,
|
||||||
`isAnimated` BOOLEAN NOT NULL DEFAULT false,
|
`isAnimated` BOOLEAN NOT NULL DEFAULT false,
|
@ -8,23 +8,16 @@ model Sprite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model SpriteAction {
|
model SpriteAction {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
spriteId String
|
spriteId String
|
||||||
sprite Sprite @relation(fields: [spriteId], references: [id], onDelete: Cascade)
|
sprite Sprite @relation(fields: [spriteId], references: [id], onDelete: Cascade)
|
||||||
action String
|
action String
|
||||||
origin_x Decimal @default(0)
|
sprites Json?
|
||||||
origin_y Decimal @default(0)
|
origin_x Decimal @default(0)
|
||||||
isAnimated Boolean @default(false)
|
origin_y Decimal @default(0)
|
||||||
isLooping Boolean @default(false)
|
isAnimated Boolean @default(false)
|
||||||
frameWidth Int @default(0)
|
isLooping Boolean @default(false)
|
||||||
frameHeight Int @default(0)
|
frameWidth Int @default(0)
|
||||||
frameSpeed Int @default(0)
|
frameHeight Int @default(0)
|
||||||
images SpriteActionImage[]
|
frameSpeed Int @default(0)
|
||||||
}
|
|
||||||
|
|
||||||
model SpriteActionImage {
|
|
||||||
id String @id @default(uuid())
|
|
||||||
spriteActionId String
|
|
||||||
spriteAction SpriteAction @relation(fields: [spriteActionId], references: [id], onDelete: Cascade)
|
|
||||||
order Int
|
|
||||||
}
|
}
|
||||||
|
@ -2,22 +2,17 @@ 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 type { SpriteAction } from '@prisma/client'
|
import type { SpriteAction } from '@prisma/client'
|
||||||
|
import path from 'path'
|
||||||
type uploadSpriteAction = SpriteAction & {
|
import { writeFile } from 'node:fs/promises'
|
||||||
base64?: string
|
import fs from 'fs/promises'
|
||||||
}
|
import spriteRepository from '../../../repositories/SpriteRepository'
|
||||||
|
|
||||||
type Payload = {
|
type Payload = {
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
spriteActions: uploadSpriteAction[]
|
spriteActions: SpriteAction[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle game master sprite update event
|
|
||||||
* @param socket
|
|
||||||
* @param io
|
|
||||||
*/
|
|
||||||
export default function (socket: TSocket, io: Server) {
|
export default function (socket: TSocket, io: Server) {
|
||||||
socket.on('gm:sprite:update', async (data: Payload, callback: (success: boolean) => void) => {
|
socket.on('gm:sprite:update', async (data: Payload, callback: (success: boolean) => void) => {
|
||||||
if (socket.character?.role !== 'gm') {
|
if (socket.character?.role !== 'gm') {
|
||||||
@ -25,7 +20,7 @@ export default function (socket: TSocket, io: Server) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const sprite = await prisma.sprite.update({
|
await prisma.sprite.update({
|
||||||
where: {
|
where: {
|
||||||
id: data.id
|
id: data.id
|
||||||
},
|
},
|
||||||
@ -35,12 +30,24 @@ export default function (socket: TSocket, io: Server) {
|
|||||||
deleteMany: {
|
deleteMany: {
|
||||||
spriteId: data.id
|
spriteId: data.id
|
||||||
},
|
},
|
||||||
create: data.spriteActions
|
create: data.spriteActions.map((spriteAction) => ({
|
||||||
|
action: spriteAction.action,
|
||||||
|
origin_x: spriteAction.origin_x,
|
||||||
|
origin_y: spriteAction.origin_y,
|
||||||
|
isAnimated: spriteAction.isAnimated,
|
||||||
|
isLooping: spriteAction.isLooping,
|
||||||
|
sprites: spriteAction.sprites as string[],
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// store the sprite image
|
const public_folder = path.join(process.cwd(), 'public', 'sprites', data.id)
|
||||||
|
|
||||||
|
// Ensure the folder exists
|
||||||
|
await fs.mkdir(public_folder, { recursive: true })
|
||||||
|
|
||||||
|
const sprite = await spriteRepository.getById(data.id)
|
||||||
|
|
||||||
callback(true)
|
callback(true)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -48,4 +55,4 @@ export default function (socket: TSocket, io: Server) {
|
|||||||
callback(false)
|
callback(false)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
@ -12,7 +12,19 @@ class SpriteRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getAll(): Promise<Sprite[]> {
|
async getAll(): Promise<Sprite[]> {
|
||||||
return prisma.sprite.findMany()
|
return prisma.sprite.findMany({
|
||||||
|
include: {
|
||||||
|
spriteActions: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSpriteActions(spriteId: string) {
|
||||||
|
return prisma.spriteAction.findMany({
|
||||||
|
where: {
|
||||||
|
spriteId
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user