initial group chat functionality

This commit is contained in:
Baipyrus 2023-02-03 13:57:29 +01:00
parent d2be753513
commit e0b0df1fcc
2 changed files with 58 additions and 9 deletions

View File

@ -23,9 +23,10 @@ function runCommand(input) {
directMessage(modeSplit[1].split(','), input); directMessage(modeSplit[1].split(','), input);
renameToSelf(); renameToSelf();
return ""; return "";
// case "chat": case "chat":
// renameToSelf(); groupMessage(modeSplit[1], input);
// return ""; renameToSelf();
return "";
} }
// Default chat mode: // Default chat mode:
@ -164,22 +165,33 @@ function cmd_nick(input) {
return null; return null;
} }
// Initialize direct chat // Check whether user is allowed to chat
function cmd_msg(input) { function canChatCheck(input) {
if (input === undefined) if (input === undefined)
return "No recipient was given!"; return "No recipient was given!";
if (!window.localStorage.getItem("connected")) if (!window.localStorage.getItem("connected"))
return "You are not connected! Use the 'nick' command to connect using your username."; return "You are not connected! Use the 'nick' command to connect using your username.";
if (window.localStorage.getItem("name") === "") if (window.localStorage.getItem("name") === "")
return "You do not have a name!"; return "You do not have a name!";
return "";
}
// Initialize messaging
function messagingIniti(mode, name) {
// Get recipient username without spaces and pretext // Get recipient username without spaces and pretext
const username = input[0]; pretext.current = `Chat (${name})> `;
pretext.current = `Chat (${username})> `;
// Set chat mode to direct messages and start pulling // Set chat mode to direct messages and start pulling
chatMode = `msg ${username}`; chatMode = `${mode} ${name}`;
userData.chatPull = setInterval(getChatMessages, 500); userData.chatPull = setInterval(getChatMessages, 500);
} }
// Initialize direct chat
function cmd_msg(input) {
let output = canChatCheck(input);
if (output !== "") return output;
messagingIniti('msg', input[0]);
}
// Exit current level (example: chat -> main) // Exit current level (example: chat -> main)
function cmd_exit(error) { function cmd_exit(error) {
const level = chatMode.split(' ')[0]; const level = chatMode.split(' ')[0];
@ -224,4 +236,12 @@ function cmd_logout() {
if (!window.localStorage.getItem("connected")) if (!window.localStorage.getItem("connected"))
return "You are not even connected yet!"; return "You are not even connected yet!";
disconnect(); disconnect();
}
// Initialize group chat
function cmd_chat(input) {
let output = canChatCheck(input);
if (output !== "") return output;
messagingIniti('chat', input[0]);
sendChatInit(input[0]);
} }

View File

@ -193,7 +193,21 @@ function directMessage(names, message) {
}).then(res => {}); }).then(res => {});
} }
// function globalMessage(message) {} // Send a group message
function groupMessage(chat, message) {
fetch('/sendGroup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id"),
message,
chat
})
}).then(res => {});
}
// Request all usernames and number of anonymous users // Request all usernames and number of anonymous users
function requestUsernames() { function requestUsernames() {
@ -230,4 +244,19 @@ function requestPing() {
const output = `Host responded after ${diff}ms.`; const output = `Host responded after ${diff}ms.`;
outputText({output}) outputText({output})
}); });
}
// Initialize group chat
function sendChatInit(chat) {
fetch('/chatInit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id"),
chat
})
}).then(res => {});
} }