From 7cd62e17354ac90fd2f661d93a1cd210549ea1f8 Mon Sep 17 00:00:00 2001 From: Chorizorro Date: Sat, 5 Dec 2020 16:01:46 +0100 Subject: [PATCH] Enhance autosave timer refresh #375 --- CookieMonster.js | 34 +++++++++++++++++++++++++++++++--- src/Disp.js | 25 ++++++++++++++++++++++--- src/Main.js | 9 +++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/CookieMonster.js b/CookieMonster.js index d6a1001..dc173ae 100644 --- a/CookieMonster.js +++ b/CookieMonster.js @@ -1121,7 +1121,7 @@ CM.Disp.Beautify = function(num, frac, forced) { } /******** - * Section: Functions related to display of the full page and initialization of the page */ + * Section: General functions related to display, drawing and initialization of the page */ /** * This function disables and shows the bars created by CookieMonster when the game is "ascending" @@ -1293,6 +1293,23 @@ CM.Disp.CreateBotBarBuildingColumn = function(buildingName) { time.appendChild(document.createElement('td')); } +/** + * This function handles custom drawing for the Game.Draw() function. + * It is hooked on 'draw' by CM.RegisterHooks() + */ +CM.Disp.Draw = function () { + // Draw autosave timer in stats menu + if ( + (Game.prefs.autosave && Game.drawT % 10 == 0) && // with autosave ON and every 10 ticks + (Game.onMenu == 'stats' && CM.Config.Stats) // while being on the stats menu only + ) { + var timer = document.getElementById('CMStatsAutosaveTimer'); + if (timer) { + timer.innerText = Game.sayTime(Game.fps * 60 - (Game.T % (Game.fps * 60)), 4); + } + } +} + /******** * Section: Functions related to the Timer Bar @@ -3263,8 +3280,10 @@ CM.Disp.AddMenuStats = function(title) { } stats.appendChild(listing('Missed Golden Cookies', document.createTextNode(Beautify(Game.missedGoldenClicks)))); if (Game.prefs.autosave) { - var timeTillAutosave = Math.min((Game.fps*60 - (Game.T%(Game.fps*60))) / Game.fps, !Game.OnAscend * 60) - stats.appendChild(listing('Seconds till autosave', document.createTextNode(Math.floor(timeTillAutosave)))); + var timer = document.createElement('span'); + timer.id = 'CMStatsAutosaveTimer'; + timer.innerText = Game.sayTime(Game.fps * 60 - (Game.OnAscend ? 0 : (Game.T % (Game.fps * 60))), 4); + stats.appendChild(listing('Time till autosave', timer)); } } @@ -3707,9 +3726,18 @@ CM.DelayInit = function() { l("upgrades").style.display = "flex"; l("upgrades").style["flex-wrap"] = "wrap"; + CM.Main.RegisterHooks(); + Game.Win('Third-party'); } +/** + * Hook custom methods into the game + */ +CM.Main.RegisterHooks = function() { + Game.registerHook('draw', CM.Disp.Draw); +} + /******** * Section: Functions related to checking for changes in Minigames/GC's/Ticker * TODO: Possibly move this section */ diff --git a/src/Disp.js b/src/Disp.js index ea8cbe1..0092f57 100644 --- a/src/Disp.js +++ b/src/Disp.js @@ -233,7 +233,7 @@ CM.Disp.Beautify = function(num, frac, forced) { } /******** - * Section: Functions related to display of the full page and initialization of the page */ + * Section: General functions related to display, drawing and initialization of the page */ /** * This function disables and shows the bars created by CookieMonster when the game is "ascending" @@ -405,6 +405,23 @@ CM.Disp.CreateBotBarBuildingColumn = function(buildingName) { time.appendChild(document.createElement('td')); } +/** + * This function handles custom drawing for the Game.Draw() function. + * It is hooked on 'draw' by CM.RegisterHooks() + */ +CM.Disp.Draw = function () { + // Draw autosave timer in stats menu + if ( + (Game.prefs.autosave && Game.drawT % 10 == 0) && // with autosave ON and every 10 ticks + (Game.onMenu == 'stats' && CM.Config.Stats) // while being on the stats menu only + ) { + var timer = document.getElementById('CMStatsAutosaveTimer'); + if (timer) { + timer.innerText = Game.sayTime(Game.fps * 60 - (Game.T % (Game.fps * 60)), 4); + } + } +} + /******** * Section: Functions related to the Timer Bar @@ -2375,8 +2392,10 @@ CM.Disp.AddMenuStats = function(title) { } stats.appendChild(listing('Missed Golden Cookies', document.createTextNode(Beautify(Game.missedGoldenClicks)))); if (Game.prefs.autosave) { - var timeTillAutosave = Math.min((Game.fps*60 - (Game.T%(Game.fps*60))) / Game.fps, !Game.OnAscend * 60) - stats.appendChild(listing('Seconds till autosave', document.createTextNode(Math.floor(timeTillAutosave)))); + var timer = document.createElement('span'); + timer.id = 'CMStatsAutosaveTimer'; + timer.innerText = Game.sayTime(Game.fps * 60 - (Game.OnAscend ? 0 : (Game.T % (Game.fps * 60))), 4); + stats.appendChild(listing('Time till autosave', timer)); } } diff --git a/src/Main.js b/src/Main.js index 6b936e7..3b336d7 100644 --- a/src/Main.js +++ b/src/Main.js @@ -250,9 +250,18 @@ CM.DelayInit = function() { l("upgrades").style.display = "flex"; l("upgrades").style["flex-wrap"] = "wrap"; + CM.Main.RegisterHooks(); + Game.Win('Third-party'); } +/** + * Hook custom methods into the game + */ +CM.Main.RegisterHooks = function() { + Game.registerHook('draw', CM.Disp.Draw); +} + /******** * Section: Functions related to checking for changes in Minigames/GC's/Ticker * TODO: Possibly move this section */