var globalmode; var waitForMatchTimerID = null; var waitForAnswerTimerID = null; var lobbyWaitForMatchTimerRunning = false; var lobbyWaitForMatchSecs = 0; var lobbyWaitForMatchCounter = 0; var lobbyWaitForAnswerTimerRunning = false var lobbyWaitForAnswerSecs = 0; var lobbytimestart, lobysecs= 0; var pendingagainstwho = 0; var difflevel = 0; var thisuser = 0; var thisusername = ""; var dontquit = false; var otherPlayerName= ""; var waitformatchperiod = 25; // wait before compete against the robot var waitforafriendperiod = 250; // wait before compete against the robot var waitforanswerperiod = 30; function lobbyInit(ithisuser, ithisusername) { // alert("lobbyInit ithisuser="+ithisuser+", ithisusername="+ithisusername); thisuser = ithisuser; thisusername = ithisusername; } function showSearchWindow(whichdiff,mode,gameid) { // alert("showSearchWindow mode="+mode); var difftext; lobbyWaitForMatchCounter = 0; if (document.getElementById("lobbydiff1").checked) { whichdiff = 1;} else if (document.getElementById("lobbydiff2").checked) { whichdiff = 2;} else if (document.getElementById("lobbydiff3").checked) { whichdiff = 3;} else if (document.getElementById("lobbydiff4").checked) { whichdiff = 4;} else {whichdiff = 1;} if (whichdiff == 1) {difftext = nep("j106");} if (whichdiff == 2) {difftext = nep("j107");} if (whichdiff == 3) {difftext = nep("j108");} if (whichdiff == 4) {difftext = nep("j109");} $('#matchwindow').jqmShow(); if (mode=="invite") { // invite mode // alert("invite"); if (isLocalhost()) { basehome="http://localhost:8888/"; } else { basehome="https://www.livesudoku.com/"; } curr_puzzle_url=basehome+getLanguageFromUrl()+"/lobby.php?game="+gameid; url=curr_puzzle_url; message_text=new_lobby_invite_msg_whatsapp+" "+curr_puzzle_url; message_text=encodeURIComponent(message_text); curr_puzzle_url=encodeURIComponent(curr_puzzle_url); whatsapp_share="https://api.whatsapp.com/send?text="+message_text; facebook_share="https://www.facebook.com/sharer.php?u="+curr_puzzle_url+""e="+new_lobby_invite_msg_whatsapp; twitter_share="https://twitter.com/intent/tweet?text="+message_text+"&url="+curr_puzzle_url; // $pinterest_share="https://www.pinterest.com/pin/create/button/?url=".$curr_puzzle_url."&media=https://www.jspuzzles.com/".$ogpiclib."/".$puzzlename.".jpg&description=".$puzzledesc; email_share="mailto:?subject="+new_lobby_invite_msg_whatsapp+"&body="+curr_puzzle_url; whatsapp_share=escapeHtml(whatsapp_share); facebook_share=escapeHtml(facebook_share); twitter_share=escapeHtml(twitter_share); // $pinterest_share=escapeHtml($pinterest_share); email_share=escapeHtml(email_share); js_invite_html='
'+ '
'+invite_js_txt1+'

'+ '

'+invite_js_jointxt1+'


'+ ''+ ''+ '' + '' + '' + '' + '' + '' + '
'+invite_js_txt2+':'+ '' + '' + '' + '' + ' ' + '' + ' ' + '

'; document.getElementById("matchwin1").innerHTML=js_invite_html ; document.getElementById('joinlink').addEventListener('click', copyToClipboard); } else { // search player mode document.getElementById("matchwin1").innerHTML= "
"+nep("j101")+"

"; } document.getElementById("matchwin2").innerHTML= ''+nep(

'; document.getElementById("matchwin3").innerHTML= ''+difftext+' '+nep("j103")+'

