Blockage u-Game from user

This commit is contained in:
NigeParis 2026-01-12 19:17:37 +01:00 committed by Maix0
parent 432fd849b8
commit 2e422eee2a
3 changed files with 38 additions and 49 deletions

View file

@ -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 = `<a href="https://localhost:8888/app/pong?game=${linkGame}" style="color: blue; text-decoration: underline; cursor: pointer;">Click me</a>`;
const inviteHtml: string = 'invites you to a game ' + link;
if (clientName !== null) {
sendInvite(fastify, inviteHtml, profilInvite);
sendInvite(fastify, inviteHtml, profilInvite, socket.id);
}
});

View file

@ -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;
}
}
}

View file

@ -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,15 +46,19 @@ 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}🔒`);
}
}
}