aboutsummaryrefslogtreecommitdiff
path: root/views/script/menu_login.js
blob: b0a31e95b9771e9ad7fcd3b09141272c8bd0ef99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
        }
    }
}