wip refacto

This commit is contained in:
Florian Schmitt 2025-06-08 18:27:17 +03:00
parent cf7fb776bd
commit 28071e585f
18 changed files with 2270 additions and 7587 deletions

View file

@ -20,7 +20,7 @@ const room_users = {}
const sid_rooms = {}
// Add a client to a room and return the sid:client mapping.
exports.add_to_room = function(client, room, callback) {
export const add_to_room = function(client, room, callback) {
// console.log('Client ' + client.username + ' (' + client.id + ') added to room ' + room);
if (!(sid_rooms.hasOwnProperty(client.id))) sid_rooms[client.id] = new sets.Set()
@ -37,7 +37,7 @@ exports.add_to_room = function(client, room, callback) {
// Remove a client from all rooms and return the username:client
// mapping for everybody in those rooms.
exports.remove_from_all_rooms = function(client, callback) {
export const remove_from_all_rooms = function(client, callback) {
const affected_clients = new sets.Set()
if (sid_rooms.hasOwnProperty(client.id)) {
const client_rooms = sid_rooms[client.id].array()
@ -79,26 +79,26 @@ function remove_from_room(client, room, callback) {
if (room_users[room].size() === 0) { delete room_users[room] }
}
callback(exports.room_clients(room))
callback(room_clients(room))
}
// Return list of clients in the given room.
exports.room_clients = function(room) {
export const room_clients = function(room) {
return rooms.hasOwnProperty(room) ? rooms[room].array() : []
}
// Return true if room contains the given client, false otherwise.
exports.client_in_room = function(room, client) {
export const client_in_room = function(room, client) {
return rooms.hasOwnProperty(room) && rooms[room].has(client)
}
// Return list of usernames in given room
exports.users_in_room = function(room) {
export const users_in_room = function(room) {
return room_users.hasOwnProperty(room) ? room_users[room].array() : []
}
// Return list of usernames in given room
exports.room_clients_other_than_me = function(room, client) {
export const room_clients_other_than_me = function(room, client) {
if (rooms.hasOwnProperty(room)) {
const clients = rooms[room]
// console.dir(clients.array());
@ -112,7 +112,7 @@ exports.room_clients_other_than_me = function(room, client) {
}
// gets the current room of the client (assumes one room -- will select first one if in multiple)
exports.get_room = function(client) {
export const get_room = function(client) {
let client_rooms = null
if (sid_rooms.hasOwnProperty(client.id)) {
@ -125,9 +125,9 @@ exports.get_room = function(client) {
// Generic server code
exports.add_to_room_and_announce = function(client, room, msg) {
export const add_to_room_and_announce = function(client, room, msg) {
// Add user info to the current dramatis personae
exports.add_to_room(client, room, (clients) => {
add_to_room(client, room, (clients) => {
// Broadcast new-user notification
for (let i = 0; i < clients.length; i++) {
if (clients[i].id != client.id) { clients[i].json.send(msg) }
@ -136,7 +136,7 @@ exports.add_to_room_and_announce = function(client, room, msg) {
}
/*
exports.on_leave_room = function (client, room) {
export const on_leave_room = function (client, room) {
remove_from_room(client, room, function(clients) {
console.log(client + ' disconnected, yo');
@ -152,8 +152,8 @@ exports.on_leave_room = function (client, room) {
} */
// remember that this announces to all rooms that this client was a member of
exports.remove_from_all_rooms_and_announce = function(client, msg) {
exports.remove_from_all_rooms(client, (clients) => {
export const remove_from_all_rooms_and_announce = function(client, msg) {
remove_from_all_rooms(client, (clients) => {
for (let i = 0; i < clients.length; i++) {
if (clients[i].id != client.id) { clients[i].json.send(msg) }
}
@ -165,7 +165,7 @@ exports.remove_from_all_rooms_and_announce = function(client, msg) {
/// ///////////////////////////
// Broadcast message to all clients
exports.broadcast = function(msg) {
export const broadcast = function(msg) {
if (socket) socket.broadcast(msg)
net_server_streams.each((stream) => {
stream.write(`${JSON.stringify(msg)}\r\n`)
@ -173,13 +173,13 @@ exports.broadcast = function(msg) {
}
// Broadcast message to all clients in a given room.
exports.broadcast_room = function(room, msg) {
const clients = exports.room_clients(room)
export const broadcast_room = function(room, msg) {
const clients = room_clients(room)
for (let i = 0; i < clients.length; i++) { clients[i].json.send(msg) }
}
// Broadcast message to all the other clients that are in rooms with this client
exports.broadcast_to_roommates = function(client, msg) {
export const broadcast_to_roommates = function(client, msg) {
let roommates = new sets.Set()
if (sid_rooms.hasOwnProperty(client.id)) {