From 39a9200d4aa8a686be0ed0d05667d8a7c4d5deb1 Mon Sep 17 00:00:00 2001 From: APEX FIGHT Date: Thu, 2 Jan 2025 16:23:18 -0500 Subject: [PATCH] CSNC implementation on NODEjs --- Server/Node/CSNC.js | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Server/Node/CSNC.js diff --git a/Server/Node/CSNC.js b/Server/Node/CSNC.js new file mode 100644 index 0000000..91d9de7 --- /dev/null +++ b/Server/Node/CSNC.js @@ -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} 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; \ No newline at end of file