1
0
forked from noxious/server

POC working new caching method - moved controllers folder, renamed assets to textures, fixed HTTP bug, formatted code

This commit is contained in:
2025-01-07 03:58:32 +01:00
parent f47023dc81
commit 010454914b
25 changed files with 131 additions and 137 deletions

View File

@ -1,10 +1,6 @@
import fs from 'fs'
import { Request, Response } from 'express'
import { BaseController } from '#application/base/baseController'
import Database from '#application/database'
import Storage from '#application/storage'
import { AssetData, UUID } from '#application/types'
import MapRepository from '#repositories/mapRepository'
import SpriteRepository from '#repositories/spriteRepository'
@ -25,7 +21,7 @@ export class AssetsController extends BaseController {
const tiles = await this.tileRepository.getAll()
for (const tile of tiles) {
assets.push({ key: tile.getId(), data: '/assets/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
assets.push({ key: tile.getId(), data: '/textures/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
}
return this.sendSuccess(res, assets)
@ -52,7 +48,7 @@ export class AssetsController extends BaseController {
const tiles = await this.tileRepository.getByMapId(mapId)
for (const tile of tiles) {
assets.push({ key: tile.getId(), data: '/assets/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
assets.push({ key: tile.getId(), data: '/textures/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
}
return this.sendSuccess(res, assets)
@ -79,7 +75,7 @@ export class AssetsController extends BaseController {
const assets: AssetData[] = sprite.getSpriteActions().map((spriteAction) => ({
key: sprite.getId() + '-' + spriteAction.getAction(),
data: '/assets/sprites/' + sprite.getId() + '/' + spriteAction.getAction() + '.png',
data: '/textures/sprites/' + sprite.getId() + '/' + spriteAction.getAction() + '.png',
group: spriteAction.getIsAnimated() ? 'sprite_animations' : 'sprites',
updatedAt: sprite.getUpdatedAt(),
originX: Number(spriteAction.getOriginX().toString()),
@ -93,31 +89,4 @@ export class AssetsController extends BaseController {
return this.sendSuccess(res, assets)
}
/**
* Download asset
* @param req
* @param res
*/
public async downloadAsset(req: Request, res: Response) {
const { type, spriteId, file } = req.params
const assetPath = type === 'sprites' && spriteId ? Storage.getPublicPath(type, spriteId, file) : Storage.getPublicPath(type, file)
if (!fs.existsSync(assetPath)) {
this.logger.error(`File not found: ${assetPath}`)
return this.sendError(res, 'Asset not found', 404)
}
res.sendFile(assetPath, (err) => {
if (err) {
this.logger.error('Error sending file:' + err)
this.sendError(res, 'Error downloading the asset', 500)
}
})
}
public async downloadCache(req: Request, res: Response) {
}
}

View File

@ -1,92 +1,45 @@
import { Request, Response } from 'express'
import { BaseController } from '#application/base/baseController'
import { AssetData, UUID } from '#application/types'
import MapObjectRepository from '#repositories/mapObjectRepository'
import MapRepository from '#repositories/mapRepository'
import SpriteRepository from '#repositories/spriteRepository'
import TileRepository from '#repositories/tileRepository'
export class DataController extends BaseController {
private readonly mapRepository = new MapRepository()
private readonly spriteRepository = new SpriteRepository()
private readonly tileRepository = new TileRepository()
export class CacheController extends BaseController {
/**
* List tiles
* Serve a list of maps and send as JSON
* @param req
* @param res
*/
public async tiles(req: Request, res: Response) {
const assets: AssetData[] = []
const tiles = await this.tileRepository.getAll()
public async maps(req: Request, res: Response) {
const items: any[] = []
for (const tile of tiles) {
assets.push({ key: tile.getId(), data: '/assets/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
const mapRepository = new MapRepository()
const maps = await mapRepository.getAll()
for (const map of maps) {
items.push(await map.cache())
}
return this.sendSuccess(res, assets)
return this.sendSuccess(res, items)
}
/**
* List tiles by map
* Serve a list of map objects and send as JSON
* @param req
* @param res
*/
public async listTilesByMap(req: Request, res: Response) {
const mapId = req.params.mapId as UUID
public async mapObjects(req: Request, res: Response) {
const items: any[] = []
if (!mapId) {
return this.sendError(res, 'Invalid map ID', 400)
const mapObjectRepository = new MapObjectRepository()
const mapObjects = await mapObjectRepository.getAll()
for (const mapObject of mapObjects) {
items.push(await mapObject.cache())
}
const map = await this.mapRepository.getById(mapId)
if (!map) {
return this.sendError(res, 'Map not found', 404)
}
const assets: AssetData[] = []
const tiles = await this.tileRepository.getByMapId(mapId)
for (const tile of tiles) {
assets.push({ key: tile.getId(), data: '/assets/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
}
return this.sendSuccess(res, assets)
}
/**
* List sprite actions
* @param req
* @param res
*/
public async listSpriteActions(req: Request, res: Response) {
const spriteId = req.params.spriteId as UUID
if (!spriteId) {
return this.sendError(res, 'Invalid sprite ID', 400)
}
const sprite = await this.spriteRepository.getById(spriteId)
if (!sprite) {
return this.sendError(res, 'Sprite not found', 404)
}
await this.spriteRepository.getEntityManager().populate(sprite, ['spriteActions'])
const assets: AssetData[] = sprite.getSpriteActions().map((spriteAction) => ({
key: sprite.getId() + '-' + spriteAction.getAction(),
data: '/assets/sprites/' + sprite.getId() + '/' + spriteAction.getAction() + '.png',
group: spriteAction.getIsAnimated() ? 'sprite_animations' : 'sprites',
updatedAt: sprite.getUpdatedAt(),
originX: Number(spriteAction.getOriginX().toString()),
originY: Number(spriteAction.getOriginY().toString()),
isAnimated: spriteAction.getIsAnimated(),
frameRate: spriteAction.getFrameRate(),
frameWidth: spriteAction.getFrameWidth(),
frameHeight: spriteAction.getFrameHeight(),
frameCount: spriteAction.getSprites()?.length
}))
return this.sendSuccess(res, assets)
return this.sendSuccess(res, items)
}
}

View File

@ -0,0 +1,19 @@
import { Request, Response } from 'express'
import { BaseController } from '#application/base/baseController'
import Storage from '#application/storage'
export class TexturesController extends BaseController {
/**
* Download texture
* @param req
* @param res
*/
public async download(req: Request, res: Response) {
const { type, spriteId, file } = req.params
const texture = type === 'sprites' && spriteId ? Storage.getPublicPath(type, spriteId, file) : Storage.getPublicPath(type, file)
this.sendFile(res, texture)
}
}