X-Git-Url: https://git.auder.net/?a=blobdiff_plain;ds=sidebyside;f=app.js;h=7f8dc274cf28376ebd9ece4b542aee6837d3b666;hb=524e87edee6bf88b8a066ff146adfb75520acd20;hp=56ba25a02854c2f60d3014372d730ebd8c5cfbe5;hpb=86f3c2cd59432a00121af015c505499a57edf568;p=xogo.git diff --git a/app.js b/app.js index 56ba25a..7f8dc27 100644 --- a/app.js +++ b/app.js @@ -4,11 +4,11 @@ let $ = document; //shortcut // Initialisations // https://stackoverflow.com/a/27747377/12660887 -function dec2hex (dec) { return dec.toString(16).padStart(2, "0") } function generateId (len) { - var arr = new Uint8Array((len || 40) / 2) - window.crypto.getRandomValues(arr) - return Array.from(arr, dec2hex).join('') + const dec2hex = (dec) => dec.toString(16).padStart(2, "0"); + let arr = new Uint8Array(len / 2); //len/2 because 2 chars per hex value + window.crypto.getRandomValues(arr); //fill with random integers + return Array.from(arr, dec2hex).join(''); } // Populate variants dropdown list @@ -37,53 +37,60 @@ const setActive = (active) => { if (active) formField.classList.add("form-field--is-active"); else { formField.classList.remove("form-field--is-active"); - inputName.value === "" ? - formField.classList.remove("form-field--is-filled") : - formField.classList.add("form-field--is-filled"); + inputName.value == '' + ? formField.classList.remove("form-field--is-filled") + : formField.classList.add("form-field--is-filled"); } }; +setActive(true); inputName.onblur = () => setActive(false); inputName.onfocus = () => setActive(true); -inputName.focus(); ///////// // Utils function setName() { + // 'onChange' event on name input text field [HTML] localStorage.setItem("name", $.getElementById("myName").value); } // Turn a "tab" on, and "close" all others function toggleVisible(element) { - for (elt of document.querySelectorAll('main > div')) { + for (elt of document.querySelectorAll("main > div")) { if (elt.id != element) elt.style.display = "none"; else elt.style.display = "block"; } - if (element.id == "newGame") { - // Workaround "superposed texts" effect - inputName.focus(); - inputName.blur(); + if (element == "boardContainer") { + // Avoid smartphone scrolling effects (TODO?) + document.querySelector("html").style.overflow = "hidden"; + document.body.style.overflow = "hidden"; + } + else { + document.querySelector("html").style.overflow = "visible"; + document.body.style.overflow = "visible"; + // Workaround "superposed texts" effect: + if (element == "newGame") setActive(false); } } let seek_vname; function seekGame() { seek_vname = $.getElementById("selectVariant").value; - send("seekgame", {vname: seek_vname, name: localStorage.getItem("name")}); - toggleVisible("pendingSeek"); + if (send("seekgame", + {vname: seek_vname, name: localStorage.getItem("name")}) + ) { + toggleVisible("pendingSeek"); + } } function cancelSeek() { - send("cancelseek", {vname: seek_vname}); - toggleVisible("newGame"); + if (send("cancelseek", {vname: seek_vname})) toggleVisible("newGame"); } function sendRematch() { - send("rematch", { gid: gid }); - toggleVisible("pendingRematch"); + if (send("rematch", {gid: gid})) toggleVisible("pendingRematch"); } function cancelRematch() { - send("norematch", { gid: gid }); - toggleVisible("newGame"); + if (send("norematch", {gid: gid})) toggleVisible("newGame"); } // Play with a friend (or not ^^) @@ -95,22 +102,25 @@ function showNewGameForm() { $.getElementById("selectColor").selectedIndex = 0; toggleVisible("newGameForm"); import(`/variants/${vname}/class.js`).then(module => { - const Rules = module.default; - prepareOptions(Rules); + window.V = module.default; + prepareOptions(); }); } } -function backToNormalSeek() { toggleVisible("newGame"); } +function backToNormalSeek() { + toggleVisible("newGame"); +} -function toggleStyle(e, word) { +function toggleStyle(event, obj) { + const word = obj.innerHTML; options[word] = !options[word]; - e.target.classList.toggle("highlight-word"); + event.target.classList.toggle("highlight-word"); } let options; -function prepareOptions(Rules) { +function prepareOptions() { options = {}; - let optHtml = Rules.Options.select.map(select => { return ` + let optHtml = V.Options.select.map(select => { return `
WhatsApp / - ToClipboard + ToClipboard
${link}
`; @@ -285,7 +335,9 @@ const messageCenter = (msg) => { break; // Receive opponent's move: case "newmove": - if (!$.hasFocus()) notifyMe("move"); + // Basic check: was it really opponent's turn? + if (vr.turn == playerColor) break; + if (document.hidden) notifyMe("move"); vr.playReceivedMove(obj.moves, () => { if (vr.getCurrentScore(obj.moves[obj.moves.length-1]) != "*") { localStorage.removeItem("gid"); @@ -307,7 +359,7 @@ const messageCenter = (msg) => { }; const handleError = (err) => { - if (err.code === 'ECONNREFUSED') { + if (err.code === "ECONNREFUSED") { removeAllListeners(); alert("Server refused connection. Please reload page later"); } @@ -321,28 +373,24 @@ const handleClose = () => { }, autoReconnectDelay()); }; -const removeAllListeners = () => { +function removeAllListeners() { socket.removeEventListener("open", tryResumeGame); socket.removeEventListener("message", messageCenter); socket.removeEventListener("error", handleError); socket.removeEventListener("close", handleClose); -}; +} -const connectToWSS = () => { +function connectToWSS() { socket = new WebSocket(`${Params.socket_server}${Params.socket_path}?sid=${sid}`); socket.addEventListener("open", tryResumeGame); socket.addEventListener("message", messageCenter); socket.addEventListener("error", handleError); socket.addEventListener("close", handleClose); - attempt++; -}; + recoAttempt++; +} connectToWSS(); -const send = (code, data) => { - socket.send(JSON.stringify(Object.assign({code: code}, data))); -}; - /////////// // Playing @@ -358,16 +406,28 @@ function notifyMe(code) { new Notification("New " + code, { vibrate: [200, 100, 200] }); new Audio("/assets/new_" + code + ".mp3").play(); } - if (Notification.permission === 'granted') doNotify(); - else if (Notification.permission !== 'denied') { - Notification.requestPermission().then((permission) => { - if (permission === 'granted') doNotify(); + if (Notification.permission === "granted") doNotify(); + else if (Notification.permission !== "denied") { + Notification.requestPermission().then(permission => { + if (permission === "granted") doNotify(); }); } } -let curMoves = []; -const afterPlay = (move) => { //pack into one moves array, then send +let curMoves = [], + lastFen; +const afterPlay = (move) => { + const callbackAfterSend = () => { + curMoves = []; + const result = vr.getCurrentScore(move); + if (result != "*") { + setTimeout(() => { + toggleVisible("gameStopped"); + send("gameover", {gid: gid}); + }, 2000); + } + }; + // Pack into one moves array, then send curMoves.push({ appear: move.appear, vanish: move.vanish, @@ -376,27 +436,13 @@ const afterPlay = (move) => { //pack into one moves array, then send }); if (vr.turn != playerColor) { toggleTurnIndicator(false); - send("newmove", { gid: gid, moves: curMoves, fen: vr.getFen() }); - curMoves = []; - const result = vr.getCurrentScore(move); - if (result != "*") { - setTimeout( () => { - toggleVisible("gameStopped"); - send("gameover", { gid: gid }); - }, 2000); - } - } -}; - -// Avoid loading twice the same stylesheet: -const conditionalLoadCSS = (vname) => { - const allIds = [].slice.call($.styleSheets).map(s => s.id); - const newId = vname + "_css"; - if (!allIds.includes(newId)) { - $.getElementsByTagName("head")[0].insertAdjacentHTML( - "beforeend", - ``); + send("newmove", + {gid: gid, moves: curMoves, fen: vr.getFen()}, + { + retry: true, + success: callbackAfterSend, + error: () => alert("Move not sent: reload page") + }); } }; @@ -404,21 +450,39 @@ let vr, playerColor; function initializeGame(obj) { const options = obj.options || {}; import(`/variants/${obj.vname}/class.js`).then(module => { - const Rules = module.default; - conditionalLoadCSS(obj.vname); + window.V = module.default; + // Load CSS. Avoid loading twice the same stylesheet: + const allIds = [].slice.call($.styleSheets).map(s => s.id); + const newId = obj.vname + "_css"; + if (!allIds.includes(newId)) { + $.getElementsByTagName("head")[0].insertAdjacentHTML( + "beforeend", + ``); + } playerColor = (sid == obj.players[0].sid ? "w" : "b"); // Init + remove potential extra DOM elements from a previous game: document.getElementById("boardContainer").innerHTML = `