gamedesigner
This commit is contained in:
+1
-1
@@ -26,7 +26,7 @@ class Db {
|
||||
try {
|
||||
dbo = db.db(app.config.db.name);
|
||||
this.instance = dbo;
|
||||
for (let c of ['users', 'user_sessions', 'history', 'log', 'assets', 'scenarios']){
|
||||
for (let c of ['users', 'user_sessions', 'history', 'log', 'assets', 'scenarios', 'games']){
|
||||
try {
|
||||
await dbo.createCollection(c);
|
||||
}catch(err){}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
|
||||
const collection = 'games';
|
||||
/**
|
||||
* Games manager class
|
||||
*/
|
||||
class GamesManager{
|
||||
name = 'games';
|
||||
name = 'game';
|
||||
|
||||
/**
|
||||
* Class initializer, инициализация на плъгин
|
||||
@@ -18,7 +19,9 @@ class GamesManager{
|
||||
* @param {Game} data the game description, дефиниция на играта
|
||||
*/
|
||||
this.create = async function(ctx, data){
|
||||
|
||||
data.id = (await db.getLastId(collection)) + 1;
|
||||
await db.create(collection, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +30,8 @@ class GamesManager{
|
||||
* @returns {Game} the game, игрова дефиниция
|
||||
*/
|
||||
this.read = async function(id){
|
||||
|
||||
id = parseInt(id);
|
||||
return await db.get(collection, {id});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +40,11 @@ class GamesManager{
|
||||
* @param {Game} data the game description, игрова дефиниция
|
||||
*/
|
||||
this.update = async function(ctx, data){
|
||||
|
||||
data.id = parseInt(data.id);
|
||||
let object = await this.read(data.id);
|
||||
data = Object.assign(object, data);
|
||||
await db.update(collection, {id: data.id}, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +52,8 @@ class GamesManager{
|
||||
* @param {Number} id game definition ID, идентификатор на игровата дефиниция
|
||||
*/
|
||||
this.remove = async function(id){
|
||||
|
||||
id = parseInt(id);
|
||||
await db.remove(collection, {id});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +62,10 @@ class GamesManager{
|
||||
* @returns {Game[]} Array of games, масив от игрови дефиниции
|
||||
*/
|
||||
this.list = async function(query){
|
||||
|
||||
return await db.list(collection, {
|
||||
query,
|
||||
project: { name:1, id:1, sceneThumb: '$scenes.data.environment'}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* Rules manager class, контролен клас за управление на игрови правила
|
||||
*/
|
||||
class RulesManager{
|
||||
name = 'rules';
|
||||
name = 'rule';
|
||||
|
||||
/**
|
||||
* Class initializer, инициализация на плъгин
|
||||
|
||||
@@ -14,7 +14,7 @@ class GamesController{
|
||||
*/
|
||||
init(app){
|
||||
const router = express.Router();
|
||||
const { games } = app;
|
||||
const { game } = app;
|
||||
|
||||
/**
|
||||
* API: PUT /api/game/ Create or update game, създаване/обновяване на игрова дефиниция
|
||||
@@ -22,7 +22,14 @@ class GamesController{
|
||||
* @memberof GamesController
|
||||
*/
|
||||
router.put('/', async (req, res)=>{
|
||||
|
||||
try{
|
||||
let data = req.body;
|
||||
let object = await game[data.id? 'update' : 'create'](req, data)
|
||||
res.json({status: 'OK', object});
|
||||
}catch(err){
|
||||
console.error(err);
|
||||
res.status(500).json({status: 'ERR', err});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -32,6 +39,8 @@ class GamesController{
|
||||
* @memberof GamesController
|
||||
*/
|
||||
router.post('/', async (req, res)=>{
|
||||
let result = await game.list(req.body);
|
||||
res.json(result);
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -42,6 +51,8 @@ class GamesController{
|
||||
* @memberof GamesController
|
||||
*/
|
||||
router.get('/:id', async (req, res)=>{
|
||||
let object = await game.read(parseInt(req.params.id));
|
||||
res.json(object);
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -51,6 +62,8 @@ class GamesController{
|
||||
* @memberof GamesController
|
||||
*/
|
||||
router.delete('/:id', async (req, res)=>{
|
||||
await scenario.remove(req.params.id);
|
||||
res.json({status: 'OK'});
|
||||
})
|
||||
|
||||
app.webServer.xapp.use(this.route, router);
|
||||
|
||||
+12
-7
@@ -9,14 +9,19 @@ console.debug = function(){
|
||||
import App from './app/App.js';
|
||||
|
||||
const modules = [
|
||||
{name:'Config', path:'app/Config.js'},
|
||||
{name:'Db', path:'app/Db.js'},
|
||||
{name:'GameObjectsManager', path:'app/bl/GameObjectsManager.js'},
|
||||
{name:'ScenariosManager', path:'app/bl/ScenariosManager.js'},
|
||||
{name:'WebServer', path:'app/WebServer.js'},
|
||||
{name: 'Config', path:'app/Config.js'},
|
||||
{name: 'Db', path:'app/Db.js'},
|
||||
|
||||
{name: 'GameObjectsManager', path:'app/bl/GameObjectsManager.js'},
|
||||
{name: 'ScenariosManager', path:'app/bl/ScenariosManager.js'},
|
||||
{name: 'GamesManager', path:'app/bl/GamesManager.js'},
|
||||
|
||||
{name: 'WebServer', path:'app/WebServer.js'},
|
||||
|
||||
{name: 'AssetController', path: 'controllers/AssetController.js'},
|
||||
{name:'GameObjectsController', path:'controllers/api/GameObjectsController.js'},
|
||||
{name:'ScenariosController', path:'controllers/api/ScenariosController.js'},
|
||||
{name: 'GameObjectsController', path:'controllers/api/GameObjectsController.js'},
|
||||
{name: 'ScenariosController', path:'controllers/api/ScenariosController.js'},
|
||||
{name: 'GamesController', path:'controllers/api/GamesController.js'},
|
||||
]
|
||||
|
||||
process.on('uncaughtException', err => {
|
||||
|
||||
Reference in New Issue
Block a user