Skip to content

Commit

Permalink
- Reldens - v4.0.0-beta.34
Browse files Browse the repository at this point in the history
- Merge pull request #239 from damian-pastorini/v4.0.0-beta.34
  • Loading branch information
damian-pastorini authored Apr 4, 2024
2 parents 931a451 + d62bfcd commit 12e3a38
Show file tree
Hide file tree
Showing 30 changed files with 2,615 additions and 860 deletions.
19 changes: 15 additions & 4 deletions scripts/commander.js → bin/commander.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /usr/bin/env node

/**
*
* Reldens - Commands
Expand All @@ -10,8 +12,7 @@ const path = require('path');
class Commander
{

scriptPath = __dirname;
projectRoot = path.join(this.scriptPath, '../');
projectRoot = process.cwd();
reldensModulePath = path.join(this.projectRoot, 'node_modules', 'reldens');
projectThemeName = 'default';

Expand All @@ -31,9 +32,18 @@ class Commander
try {
fs.opendirSync(this.reldensModulePath);
} catch (error) {
console.error('- Reldens node module folder not found, try `npm install`.');
console.error(
'- Reldens node module folder not found, try `npm install`.',
{
dirname: __dirname,
filename: __filename,
process: process.cwd(),
modulePath: this.reldensModulePath,
projectRoot: this.projectRoot,
}
);
}
const { ThemeManager } = require('reldens/lib/game/server/theme-manager');
const { ThemeManager } = require('../lib/game/server/theme-manager');
this.themeManager = new ThemeManager(this);
let parseResult = this.parseArgs();
if(!parseResult){
Expand Down Expand Up @@ -85,6 +95,7 @@ class Commander
help()
{
console.info(' - Available commands:'
+"\n"+'createApp - Create base project, copy all default files like in the skeleton.'
+"\n"+'resetDist - Delete and create the "dist" folder.'
+"\n"+'removeDist - Delete the "dist" folder.'
+"\n"+'installDefaultTheme - Copy theme and packages from node_modules into the current project theme.'
Expand Down
85 changes: 85 additions & 0 deletions bin/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#! /usr/bin/env node

const fs = require('fs');
const path = require('path');
const {
PlayersExperiencePerLevel,
MonstersExperiencePerLevel,
AttributesPerLevel
} = require('@reldens/game-data-generator');

let args = process.argv;
if(2 === args.length){
console.error('- Missing arguments.', args);
return false;
}

let validCommands = {
'players-experience-per-level': (commandParams) => {
let playersExperiencePerLevel = new PlayersExperiencePerLevel(commandParams);
playersExperiencePerLevel.generate();
},
'monsters-experience-per-level': (commandParams) => {
let monstersExperiencePerLevel = new MonstersExperiencePerLevel(commandParams);
monstersExperiencePerLevel.generate();
},
'attributes-per-level': (commandParams) => {
let attributesPerLevel = new AttributesPerLevel(commandParams);
attributesPerLevel.generate();
}
};

function fetchFileContents(filePath)
{
if (!filePath) {
console.error('- Missing data file.', filePath);
return false;
}

let relativePath = path.join(process.cwd(), filePath);
if (!relativePath) {
console.error('- Invalid data file path.', process.cwd(), filePath);
return false;
}

let fileContent = fs.readFileSync(relativePath, {encoding: 'utf8', flag:'r'});
if (!fileContent) {
console.error('- Can not read data file or file empty.', relativePath);
return false;
}

let importedJson = JSON.parse(fileContent);
if (!importedJson) {
console.error('- Can not parse data file.');
return false;
}

return importedJson;
}

let extractedParams = args.slice(2);

let command = extractedParams[0] || false;
if (!command) {
console.error('- Missing command.');
return false;
}

if (-1 === Object.keys(validCommands).indexOf(command)) {
console.error('- Invalid command.', command);
return false;
}

let importedJson = fetchFileContents(extractedParams[1] || '');

if ('monsters-experience-per-level' === command) {
let importedPlayerLevelsJson = fetchFileContents(extractedParams[2] || '');
if (!importedPlayerLevelsJson) {
console.error('- Can not parse data file for player levels.');
return false;
}

importedJson.levelsExperienceByKey = importedPlayerLevelsJson;
}

validCommands[command](importedJson);
119 changes: 119 additions & 0 deletions bin/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#! /usr/bin/env node

const fs = require('fs');
const path = require('path');
const { ServerManager } = require('../server');
const { PlayersExperiencePerLevelImporter } = require('../lib/import/server/players-experience-per-level-importer');
const { AttributesPerLevelImporter } = require('../lib/import/server/attributes-per-level-importer');
const { ClassPathsImporter } = require('../lib/import/server/class-paths-importer');
const { MapsImporter } = require('../lib/import/server/maps-importer');

let args = process.argv;
if(2 === args.length){
console.error('- Missing arguments.', args);
return false;
}

let validCommands = {
'players-experience-per-level': async (data, projectThemeName) => {
let serverManager = await initializeServer(data, projectThemeName);
if (!serverManager) {
return false;
}
let importer = new PlayersExperiencePerLevelImporter(serverManager);
await importer.import(data);
},
'attributes-per-level': async (data, projectThemeName) => {
let serverManager = await initializeServer(data, projectThemeName);
if (!serverManager) {
return false;
}
let importer = new AttributesPerLevelImporter(serverManager);
await importer.import(data);
},
'class-paths': async (data, projectThemeName) => {
let serverManager = await initializeServer(data, projectThemeName);
if (!serverManager) {
return false;
}
let importer = new ClassPathsImporter(serverManager);
await importer.import(data);
},
'maps': async (data, projectThemeName) => {
let serverManager = await initializeServer(data, projectThemeName);
if (!serverManager) {
return false;
}
let importer = new MapsImporter(serverManager);
await importer.import(data);
}
};

async function initializeServer(data, projectThemeName)
{
if (!data) {
console.error('- Missing data.', data);
return false;
}
let appServer = new ServerManager({
projectRoot: process.cwd(),
projectThemeName
});
await appServer.initializeStorage(appServer.rawConfig, appServer.dataServerDriver);
return appServer;
}

function fetchFileContents(filePath)
{
if (!filePath) {
console.error('- Missing data file.', filePath);
return false;
}

let relativePath = path.join(process.cwd(), filePath);
if (!relativePath) {
console.error('- Invalid data file path.', process.cwd(), filePath);
return false;
}

let fileContent = fs.readFileSync(relativePath, {encoding: 'utf8', flag:'r'});
if (!fileContent) {
console.error('- Can not read data file or file empty.', relativePath);
return false;
}

let importedJson = JSON.parse(fileContent);
if (!importedJson) {
console.error('- Can not parse data file.');
return false;
}

return importedJson;
}

let extractedParams = args.slice(2);

let command = extractedParams[0] || false;
if (!command) {
console.error('- Missing command.');
return false;
}

let themeName = extractedParams[1] || '';
if (!themeName) {
console.error('- Missing active theme name.');
return false;
}

if (-1 === Object.keys(validCommands).indexOf(command)) {
console.error('- Invalid command.', command);
return false;
}

validCommands[command](fetchFileContents(extractedParams[2] || ''), themeName).then(() => {
console.log('Done.');
process.exit();
}).catch((error) => {
console.error(error);
process.exit();
});
2 changes: 2 additions & 0 deletions scripts/install-test.js → bin/install-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /usr/bin/env node

/**
*
* Reldens - Install Test
Expand Down
2 changes: 2 additions & 0 deletions scripts/reldens-commands.js → bin/reldens-commands.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /usr/bin/env node

/**
*
* Reldens - Commands
Expand Down
17 changes: 17 additions & 0 deletions lib/actions/class-path-key-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
* Reldens - ClassPathsImporter
*
*/

