aboutsummaryrefslogtreecommitdiff
path: root/views/script/core_stats.js
blob: a49851dc105620279cd73e6cbe1f6e1a859b486c (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
const fs = require('fs');
const homedir = require('@electron/remote').getCurrentWindow().homedir;
const defaultStats = {
    times: {
        single: 0,
        local: 0,
        online: 0
    },
    results: {
        wins: 0,
        loses: 0
    },
    ingame: {
        walls: 0,
        laps: 0,
        turns: 0
    }
}

if (!fs.existsSync(homedir + "/.kartik/stats.json")) {
    fs.writeFileSync(homedir + "/.kartik/stats.json", JSON.stringify(defaultStats));
} else {
    try {
        JSON.parse(fs.readFileSync(homedir + "/.kartik/stats.json").toString());
    } catch (e) {
        fs.writeFileSync(homedir + "/.kartik/stats.json", JSON.stringify(defaultStats));
    }
}

session = null;
timer = null;
current = null;

webview.addEventListener('dom-ready', () => {
    if (webview.getURL() !== current) {
        if (session !== null) {
            require('electron').ipcRenderer.send('addstats', { catalog: "times", key: session, add: Math.floor((new Date() - timer)/1000) });

            session = null;
            timer = null;
            current = null;
        }
    }

    if (webview.getURL().endsWith("game.html")) { //        Local
        session = "local";
        timer = new Date();
        current = webview.getURL();
    }
    if (webview.getURL().endsWith("game.html?sp")) { //     Singleplayer
        session = "single";
        timer = new Date();
        current = webview.getURL();
    }
    if (webview.getURL().endsWith("game.html?online")) { // Online
        session = "online";
        timer = new Date();
        current = webview.getURL();
    }
})

window.addEventListener("beforeunload", function(e){
    if (session !== null) {
        require('electron').ipcRenderer.send('addstatsandclose', { catalog: "times", key: session, add: Math.floor((new Date() - timer)/1000) });

        session = null;
        timer = null;
        current = null;

        e.preventDefault();
        return false;
    }
}, false);