|
|
@ -1,92 +1,233 @@
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns the value of the url-param 'game'
|
|
|
|
|
|
|
|
* @returns {string}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function getGameParam() {
|
|
|
|
|
|
|
|
return window.location.href.match(/\?game=(\w+)/)[1];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Toggles the visiblity of the player.container
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function togglePlayerContainer() {
|
|
|
|
|
|
|
|
let playerContainer = document.querySelector('#players-container');
|
|
|
|
|
|
|
|
if (playerContainer.getAttribute('class') === 'hidden')
|
|
|
|
|
|
|
|
playerContainer.setAttribute('class', '');
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
playerContainer.setAttribute('class', 'hidden');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Submits the bingo words to create a game
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
async function submitBingoWords() {
|
|
|
|
async function submitBingoWords() {
|
|
|
|
let textContent = document.querySelector('#bingo-textarea').value;
|
|
|
|
let textContent = document.querySelector('#bingo-textarea').value;
|
|
|
|
let words = textContent.replace(/[<>]/g, '').split('\n');
|
|
|
|
let words = textContent.replace(/[<>]/g, '').split('\n');
|
|
|
|
let size = document.querySelector('#bingo-grid-size').value;
|
|
|
|
let size = document.querySelector('#bingo-grid-size').value;
|
|
|
|
let dimY = document.querySelector('#bingo-grid-y').value;
|
|
|
|
|
|
|
|
let response = await postLocData({
|
|
|
|
|
|
|
|
bingoWords: words,
|
|
|
|
|
|
|
|
size: size
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let data = JSON.parse(response.data);
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
let gameid = data.id;
|
|
|
|
mutation($words:[String!]!, $size:Int!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
createGame(input: {
|
|
|
|
|
|
|
|
words: $words,
|
|
|
|
|
|
|
|
size: $size
|
|
|
|
|
|
|
|
}) {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {
|
|
|
|
|
|
|
|
words: words,
|
|
|
|
|
|
|
|
size: Number(size)
|
|
|
|
|
|
|
|
}, `/graphql?game=${getGameParam()}`);
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
|
|
|
|
let gameid = response.data.bingo.createGame.id;
|
|
|
|
insertParam('game', gameid);
|
|
|
|
insertParam('game', gameid);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Submits the value of the username-input to set the username.
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
async function submitUsername() {
|
|
|
|
async function submitUsername() {
|
|
|
|
let unameInput = document.querySelector('#username-input');
|
|
|
|
let unameInput = document.querySelector('#username-input');
|
|
|
|
let username = unameInput.value;
|
|
|
|
let username = unameInput.value;
|
|
|
|
let response = await postLocData({
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
mutation($username:String!) {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
setUsername(input: {username: $username}) {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {
|
|
|
|
username: username
|
|
|
|
username: username
|
|
|
|
});
|
|
|
|
},`/graphql?game=${getGameParam()}`);
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
unameInput.value = '';
|
|
|
|
unameInput.value = '';
|
|
|
|
unameInput.placeholder = username;
|
|
|
|
unameInput.placeholder = response.data.username;
|
|
|
|
document.querySelector('#username-form').remove();
|
|
|
|
document.querySelector('#username-form').remove();
|
|
|
|
document.querySelector('.greyover').remove();
|
|
|
|
document.querySelector('.greyover').remove();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
console.log(response);
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* toggles a word (toggle occures on response)
|
|
|
|
|
|
|
|
* @param word {String} - the base64 encoded bingo word
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
async function submitWord(word) {
|
|
|
|
async function submitWord(word) {
|
|
|
|
let response = await postLocData({
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
bingoWord: word
|
|
|
|
mutation($word:String!) {
|
|
|
|
});
|
|
|
|
bingo {
|
|
|
|
console.log(response);
|
|
|
|
toggleWord(input: {base64Word: $word}) {
|
|
|
|
|
|
|
|
bingo
|
|
|
|
|
|
|
|
fieldGrid {
|
|
|
|
|
|
|
|
submitted
|
|
|
|
|
|
|
|
base64Word
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, {
|
|
|
|
|
|
|
|
word: word
|
|
|
|
|
|
|
|
},`/graphql?game=${getGameParam()}`);
|
|
|
|
|
|
|
|
|
|
|
|
let data = JSON.parse(response.data);
|
|
|
|
if (response.status === 200 && response.data.bingo.toggleWord) {
|
|
|
|
for (let row of data.fieldGrid) {
|
|
|
|
let fieldGrid = response.data.bingo.toggleWord.fieldGrid;
|
|
|
|
|
|
|
|
for (let row of fieldGrid) {
|
|
|
|
for (let field of row) {
|
|
|
|
for (let field of row) {
|
|
|
|
document.querySelectorAll(`.bingo-word-panel[b-word="${field.base64Word}"]`).forEach(x => {
|
|
|
|
document.querySelectorAll(`.bingo-word-panel[b-word="${field.base64Word}"]`).forEach(x => {
|
|
|
|
x.setAttribute('b-sub', field.submitted);
|
|
|
|
x.setAttribute('b-sub', field.submitted);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data.bingo) {
|
|
|
|
if (response.data.bingo.toggleWord.bingo) {
|
|
|
|
document.querySelector('#bingo-button').setAttribute('class', '');
|
|
|
|
document.querySelector('#bingo-button').setAttribute('class', '');
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
document.querySelector('#bingo-button').setAttribute('class', 'hidden');
|
|
|
|
document.querySelector('#bingo-button').setAttribute('class', 'hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Submits a bingo (Bingo button is pressed).
|
|
|
|
|
|
|
|
* The game is won if the backend validated it.
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
async function submitBingo() {
|
|
|
|
async function submitBingo() {
|
|
|
|
let response = await postLocData({
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
bingo: true
|
|
|
|
mutation {
|
|
|
|
});
|
|
|
|
bingo {
|
|
|
|
let data = JSON.parse(response.data);
|
|
|
|
submitBingo {
|
|
|
|
if (data.bingos.length > 0) {
|
|
|
|
id
|
|
|
|
displayWinner(data.users[data.bingos[0]].username);
|
|
|
|
bingos
|
|
|
|
|
|
|
|
players {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`,null,`/graphql?game=${getGameParam()}`);
|
|
|
|
|
|
|
|
if (response.status === 200 && response.data.bingo.submitBingo) {
|
|
|
|
|
|
|
|
let bingoSession = response.data.bingo.submitBingo;
|
|
|
|
|
|
|
|
if (bingoSession.bingos.length > 0) {
|
|
|
|
|
|
|
|
displayWinner(bingoSession.players.find(x => x.id === bingoSession.bingos[0]).username);
|
|
|
|
clearInterval(refrInterval)
|
|
|
|
clearInterval(refrInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(response);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Refreshes the information (by requesting information about the current game).
|
|
|
|
|
|
|
|
* Is used to see if one player has scored a bingo and which players are in the game.
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
|
|
*/
|
|
|
|
async function refresh() {
|
|
|
|
async function refresh() {
|
|
|
|
let response = await postLocData({});
|
|
|
|
let response = await postGraphqlQuery(`
|
|
|
|
|
|
|
|
query {
|
|
|
|
|
|
|
|
bingo {
|
|
|
|
|
|
|
|
gameInfo {
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
bingos
|
|
|
|
|
|
|
|
players {
|
|
|
|
|
|
|
|
username
|
|
|
|
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, null, `/graphql?game=${getGameParam()}`);
|
|
|
|
|
|
|
|
if (response.status === 200 && response.data.bingo.gameInfo) {
|
|
|
|
|
|
|
|
let bingoSession = response.data.bingo.gameInfo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bingoSession.bingos.length > 0) {
|
|
|
|
|
|
|
|
displayWinner(bingoSession.players.find(x => x.id === bingoSession.bingos[0]).username);
|
|
|
|
|
|
|
|
clearInterval(refrInterval)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for (let player of bingoSession.players) {
|
|
|
|
|
|
|
|
let foundPlayerDiv = document.querySelector(`.player-container[b-pid='${player.id}'`);
|
|
|
|
|
|
|
|
if (!foundPlayerDiv) {
|
|
|
|
|
|
|
|
let playerDiv = document.createElement('div');
|
|
|
|
|
|
|
|
playerDiv.setAttribute('class', 'player-container');
|
|
|
|
|
|
|
|
playerDiv.setAttribute('b-pid', player.id);
|
|
|
|
|
|
|
|
playerDiv.innerHTML = `<span class="player-name-span">${player.username}</span>`;
|
|
|
|
|
|
|
|
document.querySelector('#players-container').appendChild(playerDiv);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let playerNameSpan = foundPlayerDiv.querySelector('.player-name-span');
|
|
|
|
|
|
|
|
if (playerNameSpan.innerText !== player.username) {
|
|
|
|
|
|
|
|
playerNameSpan.innerText = player.username;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (response.status === 400)
|
|
|
|
if (response.status === 400)
|
|
|
|
clearInterval(refrInterval);
|
|
|
|
clearInterval(refrInterval);
|
|
|
|
let data = JSON.parse(response.data);
|
|
|
|
console.error(response);
|
|
|
|
if (data.bingos.length > 0) {
|
|
|
|
|
|
|
|
displayWinner(data.users[data.bingos[0]].username);
|
|
|
|
|
|
|
|
clearInterval(refrInterval)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(response);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Displays the winner of the game in a popup.
|
|
|
|
|
|
|
|
* @param name {String} - the name of the winner
|
|
|
|
|
|
|
|
*/
|
|
|
|
function displayWinner(name) {
|
|
|
|
function displayWinner(name) {
|
|
|
|
let winnerDiv = document.createElement('div');
|
|
|
|
let winnerDiv = document.createElement('div');
|
|
|
|
let greyoverDiv = document.createElement('div');
|
|
|
|
let greyoverDiv = document.createElement('div');
|
|
|
|
let winnerSpan = document.createElement('span');
|
|
|
|
let winnerSpan = document.createElement('span');
|
|
|
|
winnerDiv.setAttribute('class', 'popup');
|
|
|
|
winnerDiv.setAttribute('class', 'popup');
|
|
|
|
|
|
|
|
winnerDiv.setAttribute('style', 'cursor: pointer');
|
|
|
|
greyoverDiv.setAttribute('class', 'greyover');
|
|
|
|
greyoverDiv.setAttribute('class', 'greyover');
|
|
|
|
winnerSpan.innerText = `${name} has won!`;
|
|
|
|
winnerSpan.innerText = `${name} has won!`;
|
|
|
|
winnerDiv.appendChild(winnerSpan);
|
|
|
|
winnerDiv.appendChild(winnerSpan);
|
|
|
|
|
|
|
|
winnerDiv.onclick = () => {
|
|
|
|
|
|
|
|
window.location.reload();
|
|
|
|
|
|
|
|
};
|
|
|
|
document.body.append(greyoverDiv);
|
|
|
|
document.body.append(greyoverDiv);
|
|
|
|
document.body.appendChild(winnerDiv);
|
|
|
|
document.body.appendChild(winnerDiv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Executes the provided function if the key-event is an ENTER-key
|
|
|
|
|
|
|
|
* @param event {Event} - the generated key event
|
|
|
|
|
|
|
|
* @param func {function} - the function to execute on enter
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function submitOnEnter(event, func) {
|
|
|
|
|
|
|
|
if (event.which === 13)
|
|
|
|
|
|
|
|
func();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.onload = () => {
|
|
|
|
window.onload = () => {
|
|
|
|
if (window && !document.querySelector('#bingoform')) {
|
|
|
|
if (window && !document.querySelector('#bingoform')) {
|
|
|
|
refrInterval = setInterval(refresh, 1000);
|
|
|
|
refrInterval = setInterval(refresh, 1000); // global variable to clear
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let gridSizeElem = document.querySelector('#bingo-grid-size');
|
|
|
|
let gridSizeElem = document.querySelector('#bingo-grid-size');
|
|
|
|
document.querySelector('#bingo-grid-y').innerText = gridSizeElem.value;
|
|
|
|
document.querySelector('#bingo-grid-y').innerText = gridSizeElem.value;
|
|
|
|