first message prototype

This commit is contained in:
Baipyrus 2023-01-18 14:35:17 +01:00
parent cd1de28b66
commit f227016148
2 changed files with 127 additions and 88 deletions

View File

@ -1,3 +1,76 @@
// Run input as command if possible
function runCommand(input) {
// Return on no input
if (input === "")
return "";
if (input.toLowerCase().startsWith("exit")) {
cmd_exit();
return;
}
const modeSplit = chatMode.split(' ');
switch (modeSplit[0]) {
case "msg":
directMessage(modeSplit[1], input)
return "";
// case "chat":
// // Global messages
// return "";
}
let output = "";
const lowerIn = input.toLowerCase();
// Go through properties of window
for (const func in window) {
const splits = func.split("cmd_");
// If property is prefixed with 'cmd_' (a 'command', so an executable function)
if (splits.length === 2) {
const name = splits[1];
const lowerNm = name.toLowerCase();
// If command is called without parameters
if (lowerIn === lowerNm) {
output = window[func]();
break;
// If command is called with parameters
} else if (lowerIn.startsWith(lowerNm + " ")) {
// Parameters always follow the command name after first space
const params = input.split(" ").filter((e,i)=>i!==0);
output = window[func](params);
break;
}
}
}
// Standard output:
if (output === "")
output = `${input.split(" ")[0]}: command not found`;
if (output === undefined)
output = "";
// Return command output
return output;
}
function directMessage(name, message) {
// Direct message
fetch('/message', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: user.id,
message,
name
})
}).then(res => {});
}
// function globalMessage(message) {}
function cmd_help() {
// Display 'help' message
return "Commands list:<br>" +
@ -53,7 +126,7 @@ function cmd_exec(input) {
if (chars.length > 0)
return "Invalid input!";
// Execute input and return output
return (new Function(`return ${str}`))().toString();
return eval(str).toString();
}
function cmd_echo(input) {
@ -77,17 +150,15 @@ function cmd_nick(input) {
id: user.id
})
}).then(res => {
if (res.status === 200) {
user.connected = true;
if (res.status === 200)
return res.json();
}
}).then(res => {
if (res === undefined)
return;
if (res)
if (res.success)
outputText({output: `Applied name '${user.name}'.`});
else {
outputText({output: `Name '${user.name}' is already taken!.`});
outputText({output: `Name '${user.name}' is already taken!`});
user.name = "";
}
});
@ -96,26 +167,53 @@ function cmd_nick(input) {
function cmd_msg(input) {
if (input === undefined)
return "No username given!";
return "No recipient was given!";
if (!user.connected)
return "You are not connected!";
if (user.name === "")
return "You do not have a name!";
const username = input[0];
pretext = `Chat (${username})> `;
pretext.current = `Chat (${username})> `;
chatMode = `msg ${username}`;
fetch('/chatInit', {
user.chatPull = setInterval(() => {
fetch('/getChat', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: username,
id: user.id
})
}).then(res => {});
}).then(res => {
if (res.status === 200)
return res.json();
else if (res.status === 100)
return;
cmd_exit();
outputText({output: "Error occurred while fetching messages!"});
}).then(res => {
if (res === undefined)
return;
console.log(res);
for (const user in res)
for (const msg of res[user])
outputText({output: msg});
});
}, 500);
}
function cmd_exit() {
if (chatMode.split(' ')[0] !== "default")
const level = chatMode.split(' ')[0];
if (level === "default")
return "Already at top-level!";
chatMode = "default";
pretext.current = pretext.original;
switch (level) {
// case "chat":
case "msg":
clearInterval(user.chatPull);
break;
}
}

View File

@ -24,13 +24,16 @@ fetch('/connect').then(res => {
const { id } = res;
user.id = id;
});
const user = { id: "", name: "", history: [], connected: false };
const user = { id: "", name: "", history: [], connected: false, chatPull: null };
let chatMode = "default";
let pretext = "root@baipyr.us:~# ";
let pretext = {
original: "root@baipyr.us:~# ",
current: "root@baipyr.us:~# "
};
// Initiating variables
const length = pretext.length+1;
const length = pretext.current.length+1;
let cursorPosition = 0;
let cursorYOffset = 7;
let history = { index: 0, list: [] };
@ -173,7 +176,7 @@ function outputText(params) {
}
// Add new pretext to terminal
if (commandOutput !== null)
tbDiv.innerHTML += pretext;
tbDiv.innerHTML += pretext.current;
// Add elements on top of div
tbDiv.appendChild(textCur);
tbDiv.appendChild(textIn);
@ -197,72 +200,10 @@ function createText(str, safe=true) {
return link;
}
// Run input as command if possible
function runCommand(input) {
// Return on no input
if (input === "")
return "";
const modeSplit = chatMode.split(' ');
switch (modeSplit[0]) {
case "msg":
// Direct message
fetch('/message', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: user.id,
message: input,
name: modeSplit[1]
})
}).then(res => {});
return "";
// case "chat":
// // Global messages
// break;
}
let output = "";
const lowerIn = input.toLowerCase();
// Go through properties of window
for (const func in window) {
const splits = func.split("cmd_");
// If property is prefixed with 'cmd_' (a 'command', so an executable function)
if (splits.length === 2) {
const name = splits[1];
const lowerNm = name.toLowerCase();
// If command is called without parameters
if (lowerIn === lowerNm) {
output = window[func]();
break;
// If command is called with parameters
} else if (lowerIn.startsWith(lowerNm + " ")) {
// Parameters always follow the command name after first space
const params = input.split(" ").filter((e,i)=>i!==0);
output = window[func](params);
break;
}
}
}
// Standard output:
if (output === "")
output = `${input.split(" ")[0]}: command not found`;
if (output === undefined)
output = "";
// Return command output
return output;
}
// Update cursor position based on coordinates
function updateCursor() {
let cursor = document.getElementById("cursor");
cursor.style.transform = `translate(${pretext.length+cursorPosition}ch, ${2.222*cursorYOffset-0.4}ch)`;
cursor.style.transform = `translate(${pretext.current.length+cursorPosition}ch, ${2.222*cursorYOffset-0.4}ch)`;
}
// Toggle cursor visibility