From 44db9f844c2b010b9d879c33b9c76d1458e3424e Mon Sep 17 00:00:00 2001 From: Julius Date: Fri, 17 May 2019 14:58:48 +0200 Subject: [PATCH] more bingo commands - added /username bingo command --- public/javascripts/bingo-web.js | 86 +++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/public/javascripts/bingo-web.js b/public/javascripts/bingo-web.js index a4b464d..6f66821 100644 --- a/public/javascripts/bingo-web.js +++ b/public/javascripts/bingo-web.js @@ -33,7 +33,20 @@ async function submitUsername() { let username = unameInput.value.replace(/^\s+|\s+$/g, ''); if (username.length > 1) { - let response = await postGraphqlQuery(` + await setUsername(username); + } else { + showError('You need to provide a username (minimum 2 characters)!'); + return false; + } +} + +/** + * Sets the username for a user + * @param username {String} - the username + * @returns {Promise} + */ +async function setUsername(username) { + let response = await postGraphqlQuery(` mutation($username:String!) { bingo { setUsername(username: $username) { @@ -42,15 +55,11 @@ async function submitUsername() { } } }`, {username: username}); - if (response.status === 200) { - return true; - } else { - showError(`Failed to submit username. HTTP Error: ${response.status}`); - console.error(response); - return false; - } + if (response.status === 200) { + return true; } else { - showError('You need to provide a username (minimum 2 characters)!'); + showError(`Failed to submit username. HTTP Error: ${response.status}`); + console.error(response); return false; } } @@ -158,36 +167,49 @@ async function executeCommand(message) { addChatMessage({content: content, htmlContent: content, type: 'INFO'}); } let jsStyle = document.querySelector('#js-style'); - message = message.replace(/\s/g, ''); - switch(message) { - case '/help': - reply(` - Commands:
+ message = message.replace(/\s+$/g, ''); + let command = /(\/\w+) ?(.*)?/g.exec(message); + if (command && command.length >= 2) { + switch(command[1]) { + case '/help': + reply(` +
Commands:
/help - shows this help
/hideinfo - hides all info messages
/showinfo - shows all info messages
/ping - shows the current ping
+ /username {Username} - sets the username

+ Admin commands:
/abortround - aborts the current round
`); - break; - case '/hideinfo': - jsStyle.innerHTML = '.chatMessage[msg-type="INFO"] {display: none}'; - break; - case '/showinfo': - jsStyle.innerHTML = '.chatMessage[msg-type="INFO"] {}'; - break; - case '/ping': - reply(`Ping: ${await ping()} ms`); - break; - case '/abortround': - reply(await setRoundFinished()); - break; - default: - reply('Unknown command'); - break; + break; + case '/hideinfo': + jsStyle.innerHTML = '.chatMessage[msg-type="INFO"] {display: none}'; + break; + case '/showinfo': + jsStyle.innerHTML = '.chatMessage[msg-type="INFO"] {}'; + break; + case '/ping': + reply(`Ping: ${await ping()} ms`); + break; + case '/abortround': + reply(await setRoundFinished()); + break; + case '/username': + if (command[2]) { + await setUsername(command[2]); + reply(`Your username is ${command[2]} now.`) + } else { + reply('You need to provide a username'); + } + break; + default: + reply('Unknown command'); + break; + } + let chatContent = document.querySelector('#chat-content'); + chatContent.scrollTop = chatContent.scrollHeight; } - let chatContent = document.querySelector('#chat-content'); - chatContent.scrollTop = chatContent.scrollHeight; } /**