aboutsummaryrefslogtreecommitdiff
path: root/views/script/core_stats.js
blob: 5c7314fe2e6609b5e207fc0e54122e0aad34fd2d (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
const fs = require('fs');
const homedir = require('@electron/remote').getCurrentWindow().homedir;
const defaultStats = {
    times: {
        game: 0,
        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));
    }
}

class Stats {
    static add(category, counter, quantity) {
        try {
            let current = JSON.parse(fs.readFileSync(homedir + "/.kartik/stats.json").toString());
            current[category][counter] = current[category][counter] + quantity;
            fs.writeFileSync(homedir + "/.kartik/stats.json", JSON.stringify(current));
        } catch (e) {}
    }

    static set(category, counter, value) {
        try {
            let current = JSON.parse(fs.readFileSync(homedir + "/.kartik/stats.json").toString());
            current[category][counter] = value;
            fs.writeFileSync(homedir + "/.kartik/stats.json", JSON.stringify(current));
        } catch (e) {}
    }
}