104 lines
2.6 KiB
JavaScript
104 lines
2.6 KiB
JavaScript
const timeoutTickRate = 1; //timeout check every x seconds
|
|
const timeoutLength = 5000; //timeout length in ms
|
|
var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
|
|
|
|
//below is an example of the structure of gameobjectstore
|
|
/**
|
|
* {
|
|
* "2c0a48e9-40c6-4139-9697-992397773b12":
|
|
* time: Date.now(),
|
|
* gameObjects: [
|
|
* {
|
|
"type": "razorblade",
|
|
"transform": {
|
|
"position": [
|
|
0.0,
|
|
0.53,
|
|
0.0
|
|
],
|
|
"rotation": [
|
|
0.0,
|
|
0.0,
|
|
0.0,
|
|
1.0
|
|
]
|
|
}
|
|
},
|
|
{
|
|
"type": "boomerang",
|
|
"transform": {
|
|
"position": [
|
|
0.0,
|
|
0.0,
|
|
0.0
|
|
],
|
|
"rotation": [
|
|
-0.7071068,
|
|
0.0,
|
|
0.0,
|
|
0.7071067
|
|
]
|
|
}
|
|
}
|
|
* ]
|
|
* }
|
|
*
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* @param {string} uuid
|
|
*/
|
|
function removePlayer(uuid) {
|
|
delete gameObjectStore[uuid];
|
|
}
|
|
|
|
function checkTimeout() {
|
|
for (let key in gameObjectStore) {
|
|
if (Date.now() - gameObjectStore[key].time > timeoutLength) {
|
|
removePlayer(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {http.ServerResponse<http.IncomingMessage>} res
|
|
* @param {string} req
|
|
*/
|
|
|
|
//reads data from client
|
|
async function handleResponse(res, req) {
|
|
try {
|
|
var request = JSON.parse(req);
|
|
let gameObjects = request.gameObjects;
|
|
|
|
gameObjectStore[request.uuid] = {gameObjects: gameObjects, time: Date.now()}; //store gameobjects to send to other players
|
|
|
|
res.writeHead(200);
|
|
res.write(JSON.stringify(responseObjectHelper(request.uuid)));
|
|
res.end();
|
|
|
|
} catch (err) {
|
|
console.log("Invalid Request");
|
|
console.log(err);
|
|
res.end();
|
|
}
|
|
|
|
}
|
|
|
|
//preps and returns data to send back to client
|
|
function responseObjectHelper(uuid) {
|
|
var responseObject = {
|
|
gameObjects: []
|
|
};
|
|
|
|
for (let key in gameObjectStore) {
|
|
if (key == uuid) continue; //dont send back players own data
|
|
responseObject.gameObjects = responseObject.gameObjects.concat(gameObjectStore[key].gameObjects);
|
|
}
|
|
|
|
return responseObject;
|
|
}
|
|
|
|
setInterval(checkTimeout, timeoutTickRate * 1000);
|
|
exports.handleResponse = handleResponse; |