'; } function escapeHtml(text) { return text .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function isLocalhost() { var url = window.location.href; // Get the current URL if (url.includes("localhost")) { return true; } else { return false; } } function getLanguageFromUrl() { var url = window.location.href; // Get the current URL var language = ""; // Variable to store the language // Check if the URL has at least two segments if (url.includes("/")) { var segments = url.split("/"); // Split the URL by '/' // alert (segments[3]); // Check if the second segment exists and is not empty if (segments.length > 2 && segments[3] !== "") { language = segments[3]; // Set the language to the second segment } } return language; } function waitForPlayersToJoin(whichdiff,mode,gameid) { showSearchWindow(whichdiff,mode,gameid); multimode=mode; // alert('multimode='+multimode); lobbyWaitForMatchTimer(thisuser); } function initLobbyAjax(mode) { globalmode=mode; if (window.XMLHttpRequest){regxmlhttp=new XMLHttpRequest();}else {regxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} regxmlhttp.onreadystatechange=function() { if (regxmlhttp.readyState==4 && regxmlhttp.status==200) { var answer = regxmlhttp.responseText; // alert('answer 222='+answer+" answer.substring(0,2)="+answer.substring(0,2)); if (answer.substring(0,2) == "0~") { gameid=answer.substring(2); waitForPlayersToJoin(difflevel,mode,gameid); } else if (answer.substring(0,2) == "1~") { lobbyStartRobotMatch(answer.substring(2,3)); // get the robot number from the answer } else { lobbyaskforapprove(answer); } } } if (document.getElementById("lobbydiff1").checked) { difflevel = 1;} else if (document.getElementById("lobbydiff2").checked) { difflevel = 2;} else if (document.getElementById("lobbydiff3").checked) { difflevel = 3;} else if (document.getElementById("lobbydiff4").checked) { difflevel = 4;} else {difflevel = 1;} initlobbyurl = "ilobbyuserid="+thisuser+"&idifflevel="+difflevel+"&imode="+mode+"&" + Math.random(); regxmlhttp.open("POST",'/lobbyinit.php',true); regxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); regxmlhttp.send(initlobbyurl); } function lobbyWaitForMatchAjax() { if (window.XMLHttpRequest){regxmlhttp=new XMLHttpRequest();}else {regxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} regxmlhttp.onreadystatechange=function() { if (regxmlhttp.readyState==4 && regxmlhttp.status==200) { var answer = regxmlhttp.responseText; if (answer == "0") { lobbyWaitForMatchCounter = lobbyWaitForMatchCounter + 1; if ((lobbyWaitForMatchCounter == 1) || (lobbyWaitForMatchCounter == 2)) { var waitmatchtext = nep("j130"); } else if ((lobbyWaitForMatchCounter == 3) || (lobbyWaitForMatchCounter == 4)) { var waitmatchtext = nep("j131"); } else { var waitmatchtext = nep("j132"); } document.getElementById("matchwin3").innerHTML= '
'+waitmatchtext+' '+nep("j103")+'

'; } else { lobbyStopWaitForMatchTimer(); lobbyaskforapprove(answer); } } } quertylobbyurl = "&rti=" + Math.random(); // check if already waiting for specific game // alert(multimode); if (multimode.substring(0,7) == "invite-") { regxmlhttp.open("POST",baseurl+'lobbycheckmatch.php?multimode='+multimode,true); } else { regxmlhttp.open("POST",baseurl+'lobbycheckmatch.php',true); } regxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); regxmlhttp.send(quertylobbyurl); } function lobbyaskforapprove(rtndata) { var rtnarray = rtndata.split("~"); pendingagainstwho = rtnarray[0]; otherPlayerName = rtnarray[1]; document.getElementById("matchwin1").innerHTML= '
'+nep("j110")+' '+rtnarray[1]+'

'+nep("j111")+'
'; document.getElementById("matchwin2").innerHTML= '


