From 9f49fc848d251a614d2db4bc2a180438ce83284b Mon Sep 17 00:00:00 2001 From: Index Date: Sun, 14 Jul 2024 16:35:29 -0500 Subject: [PATCH] feat: new "time played" feature (a bit buggy right now) --- js/background.js | 41 ++++++++++++++++++++++++++++++++++++++++- js/places/place-view.js | 23 ++++++++++++++++++++++- resources/utils.js | 3 ++- settings.html | 18 ++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/js/background.js b/js/background.js index 9430042..85a8ffa 100755 --- a/js/background.js +++ b/js/background.js @@ -95,7 +95,8 @@ const DefaultSettings = { ProgressBarOn: true, PercentageOn: true, OpacityOn: true - } + }, + TimePlayedOn: true } // ON EXTENSION INSTALL / RELOAD @@ -108,9 +109,47 @@ chrome.runtime.onInstalled.addListener(() => { }) }); +let RecordingTimePlayed = false chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if (request.action === 'reload') { chrome.runtime.reload(); + } else if (request.action === 'start_time_played') { + if (RecordingTimePlayed === true) { + console.log('Time Played: Already Started Interval') + return + } + RecordingTimePlayed = true + + chrome.storage.sync.get(['PolyPlus_TimePlayed'], function(result){ + console.log('Time Played: Start Interval') + + const Playtime = result.PolyPlus_TimePlayed || { + [request.placeID]: 0 + }; + let LoadedIn = false + const TimePlayedInterval = setInterval(async () => { + console.log('Time Played: Run Check') + const PlaceStatus = (await (await fetch('https://api.polytoria.com/v1/users/' + request.userID)).json()).playing + + if (PlaceStatus === null) { + console.log('Time Played: Not Playing Anything') + if (LoadedIn === true) { + console.log('Time Played: End Interval') + clearInterval(TimePlayedInterval) + } + } else { + LoadedIn = true + if (!Playtime[PlaceStatus.placeID]) { + Playtime[PlaceStatus.placeID] = 0 + } + Playtime[PlaceStatus.placeID] += 5 + console.log('Time Played: Time Increase: ', new Date(Playtime[PlaceStatus.placeID] * 1000).toISOString().slice(11, 19), PlaceStatus) + chrome.storage.sync.set({'PolyPlus_TimePlayed': Playtime}, function(){ + console.log('Time Played: Saved Playtime') + }) + } + }, 5000); + }) } else if (request.action === 'greatdivide_stats') { chrome.storage.local.get(['PolyPlus_GreatDivideStats_' + request.userID], async function(result){ let Statistics = result['PolyPlus_GreatDivideStats_' + request.userID] diff --git a/js/places/place-view.js b/js/places/place-view.js index dbca9c4..ee38c22 100644 --- a/js/places/place-view.js +++ b/js/places/place-view.js @@ -6,6 +6,7 @@ let Utilities; let PlaceDetails = null; var Settings; +let TimePlayed; var PinnedGamesData = []; let GamePinned; @@ -45,8 +46,9 @@ const Gamepasses = Array.from(GamepassesTab.getElementsByClassName('card')) || [ RatingsContainer.children[0].appendChild(PercentageLabel); - chrome.storage.sync.get(['PolyPlus_Settings'], async function (result) { + chrome.storage.sync.get(['PolyPlus_Settings', 'PolyPlus_TimePlayed'], async function (result) { Settings = result.PolyPlus_Settings || {}; + TimePlayed = result.PolyPlus_TimePlayed || {}; Utilities = await import(chrome.runtime.getURL('resources/utils.js')); Utilities = Utilities.default; @@ -72,6 +74,25 @@ const Gamepasses = Array.from(GamepassesTab.getElementsByClassName('card')) || [ IRLPrice(); } + if (Settings.TimePlayedOn === true) { + const TimePlayedNameRow = document.createElement('li'); + TimePlayedNameRow.innerText = 'Time Played:'; + + const TimePlayedValueRow = document.createElement('li') + if (TimePlayed[PlaceID]) { + TimePlayedValueRow.innerText = new Date(TimePlayed[PlaceID] * 1000).toISOString().slice(11, 19) + } else { + TimePlayedValueRow.innerText = '-' + } + + InfoColumns[0].appendChild(TimePlayedNameRow); + InfoColumns[1].appendChild(TimePlayedValueRow); + + document.getElementById('btn-play').addEventListener('click', function(){ + chrome.runtime.sendMessage({ action: "start_time_played", placeID: PlaceID, userID: UserID }) + }) + } + if (Settings.ShowPlaceRevenueOn === true) { const NameRow = document.createElement('li'); NameRow.innerText = 'Revenue:'; diff --git a/resources/utils.js b/resources/utils.js index cdce273..488e6d9 100644 --- a/resources/utils.js +++ b/resources/utils.js @@ -115,7 +115,8 @@ export default { ProgressBarOn: true, PercentageOn: true, OpacityOn: true - } + }, + TimePlayedOn: true }, Limits: { PinnedGames: 10, diff --git a/settings.html b/settings.html index e66a42b..3cb1e71 100755 --- a/settings.html +++ b/settings.html @@ -777,6 +777,24 @@ * This feature does not make it possible to set your place(s) as "copyable" +
+ New in v1.3! +
+   + + Time Played + +
+ +
+
+ + Track your playtime in places around Polytoria! + + + * Past playtime data (prior to extension install/prior to setting enable) is, understandably, not able to be tracked + +