diff options
Diffstat (limited to 'views/script/core_stats.js')
-rw-r--r-- | views/script/core_stats.js | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/views/script/core_stats.js b/views/script/core_stats.js index 5c7314f..a49851d 100644 --- a/views/script/core_stats.js +++ b/views/script/core_stats.js @@ -2,7 +2,6 @@ const fs = require('fs'); const homedir = require('@electron/remote').getCurrentWindow().homedir; const defaultStats = { times: { - game: 0, single: 0, local: 0, online: 0 @@ -28,20 +27,47 @@ if (!fs.existsSync(homedir + "/.kartik/stats.json")) { } } -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) {} +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; - 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) {} + e.preventDefault(); + return false; } -}
\ No newline at end of file +}, false);
\ No newline at end of file |