connect and list usernames methods and more comments

This commit is contained in:
Baipyrus 2023-01-19 15:31:13 +01:00
parent 71d8f13d08
commit b5bb18fe7f
2 changed files with 68 additions and 31 deletions

View File

@ -57,8 +57,8 @@ function runCommand(input) {
return output;
}
// Display 'help' message
function cmd_help() {
// Display 'help' message
return "Commands list:<br>" +
" -about Information about this website<br>"+
" -clear Clear terminal screen<br>" +
@ -68,19 +68,19 @@ function cmd_help() {
" -msg Open a direct chat to the provided user by name";
}
// Display 'about' message
function cmd_about() {
// Display 'about' message
return "This website is based on the general idea and design of a terminal.<br>" +
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
}
// Clear terminal by reloading page
function cmd_clear() {
// Clear terminal by reloading page
window.location.reload();
}
// Display the command history line by line
function cmd_history() {
// Display the command history line by line
let output = "";
const hl = history.list;
@ -92,6 +92,7 @@ function cmd_history() {
return output;
}
// Execute arbitrary math and logic equations
function cmd_exec(input) {
// No input was given
if (input === undefined)
@ -128,8 +129,9 @@ function cmd_echo(input) {
function cmd_nick(input) {
if (input === undefined)
return "No nickname was given!";
user.name = input[0];
sendNickname();
if (!user.connected)
return "You are not connected!";
sendNickname(input[0]);
return null;
}
@ -171,3 +173,17 @@ function cmd_exit(error) {
if (error)
outputText({output: error});
}
// List all users to be able to chat with
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();
}

View File

@ -19,24 +19,27 @@ document.body.onunload = document.body.onbeforeunload = () => {
};
// Connect to server
fetch('/connect').then(res => {
function connect() {
fetch('/connect').then(res => {
if (res.status === 200) {
user.connected = true;
return res.json();
}
// No valid status code was sent
console.error("Could not connect to server!");
}).then(res => {
}).then(res => {
if (res === undefined)
return;
// Save provided id
const { id } = res;
const {id} = res;
user.id = id;
});
});
}
connect();
// Send nickname to server, receive verification
function sendNickname() {
function sendNickname(name) {
fetch('/nickname', {
method: 'POST',
headers: {
@ -44,8 +47,8 @@ function sendNickname() {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: user.name,
id: user.id
id: user.id,
name
})
}).then(res => {
if (res.status === 200)
@ -54,13 +57,11 @@ function sendNickname() {
// No valid status code
if (res === undefined)
return;
// Receive name and save it
const { name } = res;
user.name = name;
// Reply whether name is taken or not
if (res.success)
outputText({output: `Applied name '${user.name}'.`});
else {
outputText({output: `Name '${user.name}' is already taken!`});
user.name = "";
}
outputText({output: `Applied name '${name}'.`});
});
}
@ -99,7 +100,6 @@ function getChatMessages() {
// Send a direct message, do not look at response
function directMessage(name, message) {
// Direct message
fetch('/message', {
method: 'POST',
headers: {
@ -115,3 +115,24 @@ function directMessage(name, message) {
}
// function globalMessage(message) {}
// Request all usernames and number of anonymous users
function requestUsernames() {
fetch('/getNames', {
method: 'GET',
headers: {
'Accept': 'application/json',
}
}).then(res => {
if (res.status === 200)
return res.json();
}).then(res => {
if (res === undefined)
return;
for (const u of res)
outputText({
preNext: `${u.id}: `,
output: u.name
});
});
}