From 4ec6f33be6130705b81f19fdc9e2fe01b4a3e7e2 Mon Sep 17 00:00:00 2001 From: Minteck Date: Sat, 5 Jun 2021 21:03:47 +0200 Subject: Stats presque fini --- views/script/core_stats.js | 56 +++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'views/script/core_stats.js') 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 -- cgit