Improved asset loading
This commit is contained in:
parent
060af95dc6
commit
c2fab4e999
@ -10,18 +10,30 @@ import {loginAccountSchema, registerAccountSchema} from "./ZodTypes";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { TAsset } from './Types'
|
import { TAsset } from './Types'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
import tileRepository from '../repositories/TileRepository'
|
||||||
|
import objectRepository from '../repositories/ObjectRepository'
|
||||||
|
|
||||||
async function addHttpRoutes(app: Application) {
|
async function addHttpRoutes(app: Application) {
|
||||||
app.get('/assets', (req: Request, res: Response) => {
|
app.get('/assets', async (req: Request, res: Response) => {
|
||||||
let assets: TAsset[] = [];
|
let assets: TAsset[] = [];
|
||||||
const tiles = listTiles();
|
const tiles = await tileRepository.getAll();
|
||||||
tiles.forEach(tile => {
|
tiles.forEach(tile => {
|
||||||
assets.push({key: tile, value: '/tiles/' + tile, group: 'tiles', type: 'link'});
|
assets.push({
|
||||||
|
key: tile.name,
|
||||||
|
value: '/assets/tiles/' + tile.name + '.png',
|
||||||
|
group: 'tiles',
|
||||||
|
type: 'link'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const objects = listObjects();
|
const objects = await objectRepository.getAll();
|
||||||
objects.forEach(object => {
|
objects.forEach(object => {
|
||||||
assets.push({key: object, value: '/objects/' + object, group: 'objects', type: 'link'});
|
assets.push({
|
||||||
|
key: object.name,
|
||||||
|
value: '/assets/objects/' + object.name + '.png',
|
||||||
|
group: 'objects',
|
||||||
|
type: 'link'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json(assets);
|
res.json(assets);
|
||||||
@ -88,54 +100,4 @@ async function addHttpRoutes(app: Application) {
|
|||||||
console.log('[✅] Web routes added');
|
console.log('[✅] Web routes added');
|
||||||
}
|
}
|
||||||
|
|
||||||
export { addHttpRoutes };
|
export { addHttpRoutes };
|
||||||
|
|
||||||
function listTiles(): string[] {
|
|
||||||
// get root path
|
|
||||||
const folder = path.join(process.cwd(), 'public', 'tiles');
|
|
||||||
|
|
||||||
// if folder does not exist, create it
|
|
||||||
if (!fs.existsSync(folder)) {
|
|
||||||
fs.mkdirSync(folder);
|
|
||||||
}
|
|
||||||
|
|
||||||
// list the files in the folder
|
|
||||||
let tiles: string[] = [];
|
|
||||||
|
|
||||||
try {
|
|
||||||
const files = fs.readdirSync(folder);
|
|
||||||
|
|
||||||
files.forEach(file => {
|
|
||||||
tiles.push(file.replace('.png', ''));
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
function listObjects(): string[] {
|
|
||||||
// get root path
|
|
||||||
const folder = path.join(process.cwd(), 'public', 'objects');
|
|
||||||
|
|
||||||
// if folder does not exist, create it
|
|
||||||
if (!fs.existsSync(folder)) {
|
|
||||||
fs.mkdirSync(folder);
|
|
||||||
}
|
|
||||||
|
|
||||||
// list the files in the folder
|
|
||||||
let objects: string[] = [];
|
|
||||||
|
|
||||||
try {
|
|
||||||
const files = fs.readdirSync(folder);
|
|
||||||
|
|
||||||
files.forEach(file => {
|
|
||||||
objects.push(file.replace('.png', ''));
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
return objects;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user