From 2e422eee2ac743b74cfcb00761548b6f7218e1f4 Mon Sep 17 00:00:00 2001 From: NigeParis Date: Mon, 12 Jan 2026 19:17:37 +0100 Subject: [PATCH] Blockage u-Game from user --- src/chat/src/app.ts | 14 ++++- .../src/chatBackHelperFunctions/sendInvite.ts | 59 +++++-------------- .../sendPrivMessage.ts | 14 +++-- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/chat/src/app.ts b/src/chat/src/app.ts index 9fb4a75..1244cb5 100644 --- a/src/chat/src/app.ts +++ b/src/chat/src/app.ts @@ -174,6 +174,10 @@ 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("------------------------------------------------") + console.log("PRUV GAME") + console.log(data) + console.log("------------------------------------------------") if (clientName !== null) { const obj: ClientMessage = { command: prvMessage.command, @@ -183,7 +187,7 @@ async function onReady(fastify: FastifyInstance) { text: prvMessage.text, timestamp: Date.now(), SenderWindowID: socket.id, - }; + };`` sendPrivMessage(fastify, obj, obj.SenderWindowID); } }); @@ -217,12 +221,18 @@ async function onReady(fastify: FastifyInstance) { socket.on('inviteGame', async (data: string) => { const clientName: string = clientChat.get(socket.id)?.user || ''; const profilInvite: ClientProfil = JSON.parse(data) || ''; + console.log("------------------------------------------------") + console.log("INVITE GAME") + console.log(data) + console.log("------------------------------------------------") + const linkGame: PongGameId | undefined = await setGameLink(fastify, data); if (!linkGame) return; const link: string = `Click me`; const inviteHtml: string = 'invites you to a game ' + link; + if (clientName !== null) { - sendInvite(fastify, inviteHtml, profilInvite); + sendInvite(fastify, inviteHtml, profilInvite, socket.id); } }); diff --git a/src/chat/src/chatBackHelperFunctions/sendInvite.ts b/src/chat/src/chatBackHelperFunctions/sendInvite.ts index 02cc8eb..e2842ad 100644 --- a/src/chat/src/chatBackHelperFunctions/sendInvite.ts +++ b/src/chat/src/chatBackHelperFunctions/sendInvite.ts @@ -1,18 +1,8 @@ -import type { ClientMessage, ClientProfil } from '../chat_types'; +import type { ClientProfil } from '../chat_types'; import { clientChat } from '../app'; import { FastifyInstance } from 'fastify'; import { sendPrivMessage } from './sendPrivMessage'; -/** - * TODO - * function needed to transfer the game number -*/ - -function getGameNumber():string { - const gameNumber = '123456GameNum'; - return gameNumber; -} - /** * function looks for the user online in the chat * and sends emit to invite - format HTML to make clickable @@ -22,37 +12,20 @@ function getGameNumber():string { * @param profil */ -export async function sendInvite(fastify: FastifyInstance, innerHtml: string, profil: ClientProfil) { - const sockets = await fastify.io.fetchSockets(); - let targetSocket; - const senderSocket = sockets.find(socket => { - const clientInfo = clientChat.get(socket.id); +export async function sendInvite(fastify: FastifyInstance, innerHtml: string, profil: ClientProfil, senderSocketId: string) { + + const clientName: string = clientChat.get(senderSocketId)?.user || ''; + + sendPrivMessage(fastify, { + command: `@${profil.user}`, + destination: 'inviteMsg', + type: 'chat', + user: clientName, + userID: profil.userID, + SenderWindowID: senderSocketId, + SenderUserID: profil.SenderID, + SenderUserName: profil.SenderName, + innerHtml: innerHtml, + }, senderSocketId); - return clientInfo?.user === profil.SenderName - }); - for (const socket of sockets) { - const clientInfo: string | undefined = clientChat.get(socket.id)?.user || undefined; - targetSocket = socket || null; - if (!targetSocket) continue; - if (clientInfo === profil.user) { - profil.innerHtml = innerHtml ?? ''; - if (targetSocket.id) { - const data: ClientMessage = { - ...profil, - command: `@${clientInfo}`, - destination: 'inviteMsg', - type: 'chat', - user: profil.user, - text: getGameNumber(), - timestamp: Date.now(), - SenderWindowID: socket.id, - userID: profil.userID, - SenderUserName: profil.SenderName, - innerHtml: innerHtml, - }; - sendPrivMessage(fastify, data, senderSocket?.id); - } - return; - } - } } \ No newline at end of file diff --git a/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts b/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts index d062ea5..a53d53f 100644 --- a/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts +++ b/src/chat/src/chatBackHelperFunctions/sendPrivMessage.ts @@ -23,7 +23,9 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess const sockets = await fastify.io.fetchSockets(); const allUsers: User[] = fastify.db.getAllUsers() ?? []; + console.log('sender', sender); const senderSocket = sockets.find(socket => socket.id === sender); + console.log('senderSOcket', senderSocket?.id); for (const socket of sockets) { if (socket.id === sender) continue; @@ -44,16 +46,20 @@ export async function sendPrivMessage(fastify: FastifyInstance, data: ClientMess } blockMsgFlag = checkNamePair(list, UserID, receiverUser.id) || false; - + console.log("userID", UserID); + console.log("receiverUserID", receiverUser.id) if (!blockMsgFlag) { socket.emit('MsgObjectServer', { message: data }); fastify.log.info({ senderID: `${UserID}`, msgPriv: data.text, target: `${receiverUser.id}` }); if (senderSocket) { - if (!data.innerHtml) + if (!data.innerHtml) { + console.log('privMsg text'); senderSocket.emit('privMessageCopy', `${data.command}: ${data.text}🔒`); - else + } else { + console.log('privMsg texthtml'); senderSocket.emit('privMessageCopy', `${data.command}: ${data.innerHtml}🔒`); - + } + } } }