diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 21:28:11 +0000 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 21:28:11 +0000 |
| commit | 7d8cca54e548d2a85287fd2325db88f2697be55a (patch) | |
| tree | 3daa82c6a709363f2f08d9f875d849e4babe1f8f /backend/src/config/session-store.ts | |
| parent | e1633f3348ff7fc5e9131eeae2f2feba09f04838 (diff) | |
Add more shit
Diffstat (limited to 'backend/src/config/session-store.ts')
| -rw-r--r-- | backend/src/config/session-store.ts | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/backend/src/config/session-store.ts b/backend/src/config/session-store.ts new file mode 100644 index 0000000..283031b --- /dev/null +++ b/backend/src/config/session-store.ts @@ -0,0 +1,77 @@ +export type Session = { + id: string; + state: string; + host?: string; + clients: string[]; +}; + +const sessions: { [key: string]: Session } = {}; + +function makeid(length) { + let result = ''; + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + const charactersLength = characters.length; + let counter = 0; + while (counter < length) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + counter += 1; + } + return result; +} + + +export const createNewSession = (): Session => { + const id = makeid(10); + const session = { + id, + state: "waiting", + host: undefined, + clients: [], + }; + sessions[id] = session; + return session; +}; + +export const setSessionState = (id: string, state: string): void => { + if (!sessions[id]) { + return; + } + + sessions[id].state = state; +}; + +export const setSessionHost = (id: string, clientId: string): void => { + if (!sessions[id]) { + return; + } + + sessions[id].host = clientId; +}; + +export const addSessionClient = (id: string, clientId: string): void => { + if (!sessions[id]) { + return; + } + + sessions[id]?.clients.push(clientId); +}; + +export const cleanupSession = (id: string): void => { + if (!sessions[id]) { + return; + } + +// if (sessions[id].host) { +// sessions[id].host!.close(); +// } +// +// sessions[id].clients.forEach((client) => { +// client.close(); +// }); + + delete sessions[id]; +} + +export const getSession = (id: string): Session => { + return sessions[id]; +};
\ No newline at end of file |
