From 4ec6f33be6130705b81f19fdc9e2fe01b4a3e7e2 Mon Sep 17 00:00:00 2001 From: Minteck Date: Sat, 5 Jun 2021 21:03:47 +0200 Subject: Stats presque fini --- app.js | 15 ++++++++++-- crash/client.js | 2 +- crash/crash.html | 2 +- cursors/click.png | Bin 350 -> 0 bytes cursors/main.png | Bin 336 -> 0 bytes cursors/text.png | Bin 230 -> 0 bytes index.html | 2 +- package.json | 2 +- race/cars/car0.js | 4 ++++ race/cars/car1.js | 1 + race/cars/keymap.js | 4 ++++ views/common/fonts.css | 6 +---- views/script/core_stats.js | 56 +++++++++++++++++++++++++++++++++------------ 13 files changed, 68 insertions(+), 26 deletions(-) delete mode 100644 cursors/click.png delete mode 100644 cursors/main.png delete mode 100644 cursors/text.png diff --git a/app.js b/app.js index 1c02825..da35e78 100644 --- a/app.js +++ b/app.js @@ -51,9 +51,9 @@ const { app, BrowserWindow } = require('electron'); process.on('uncaughtException', (error) => { console.log(" * Starting recovery procedure: E_ERROR"); - id = new Date().toISOString(); + id = new Date().toISOString().replaceAll(":", "-"); - require('fs').writeFileSync(homedir + "/.kartik/crashes/" + id + ".txt", "Kartik Bootstraper Crash\n\n" + error.stack); + require('fs').writeFileSync(require('os').userInfo().homedir + "/.kartik/crashes/" + id + ".txt", "Kartik Bootstraper Crash\n\n" + error.stack); if (require('os').platform() === "win32") { require('child_process').exec("runtime\\kartik-crash.bat " + id); } else if (require('os').platform() === "darwin") { @@ -278,6 +278,17 @@ const { app, BrowserWindow } = require('electron'); musicIpc.on('preunfademusic', (event, value) => { win.webContents.send('unfademusic', value); }) + + stats = require(homedir + "/.kartik/stats.json"); + musicIpc.on('addstats', (event, value) => { + stats[value.catalog][value.key] = stats[value.catalog][value.key] + value.add; + fs.writeFile(homedir + "/.kartik/stats.json", JSON.stringify(stats), () => {}); + }) + musicIpc.on('addstatsandclose', (event, value) => { + stats[value.catalog][value.key] = stats[value.catalog][value.key] + value.add; + fs.writeFile(homedir + "/.kartik/stats.json", JSON.stringify(stats), () => {}); + win.destroy(); + }) }) } diff --git a/crash/client.js b/crash/client.js index d5ea355..e70467f 100644 --- a/crash/client.js +++ b/crash/client.js @@ -3,7 +3,7 @@ if (native) { try { require('@electron/remote').getCurrentWindow().hide(); } catch (e) {} - id = new Date().toISOString(); + id = new Date().toISOString().replaceAll(":", "-"); try { global.pkg = require('./package.json'); diff --git a/crash/crash.html b/crash/crash.html index 5b922aa..ec4e5ca 100644 --- a/crash/crash.html +++ b/crash/crash.html @@ -46,7 +46,7 @@

- + diff --git a/package.json b/package.json index 9467cd8..69a8864 100644 --- a/package.json +++ b/package.json @@ -1 +1 @@ -{"name":"kartik","version":"5f37145","channel":"git","description":"A multiplatform 2D karting game","main":"app.js","dependencies":{"@electron/remote":"^1.0.4","discord-rpc":"^3.2.0","electron":"^12.0.1","ini":"^1.3.8","jquery":"^3.6.0","os-locale":"^5.0.0","uuid":"^8.3.2","yaml":"^1.10.2"},"scripts":{"test":"node_modules\\electron\\dist\\electron.exe app.js"},"author":"Minteck Projects","license":"UNLICENSED","serial":"XXX99999999999"} \ No newline at end of file +{"name":"kartik","version":"97ebd65","channel":"git","description":"A multiplatform 2D karting game","main":"app.js","dependencies":{"@electron/remote":"^1.0.4","discord-rpc":"^3.2.0","electron":"^12.0.1","ini":"^1.3.8","jquery":"^3.6.0","os-locale":"^5.0.0","uuid":"^8.3.2","yaml":"^1.10.2"},"scripts":{"test":"node_modules\\electron\\dist\\electron.exe app.js"},"author":"Minteck Projects","license":"UNLICENSED","serial":"XXX99999999999"} \ No newline at end of file diff --git a/race/cars/car0.js b/race/cars/car0.js index ddfe8c5..ad1c227 100644 --- a/race/cars/car0.js +++ b/race/cars/car0.js @@ -54,6 +54,7 @@ startHooks.push(() => { Sound.crash() document.getElementById("car0").style.left = car0startx + "px"; document.getElementById("car0").style.top = car0starty + "px"; + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "walls", add: 1 }); Array.from(document.getElementById('oil').children).forEach((item) => { spreadOil(item); }) @@ -96,6 +97,7 @@ startHooks.push(() => { } else { Sound.last() } + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "laps", add: 1 }); document.getElementById('laps-car0').innerText = (document.getElementById('laps-car0').innerText.split("/")[0] - 1 + 2).toString(); if (location.search === "?sp") { if ((document.getElementById('laps-car0').innerText.split("/")[0] - 1 + 1) > (document.getElementById('laps-car1').innerText.split("/")[0] - 1 + 1)) { @@ -106,6 +108,7 @@ startHooks.push(() => { } } else { info("CarManager:car0", "Car won the game"); + require('electron').ipcRenderer.send('addstats', { catalog: "results", key: "wins", add: 1 }); if (location.search === "?sp") { scenar("won1", "sad"); } @@ -156,6 +159,7 @@ startHooks.push(() => { scenar("wall1", "happy"); } Sound.crash(); + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "walls", add: 1 }); document.getElementById("car0").style.left = car0startx + "px"; document.getElementById("car0").style.top = car0starty + "px"; Array.from(document.getElementById('oil').children).forEach((item) => { diff --git a/race/cars/car1.js b/race/cars/car1.js index 750cbb3..0c4c9f0 100644 --- a/race/cars/car1.js +++ b/race/cars/car1.js @@ -113,6 +113,7 @@ startHooks.push(() => { if (location.search === "?sp") { scenar("won2", "happy"); } + require('electron').ipcRenderer.send('addstats', { catalog: "results", key: "loses", add: 1 }); try { document.getElementById('music').src = "about:blank"; } catch (e) { console.error(e); } Sound.win(); keysEnabled = false; diff --git a/race/cars/keymap.js b/race/cars/keymap.js index 74cce0a..221a7ec 100644 --- a/race/cars/keymap.js +++ b/race/cars/keymap.js @@ -29,6 +29,7 @@ startHooks.push(() => { car0cspeed = car0cspeed + 0.2; } if (document.getElementById("car0").style.transform !== "rotate(-90deg)") { + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "turns", add: 1 }); document.getElementById("car0").style.transform = "rotate(-90deg)"; car0collisionon = false; setTimeout(() => { @@ -41,6 +42,7 @@ startHooks.push(() => { car0cspeed = car0cspeed + 0.2; } if (document.getElementById("car0").style.transform !== "rotate(90deg)") { + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "turns", add: 1 }); document.getElementById("car0").style.transform = "rotate(90deg)"; car0collisionon = false; setTimeout(() => { @@ -53,6 +55,7 @@ startHooks.push(() => { car0cspeed = car0cspeed + 0.2; } if (document.getElementById("car0").style.transform !== "rotate(180deg)") { + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "turns", add: 1 }); document.getElementById("car0").style.transform = "rotate(180deg)"; car0collisionon = false; setTimeout(() => { @@ -65,6 +68,7 @@ startHooks.push(() => { car0cspeed = car0cspeed + 0.2; } if (document.getElementById("car0").style.transform !== "rotate(0deg)") { + require('electron').ipcRenderer.send('addstats', { catalog: "ingame", key: "turns", add: 1 }); document.getElementById("car0").style.transform = "rotate(0deg)"; car0collisionon = false; setTimeout(() => { diff --git a/views/common/fonts.css b/views/common/fonts.css index ac8f9f0..190dd75 100644 --- a/views/common/fonts.css +++ b/views/common/fonts.css @@ -19,9 +19,5 @@ } body, * { - cursor: url("../../cursors/main.png"), default !important; -} - -input { - cursor: url("../../cursors/text.png"), default !important; + cursor: default !important; } \ No newline at end of file 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