CSNC implementation on NODEjs
This commit is contained in:
parent
f7926482d3
commit
39a9200d4a
84
Server/Node/CSNC.js
Normal file
84
Server/Node/CSNC.js
Normal file
@ -0,0 +1,84 @@
|
||||
var gameObjectStore = {}; //object which stores CSNCgameobjects for each client
|
||||
|
||||
//below is an example of the structure of gameobjectstore
|
||||
/**
|
||||
* {
|
||||
* "2c0a48e9-40c6-4139-9697-992397773b12": [
|
||||
* {
|
||||
"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 {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; //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]);
|
||||
}
|
||||
|
||||
return responseObject;
|
||||
}
|
||||
exports.handleResponse = handleResponse;
|
Loading…
x
Reference in New Issue
Block a user