class ClassPathKeyFactory
{

static fromLabel(label)
{
return label.toLowerCase().replace(/ /g, '-').replace('---', '-');
}

}

module.exports.ClassPathKeyFactory = ClassPathKeyFactory;
42 changes: 42 additions & 0 deletions lib/game/client/fps-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
*
* Reldens - GameDom
*
*/

class FPSCounter
{

constructor(gameDom)
{
this.lastFrameTime = performance.now();
this.frameCount = 0;
this.fpsDisplay = gameDom.createElementWithStyles('div', 'fps-counter', {
padding: '0 20px',
background: '#000',
color: '#00ff00'
});
gameDom.getElement('.header').appendChild(this.fpsDisplay);
}

updateFPS()
{
let currentTime = performance.now();
let deltaTime = currentTime - this.lastFrameTime;
this.lastFrameTime = currentTime;
const fps = Math.round(1000 / deltaTime);
this.frameCount++;
if (this.frameCount % 60 === 0) {
this.fpsDisplay.textContent = 'FPS: ' + fps;
}
requestAnimationFrame(this.updateFPS.bind(this));
}

start()
{
this.updateFPS();
}

}

module.exports.FPSCounter = FPSCounter
11 changes: 9 additions & 2 deletions lib/game/client/game-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class GameDom
this.getElement(querySelector).remove();
}

createElement(type, id = false)
createElement(type, id = '')
{
let element = document.createElement(type);
if(id){
if('' !== id){
element.id = id;
}
return element;
Expand All @@ -104,6 +104,13 @@ class GameDom
}
}

createElementWithStyles(type, id = '', styles = {})
{
let element = this.createElement(type, id);
this.setElementStyles(element, styles);
return element;
}

activeElement()
{
return document.activeElement;
Expand Down
7 changes: 7 additions & 0 deletions lib/game/client/game-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const TemplateEngine = require('mustache');
const { Game, Input } = require('phaser');
const { FPSCounter } = require('./fps-counter');
const { GameConst } = require('../constants');
const { ObjectsConst } = require('../../objects/constants');
const { sc } = require('@reldens/utils');
Expand Down Expand Up @@ -184,6 +185,12 @@ class GameEngine extends Game
this.eventsManager.emit('reldens.gameEngineTabTarget', this, closerTarget, previousTarget);
}

showFPS()
{
this.fpsCounter = new FPSCounter(this.uiScene.gameManager.gameDom);
this.fpsCounter.start();
}

}

module.exports.GameEngine = GameEngine;
Loading

0 comments on commit 12e3a38

Please sign in to comment.