aboutsummaryrefslogtreecommitdiff
path: root/views/script/menu_login.js
blob: 2af88b2708aa4cec940623df9d558304b9061dd5 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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";
                currentNest.auth = playerData;
                Nest.export(homedir + "/.kartik/current.kfn", currentNest);require('electron').ipcRenderer.send("reloadNest")
                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() {
    try {
        if (currentNest.auth === null || !onlineMode) {
            $(document).keydown(function(e) {
                if (e.keyCode === 76 && !loggingIn && onlineMode) {
                    startLogin();
                }
            })
        } else {
            authData = currentNest.auth;

            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;
                currentNest.auth = authData;
                Nest.export(homedir + "/.kartik/current.kfn", currentNest);require('electron').ipcRenderer.send("reloadNest")
            }

            ostats = JSON.parse($.ajax({
                type: "GET",
                url: 'https://kartik.hopto.org/online/ingame/api/profile.stats.php?kartik_online_token=' + authData.token,
                async: false,
                error: (e) => { throw e; }
            }).responseText.trim())

            if (ostats === null) {
                cstats = currentNest.stats;
            } else {
                cstats = {};

                for (group in currentNest.stats) {
                    cstats[group] = {};

                    for (item in currentNest.stats[group]) {
                        if (ostats[group][item]) {
                            if (ostats[group][item] > currentNest.stats[group][item]) {
                                cstats[group][item] = ostats[group][item];
                            } else {
                                cstats[group][item] = currentNest.stats[group][item];
                            }
                        } else {
                            cstats[group][item] = currentNest.stats[group][item];
                        }
                    }
                }
            }

            $.ajax({
                type: "GET",
                url: 'https://kartik.hopto.org/online/ingame/api/set.stats.php?kartik_online_token=' + authData.token + "&stats=" + Buffer.from(JSON.stringify(cstats)).toString("base64"),
                async: false,
                error: (e) => { throw e; }
            });

            currentNest.stats = JSON.parse($.ajax({
                type: "GET",
                url: 'https://kartik.hopto.org/online/ingame/api/profile.stats.php?kartik_online_token=' + authData.token,
                async: false,
                error: (e) => { throw e; }
            }).responseText.trim());
            Nest.export(homedir + "/.kartik/current.kfn", currentNest);require('electron').ipcRenderer.send("reloadNest")

            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;
            }
        }
    } catch (e) {
        require('@electron/remote').getCurrentWindow().webContents.send("notification", {title: lang.polymer.error[0], message: lang.polymer.error[1]});
        console.error(e);
        onlineMode = false;
        document.getElementById("loginIntro").innerText = lang.polymer.error[2];
        document.getElementById("online").parentElement.parentElement.outerHTML = "";
    }
}