Skip to content

Commit 9bb76b4

Browse files
authored
[Fix] Keep playstreak quests in sync (#1111)
* keep playstreak quests in sync * add quests-ui * make it pretty * use useref * fix tanstack error * move sync state init * update quests ui * update quests-ui * revert store controller change * fix rounding errors * fix lint * update quests-ui * rm duplicate export
1 parent 0fb9df3 commit 9bb76b4

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@
183183
"@sentry/electron": "^4.24.0",
184184
"@sentry/react": "7.50.0",
185185
"@shockpkg/icon-encoder": "^2.1.3",
186-
"@tanstack/query-core": "^5.59.0",
187-
"@tanstack/react-query": "^5.51.23",
186+
"@tanstack/query-core": "^5.59.13",
187+
"@tanstack/react-query": "^5.59.15",
188188
"@valist/sdk": "^2.10.5",
189189
"@walletconnect/browser-utils": "^1.8.0",
190190
"@walletconnect/ethereum-provider": "^2.10.6",

src/backend/main.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,24 +1103,24 @@ let powerDisplayId: number | null
11031103
let gamePlaySessionStartTimes: Record<string, bigint> = {}
11041104

11051105
function startNewPlaySession(appName: string) {
1106+
const prevStartTime = gamePlaySessionStartTimes[appName]
11061107
gamePlaySessionStartTimes = {}
11071108
// Uses hrtime for monotonic timer not subject to clock drift or sync errors
11081109
const startPlayingTimeMonotonic = hrtime.bigint()
11091110
gamePlaySessionStartTimes[appName] = startPlayingTimeMonotonic
1111+
return prevStartTime
11101112
}
11111113

11121114
async function syncPlaySession(appName: string, runner: Runner) {
11131115
if (!Object.hasOwn(gamePlaySessionStartTimes, appName)) {
11141116
return
11151117
}
11161118

1119+
// reset the time counter and start new session slightly before ending current session to prevent time loss
1120+
const startPlayingTimeMonotonic = startNewPlaySession(appName)
11171121
const stopPlayingTimeMonotonic = hrtime.bigint()
11181122
const sessionPlaytimeInMs =
1119-
(stopPlayingTimeMonotonic - gamePlaySessionStartTimes[appName]) /
1120-
BigInt(1000000)
1121-
1122-
// reset the time counter
1123-
startNewPlaySession(appName)
1123+
(stopPlayingTimeMonotonic - startPlayingTimeMonotonic) / BigInt(1000000)
11241124

11251125
// update local json with time played
11261126
const sessionPlaytimeInMinutes =
@@ -1132,9 +1132,10 @@ async function syncPlaySession(appName: string, runner: Runner) {
11321132

11331133
const game = gameManagerMap[runner].getGameInfo(appName)
11341134
const { hyperPlayListing } = await gameIsEpicForwarderOnHyperPlay(game)
1135-
postPlaySessionTime(
1135+
await postPlaySessionTime(
11361136
hyperPlayListing?.project_id || appName,
1137-
parseInt((sessionPlaytimeInMs / BigInt(1000)).toString())
1137+
// round up to prevent session time loss
1138+
parseInt(((sessionPlaytimeInMs + BigInt(1000)) / BigInt(1000)).toString())
11381139
)
11391140

11401141
return sessionPlaytimeInMs

src/frontend/OverlayManager/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ import { Overlay } from './Overlay'
99
import { WebviewTag } from 'electron'
1010
import WebviewControls from 'frontend/components/UI/WebviewControls'
1111
import AuthModal from 'frontend/components/UI/AuthModal'
12+
import { useKeepPlaystreaksInSync } from '@hyperplay/quests-ui'
1213

1314
const OverlayManager = observer(function ({
1415
appName,
1516
runner
1617
}: BrowserGameProps) {
18+
useKeepPlaystreaksInSync({
19+
appName,
20+
runner,
21+
getQuest: window.api.getQuest,
22+
getQuests: window.api.getQuests,
23+
getUserPlayStreak: window.api.getUserPlayStreak,
24+
syncPlaySession: window.api.syncPlaySession
25+
})
1726
const url = OverlayState.renderState.browserGameUrl
1827

1928
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,10 +2993,10 @@
29932993
dependencies:
29942994
defer-to-connect "^2.0.0"
29952995

2996-
"@tanstack/query-core@5.52.2":
2997-
version "5.52.2"
2998-
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.52.2.tgz#a023864a892fda9858b724d667eb19cd84ce054a"
2999-
integrity sha512-9vvbFecK4A0nDnrc/ks41e3UHONF1DAnGz8Tgbxkl59QcvKWmc0ewhYuIKRh8NC4ja5LTHT9EH16KHbn2AIYWA==
2996+
"@tanstack/query-core@5.59.13", "@tanstack/query-core@^5.59.13":
2997+
version "5.59.13"
2998+
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.59.13.tgz#8c962980af174bbd446b7e9b9999f7432897df80"
2999+
integrity sha512-Oou0bBu/P8+oYjXsJQ11j+gcpLAMpqW42UlokQYEz4dE7+hOtVO9rVuolJKgEccqzvyFzqX4/zZWY+R/v1wVsQ==
30003000

30013001
"@tanstack/query-core@^5.59.0":
30023002
version "5.59.0"
@@ -3020,7 +3020,7 @@
30203020
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.52.2.tgz#3fffbc86351edcaeec335bc8958bcab4204bd169"
30213021
integrity sha512-d4OwmobpP+6+SvuAxW1RzAY95Pv87Gu+0GjtErzFOUXo+n0FGcwxKvzhswCsXKxsgnAr3bU2eJ2u+GXQAutkCQ==
30223022
dependencies:
3023-
"@tanstack/query-core" "5.52.2"
3023+
"@tanstack/query-core" "5.59.13"
30243024

30253025
"@testing-library/dom@^7.31.2":
30263026
version "7.31.2"

0 commit comments

Comments
 (0)