clean up
This commit is contained in:
parent
581a2330de
commit
1ca84b30c6
12 changed files with 47 additions and 192 deletions
|
|
@ -26,8 +26,8 @@
|
|||
"@fastify/websocket": "^11.2.0",
|
||||
"fastify": "^5.6.2",
|
||||
"fastify-plugin": "^5.1.0",
|
||||
"socket.io": "^4.8.2",
|
||||
"typebox": "^1.0.65"
|
||||
"socket.io": "^4.8.3",
|
||||
"typebox": "^1.0.69"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.19.3",
|
||||
|
|
|
|||
|
|
@ -144,7 +144,6 @@ function isUser_BlockedBy_me(fastify: FastifyInstance, blockedBy_Id : string, is
|
|||
};
|
||||
|
||||
async function onReady(fastify: FastifyInstance) {
|
||||
// shows address for connection au server transcendance
|
||||
const session = process.env.SESSION_MANAGER ?? '';
|
||||
if (session) {
|
||||
const part = session.split('/')[1];
|
||||
|
|
@ -154,30 +153,10 @@ async function onReady(fastify: FastifyInstance) {
|
|||
|
||||
fastify.io.on('connection', (socket: Socket) => {
|
||||
socket.on('message', (message: string) => {
|
||||
// console.info(color.blue, 'DEBUG LOG: Socket connected!', color.reset, socket.id);
|
||||
// console.log( color.blue, 'DEBUG LOG: Received message from client', color.reset, message);
|
||||
const obj: ClientMessage = JSON.parse(message) as ClientMessage;
|
||||
clientChat.set(socket.id, { user: obj.user, socket: socket.id, lastSeen: Date.now() });
|
||||
// console.log(color.green, 'DEBUG LOG: Message from client', color.reset, `Sender: login name: ${obj.user} - windowID ${obj.SenderWindowID} - text message: ${obj.text}`);
|
||||
socket.emit('welcome', { msg: 'Welcome to the chat! : ' });
|
||||
// Send object directly — DO NOT wrap it in a string
|
||||
broadcast(fastify, obj, obj.SenderWindowID);
|
||||
|
||||
// const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
// console.log('DEBUG: senderWindow :', getUserById(users, obj.SenderUserID)?.name);
|
||||
// fastify.io.fetchSockets().then((sockets) => {
|
||||
// for (const socket of sockets) {
|
||||
// const clientInfo = clientChat.get(socket.id);
|
||||
// if (!clientInfo?.user) {
|
||||
// console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`);
|
||||
// continue;
|
||||
// }
|
||||
// console.log('DEBUG: UserIDWindow :', getUserByName(users, clientInfo.user)?.id);
|
||||
// const IDUser = getUserByName(users, clientInfo.user)?.id;
|
||||
// console.log(filter_Blocked_user(fastify, obj, IDUser?? ""));
|
||||
// }
|
||||
// });
|
||||
// console.log(color.red, 'DEBUG LOG: connected in the Chat :', connectedUser(fastify.io), color.reset);
|
||||
});
|
||||
nextGame_SocketListener(fastify, socket);
|
||||
list_SocketListener(fastify, socket);
|
||||
|
|
@ -185,13 +164,7 @@ async function onReady(fastify: FastifyInstance) {
|
|||
socket.on('updateClientName', (object) => {
|
||||
const userFromFrontend = object || null;
|
||||
const client = clientChat.get(socket.id) || null;
|
||||
// console.log(color.red, 'DEBUG LOG: whoAMi activated', userFromFrontend, color.reset, socket.id);
|
||||
if (userFromFrontend.oldUser !== userFromFrontend.user) {
|
||||
// console.log(color.red, 'DEBUG LOG: whoAMi activated', userFromFrontend.oldUser, color.reset);
|
||||
// if (client === null) {
|
||||
// console.log('ERROR: clientName is NULL');
|
||||
// return;
|
||||
// };
|
||||
if (client) {
|
||||
client.user = userFromFrontend.user;
|
||||
console.log(color.yellow, `'DEBUG LOG: client.user is, '${client.user}'`);
|
||||
|
|
@ -204,7 +177,6 @@ async function onReady(fastify: FastifyInstance) {
|
|||
const clientName = clientInfo?.user;
|
||||
|
||||
if (!clientName) return;
|
||||
// console.log(color.green, `Client logging out: ${clientName} (${socket.id})`);
|
||||
const obj: ClientMessage = {
|
||||
command: '',
|
||||
destination: 'system-info',
|
||||
|
|
@ -222,15 +194,12 @@ async function onReady(fastify: FastifyInstance) {
|
|||
SenderUserID: '',
|
||||
};
|
||||
broadcast(fastify, obj, socket.id);
|
||||
// Optional: remove from map
|
||||
clientChat.delete(socket.id);
|
||||
// Ensure socket is fully disconnected
|
||||
if (socket.connected) socket.disconnect(true);
|
||||
});
|
||||
|
||||
socket.on('disconnecting', (reason) => {
|
||||
const clientName = clientChat.get(socket.id)?.user || null;
|
||||
// console.log(color.green, `Client disconnecting: ${clientName} (${socket.id}) reason:`, reason,);
|
||||
if (reason === 'transport error') return;
|
||||
|
||||
if (clientName !== null) {
|
||||
|
|
@ -258,7 +227,6 @@ async function onReady(fastify: FastifyInstance) {
|
|||
socket.on('client_left', (data) => {
|
||||
void data;
|
||||
const clientName = clientChat.get(socket.id)?.user || null;
|
||||
// const leftChat = data || null; console.log(color.green, `Left the Chat User: ${clientName} id Socket: ${socket.id} reason:`, leftChat.why,);
|
||||
if (clientName !== null) {
|
||||
const obj: ClientMessage = {
|
||||
command: '',
|
||||
|
|
@ -276,10 +244,7 @@ async function onReady(fastify: FastifyInstance) {
|
|||
SenderUserName: '',
|
||||
SenderUserID: '',
|
||||
};
|
||||
// console.log(color.blue, 'DEBUG LOG: BROADCASTS OUT :', obj.SenderWindowID);
|
||||
|
||||
broadcast(fastify, obj, obj.SenderWindowID);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -287,7 +252,6 @@ async function onReady(fastify: FastifyInstance) {
|
|||
socket.on('privMessage', (data) => {
|
||||
const clientName: string = clientChat.get(socket.id)?.user || '';
|
||||
const prvMessage: ClientMessage = JSON.parse(data) || '';
|
||||
// console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target Name:`, prvMessage.command,);
|
||||
if (clientName !== null) {
|
||||
const obj: ClientMessage = {
|
||||
command: prvMessage.command,
|
||||
|
|
@ -305,38 +269,26 @@ async function onReady(fastify: FastifyInstance) {
|
|||
SenderUserName: '',
|
||||
SenderUserID:'',
|
||||
};
|
||||
// console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID);
|
||||
sendPrivMessage(fastify, obj, obj.SenderWindowID);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('profilMessage', async (data: string) => {
|
||||
const clientName: string = clientChat.get(socket.id)?.user || '';
|
||||
const profilMessage: ClientMessage = JSON.parse(data) || '';
|
||||
// const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
// console.log(color.yellow, 'DEBUG LOG: ALL USERS EVER CONNECTED:', users);
|
||||
// console.log(color.blue, `DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target profil:`, profilMessage.user);
|
||||
const profile: ClientProfil = await makeProfil(fastify, profilMessage.user, socket);
|
||||
if (clientName !== null) {
|
||||
// const testuser: User | null = getUserByName(users, profilMessage.user);
|
||||
// console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
|
||||
// console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profile.SenderWindowID);
|
||||
sendProfil(fastify, profile, profile.SenderWindowID);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('inviteGame', async (data: string) => {
|
||||
const clientName: string = clientChat.get(socket.id)?.user || '';
|
||||
const profilInvite: ClientProfil = JSON.parse(data) || '';
|
||||
// const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
|
||||
const inviteHtml: string = 'invites you to a game ' + setGameLink('');
|
||||
if (clientName !== null) {
|
||||
console.log(`DEBUG LOG: clientName =${clientName}`);
|
||||
// const testuser: User | null = getUserByName(users, profilInvite.user ?? '');
|
||||
// console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
|
||||
sendInvite(fastify, inviteHtml, profilInvite);
|
||||
}
|
||||
});
|
||||
|
|
@ -346,15 +298,7 @@ async function onReady(fastify: FastifyInstance) {
|
|||
const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`);
|
||||
const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`);
|
||||
|
||||
// console.log(color.yellow, `user to block: ${profilBlock.user}`);
|
||||
// console.log(color.yellow, UserToBlock);
|
||||
// console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
|
||||
// console.log(color.yellow, UserAskingToBlock);
|
||||
// console.log(color.red, clientName);
|
||||
|
||||
if (!UserAskingToBlock || !UserToBlock) return;
|
||||
|
||||
if (isUser_BlockedBy_me(fastify, UserAskingToBlock!.id, UserToBlock!.id)) {
|
||||
const message: blockedUnBlocked = {
|
||||
userState: 'block',
|
||||
|
|
@ -380,13 +324,6 @@ async function onReady(fastify: FastifyInstance) {
|
|||
const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||
const UserToBlock: User | null = getUserByName(users, `${profilBlock.user}`);
|
||||
const UserAskingToBlock: User | null = getUserByName(users, `${profilBlock.SenderName}`);
|
||||
|
||||
// console.log(color.yellow, `user to block: ${profilBlock.user}`);
|
||||
// console.log(color.yellow, UserToBlock);
|
||||
// console.log(color.yellow, `user Asking to block: ${profilBlock.SenderName}`);
|
||||
// console.log(color.yellow, UserAskingToBlock);
|
||||
// console.log(color.red, clientName);
|
||||
|
||||
const usersBlocked: BlockedData[] = fastify.db.getAllBlockedUsers() ?? [];
|
||||
if (!UserAskingToBlock || !UserToBlock || !usersBlocked) return;
|
||||
const userAreBlocked: boolean = isBlocked(UserAskingToBlock, UserToBlock, usersBlocked);
|
||||
|
|
@ -408,13 +345,7 @@ async function onReady(fastify: FastifyInstance) {
|
|||
}
|
||||
|
||||
if (userAreBlocked) {
|
||||
// console.log(color.green, 'Both users are blocked as requested');
|
||||
// return true; // or any other action you need to take
|
||||
|
||||
// console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
|
||||
fastify.db.removeBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
|
||||
// const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
// console.log(color.green, 'remove A BLOCKED USER:', usersBlocked2);
|
||||
if (clientName !== null) {
|
||||
const blockedMessage = 'I have un-blocked you';
|
||||
if (clientName !== null) {
|
||||
|
|
@ -431,29 +362,13 @@ async function onReady(fastify: FastifyInstance) {
|
|||
SenderWindowID: socket.id,
|
||||
Sendertext: 'You have un-blocked',
|
||||
};
|
||||
// console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID);
|
||||
socket.emit('privMessageCopy', `${obj.Sendertext}: ${UserToBlock.name}💚`);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
// profilBlock.Sendertext = `'You have un-blocked '`;
|
||||
sendBlocked(fastify, blockedMessage, profilBlock);
|
||||
// const message: blockedUnBlocked =
|
||||
// {
|
||||
// userState: "un-block",
|
||||
// userTarget: "",
|
||||
// by: "",
|
||||
|
||||
// };
|
||||
// socket.emit('blockBtn', message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
// console.log(color.red, 'The users are not blocked in this way');
|
||||
// console.log(color.red, 'ALL BLOCKED USERS:', usersBlocked);
|
||||
fastify.db.addBlockedUserFor(UserAskingToBlock!.id, UserToBlock!.id);
|
||||
// const usersBlocked2 = fastify.db.getAllBlockedUsers();
|
||||
// console.log(color.green, 'ALL BLOCKED USERS:', usersBlocked2);
|
||||
if (clientName !== null) {
|
||||
const blockedMessage = 'I have blocked you';
|
||||
profilBlock.Sendertext = 'You have blocked ';
|
||||
|
|
@ -471,19 +386,9 @@ async function onReady(fastify: FastifyInstance) {
|
|||
SenderWindowID: socket.id,
|
||||
Sendertext: 'You have blocked',
|
||||
};
|
||||
// console.log(color.blue, 'DEBUG LOG: PRIV MESSAGE OUT :', obj.SenderWindowID);
|
||||
socket.emit('privMessageCopy', `${obj.Sendertext}: ${UserToBlock.name}⛔`);
|
||||
// clientChat.delete(obj.user);
|
||||
}
|
||||
sendBlocked(fastify, blockedMessage, profilBlock);
|
||||
// const message: blockedUnBlocked =
|
||||
// {
|
||||
// userState: "block",
|
||||
// userTarget: UserToBlock.name,
|
||||
// by: UserAskingToBlock.name,
|
||||
|
||||
// };
|
||||
// socket.emit('blockBtn', message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { clientChat } from './app';
|
|||
import { FastifyInstance } from 'fastify';
|
||||
import { getUserByName } from './getUserByName';
|
||||
import type { User } from '@shared/database/mixin/user';
|
||||
import { color } from './app';
|
||||
import { checkNamePair } from './checkNamePair';
|
||||
import { whoBlockedMe } from './whoBlockedMe';
|
||||
|
||||
|
|
@ -16,7 +15,6 @@ export async function broadcast(fastify: FastifyInstance, data: ClientMessage, s
|
|||
for (const socket of sockets) {
|
||||
const clientInfo = clientChat.get(socket.id);
|
||||
if (!clientInfo?.user) {
|
||||
console.log(color.red, `Skipping socket ${socket.id} (no user found)`);
|
||||
continue;
|
||||
}
|
||||
let blockMsgFlag: boolean = false;
|
||||
|
|
@ -25,12 +23,9 @@ export async function broadcast(fastify: FastifyInstance, data: ClientMessage, s
|
|||
blockMsgFlag = checkNamePair(list, data.SenderUserID, UserByID) || false;
|
||||
|
||||
if (socket.id === sender) {
|
||||
console.log(color.red, 'sKip Sender ', socket.id);
|
||||
continue;
|
||||
}
|
||||
console.log(`BROADCAST blockFlag=${blockMsgFlag} : Target ${clientInfo.user}`);
|
||||
if (!blockMsgFlag) {
|
||||
console.log(color.blue, 'Emit message: ', data.command, 'blockMsgFlag: ', blockMsgFlag);
|
||||
socket.emit('MsgObjectServer', { message: data });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { FastifyInstance } from 'fastify';
|
||||
import { clientChat, color } from './app';
|
||||
import { clientChat } from './app';
|
||||
|
||||
/**
|
||||
* function broadcast a clickable link
|
||||
|
|
@ -8,13 +8,11 @@ import { clientChat, color } from './app';
|
|||
*/
|
||||
export async function broadcastNextGame(fastify: FastifyInstance, gameLink?: Promise<string>) {
|
||||
const link = gameLink ? await gameLink : undefined;
|
||||
console.log(color.green, 'link===========> ', link);
|
||||
const sockets = await fastify.io.fetchSockets();
|
||||
// fastify.io.fetchSockets().then((sockets) => {
|
||||
for (const socket of sockets) {
|
||||
const clientInfo = clientChat.get(socket.id);
|
||||
if (!clientInfo?.user) {
|
||||
console.log(color.yellow, `DEBUG LOG: Skipping socket ${socket.id} (no user found)`);
|
||||
continue;
|
||||
}
|
||||
if (link) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import { sendGameLinkToChatService } from './sendGameLinkToChatService';
|
|||
*/
|
||||
export function nextGame_SocketListener(fastify: FastifyInstance, socket: Socket) {
|
||||
socket.on('nextGame', () => {
|
||||
const link = createNextGame();
|
||||
const link: string = createNextGame();
|
||||
const game: Promise<string> = sendGameLinkToChatService(link);
|
||||
broadcastNextGame(fastify, game);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue