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;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display 'help' message
|
||||||
function cmd_help() {
|
function cmd_help() {
|
||||||
// Display 'help' message
|
|
||||||
return "Commands list:<br>" +
|
return "Commands list:<br>" +
|
||||||
" -about Information about this website<br>"+
|
" -about Information about this website<br>"+
|
||||||
" -clear Clear terminal screen<br>" +
|
" -clear Clear terminal screen<br>" +
|
||||||
@ -68,19 +68,19 @@ function cmd_help() {
|
|||||||
" -msg Open a direct chat to the provided user by name";
|
" -msg Open a direct chat to the provided user by name";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display 'about' message
|
||||||
function cmd_about() {
|
function cmd_about() {
|
||||||
// Display 'about' message
|
|
||||||
return "This website is based on the general idea and design of a terminal.<br>" +
|
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.";
|
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear terminal by reloading page
|
||||||
function cmd_clear() {
|
function cmd_clear() {
|
||||||
// Clear terminal by reloading page
|
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display the command history line by line
|
||||||
function cmd_history() {
|
function cmd_history() {
|
||||||
// Display the command history line by line
|
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
const hl = history.list;
|
const hl = history.list;
|
||||||
@ -92,6 +92,7 @@ function cmd_history() {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute arbitrary math and logic equations
|
||||||
function cmd_exec(input) {
|
function cmd_exec(input) {
|
||||||
// No input was given
|
// No input was given
|
||||||
if (input === undefined)
|
if (input === undefined)
|
||||||
@ -128,8 +129,9 @@ function cmd_echo(input) {
|
|||||||
function cmd_nick(input) {
|
function cmd_nick(input) {
|
||||||
if (input === undefined)
|
if (input === undefined)
|
||||||
return "No nickname was given!";
|
return "No nickname was given!";
|
||||||
user.name = input[0];
|
if (!user.connected)
|
||||||
sendNickname();
|
return "You are not connected!";
|
||||||
|
sendNickname(input[0]);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,3 +173,17 @@ function cmd_exit(error) {
|
|||||||
if (error)
|
if (error)
|
||||||
outputText({output: 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,24 +19,27 @@ document.body.onunload = document.body.onbeforeunload = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Connect to server
|
// Connect to server
|
||||||
fetch('/connect').then(res => {
|
function connect() {
|
||||||
|
fetch('/connect').then(res => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
user.connected = true;
|
user.connected = true;
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
// No valid status code was sent
|
// No valid status code was sent
|
||||||
console.error("Could not connect to server!");
|
console.error("Could not connect to server!");
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res === undefined)
|
if (res === undefined)
|
||||||
return;
|
return;
|
||||||
// Save provided id
|
// Save provided id
|
||||||
const { id } = res;
|
const {id} = res;
|
||||||
user.id = id;
|
user.id = id;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
connect();
|
||||||
|
|
||||||
|
|
||||||
// Send nickname to server, receive verification
|
// Send nickname to server, receive verification
|
||||||
function sendNickname() {
|
function sendNickname(name) {
|
||||||
fetch('/nickname', {
|
fetch('/nickname', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -44,8 +47,8 @@ function sendNickname() {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: user.name,
|
id: user.id,
|
||||||
id: user.id
|
name
|
||||||
})
|
})
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res.status === 200)
|
if (res.status === 200)
|
||||||
@ -54,13 +57,11 @@ function sendNickname() {
|
|||||||
// No valid status code
|
// No valid status code
|
||||||
if (res === undefined)
|
if (res === undefined)
|
||||||
return;
|
return;
|
||||||
|
// Receive name and save it
|
||||||
|
const { name } = res;
|
||||||
|
user.name = name;
|
||||||
// Reply whether name is taken or not
|
// Reply whether name is taken or not
|
||||||
if (res.success)
|
outputText({output: `Applied name '${name}'.`});
|
||||||
outputText({output: `Applied name '${user.name}'.`});
|
|
||||||
else {
|
|
||||||
outputText({output: `Name '${user.name}' is already taken!`});
|
|
||||||
user.name = "";
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +100,6 @@ function getChatMessages() {
|
|||||||
|
|
||||||
// Send a direct message, do not look at response
|
// Send a direct message, do not look at response
|
||||||
function directMessage(name, message) {
|
function directMessage(name, message) {
|
||||||
// Direct message
|
|
||||||
fetch('/message', {
|
fetch('/message', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -115,3 +115,24 @@ function directMessage(name, message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// function globalMessage(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