connect and list usernames methods and more comments
This commit is contained in:
parent
71d8f13d08
commit
b5bb18fe7f
@ -57,8 +57,8 @@ function runCommand(input) {
|
||||
return output;
|
||||
}
|
||||
|
||||
function cmd_help() {
|
||||
// Display 'help' message
|
||||
function cmd_help() {
|
||||
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";
|
||||
}
|
||||
|
||||
function cmd_about() {
|
||||
// Display 'about' message
|
||||
function cmd_about() {
|
||||
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.";
|
||||
}
|
||||
|
||||
function cmd_clear() {
|
||||
// Clear terminal by reloading page
|
||||
function cmd_clear() {
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
function cmd_history() {
|
||||
// Display the command history line by line
|
||||
function cmd_history() {
|
||||
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();
|
||||
}
|
@ -19,6 +19,7 @@ document.body.onunload = document.body.onbeforeunload = () => {
|
||||
};
|
||||
|
||||
// Connect to server
|
||||
function connect() {
|
||||
fetch('/connect').then(res => {
|
||||
if (res.status === 200) {
|
||||
user.connected = true;
|
||||
@ -33,10 +34,12 @@ fetch('/connect').then(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
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user