getProfil() returns profil under an object form
This commit is contained in:
parent
e0610ca001
commit
74f96d6568
2 changed files with 85 additions and 65 deletions
|
|
@ -22,6 +22,18 @@ export type ClientMessage = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export type ClientProfil = {
|
||||||
|
command: string,
|
||||||
|
destination: string,
|
||||||
|
type: string,
|
||||||
|
user: string,
|
||||||
|
loginName: string,
|
||||||
|
userID: string,
|
||||||
|
text: string,
|
||||||
|
timestamp: number,
|
||||||
|
SenderWindowID:string,
|
||||||
|
};
|
||||||
|
|
||||||
// get the name of the machine used to connect
|
// get the name of the machine used to connect
|
||||||
const machineHostName = window.location.hostname;
|
const machineHostName = window.location.hostname;
|
||||||
console.log('connect to login at %chttps://' + machineHostName + ':8888/app/login',color.yellow);
|
console.log('connect to login at %chttps://' + machineHostName + ':8888/app/login',color.yellow);
|
||||||
|
|
@ -68,14 +80,28 @@ function isLoggedIn() {
|
||||||
return getUser() || null;
|
return getUser() || null;
|
||||||
};
|
};
|
||||||
|
|
||||||
function actionBtnPopUp() {
|
function inviteToPlayPong(profil: ClientProfil, senderSocket: Socket) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function actionBtnPopUp(profil: ClientProfil, senderSocket: Socket) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const clearTextBtn = document.querySelector("#popup-b-clear");
|
const clearTextBtn = document.querySelector("#popup-b-clear");
|
||||||
clearTextBtn?.addEventListener("click", () => {
|
clearTextBtn?.addEventListener("click", () => {
|
||||||
clearText();
|
clearText();
|
||||||
});
|
});
|
||||||
|
const InvitePongBtn = document.querySelector("#popup-b-invite");
|
||||||
|
InvitePongBtn?.addEventListener("click", () => {
|
||||||
|
inviteToPlayPong(profil, senderSocket);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}, 0)
|
}, 0)
|
||||||
}
|
};
|
||||||
|
|
||||||
// getProfil get the profil of user
|
// getProfil get the profil of user
|
||||||
function getProfil(socket: Socket, user: string) {
|
function getProfil(socket: Socket, user: string) {
|
||||||
|
|
@ -192,14 +218,6 @@ async function listBuddies(socket: Socket, buddies: HTMLDivElement, listBuddies:
|
||||||
buddiesElement.addEventListener("dblclick", () => {
|
buddiesElement.addEventListener("dblclick", () => {
|
||||||
console.log("Open profile:", listBuddies);
|
console.log("Open profile:", listBuddies);
|
||||||
getProfil(socket, listBuddies);
|
getProfil(socket, listBuddies);
|
||||||
// openProfilePopup(`${profile}`);
|
|
||||||
// setTimeout(() => {
|
|
||||||
// const clearTextBtn = document.querySelector("#popup-b-clear");
|
|
||||||
// clearTextBtn?.addEventListener("click", () => {
|
|
||||||
// clearText();
|
|
||||||
// });
|
|
||||||
// }, 0)
|
|
||||||
// actionBtnPopUp();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
buddies.appendChild(buddiesElement);
|
buddies.appendChild(buddiesElement);
|
||||||
|
|
@ -326,12 +344,23 @@ async function whoami(socket: Socket) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function openProfilePopup(profil: string) {
|
async function openProfilePopup(profil: ClientProfil) {
|
||||||
|
|
||||||
|
|
||||||
const modalname = document.getElementById("modal-name") ?? null;
|
const modalname = document.getElementById("modal-name") ?? null;
|
||||||
if (modalname)
|
if (modalname)
|
||||||
modalname.innerHTML = `${profil}`;
|
modalname.innerHTML = `
|
||||||
|
<div class="profile-info">
|
||||||
|
<div-profil-name id="profilName"> Profil of ${profil.user} </div>
|
||||||
|
<div-login-name id="loginName"> Login Name: '${profil.loginName ?? 'Guest'}' </div>
|
||||||
|
</br>
|
||||||
|
<div-login-name id="loginName"> Login ID: '${profil.userID ?? ''}' </div>
|
||||||
|
</br>
|
||||||
|
<button id="popup-b-clear" class="btn-style popup-b-clear">Clear Text</button>
|
||||||
|
<button id="popup-b-invite" class="btn-style popup-b-invite">Pong Us ?</button>
|
||||||
|
<div id="profile-about">About: '${profil.text}' </div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
const profilList = document.getElementById("profile-modal") ?? null;
|
const profilList = document.getElementById("profile-modal") ?? null;
|
||||||
if (profilList)
|
if (profilList)
|
||||||
profilList.classList.remove("hidden");
|
profilList.classList.remove("hidden");
|
||||||
|
|
@ -416,9 +445,9 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
||||||
console.log("Getuser():", getUser());
|
console.log("Getuser():", getUser());
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('profilMessage', (profil) => {
|
socket.on('profilMessage', (profil: ClientProfil) => {
|
||||||
openProfilePopup(profil);
|
openProfilePopup(profil);
|
||||||
actionBtnPopUp();
|
actionBtnPopUp(profil, socket);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -535,21 +564,6 @@ function handleChat(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
|
||||||
break;
|
break;
|
||||||
case '@profil':
|
case '@profil':
|
||||||
getProfil(socket, msgCommand[1]);
|
getProfil(socket, msgCommand[1]);
|
||||||
// Ensure we have a user AND socket is connected
|
|
||||||
// if (!socket.connected) return;
|
|
||||||
// const profil = {
|
|
||||||
// command: msgCommand[0],
|
|
||||||
// destination: 'profil',
|
|
||||||
// type: "chat",
|
|
||||||
// user: msgCommand[1],
|
|
||||||
// token: document.cookie ?? "",
|
|
||||||
// text: msgCommand[1],
|
|
||||||
// timestamp: Date.now(),
|
|
||||||
// SenderWindowID: socket.id,
|
|
||||||
// };
|
|
||||||
// //socket.emit('MsgObjectServer', message);
|
|
||||||
// addMessage(JSON.stringify(profil));
|
|
||||||
// socket.emit('profilMessage', JSON.stringify(profil));
|
|
||||||
break;
|
break;
|
||||||
case '@cls':
|
case '@cls':
|
||||||
chatWindow.innerHTML = '';
|
chatWindow.innerHTML = '';
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,19 @@ export type ClientMessage = {
|
||||||
SenderWindowID: string;
|
SenderWindowID: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export type ClientProfil = {
|
||||||
|
command?: string,
|
||||||
|
destination?: string,
|
||||||
|
type?: string,
|
||||||
|
user?: string,
|
||||||
|
loginName?: string,
|
||||||
|
userID?: string,
|
||||||
|
text?: string,
|
||||||
|
timestamp?: number,
|
||||||
|
SenderWindowID?:string,
|
||||||
|
};
|
||||||
|
|
||||||
const clientChat = new Map<string, ClientInfo>();
|
const clientChat = new Map<string, ClientInfo>();
|
||||||
|
|
||||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||||
|
|
@ -85,7 +98,7 @@ declare module 'fastify' {
|
||||||
hello: (message: string) => string;
|
hello: (message: string) => string;
|
||||||
MsgObjectServer: (data: { message: ClientMessage }) => void;
|
MsgObjectServer: (data: { message: ClientMessage }) => void;
|
||||||
privMessage: (data: string) => void;
|
privMessage: (data: string) => void;
|
||||||
profilMessage: (data: string) => void;
|
profilMessage: (data: ClientProfil) => void;
|
||||||
privMessageCopy: (msg: string) => void;
|
privMessageCopy: (msg: string) => void;
|
||||||
message: (msg: string) => void;
|
message: (msg: string) => void;
|
||||||
listBud: (msg: string) => void;
|
listBud: (msg: string) => void;
|
||||||
|
|
@ -185,39 +198,42 @@ async function onReady(fastify: FastifyInstance) {
|
||||||
return users.find(u => u.name === name) || null;
|
return users.find(u => u.name === name) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAboutPlayer( ): string{
|
||||||
|
const description = 'Player is good Shape this needs to be replaced by a bd.function()';
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
// this function returns html the profil pop up in CHAT of a user 'nickname unique' TODO ....
|
// this function returns html the profil pop up in CHAT of a user 'nickname unique' TODO ....
|
||||||
async function getProfil(user: string): Promise <string> {
|
async function getProfil(user: string, socket: Socket): Promise <ClientProfil> {
|
||||||
let profilHtmlPopup = '404: Error: Profil not found';
|
|
||||||
|
let clientProfil: ClientProfil = {};
|
||||||
const users: User[] = fastify.db.getAllUsers() ?? [];
|
const users: User[] = fastify.db.getAllUsers() ?? [];
|
||||||
const allUsers: User | null = getUserByName(users, user);
|
const allUsers: User | null = getUserByName(users, user);
|
||||||
console.log(color.yellow, `'userFound is:'${allUsers?.name}`);
|
console.log(color.yellow, `'userFound is:'${allUsers?.name}`);
|
||||||
if (user === allUsers?.name) {
|
if (user === allUsers?.name) {
|
||||||
console.log(color.yellow, `'login Name: '${allUsers.login}' user: '${user}'`);
|
console.log(color.yellow, `'login Name: '${allUsers.login}' user: '${user}'`);
|
||||||
profilHtmlPopup = `<div class="profile-info">
|
|
||||||
<div-profil-name id="profilName"> Profil of ${allUsers.name} </div>
|
|
||||||
<div-login-name id="loginName"> Login Name: '${allUsers?.login ?? 'Guest'}' </div>
|
|
||||||
</br>
|
|
||||||
<button id="popup-b-clear" class="btn-style popup-b-clear">Clear Text</button>
|
|
||||||
<button id="popup-b-invite" class="btn-style popup-b-invite">Pong Us ?</button>
|
|
||||||
<div id="profile-about">About: No description</div>
|
|
||||||
</div>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return profilHtmlPopup;
|
clientProfil = {
|
||||||
|
command: 'getProfil',
|
||||||
|
destination: 'profilMsg',
|
||||||
|
type: 'chat' as const,
|
||||||
|
user: `${allUsers.name}`,
|
||||||
|
loginName: `${allUsers?.login ?? 'Guest'}`,
|
||||||
|
userID: `${allUsers?.id ?? ''}`,
|
||||||
|
text: getAboutPlayer(),
|
||||||
|
timestamp: Date.now(),
|
||||||
|
SenderWindowID: socket.id,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return clientProfil;
|
||||||
};
|
};
|
||||||
|
|
||||||
function sendProfil(data: ClientMessage, clientProfil?: string) {
|
function sendProfil(data: ClientProfil, clientProfil?: string) {
|
||||||
|
|
||||||
fastify.io.fetchSockets().then((sockets) => {
|
fastify.io.fetchSockets().then((sockets) => {
|
||||||
const senderSocket = sockets.find(socket => socket.id === clientProfil);
|
const senderSocket = sockets.find(socket => socket.id === clientProfil);
|
||||||
for (const socket of sockets) {
|
if (senderSocket) {
|
||||||
const clientInfo = clientChat.get(socket.id);
|
console.log(color.yellow, 'user inFO:', data.user);
|
||||||
if (clientInfo?.user === data.user) {
|
senderSocket.emit('profilMessage', data);
|
||||||
if (senderSocket) {
|
|
||||||
socket.emit('profilMessage', `${data.text}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -436,22 +452,12 @@ async function onReady(fastify: FastifyInstance) {
|
||||||
`DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target profil:`,
|
`DEBUG LOG: ClientName: '${clientName}' id Socket: '${socket.id}' target profil:`,
|
||||||
profilMessage.user,
|
profilMessage.user,
|
||||||
);
|
);
|
||||||
const profileHtml: string = await getProfil(profilMessage.user);
|
const profileHtml: ClientProfil = await getProfil(profilMessage.user, socket);
|
||||||
if (clientName !== null) {
|
if (clientName !== null) {
|
||||||
const testuser: User | null = getUserByName(users, profilMessage.user);
|
const testuser: User | null = getUserByName(users, profilMessage.user);
|
||||||
console.log(color.yellow, 'user:', testuser?.login ?? 'Guest');
|
console.log(color.yellow, 'user:', testuser?.name ?? 'Guest');
|
||||||
const obj = {
|
console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', profileHtml.SenderWindowID);
|
||||||
command: profilMessage.command,
|
sendProfil(profileHtml, profileHtml.SenderWindowID);
|
||||||
destination: 'profilMsg',
|
|
||||||
type: 'chat',
|
|
||||||
user: clientName,
|
|
||||||
token: '',
|
|
||||||
text: profileHtml,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
SenderWindowID: socket.id,
|
|
||||||
};
|
|
||||||
console.log(color.blue, 'DEBUG - profil message MESSAGE OUT :', obj.SenderWindowID);
|
|
||||||
sendProfil(obj, obj.SenderWindowID);
|
|
||||||
// clientChat.delete(obj.user);
|
// clientChat.delete(obj.user);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue