| 1 | const users = [] |
| 2 | |
| 3 | const addUser = ({ id, username, room }) => { |
| 4 | // Clean the user data |
| 5 | username = username.trim().toLowerCase() |
| 6 | room = room.trim().toLowerCase() |
| 7 | |
| 8 | // Validate the user data |
| 9 | if (!username || !room) { |
| 10 | return { |
| 11 | error: 'Username and room are required!' |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | // Check for existing user |
| 16 | const existingUser = users.find((user) => { |
| 17 | return user.room === room && user.username === username |
| 18 | }) |
| 19 | |
| 20 | // Validate username |
| 21 | if (existingUser) { |
| 22 | return { |
| 23 | error: 'Username is in use!' |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // Store user |
| 28 | const user = { id, username, room } |
| 29 | users.push(user) |
| 30 | return { user } |
| 31 | } |
| 32 | |
| 33 | const removeUser = (id) => { |
| 34 | const index = users.findIndex((user) => user.id === id) |