diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 23:59:44 +0000 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 23:59:44 +0000 |
| commit | c6c93acc32893d891adb39011674c02f3f04999e (patch) | |
| tree | c58a17d789301cbc59bef1e0e3730dd9dc9aae98 /backend/dist/websocket/game.js | |
| parent | 9125c0bc964e499b40ddfa3e87d0b474dc63f5db (diff) | |
gitignore some shit
Diffstat (limited to 'backend/dist/websocket/game.js')
| -rw-r--r-- | backend/dist/websocket/game.js | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/backend/dist/websocket/game.js b/backend/dist/websocket/game.js deleted file mode 100644 index 35d7ad8..0000000 --- a/backend/dist/websocket/game.js +++ /dev/null @@ -1,84 +0,0 @@ -import { addSessionClient, getSession, setSessionHost } from "../config/session-store.js"; -import { WebSocketServer } from "ws"; -import { v4 as uuidv4 } from "uuid"; -const wss = new WebSocketServer({ noServer: true }); -const sendToClient = (clientId, message) => { - wss.clients.forEach((client) => { - if (client.clientId === clientId) { - client.send(JSON.stringify(message)); - } - }); -}; -const broadcastToClients = (clientIds, message) => { - wss.clients.forEach((client) => { - if (clientIds.includes(client.clientId)) { - client.send(JSON.stringify(message)); - } - }); -}; -export const createWebsocketServer = (server) => { - server.on("upgrade", (req, socket, head) => { - wss.handleUpgrade(req, socket, head, (ws) => { - wss.emit("connection", ws, req); - }); - }); - wss.on("connection", (ws) => { - ws.clientId = uuidv4(); - ws.on("message", (message) => { - console.log("received: %s", message); - let data; - try { - data = JSON.parse(message.toString()); - } - catch (e) { - console.log("Invalid JSON"); - return; - } - if (data.action === "host") { - setSessionHost(data.sessionId, ws.clientId); - } - else if (data.action === "move") { - const session = getSession(data.sessionId); - if (!session) { - return; - } - sendToClient(session.host, { - action: "move", - move: data.move, - }); - } - else if (data.action === "join") { - const session = getSession(data.sessionId); - if (!session) { - return; - } - addSessionClient(data.sessionId, ws.clientId); - sendToClient(session.host, { - action: "join", - clients: session.clients.length, - }); - ws.send(JSON.stringify({ - action: "state", - state: session.state, - })); - } - else if (data.action === "start") { - const session = getSession(data.sessionId); - if (!session) { - return; - } - sendToClient(session.host, { - action: "state", - state: "playing", - }); - broadcastToClients(session.clients, { - action: "state", - state: "playing", - }); - } - }); - }); - return wss; -}; -export default createWebsocketServer; -//# sourceMappingURL=game.js.map
\ No newline at end of file |