'+ '
'+rtnarray[1]+'

'+nep("j112")+''+ rtnarray[2]+'
'+nep("j113")+' '+rtnarray[5]+' ('+rtnarray[4]+')
' +'
'+nep("j114")+'
' +'
'+nep("j115")+'
' +'
'; document.getElementById("matchwin3").innerHTML= "

"; $('#matchwindow').jqmShow(); } function lobbySendMatchAnswerAjax(answer, againstwho, waswaiting,whichrobot) { // alert('lobbySendMatchAnswerAjax answer='+answer); if (window.XMLHttpRequest){regxmlhttp=new XMLHttpRequest();}else {regxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} regxmlhttp.onreadystatechange=function() { if (regxmlhttp.readyState==4 && regxmlhttp.status==200) { var answer = regxmlhttp.responseText; if (answer == "wait") { document.getElementById("matchwin1").innerHTML= "
"+nep("j120")+"

"; document.getElementById("matchwin2").innerHTML= '
'; document.getElementById("matchwin3").innerHTML= '


'; if (waswaiting == 0) { lobbyWaitForAnswerTimer(); } else { document.getElementById("matchwin3").innerHTML= '
'+nep("j121")+'

'; } } else if (answer == "yes") { document.getElementById("matchwin1").innerHTML= "

"+nep("j125")+"

"; document.getElementById("matchwin2").innerHTML= "
"; document.getElementById("matchwin3").innerHTML= "
"; lobbyStopWaitForAnswerTimer(); lobbyredirecttogame(); } else if (answer == "cancel") { $('#matchwindow').jqmHide(); lobbyStopWaitForMatchTimer(); lobbyStopWaitForAnswerTimer(); } else if (answer == "selfno") { $('#matchwindow').jqmHide(); lobbyStopWaitForMatchTimer(); lobbyStopWaitForAnswerTimer(); initLobbyAjax("normal"); } if (answer == "no") { $('#matchwindow').jqmHide(); lobbyStopWaitForMatchTimer(); lobbyStopWaitForAnswerTimer(); $('#rejectwindow').jqmShow(); } } } answerlobbyurl = "lobbyyesno="+answer+"&againstwho="+againstwho+"&waswaiting="+waswaiting+"&difflevel="+difflevel+"&whichrob="+whichrobot+"&othername="+encodeURI(otherPlayerName)+"&myname="+encodeURI(thisusername)+"&rti=" + Math.random(); regxmlhttp.open("POST",baseurl+'lobbyanswer.php',true); regxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); regxmlhttp.send(answerlobbyurl); } function drawRobotWindow(mode) { var noopfound = ""; var lookforother = ""; if (mode == "frompause") { whattodo = "lobbyStartRobotMatch"; noopfound = '
'+nep("j141"); lookforother = '
'+nep("j114")+'
'; } else if (mode == "fromlobby") { whattodo = "lobbyRobotGame"; noopfound = ""; lookforother = "
"; } document.getElementById("matchwin1").innerHTML= noopfound+'
'+nep("j142")+'

' +'' +'' +'' +'
'+nep("j144")+''+nep("j145")+'
'+nep("j146")+''+nep("j147")+'
'+nep("j149")+''+nep("j150")+'
'; var secondpart = '


'+ '
'+nep("j155")+'

'+nep("j112")+' '+nep("j143")+'
'+nep("j113")+' '+nep("j158")+'
' +lookforother +'
'+nep("j115")+'
' +'
'; document.getElementById("matchwin2").innerHTML = secondpart; document.getElementById("matchwin3").innerHTML = "

