changed from uuid + username to username + '#' + id

This commit is contained in:
Baipyrus 2023-01-20 08:13:35 +01:00
parent b5bb18fe7f
commit cd75deef30
3 changed files with 30 additions and 23 deletions

View File

@ -129,9 +129,10 @@ function cmd_echo(input) {
function cmd_nick(input) {
if (input === undefined)
return "No nickname was given!";
if (!user.connected)
return "You are not connected!";
if (user.connected)
sendNickname(input[0]);
else
connect(input[0]);
return null;
}
@ -179,11 +180,5 @@ function cmd_ll() {
if (!user.connected)
return "You are not connected!";
requestUsernames();
}
// Connect to server
function cmd_connect() {
if (user.connected)
return "You are already connected!";
connect();
return null;
}

View File

@ -188,7 +188,7 @@ function createText(str, safe=true) {
// Rename last-posted pretext to own username
function renameToSelf() {
const tbc = tbDiv.children;
tbc[tbc.length - 3].innerHTML = `${user.name}: `;
tbc[tbc.length - 3].innerHTML = `${user.name}#${user.id}: `;
}
// Update cursor position based on coordinates

View File

@ -13,14 +13,23 @@ document.body.onunload = document.body.onbeforeunload = () => {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: user.name,
id: user.id
})
}).then(res => {});
};
// Connect to server
function connect() {
fetch('/connect').then(res => {
function connect(name) {
user.name = name;
fetch('/connect', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ name })
}).then(res => {
if (res.status === 200) {
user.connected = true;
return res.json();
@ -31,11 +40,11 @@ function connect() {
if (res === undefined)
return;
// Save provided id
const {id} = res;
const { id } = res;
user.id = id;
outputText({output: `Connected as '${name}#${id}'.`});
});
}
connect();
// Send nickname to server, receive verification
@ -47,6 +56,7 @@ function sendNickname(name) {
'Content-Type': 'application/json'
},
body: JSON.stringify({
oldName: user.name,
id: user.id,
name
})
@ -58,10 +68,11 @@ function sendNickname(name) {
if (res === undefined)
return;
// Receive name and save it
const { name } = res;
const { id } = res;
user.name = name;
user.id = id;
// Reply whether name is taken or not
outputText({output: `Applied name '${name}'.`});
outputText({output: `Applied name '${name}#${id}'.`});
});
}
@ -74,6 +85,7 @@ function getChatMessages() {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: user.name,
id: user.id
})
}).then(res => {
@ -107,9 +119,10 @@ function directMessage(name, message) {
'Content-Type': 'application/json'
},
body: JSON.stringify({
recipient: name,
name: user.name,
id: user.id,
message,
name
message
})
}).then(res => {});
}
@ -129,10 +142,9 @@ function requestUsernames() {
}).then(res => {
if (res === undefined)
return;
let output = "";
for (const u of res)
outputText({
preNext: `${u.id}: `,
output: u.name
});
output += `${u.name}#${u.id} `;
outputText({ output });
});
}