diff options
Diffstat (limited to 'views/script/menu_login.js')
-rw-r--r-- | views/script/menu_login.js | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/views/script/menu_login.js b/views/script/menu_login.js new file mode 100644 index 0000000..b0a31e9 --- /dev/null +++ b/views/script/menu_login.js @@ -0,0 +1,155 @@ +global.loggingIn = false; +homedir = require('@electron/remote').getCurrentWindow().homedir; + +function startLogin() { + loggingIn = true; + + document.getElementById('loggingIn').style.display = "flex"; + var http = require('http'); + + reqid = 0; + + var server = http.createServer(function (req, res) { + + const queryObject = require('querystring').parse(req.url,true); + token = queryObject[Object.keys(queryObject)[0]]; + res.end(lang.polymer.loginClose); + reqid++; + + if (reqid === 1) { + document.getElementById("loggingIn").innerText = lang.polymer.gatheringLogin + require('@electron/remote').getCurrentWindow().focus(); + server.close(); + + playerData = { + "picture": null, + "name": null, + "level": -1, + "token": token + }; + + playerData.picture = $.ajax({ + type: "GET", + url: 'https://kartik.hopto.org/online/ingame/api/profile.picture.php?kartik_online_token=' + token, + async: false, + error: (e) => { throw e; } + }).responseText.trim(); + + playerData.name = $.ajax({ + type: "GET", + url: 'https://kartik.hopto.org/online/ingame/api/profile.name.php?kartik_online_token=' + token, + async: false, + error: (e) => { throw e; } + }).responseText.trim(); + + playerData.level = $.ajax({ + type: "GET", + url: 'https://kartik.hopto.org/online/ingame/api/profile.level.php?kartik_online_token=' + token, + async: false, + error: (e) => { throw e; } + }).responseText.trim() - 1 + 1; + + if (playerData.level >= 0 && playerData.name !== null && playerData.picture !== null) { + console.log(playerData); + loggingIn = false; + document.getElementById('loggingIn').style.display = "none"; + require('fs').writeFileSync(homedir + "/.kartik/authentication.json", JSON.stringify(playerData)); + keysEnabled = false; + require('electron').ipcRenderer.send('prefademusic', ""); + $("#box").fadeOut(500); + setTimeout(() => { + location.href = "menu.html"; + }, 1000) + } else { + throw new Error("Incomplete information received"); + } + } + + }); + + server.listen(14552); + + console.log('Waiting for login requests on port 14552') + require('open')("https://kartik.hopto.org/online/ingame"); +} + +window.addEventListener('load', () => { + onlineMode = false; + + window.fetch("https://kartik.hopto.org/latest.php?v=" + require('@electron/remote').getCurrentWindow().update).then((data) => { + data.blob().then((a) => { + a.text().then((b) => { + onlineMode = true; + postOnlineMode(); + }).catch((e) => { + require('@electron/remote').getCurrentWindow().webContents.send("notification", {title: lang.polymer.error[0], message: lang.polymer.error[1]}); + console.error(e); + onlineMode = false; + postOnlineMode(); + document.getElementById("loginIntro").innerText = lang.polymer.error[2]; + document.getElementById("online").parentElement.parentElement.outerHTML = ""; + }) + }).catch((e) => { + require('@electron/remote').getCurrentWindow().webContents.send("notification", {title: lang.polymer.error[0], message: lang.polymer.error[1]}); + console.error(e); + onlineMode = false; + postOnlineMode(); + document.getElementById("loginIntro").innerText = lang.polymer.error[2]; + document.getElementById("online").parentElement.parentElement.outerHTML = ""; + }) + }).catch((e) => { + require('@electron/remote').getCurrentWindow().webContents.send("notification", {title: lang.polymer.error[0], message: lang.polymer.error[1]}); + console.error(e); + onlineMode = false; + postOnlineMode(); + document.getElementById("loginIntro").innerText = lang.polymer.error[2]; + document.getElementById("online").parentElement.parentElement.outerHTML = ""; + }) +}) + +function postOnlineMode() { + if (!require('fs').existsSync(homedir + "/.kartik/authentication.json") || !onlineMode) { + $(document).keydown(function(e) { + if (e.keyCode === 76 && !loggingIn && onlineMode) { + startLogin(); + } + }) + } else { + authData = JSON.parse(require('fs').readFileSync(homedir + "/.kartik/authentication.json")); + + document.getElementById('loginIntro').style.display = "none"; + document.getElementById('loginUser').style.display = "grid"; + + olevel = $.ajax({ + type: "GET", + url: 'https://kartik.hopto.org/online/ingame/api/profile.level.php?kartik_online_token=' + authData.token, + async: false, + error: (e) => { throw e; } + }).responseText.trim() - 1 + 1; + + if (authData.level > olevel) { + $.ajax({ + type: "GET", + url: 'https://kartik.hopto.org/online/ingame/api/set.level.php?kartik_online_token=' + authData.token + "&level=" + authData.level, + async: false, + error: (e) => { throw e; } + }); + } else if (authData.level < olevel) { + authData.level = $.ajax({ + type: "GET", + url: 'https://kartik.hopto.org/online/ingame/api/profile.level.php?kartik_online_token=' + authData.token, + async: false, + error: (e) => { throw e; } + }).responseText.trim() - 1 + 1; + fs.writeFileSync(homedir + "/.kartik/authentication.json", JSON.stringify(authData)); + } + + document.getElementById('kto-picture').src = authData.picture; + document.getElementById('kto-username').innerText = authData.name; + if (authData.level < 200) { + document.getElementById('kto-level').innerText = authData.level; + } else { + document.getElementById('kto-level').innerText = lang.polymer.ktoMaxLevel; + } + } +}
\ No newline at end of file |