gameobject module dev

This commit is contained in:
2024-11-04 20:26:27 +02:00
parent 53350ef548
commit 2978f11869
20 changed files with 1391 additions and 114 deletions
+23 -1
View File
@@ -3,10 +3,13 @@ import { ObjectId, MongoClient } from 'mongodb';
let db;
let dbo;
/**
* Manages database operations
*/
class Db {
name = 'db';
init(app){
this.app = app;
}
async start(app){
@@ -23,6 +26,12 @@ class Db {
}
}
/**
* Inserts a record in a db collection
* @param {string} collection The name of the collection
* @param {Object} value The object to insert
* @returns Inserted Id
*/
async create(collection, value){
try {
delete value._id;
@@ -31,6 +40,13 @@ class Db {
}
}
/**
* Loads a record from db collection
* @param {string} collection The name of the collection
* @param {Object} key Record identifier
* @param {Object} projection What data to take from the object
* @returns A record
*/
async get(collection, key, projection){
try {
let res = await dbo.collection(collection).findOne(key, projection ? {projection} : undefined)
@@ -39,6 +55,12 @@ class Db {
}
}
/**
* Performs a database query
* @param {string} collection Collection name
* @param {Object} query A mongo db query
* @returns Array of records
*/
async list(collection, query){
try {
let cursor = dbo.collection(collection).find(query.query, query.project ? {projection: query.project} : undefined);
+170
View File
@@ -0,0 +1,170 @@
import decompress from "decompress";
import sharp from 'sharp';
import fs from 'fs';
import path from 'path';
const collection = 'assets';
/**
* Application layer manager
*/
class GameObjectsManager{
name = 'gameObject';
/**
* Plugin initializer
* @param {App} app The Application
*/
init(app){
const {db, config} = app;
/**
* Gets last asset Id from database
* @returns {Number} Last Asset Id
*/
this.getLastId = async function(){
let ag = await db.aggregate(collection, [
{
$group:{
_id: null,
max: {
$max: "$id",
},
},
},
]);
return ag.max || 0;
}
/**
* Creates a game object
* @param {Context} ctx Request context
* @param {GameObject} data Asset data
*/
this.create = async function(ctx, data){
data.id = (await this.getLastId()) + 1;
await db.create(collection, data);
if (ctx.files?.file){
await this.addFile(data, ctx.files.file)
await fs.promises.unlink(ctx.files.file.path)
await db.update(collection, {id: data.id}, data);
}
return data;
}
/**
* Retrieves game object from database
* @param {Number} id Game object ID
* @returns {GameObject} The game object
*/
this.read = async function(id){
id = parseInt(id);
return await db.get(collection, {id});
}
/**
* Updates game object into the database
* @param {Context} ctx Request context
* @param {GameObject} data Game object
*/
this.update = async function(ctx, data){
data.id = parseInt(data.id);
let object = await this.read(data.id);
data = Object.assign(object, data);
if (ctx.files?.file){
await this.addFile(data, ctx.files.file)
await fs.promises.unlink(ctx.files.file.path)
}if (ctx.files?.thumb){
await this.addThumb(data, ctx.files.thumb.path);
await fs.promises.unlink(ctx.files.thumb.path)
}
await db.update(collection, {id: data.id}, data);
return data;
}
/**
* Removes game object from database
* @param {Number} id Game object ID
*/
this.remove = async function(id){
id = parseInt(id);
await db.remove(collection, {id});
}
/**
* Assigns a file to a game object
* @param {GameObject} object Game object
* @param {File} tmpFile A file
*/
this.addFile = async function(object, tmpFile){
let i = tmpFile;
let ofn = i.name;
let ext = path.extname(ofn).toLowerCase();
let src = `${config.fs.repo}/source/${object.id}${ext}`;
let def = `${config.fs.repo}/default/${object.id}`;
await fs.promises.copyFile(i.path, src);
object.asset = {
ofn,
name: `${object.id}${ext}`
}
if (ext == '.zip'){
let result = await decompress(src, def);
object.asset.list = result.map(f=>f.path);
object.asset.type = 'bundle';
}else{
object.asset.type = 'single';
await fs.promises.copyFile(src, def + ext);
await this.addThumb(object, src);
}
}
/**
* Assigns a thumbnail to a game object
* @param {GameObject} object Game object
* @param {File} thumbSrc A thumbnail
*/
this.addThumb = async function(object, thumbSrc){
let dest = `${config.fs.repo}/thumb/${object.id}.webp`;
await sharp(thumbSrc).resize({height: 250}).toFile(dest);
object.asset.thumb = `${object.id}.webp`;
}
/**
* Returns list of GameObjects
* @param {Object} query Query to DB
* @returns {GameObject[]} Array of game objects
*/
this.list = async function(query){
return await db.list(collection, {
query: {},
project: { name:1, id:1, type:1, asset:1}
});
}
}
/**
* Class starter
* @param {App} app The application
*/
async start(app){
}
}
/**
* GameObject entity
*/
class GameObject {
/**
* Game object name
* @type string
*/
name = null;
type = null;
file = null;
}
export { GameObjectsManager }