|
|
@ -1,66 +1,13 @@
|
|
|
|
/* eslint-disable no-unused-vars, no-undef */
|
|
|
|
/* eslint-disable no-unused-vars, no-undef */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
class BingoGraphqlHelper {
|
|
|
|
* Returns the value of the url-param 'g'
|
|
|
|
|
|
|
|
* @returns {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getLobbyParam() {
|
|
|
|
|
|
|
|
let matches = window.location.href.match(/\??&?g=(\d+)/);
|
|
|
|
|
|
|
|
if (matches)
|
|
|
|
|
|
|
|
return matches[1];
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* REturns the value of the r url param
|
|
|
|
|
|
|
|
* @returns {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getRoundParam() {
|
|
|
|
|
|
|
|
let matches = window.location.href.match(/\??&?r=(\d+)/);
|
|
|
|
|
|
|
|
if (matches)
|
|
|
|
|
|
|
|
return matches[1];
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Spawns a notification when the window is inactive (hidden).
|
|
|
|
|
|
|
|
* @param body
|
|
|
|
|
|
|
|
* @param title
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function spawnNotification(body, title) {
|
|
|
|
|
|
|
|
if (Notification.permission !== 'denied' && document[getHiddenNames().hidden]) {
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
|
|
|
|
body: body,
|
|
|
|
|
|
|
|
icon: '/favicon.ico'
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let n = new Notification(title, options);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Submits the value of the username-input to set the username.
|
|
|
|
|
|
|
|
* @returns {Promise<Boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function submitUsername() {
|
|
|
|
|
|
|
|
let unameInput = document.querySelector('#input-username');
|
|
|
|
|
|
|
|
let username = unameInput.value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (username.length > 1 && username.length <= 30) {
|
|
|
|
|
|
|
|
return await setUsername(username);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('You need to provide a username (min. 2 characters, max. 30)!');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Sets the username for a user
|
|
|
|
* Sets the username for a user
|
|
|
|
* @param username {String} - the username
|
|
|
|
* @param username {String} - the username
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function setUsername(username) {
|
|
|
|
static async setUsername(username) {
|
|
|
|
username = username.replace(/^\s+|\s+$/g, '');
|
|
|
|
username = username.replace(/^\s+|\s+$/g, '');
|
|
|
|
let uname = username.replace(/[\n\t👑🌟]|^\s+|\s+$/gu, '');
|
|
|
|
let uname = username.replace(/[\n\t👑🌟]|^\s+|\s+$/gu, '');
|
|
|
|
if (uname.length === username.length) {
|
|
|
|
if (uname.length === username.length) {
|
|
|
@ -80,6 +27,7 @@ async function setUsername(username) {
|
|
|
|
showError(response.errors[0].message);
|
|
|
|
showError(response.errors[0].message);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
showError(`Failed to submit username.`);
|
|
|
|
showError(`Failed to submit username.`);
|
|
|
|
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
console.error(response);
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -89,38 +37,10 @@ async function setUsername(username) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Function that displays the ping in the console.
|
|
|
|
* Creates a lobby via the graphql endpoint
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function ping() {
|
|
|
|
|
|
|
|
let start = new Date().getTime();
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query {
|
|
|
|
|
|
|
|
time
|
|
|
|
|
|
|
|
}`);
|
|
|
|
|
|
|
|
console.log(`Ping: ${(new Date().getTime()) - start} ms`);
|
|
|
|
|
|
|
|
return (new Date().getTime()) - start;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Joins a lobby or says to create one if none is found
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function joinLobby() {
|
|
|
|
|
|
|
|
if (getLobbyParam()) {
|
|
|
|
|
|
|
|
if (await submitUsername())
|
|
|
|
|
|
|
|
window.location.reload();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('No lobby found. Please create one.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Creates a lobby and redirects to the lobby.
|
|
|
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function createLobby() {
|
|
|
|
static async createLobby() {
|
|
|
|
if (await submitUsername()) {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
mutation {
|
|
|
|
mutation {
|
|
|
|
bingo {
|
|
|
|
bingo {
|
|
|
@ -139,13 +59,12 @@ async function createLobby() {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Lets the player leave the lobby
|
|
|
|
* Leaves a lobby via the graphql endpoint
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function leaveLobby() {
|
|
|
|
static async leaveLobby() {
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
mutation($lobbyId:ID!){
|
|
|
|
mutation($lobbyId:ID!){
|
|
|
|
bingo {
|
|
|
|
bingo {
|
|
|
@ -164,11 +83,11 @@ async function leaveLobby() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Kicks a player by id.
|
|
|
|
* Kicks a player
|
|
|
|
* @param pid
|
|
|
|
* @param pid
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function kickPlayer(pid) {
|
|
|
|
static async kickPlayer(pid) {
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
mutation ($lobbyId: ID!, $playerId:ID!) {
|
|
|
|
mutation ($lobbyId: ID!, $playerId:ID!) {
|
|
|
|
bingo {
|
|
|
|
bingo {
|
|
|
@ -189,6 +108,240 @@ async function kickPlayer(pid) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Loads information about the rounds winner and the round stats.
|
|
|
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static async loadWinnerInfo() {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query($lobbyId:ID!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
currentRound {
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
|
|
winner {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
start
|
|
|
|
|
|
|
|
finish
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let roundInfo = response.data.bingo.lobby.currentRound;
|
|
|
|
|
|
|
|
if (roundInfo.winner)
|
|
|
|
|
|
|
|
displayWinner(roundInfo);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
window.location.reload();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
showError('Failed to get round information');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Loads the lobby wors in the words element via graphql.
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static async loadLobbyWords() {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query($lobbyId:ID!){
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
words {
|
|
|
|
|
|
|
|
content
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let wordContainer = document.querySelector('#bingo-words');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (wordContainer)
|
|
|
|
|
|
|
|
wordContainer.innerHTML = `<span class="bingoWord">
|
|
|
|
|
|
|
|
${response.data.bingo.lobby.words.map(x => x.content).join('</span><span class="bingoWord">')}</span>`;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('Failed to load words.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Sets the settings of the lobby
|
|
|
|
|
|
|
|
* @param words
|
|
|
|
|
|
|
|
* @param gridSize
|
|
|
|
|
|
|
|
* @returns {Promise<LobbyWrapper.words|*|properties.words|{default, type}|boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static async setLobbySettings(words, gridSize) {
|
|
|
|
|
|
|
|
gridSize = Number(gridSize);
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
mutation ($lobbyId: ID!, $words: [String!]!, $gridSize:Int!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
mutateLobby(id: $lobbyId) {
|
|
|
|
|
|
|
|
setWords(words: $words) {
|
|
|
|
|
|
|
|
words {
|
|
|
|
|
|
|
|
content
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setGridSize(gridSize: $gridSize) {
|
|
|
|
|
|
|
|
gridSize
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`, {lobbyId: getLobbyParam(), words: words, gridSize: gridSize});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
return response.data.bingo.mutateLobby.setWords.words;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
if (response.errors)
|
|
|
|
|
|
|
|
showError(response.errors[0].message);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
showError('Error when submitting lobby settings.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Refreshes the bingo chat
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static async refreshChat() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query($lobbyId:ID!){
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
player {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
messages {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
htmlContent
|
|
|
|
|
|
|
|
content
|
|
|
|
|
|
|
|
author {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let messages = response.data.bingo.lobby.messages;
|
|
|
|
|
|
|
|
for (let message of messages)
|
|
|
|
|
|
|
|
if (!document.querySelector(`.chatMessage[msg-id="${message.id}"]`))
|
|
|
|
|
|
|
|
addChatMessage(message, response.data.bingo.player.id);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('Failed to refresh messages');
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
showError('Failed to refresh messages');
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns the value of the url-param 'g'
|
|
|
|
|
|
|
|
* @returns {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getLobbyParam() {
|
|
|
|
|
|
|
|
let matches = window.location.href.match(/\??&?g=(\d+)/);
|
|
|
|
|
|
|
|
if (matches)
|
|
|
|
|
|
|
|
return matches[1];
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Spawns a notification when the window is inactive (hidden).
|
|
|
|
|
|
|
|
* @param body
|
|
|
|
|
|
|
|
* @param title
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function spawnNotification(body, title) {
|
|
|
|
|
|
|
|
if (Notification.permission !== 'denied' && document[getHiddenNames().hidden]) {
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
|
|
|
|
body: body,
|
|
|
|
|
|
|
|
icon: '/favicon.ico'
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let n = new Notification(title, options);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Submits the value of the username-input to set the username.
|
|
|
|
|
|
|
|
* @returns {Promise<Boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function submitUsername() {
|
|
|
|
|
|
|
|
let unameInput = document.querySelector('#input-username');
|
|
|
|
|
|
|
|
let username = unameInput.value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (username.length > 1 && username.length <= 30) {
|
|
|
|
|
|
|
|
return await BingoGraphqlHelper.setUsername(username);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('You need to provide a username (min. 2 characters, max. 30)!');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Function that displays the ping in the console.
|
|
|
|
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function ping() {
|
|
|
|
|
|
|
|
let start = new Date().getTime();
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query {
|
|
|
|
|
|
|
|
time
|
|
|
|
|
|
|
|
}`);
|
|
|
|
|
|
|
|
console.log(`Ping: ${(new Date().getTime()) - start} ms`);
|
|
|
|
|
|
|
|
return (new Date().getTime()) - start;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Joins a lobby or says to create one if none is found
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function joinLobby() {
|
|
|
|
|
|
|
|
if (getLobbyParam()) {
|
|
|
|
|
|
|
|
if (await submitUsername())
|
|
|
|
|
|
|
|
window.location.reload();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('No lobby found. Please create one.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Creates a lobby and redirects to the lobby.
|
|
|
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function createLobby() {
|
|
|
|
|
|
|
|
if (await submitUsername())
|
|
|
|
|
|
|
|
await BingoGraphqlHelper.createLobby();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Lets the player leave the lobby
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function leaveLobby() {
|
|
|
|
|
|
|
|
await BingoGraphqlHelper.leaveLobby();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Kicks a player by id.
|
|
|
|
|
|
|
|
* @param pid
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function kickPlayer(pid) {
|
|
|
|
|
|
|
|
await BingoGraphqlHelper.kickPlayer(pid);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Executes a command
|
|
|
|
* Executes a command
|
|
|
|
* @param message {String} - the message
|
|
|
|
* @param message {String} - the message
|
|
|
@ -197,6 +350,7 @@ async function executeCommand(message) {
|
|
|
|
function reply(content) {
|
|
|
|
function reply(content) {
|
|
|
|
addChatMessage({content: content, htmlContent: content, type: 'INFO'});
|
|
|
|
addChatMessage({content: content, htmlContent: content, type: 'INFO'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let jsStyle = document.querySelector('#js-style');
|
|
|
|
let jsStyle = document.querySelector('#js-style');
|
|
|
|
message = message.replace(/\s+$/g, '');
|
|
|
|
message = message.replace(/\s+$/g, '');
|
|
|
|
let command = /(\/\w+) ?(.*)?/g.exec(message);
|
|
|
|
let command = /(\/\w+) ?(.*)?/g.exec(message);
|
|
|
@ -228,7 +382,7 @@ async function executeCommand(message) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case '/username':
|
|
|
|
case '/username':
|
|
|
|
if (command[2]) {
|
|
|
|
if (command[2]) {
|
|
|
|
let uname = await setUsername(command[2]);
|
|
|
|
let uname = await BingoGraphqlHelper.setUsername(command[2]);
|
|
|
|
reply(`Your username is <b>${uname}</b> now.`);
|
|
|
|
reply(`Your username is <b>${uname}</b> now.`);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
reply('You need to provide a username');
|
|
|
|
reply('You need to provide a username');
|
|
|
@ -252,67 +406,11 @@ async function sendChatMessage() {
|
|
|
|
if (messageInput.value && messageInput.value.length > 0) {
|
|
|
|
if (messageInput.value && messageInput.value.length > 0) {
|
|
|
|
let message = messageInput.value;
|
|
|
|
let message = messageInput.value;
|
|
|
|
messageInput.value = '';
|
|
|
|
messageInput.value = '';
|
|
|
|
if (/^\/\.*/g.test(message)) {
|
|
|
|
|
|
|
|
await executeCommand(message);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
mutation($lobbyId:ID!, $message:String!){
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
mutateLobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
sendMessage(message:$message) {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
htmlContent
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
author {
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {message: message, lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
addChatMessage(response.data.bingo.mutateLobby.sendMessage);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
messageInput.value = message;
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
showError('Error when sending message.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
if (/^\/\.*/g.test(message))
|
|
|
|
* Sets the words for the lobby
|
|
|
|
await executeCommand(message);
|
|
|
|
* @param words
|
|
|
|
|
|
|
|
* @param gridSize
|
|
|
|
|
|
|
|
* @returns {Promise<LobbyWrapper.words|*|properties.words|{default, type}|boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function setLobbySettings(words, gridSize) {
|
|
|
|
|
|
|
|
gridSize = Number(gridSize);
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
mutation ($lobbyId: ID!, $words: [String!]!, $gridSize:Int!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
mutateLobby(id: $lobbyId) {
|
|
|
|
|
|
|
|
setWords(words: $words) {
|
|
|
|
|
|
|
|
words {
|
|
|
|
|
|
|
|
content
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setGridSize(gridSize: $gridSize) {
|
|
|
|
|
|
|
|
gridSize
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`, {lobbyId: getLobbyParam(), words: words, gridSize: gridSize});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
return response.data.bingo.mutateLobby.setWords.words;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
if (response.errors)
|
|
|
|
|
|
|
|
showError(response.errors[0].message);
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
showError('Error when submitting lobby settings.');
|
|
|
|
socket.emit('message', message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -321,11 +419,13 @@ async function setLobbySettings(words, gridSize) {
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function startRound() {
|
|
|
|
async function startRound() {
|
|
|
|
|
|
|
|
let roundStart = document.querySelector('#button-round-start');
|
|
|
|
let textinput = document.querySelector('#input-bingo-words');
|
|
|
|
let textinput = document.querySelector('#input-bingo-words');
|
|
|
|
let words = getLobbyWords();
|
|
|
|
let words = getLobbyWords();
|
|
|
|
if (words.length > 0) {
|
|
|
|
if (words.length > 0) {
|
|
|
|
|
|
|
|
roundStart.setAttribute('class', 'pending');
|
|
|
|
let gridSize = document.querySelector('#input-grid-size').value || 3;
|
|
|
|
let gridSize = document.querySelector('#input-grid-size').value || 3;
|
|
|
|
let resultWords = await setLobbySettings(words, gridSize);
|
|
|
|
let resultWords = await BingoGraphqlHelper.setLobbySettings(words, gridSize);
|
|
|
|
if (resultWords) {
|
|
|
|
if (resultWords) {
|
|
|
|
textinput.value = resultWords.map(x => x.content).join('\n');
|
|
|
|
textinput.value = resultWords.map(x => x.content).join('\n');
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
@ -345,6 +445,7 @@ async function startRound() {
|
|
|
|
console.error(response);
|
|
|
|
console.error(response);
|
|
|
|
showError('Error when starting round.');
|
|
|
|
showError('Error when starting round.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
roundStart.setAttribute('class', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new Error('No words provided.');
|
|
|
|
throw new Error('No words provided.');
|
|
|
@ -372,31 +473,8 @@ async function submitFieldToggle(wordPanel) {
|
|
|
|
let column = Number(wordPanel.getAttribute('b-column'));
|
|
|
|
let column = Number(wordPanel.getAttribute('b-column'));
|
|
|
|
let wordClass = wordPanel.getAttribute('class');
|
|
|
|
let wordClass = wordPanel.getAttribute('class');
|
|
|
|
wordPanel.setAttribute('class', wordClass + ' pending');
|
|
|
|
wordPanel.setAttribute('class', wordClass + ' pending');
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
mutation($lobbyId:ID!, $row:Int!, $column:Int!){
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
mutateLobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
toggleGridField(location:{row:$row, column:$column}) {
|
|
|
|
|
|
|
|
submitted
|
|
|
|
|
|
|
|
grid {
|
|
|
|
|
|
|
|
bingo
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam(), row: row, column: column});
|
|
|
|
|
|
|
|
wordPanel.setAttribute('class', wordClass);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
socket.emit('fieldToggle', {row: row, column: column});
|
|
|
|
wordPanel.setAttribute('b-sub', response.data.bingo.mutateLobby.toggleGridField.submitted);
|
|
|
|
|
|
|
|
if (response.data.bingo.mutateLobby.toggleGridField.grid.bingo)
|
|
|
|
|
|
|
|
document.querySelector('#container-bingo-button').setAttribute('class', '');
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
document.querySelector('#container-bingo-button').setAttribute('class', 'hidden');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
showError('Error when submitting field toggle');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -425,7 +503,7 @@ async function setRoundFinished() {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Submits bingo
|
|
|
|
* Submits bingo
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function submitBingo() {
|
|
|
|
async function submitBingo() {
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
@ -446,8 +524,7 @@ async function submitBingo() {
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 200 && response.data.bingo.mutateLobby.submitBingo) {
|
|
|
|
if (response.status === 200 && response.data.bingo.mutateLobby.submitBingo) {
|
|
|
|
let round = response.data.bingo.mutateLobby.submitBingo;
|
|
|
|
return true;
|
|
|
|
displayWinner(round);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.error(response);
|
|
|
|
console.error(response);
|
|
|
|
showError('Failed to submit bingo');
|
|
|
|
showError('Failed to submit bingo');
|
|
|
@ -456,10 +533,10 @@ async function submitBingo() {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Displays the winner of the game in a popup.
|
|
|
|
* Displays the winner of the game in a popup.
|
|
|
|
* @param roundInfo {Object} - the round object as returned by graphql
|
|
|
|
* @param winner {Object} - the round object as returned by graphql
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function displayWinner(roundInfo) {
|
|
|
|
function displayWinner(winner) {
|
|
|
|
let name = roundInfo.winner.username;
|
|
|
|
let name = winner.username;
|
|
|
|
let winnerDiv = document.createElement('div');
|
|
|
|
let winnerDiv = document.createElement('div');
|
|
|
|
let greyoverDiv = document.createElement('div');
|
|
|
|
let greyoverDiv = document.createElement('div');
|
|
|
|
winnerDiv.setAttribute('class', 'popup');
|
|
|
|
winnerDiv.setAttribute('class', 'popup');
|
|
|
@ -506,43 +583,11 @@ async function statusWrap(func) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Loads information about the rounds winner and the round stats.
|
|
|
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function loadWinnerInfo() {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query($lobbyId:ID!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
currentRound {
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
|
|
winner {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
start
|
|
|
|
|
|
|
|
finish
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let roundInfo = response.data.bingo.lobby.currentRound;
|
|
|
|
|
|
|
|
if (roundInfo.winner)
|
|
|
|
|
|
|
|
displayWinner(roundInfo);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
window.location.reload();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
showError('Failed to get round information');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Adds a message to the chat
|
|
|
|
* Adds a message to the chat
|
|
|
|
* @param messageObject {Object} - the message object returned by graphql
|
|
|
|
* @param messageObject {Object} - the message object returned by graphql
|
|
|
|
* @param player {Number} - the id of the player
|
|
|
|
* @param [player] {Number} - the id of the player
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function addChatMessage(messageObject, player) {
|
|
|
|
function addChatMessage(messageObject, player) {
|
|
|
|
let msgSpan = document.createElement('span');
|
|
|
|
let msgSpan = document.createElement('span');
|
|
|
@ -558,8 +603,10 @@ function addChatMessage(messageObject, player) {
|
|
|
|
msgSpan.innerHTML = `
|
|
|
|
msgSpan.innerHTML = `
|
|
|
|
<span class="chatMessageContent ${messageObject.type}">${messageObject.htmlContent}</span>`;
|
|
|
|
<span class="chatMessageContent ${messageObject.type}">${messageObject.htmlContent}</span>`;
|
|
|
|
|
|
|
|
|
|
|
|
if (messageObject.author && messageObject.author.id !== player)
|
|
|
|
|
|
|
|
|
|
|
|
if (messageObject.type === 'USER' && messageObject.author && messageObject.author.id !== player)
|
|
|
|
spawnNotification(messageObject.content, messageObject.author.username);
|
|
|
|
spawnNotification(messageObject.content, messageObject.author.username);
|
|
|
|
|
|
|
|
|
|
|
|
let chatContent = document.querySelector('#chat-content');
|
|
|
|
let chatContent = document.querySelector('#chat-content');
|
|
|
|
chatContent.appendChild(msgSpan);
|
|
|
|
chatContent.appendChild(msgSpan);
|
|
|
|
chatContent.scrollTop = chatContent.scrollHeight; // auto-scroll to bottom
|
|
|
|
chatContent.scrollTop = chatContent.scrollHeight; // auto-scroll to bottom
|
|
|
@ -577,231 +624,133 @@ function addPlayer(player, options) {
|
|
|
|
|
|
|
|
|
|
|
|
if (options.isAdmin && player.id !== options.admin)
|
|
|
|
if (options.isAdmin && player.id !== options.admin)
|
|
|
|
playerContainer.innerHTML = `<button class="kickPlayerButton" onclick="kickPlayer(${player.id})">⨯</button>`;
|
|
|
|
playerContainer.innerHTML = `<button class="kickPlayerButton" onclick="kickPlayer(${player.id})">⨯</button>`;
|
|
|
|
|
|
|
|
|
|
|
|
playerContainer.innerHTML += `<span class="playernameSpan">${player.username}</span>`;
|
|
|
|
playerContainer.innerHTML += `<span class="playernameSpan">${player.username}</span>`;
|
|
|
|
|
|
|
|
|
|
|
|
if (player.id === options.admin)
|
|
|
|
if (player.id === options.admin)
|
|
|
|
playerContainer.innerHTML += "<span class='adminSpan'> 👑</span>";
|
|
|
|
playerContainer.innerHTML += "<span class='adminSpan'> 👑</span>";
|
|
|
|
|
|
|
|
|
|
|
|
document.querySelector('#player-list').appendChild(playerContainer);
|
|
|
|
document.querySelector('#player-list').appendChild(playerContainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Refreshes the bingo chat
|
|
|
|
* Returns the current player id
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function refreshChat() {
|
|
|
|
async function getPlayerInfo() {
|
|
|
|
try {
|
|
|
|
let result = await postGraphqlQuery(`
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query ($lobbyId:ID!) {
|
|
|
|
query ($lobbyId:ID!) {
|
|
|
|
bingo {
|
|
|
|
bingo {
|
|
|
|
player {
|
|
|
|
player {
|
|
|
|
id
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
messages {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
htmlContent
|
|
|
|
|
|
|
|
content
|
|
|
|
|
|
|
|
author {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
username
|
|
|
|
username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let messages = response.data.bingo.lobby.messages;
|
|
|
|
|
|
|
|
for (let message of messages)
|
|
|
|
|
|
|
|
if (!document.querySelector(`.chatMessage[msg-id="${message.id}"]`))
|
|
|
|
|
|
|
|
addChatMessage(message, response.data.bingo.player.id);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('Failed to refresh messages');
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
showError('Failed to refresh messages');
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Refreshes the player list
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function refreshPlayers() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query ($lobbyId: ID!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
player {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
players {
|
|
|
|
|
|
|
|
id
|
|
|
|
id
|
|
|
|
username
|
|
|
|
|
|
|
|
wins(lobbyId: $lobbyId)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
admin {
|
|
|
|
admin {
|
|
|
|
id
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
`, {lobbyId: getLobbyParam()});
|
|
|
|
if (result.status === 200) {
|
|
|
|
if (response.status === 200) {
|
|
|
|
let bingoData = result.data.bingo;
|
|
|
|
let players = response.data.bingo.lobby.players;
|
|
|
|
return {
|
|
|
|
let adminId = response.data.bingo.lobby.admin.id;
|
|
|
|
id: bingoData.player.id,
|
|
|
|
let isAdmin = response.data.bingo.player.id === adminId;
|
|
|
|
username: bingoData.player.username,
|
|
|
|
for (let player of players)
|
|
|
|
isAdmin: bingoData.lobby.admin.id === bingoData.player.id
|
|
|
|
if (!document.querySelector(`.playerEntryContainer[b-pid="${player.id}"]`))
|
|
|
|
};
|
|
|
|
addPlayer(player, {admin: adminId, isAdmin: isAdmin});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
showError('Failed to refresh players');
|
|
|
|
showError('Failed to fetch player Id');
|
|
|
|
console.error(response);
|
|
|
|
console.error(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
showError('Failed to refresh players');
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Removes players that are not existent in the player array
|
|
|
|
* Initializes all socket events
|
|
|
|
* @param players {Array<Object>} - player id response of graphql
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function removeLeftPlayers(players) {
|
|
|
|
function initSocketEvents(data) {
|
|
|
|
for (let playerEntry of document.querySelectorAll('.playerEntryContainer'))
|
|
|
|
let playerId = data.id;
|
|
|
|
if (!players.find(x => (x.id === playerEntry.getAttribute('b-pid'))))
|
|
|
|
let indicator = document.querySelector('#status-indicator');
|
|
|
|
playerEntry.remove();
|
|
|
|
indicator.setAttribute('status', 'error');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
socket.on('connect', () => {
|
|
|
|
* Refreshes if a player-refresh is needed.
|
|
|
|
indicator.setAttribute('socket-status', 'connected');
|
|
|
|
* Removes players that are not in the lobby anyomre.
|
|
|
|
});
|
|
|
|
* @param players
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function checkPlayerRefresh(players) {
|
|
|
|
|
|
|
|
let playerRefresh = false;
|
|
|
|
|
|
|
|
removeLeftPlayers(players);
|
|
|
|
|
|
|
|
for (let player of players)
|
|
|
|
|
|
|
|
if (!document.querySelector(`.playerEntryContainer[b-pid="${player.id}"]`))
|
|
|
|
|
|
|
|
playerRefresh = true;
|
|
|
|
|
|
|
|
if (playerRefresh)
|
|
|
|
|
|
|
|
statusWrap(refreshPlayers);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
socket.on('reconnect', async () => {
|
|
|
|
* Checks if messages need to be refreshed and does it if it needs to.
|
|
|
|
indicator.setAttribute('socket-status', 'connected');
|
|
|
|
* @param messages
|
|
|
|
await BingoGraphqlHelper.refreshChat();
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
function checkMessageRefresh(messages) {
|
|
|
|
|
|
|
|
let messageRefresh = false;
|
|
|
|
|
|
|
|
for (let message of messages)
|
|
|
|
|
|
|
|
if (!document.querySelector(`.chatMessage[msg-id="${message.id}"]`))
|
|
|
|
|
|
|
|
messageRefresh = true;
|
|
|
|
|
|
|
|
if (messageRefresh)
|
|
|
|
|
|
|
|
statusWrap(refreshChat);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
socket.on('disconnect', () => {
|
|
|
|
* refreshes the lobby and calls itself with a timeout
|
|
|
|
indicator.setAttribute('socket-status', 'disconnected');
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
showError('Disconnected from socket!');
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
async function refreshLobby() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query($lobbyId:ID!){
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
|
|
|
|
players {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
messages {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
currentRound {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
words {
|
|
|
|
|
|
|
|
content
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let {players, messages, currentRound} = response.data.bingo.lobby;
|
|
|
|
|
|
|
|
checkPlayerRefresh(players);
|
|
|
|
|
|
|
|
checkMessageRefresh(messages);
|
|
|
|
|
|
|
|
let wordContainer = document.querySelector('#bingo-words');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (wordContainer)
|
|
|
|
socket.on('reconnecting', () => {
|
|
|
|
wordContainer.innerHTML = `<span class="bingoWord">
|
|
|
|
indicator.setAttribute('socket-status', 'reconnecting');
|
|
|
|
${response.data.bingo.lobby.words.map(x => x.content).join('</span><span class="bingoWord">')}</span>`;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (currentRound && currentRound.status === 'ACTIVE' && Number(currentRound.id) !== Number(getRoundParam())) {
|
|
|
|
socket.on('error', (error) => {
|
|
|
|
insertParam('r', currentRound.id);
|
|
|
|
showError(`Socket Error: ${JSON.stringify(error)}`);
|
|
|
|
spawnNotification('The round started!', 'Bingo');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('message', (msg) => {
|
|
|
|
|
|
|
|
addChatMessage(msg, playerId);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('statusChange', (status, winner) => {
|
|
|
|
|
|
|
|
if (status === 'FINISHED' && winner) {
|
|
|
|
|
|
|
|
if (document.querySelector('#container-bingo-round'))
|
|
|
|
|
|
|
|
displayWinner(winner);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
showError('Failed to refresh lobby');
|
|
|
|
window.location.reload();
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('playerJoin', (playerObject) => {
|
|
|
|
|
|
|
|
addPlayer(playerObject, data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('playerLeave', (playerId) => {
|
|
|
|
|
|
|
|
document.querySelector(`.playerEntryContainer[b-pid='${playerId}']`).remove();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('usernameChange', (playerObject) => {
|
|
|
|
|
|
|
|
document.querySelector(`.playerEntryContainer[b-pid='${playerObject.id}'] .playerNameSpan`).innerText = playerObject.username;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('wordsChange', async () => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await BingoGraphqlHelper.loadLobbyWords();
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
showError('Failed to refresh lobby');
|
|
|
|
showError('Failed to load new lobby words.');
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
setTimeout(refreshLobby, 1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('fieldChange', (field) => {
|
|
|
|
|
|
|
|
let wordPanel = document.querySelector(`.bingoWordPanel[b-row='${field.row}'][b-column='${field.column}']`);
|
|
|
|
|
|
|
|
wordPanel.setAttribute('b-sub', field.submitted);
|
|
|
|
|
|
|
|
wordPanel.setAttribute('class', 'bingoWordPanel');
|
|
|
|
|
|
|
|
if (field.bingo)
|
|
|
|
|
|
|
|
document.querySelector('#container-bingo-button').setAttribute('class', '');
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
document.querySelector('#container-bingo-button').setAttribute('class', 'hidden');
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Checks the status of the lobby and the current round.
|
|
|
|
* Initializes the lobby refresh with sockets or graphql
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function refreshRound() {
|
|
|
|
function initRefresh() {
|
|
|
|
let roundOver = false;
|
|
|
|
getPlayerInfo().then((data) => {
|
|
|
|
try {
|
|
|
|
socket = new SimpleSocket(`/bingo/${getLobbyParam()}`, {playerId: data.id});
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
initSocketEvents(data);
|
|
|
|
query($lobbyId:ID!) {
|
|
|
|
});
|
|
|
|
bingo {
|
|
|
|
let chatContent = document.querySelector('#chat-content');
|
|
|
|
lobby(id:$lobbyId) {
|
|
|
|
chatContent.scrollTop = chatContent.scrollHeight;
|
|
|
|
players {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
messages {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
currentRound {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {lobbyId: getLobbyParam()});
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let {players, messages, currentRound} = response.data.bingo.lobby;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checkPlayerRefresh(players);
|
|
|
|
|
|
|
|
checkMessageRefresh(messages);
|
|
|
|
|
|
|
|
if (!currentRound || currentRound.status === "FINISHED") {
|
|
|
|
|
|
|
|
roundOver = true;
|
|
|
|
|
|
|
|
await loadWinnerInfo();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
showError('Failed to refresh round');
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
showError('Failed to refresh round');
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
if (!roundOver)
|
|
|
|
|
|
|
|
setTimeout(refreshRound, 1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener("unhandledrejection", function (promiseRejectionEvent) {
|
|
|
|
window.addEventListener("unhandledrejection", function (promiseRejectionEvent) {
|
|
|
@ -812,10 +761,10 @@ window.addEventListener("unhandledrejection", function (promiseRejectionEvent) {
|
|
|
|
// prevent ctrl + s
|
|
|
|
// prevent ctrl + s
|
|
|
|
window.addEventListener("keydown", async (e) => {
|
|
|
|
window.addEventListener("keydown", async (e) => {
|
|
|
|
if (e.which === 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
|
|
|
|
if (e.which === 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (document.querySelector('#input-bingo-words')) {
|
|
|
|
if (document.querySelector('#input-bingo-words')) {
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
let gridSize = document.querySelector('#input-grid-size').value || 3;
|
|
|
|
let gridSize = document.querySelector('#input-grid-size').value || 3;
|
|
|
|
await statusWrap(async () => await setLobbySettings(getLobbyWords(), gridSize));
|
|
|
|
await statusWrap(async () => await BingoGraphqlHelper.setLobbySettings(getLobbyWords(), gridSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
}, false);
|
|
|
@ -830,3 +779,5 @@ window.onload = async () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let socket = null;
|
|
|
|