"; } function lobbyPauseResumeAjax(whichmode) { if (window.XMLHttpRequest){regxmlhttp=new XMLHttpRequest();}else {regxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} regxmlhttp.onreadystatechange=function() { if (regxmlhttp.readyState==4 && regxmlhttp.status==200) { var answer = regxmlhttp.responseText; // the php script will check last time for a match before pausing if (answer == 0) { drawRobotWindow("frompause"); } else { lobbyaskforapprove(answer); } } } lobbyStopWaitForMatchTimer(); lobbyStopWaitForAnswerTimer(); pauselobbyurl = "mode="+whichmode+"&rti=" + Math.random(); regxmlhttp.open("POST",baseurl+'lobbypause.php',true); regxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); regxmlhttp.send(pauselobbyurl); } function lobbyQuitAjax() { if (window.XMLHttpRequest){regxmlhttp=new XMLHttpRequest();}else {regxmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} regxmlhttp.onreadystatechange=function() { if (regxmlhttp.readyState==4 && regxmlhttp.status==200) { } } lobbyStopWaitForMatchTimer(); lobbyStopWaitForAnswerTimer(); leavelobbyurl = "&rti=" + Math.random(); regxmlhttp.open("POST",baseurl+'lobbyquit.php',true); regxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); regxmlhttp.send(leavelobbyurl); $('#matchwindow').jqmHide(); } function lobbyRobotGame(whichrobot) // go directly to robot game { initLobbyAjax("robot"+whichrobot); } function lobbyStopWaitForMatchTimer() { if (lobbyWaitForMatchTimerRunning) { window.clearInterval(waitForMatchTimerID) } lobbyWaitForMatchTimerRunning = false } function lobbyStopWaitForAnswerTimer() { if (lobbyWaitForAnswerTimerRunning) { window.clearInterval(waitForAnswerTimerID) } lobbyWaitForAnswerTimerRunning = false } function lobbyWaitForMatchTimer() { lobbytimestart = new Date().getTime(); lobbyWaitForMatchTimerRunning = true; waitForMatchTimerID = self.setInterval("lobbyWaitForMatchInterval()", 3000) } function copyToClipboard() { /* Get the input element */ var inputElement = document.getElementById('joinlink'); /* Select the text inside the input element */ inputElement.select(); /* Execute the copy command */ document.execCommand('copy'); /* Deselect the input element */ inputElement.setSelectionRange(0, inputElement.value.length); // alert('copied'); $('#taptocopy').text(invite_js_jointxt2); } function lobbyWaitForMatchInterval() { var lobbytimediff = new Date().getTime() - lobbytimestart; lobbyWaitForMatchSecs = Math.floor(lobbytimediff / 1000); if (globalmode=="invite") { waitformatchperiod=waitforafriendperiod; } if (lobbyWaitForMatchSecs > waitformatchperiod ) { lobbyStopWaitForMatchTimer(); lobbyPauseResumeAjax(0); // 0 = pause } else { lobbyWaitForMatchAjax(); } } function lobbyredirecttogame() { dontquit = true; window.location.href = "livesudoku.php"; } function lobbyStartRobotMatch(whichrobot) { // alert('lobbyStartRobotMatch'); lobbySendMatchAnswerAjax(2,0,0,whichrobot); } function lobbyWaitForAnswerTimer() { lobbytimestart = new Date().getTime(); lobbyWaitForAnswerTimerRunning = true; waitForAnswerTimerID = self.setInterval("lobbyWaitForAnswerInterval()", 3000); } function cancelWaitForAnswer() { lobbySendMatchAnswerAjax(0,pendingagainstwho, 1,0); lobbyStopWaitForAnswerTimer(); } function lobbyWaitForAnswerInterval() { var lobbytimediff = new Date().getTime() - lobbytimestart; lobbyWaitForAnswerSecs = Math.floor(lobbytimediff / 1000); if (lobbyWaitForAnswerSecs > waitforanswerperiod) { cancelWaitForAnswer(); } else { lobbySendMatchAnswerAjax(1,pendingagainstwho, 1,0); } } window.onbeforeunload=function(e) { if ((!dontquit) && (thisuser != 0)) { lobbyQuitAjax(); } }