Skip to content

Commit

Permalink
Services 2 (#96)
Browse files Browse the repository at this point in the history
* wip

* wip

* more

* wip

* better

* cool

* nice

* prompt
  • Loading branch information
prosif authored Apr 14, 2024
1 parent 8c9a877 commit bda640f
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 94 deletions.
77 changes: 2 additions & 75 deletions game_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { socketServer } = require('./src/util/socket');
const Homenames = require('./src/Homenames');
const path = require('path');
const baseDir = path.dirname(require.main.filename);
const { getService } = require('./src/services/index');
// const viewtest = require('./src/games/view-test');

const { getConfigValue } = require('homegames-common');
Expand Down Expand Up @@ -33,83 +34,9 @@ const server = (certPath, squishMap, username) => {

let services = {};

const supportedServices = {
'contentGenerator': {
requestContent: (request) => new Promise((resolve, reject) => {
const makePost = (path, _payload) => new Promise((resolve, reject) => {
const payload = JSON.stringify(_payload);

let module, hostname, port;

module = https;
port = 443;
hostname = 'api.homegames.io';

const headers = {};

Object.assign(headers, {
'Content-Type': 'application/json',
'Content-Length': payload.length
});

const options = {
hostname,
path,
port,
method: 'POST',
headers
};

let responseData = '';

const req = module.request(options, (res) => {
res.on('data', (chunk) => {
responseData += chunk;
});

res.on('end', () => {
resolve(responseData);
});
});

req.write(payload);
req.end();
});

makePost('https://api.homegames.io/services', request).then((response) => {
if (!response.startsWith('{')) {
reject(response);
} else {
const requestId = JSON.parse(response).requestId;
const interval = setInterval(() => {
https.get(`https://api.homegames.io/service_requests/${requestId}`, {}, (res) => {
let bufs = [];
res.on('data', (chunk) => {
bufs.push(chunk);
});

res.on('end', () => {
const fin = JSON.parse(Buffer.concat(bufs));
const parsed = fin;//JSON.parse(fin);
if (parsed.response) {
clearInterval(interval);
resolve(parsed.response);
}
});
});
}, 5000);
}
}).catch(err => {
reject(err);
});
})
}
};

if (customStartModule?.metadata) {
const requestedServices = customStartModule.metadata().services || [];
services = {};
requestedServices.forEach(s => services[s] = supportedServices[s]);
requestedServices.forEach(s => services[s] = getService(s));
}

const dashboard = customStartModule ? new customStartModule({
Expand Down
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homegames-core",
"version": "1.0.29",
"version": "1.0.30",
"description": "Play games at home",
"directories": {
"lib": "lib"
Expand Down Expand Up @@ -28,6 +28,7 @@
"homepage": "https://github.com/homegamesio/homegames#readme",
"dependencies": {
"acorn": "^8.11.3",
"decompress": "^4.2.1",
"homegames-common": "1.0.9",
"squish-0756": "npm:squishjs@1.0.4",
"squish-0762": "npm:squishjs@1.0.4",
Expand All @@ -38,7 +39,7 @@
"squish-1004": "npm:squishjs@1.0.4",
"squish-1005": "npm:squishjs@1.0.5",
"squish-1006": "npm:squishjs@1.0.6",
"decompress": "^4.2.1",
"squish-1007": "npm:squishjs@1.0.7",
"ws": "7.4.6"
}
}
11 changes: 11 additions & 0 deletions src/GameSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ class GameSession {
// }
} else if (input.type === 'mouseup') {
this.game.handleMouseUp && this.game.handleMouseUp(playerId, input.data);
} else if (input.type === 'onhover') {
const node = this.game.findNode(input.nodeId) || this.customTopLayer.root.findChild(input.nodeId);
if (node && node.node?.onHover) {
node.node.onHover(playerId);
}
} else if (input.type === 'offhover') {
const node = this.game.findNode(input.nodeId) || this.customTopLayer.root.findChild(input.nodeId);
if (node && node.node?.offHover) {
node.node.offHover(playerId);
}

} else {
log.info('Unknown input type: ', input.type);
}
Expand Down
21 changes: 18 additions & 3 deletions src/child_game_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const crypto = require('crypto');
const fs = require('fs');
const { socketServer } = require('./util/socket');
const { reportBug } = require('./common/util');
const { getService } = require('./services/index');

const process = require('process');

Expand Down Expand Up @@ -37,9 +38,17 @@ const startServer = (sessionInfo) => {
try {
log.info("THIS IS SESSION INFO");
log.info(sessionInfo);


let services = {};

if (sessionInfo.gamePath) {
const _gameClass = require(sessionInfo.gamePath);

if (_gameClass.metadata) {
const requestedServices = _gameClass.metadata().services || [];
requestedServices.forEach(s => services[s] = getService(s));
}

let saveData;
const savePath = crypto.createHash('md5').update(sessionInfo.gamePath).digest('hex');

Expand All @@ -60,9 +69,15 @@ const startServer = (sessionInfo) => {
console.log(err);
}
}
gameInstance = new _gameClass({ addAsset, saveGame, saveData });
gameInstance = new _gameClass({ addAsset, saveGame, saveData, services });
} else {
gameInstance = new games[sessionInfo.key]({ addAsset });
const _gameClass = games[sessionInfo.key];

if (_gameClass.metadata) {
const requestedServices = _gameClass.metadata().services || [];
requestedServices.forEach(s => services[s] = getService(s));
}
gameInstance = new _gameClass({ addAsset, services });

}
gameSession = new GameSession(gameInstance, sessionInfo.port, sessionInfo.username);
Expand Down
3 changes: 2 additions & 1 deletion src/common/squish-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
'1000': isMain ? require.resolve('squish-1000') : electronOverrideVersion,
'1004': require.resolve('squish-1004'),
'1005': require.resolve('squish-1005'),
'1006': require.resolve('squish-1006')
'1006': require.resolve('squish-1006'),
'1007': require.resolve('squish-1007')
};
Loading

0 comments on commit bda640f

Please sign in to comment.