From d33d31debf76a9328ecf7f7e992fc8b7b192e0cc Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:32:33 +0300 Subject: [PATCH] Got rid of userName and userAvatar in exchange of config.userName and config.userAvatar --- app.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index 3fa777f..18aceb7 100644 --- a/app.js +++ b/app.js @@ -14,8 +14,6 @@ const drive = new Hyperdrive(store); await drive.ready(); let swarm; -let userName = 'Anonymous'; -let userAvatar = ''; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; let peerCount = 0; let currentRoom = null; @@ -26,7 +24,7 @@ let servePort; // Object to store all the information we want to save let config = { - username: '', + userName: 'Anonymous', userAvatar: '', rooms: [] }; @@ -90,7 +88,7 @@ async function initialize() { const filePath = `/files/${file.name}`; await drive.put(filePath, buffer); const fileUrl = `http://localhost:${servePort}${filePath}`; - sendFileMessage(userName, fileUrl, file.type, userAvatar); + sendFileMessage(config.userName, fileUrl, file.type, config.userAvatar); }; reader.readAsArrayBuffer(file); } @@ -121,11 +119,11 @@ async function initialize() { console.log('Peer connected, current peer count:', peerCount); // Send the current user's icon to the new peer - const iconBuffer = await drive.get(`/icons/${userName}.png`); + const iconBuffer = await drive.get(`/icons/${config.userName}.png`); if (iconBuffer) { const iconMessage = JSON.stringify({ type: 'icon', - username: userName, + username: config.userName, avatar: iconBuffer.toString('base64'), }); connection.write(iconMessage); @@ -167,8 +165,8 @@ function registerUser(e) { reader.onload = async (event) => { const buffer = new Uint8Array(event.target.result); await drive.put(`/icons/${regUsername}.png`, buffer); - config.userAvatar = userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL - registeredUsers[regUsername] = userAvatar; + config.userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL + registeredUsers[regUsername] = config.userAvatar; localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers)); continueRegistration(regUsername); }; @@ -187,7 +185,7 @@ async function continueRegistration(regUsername) { return; } - config.username = userName = regUsername; + config.username = regUsername; setupDiv.classList.remove('hidden'); document.querySelector('#register').classList.add('hidden'); loadingDiv.classList.add('hidden'); @@ -272,13 +270,13 @@ function sendMessage(e) { const message = document.querySelector('#message').value; document.querySelector('#message').value = ''; - onMessageAdded(userName, message, userAvatar); + onMessageAdded(config.userName, message, config.userAvatar); const messageObj = JSON.stringify({ type: 'message', - name: userName, + name: config.userName, message, - avatar: userAvatar, + avatar: config.userAvatar, timestamp: Date.now(), });