diff --git a/js/commands.js b/js/commands.js index a04a0d6..b3a6568 100644 --- a/js/commands.js +++ b/js/commands.js @@ -23,9 +23,10 @@ function runCommand(input) { directMessage(modeSplit[1].split(','), input); renameToSelf(); return ""; - // case "chat": - // renameToSelf(); - // return ""; + case "chat": + groupMessage(modeSplit[1], input); + renameToSelf(); + return ""; } // Default chat mode: @@ -164,22 +165,33 @@ function cmd_nick(input) { return null; } -// Initialize direct chat -function cmd_msg(input) { +// Check whether user is allowed to chat +function canChatCheck(input) { if (input === undefined) return "No recipient was given!"; if (!window.localStorage.getItem("connected")) return "You are not connected! Use the 'nick' command to connect using your username."; if (window.localStorage.getItem("name") === "") return "You do not have a name!"; + return ""; +} + +// Initialize messaging +function messagingIniti(mode, name) { // Get recipient username without spaces and pretext - const username = input[0]; - pretext.current = `Chat (${username})> `; + pretext.current = `Chat (${name})> `; // Set chat mode to direct messages and start pulling - chatMode = `msg ${username}`; + chatMode = `${mode} ${name}`; 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) function cmd_exit(error) { const level = chatMode.split(' ')[0]; @@ -224,4 +236,12 @@ function cmd_logout() { if (!window.localStorage.getItem("connected")) return "You are not even connected yet!"; disconnect(); +} + +// Initialize group chat +function cmd_chat(input) { + let output = canChatCheck(input); + if (output !== "") return output; + messagingIniti('chat', input[0]); + sendChatInit(input[0]); } \ No newline at end of file diff --git a/js/networking.js b/js/networking.js index c2da41e..d89465c 100644 --- a/js/networking.js +++ b/js/networking.js @@ -193,7 +193,21 @@ function directMessage(names, message) { }).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 function requestUsernames() { @@ -230,4 +244,19 @@ function requestPing() { const output = `Host responded after ${diff}ms.`; 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 => {}); } \ No newline at end of file