Skip to content

Commit

Permalink
[Fix] Keep playstreak quests in sync (#1111)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BrettCleary authored Nov 14, 2024
1 parent 0fb9df3 commit 9bb76b4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@
"@sentry/electron": "^4.24.0",
"@sentry/react": "7.50.0",
"@shockpkg/icon-encoder": "^2.1.3",
"@tanstack/query-core": "^5.59.0",
"@tanstack/react-query": "^5.51.23",
"@tanstack/query-core": "^5.59.13",
"@tanstack/react-query": "^5.59.15",
"@valist/sdk": "^2.10.5",
"@walletconnect/browser-utils": "^1.8.0",
"@walletconnect/ethereum-provider": "^2.10.6",
Expand Down
15 changes: 8 additions & 7 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1103,24 +1103,24 @@ let powerDisplayId: number | null
let gamePlaySessionStartTimes: Record<string, bigint> = {}

function startNewPlaySession(appName: string) {
const prevStartTime = gamePlaySessionStartTimes[appName]
gamePlaySessionStartTimes = {}
// Uses hrtime for monotonic timer not subject to clock drift or sync errors
const startPlayingTimeMonotonic = hrtime.bigint()
gamePlaySessionStartTimes[appName] = startPlayingTimeMonotonic
return prevStartTime
}

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

// reset the time counter and start new session slightly before ending current session to prevent time loss
const startPlayingTimeMonotonic = startNewPlaySession(appName)
const stopPlayingTimeMonotonic = hrtime.bigint()
const sessionPlaytimeInMs =
(stopPlayingTimeMonotonic - gamePlaySessionStartTimes[appName]) /
BigInt(1000000)

// reset the time counter
startNewPlaySession(appName)
(stopPlayingTimeMonotonic - startPlayingTimeMonotonic) / BigInt(1000000)

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

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

return sessionPlaytimeInMs
Expand Down
9 changes: 9 additions & 0 deletions src/frontend/OverlayManager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ import { Overlay } from './Overlay'
import { WebviewTag } from 'electron'
import WebviewControls from 'frontend/components/UI/WebviewControls'
import AuthModal from 'frontend/components/UI/AuthModal'
import { useKeepPlaystreaksInSync } from '@hyperplay/quests-ui'

const OverlayManager = observer(function ({
appName,
runner
}: BrowserGameProps) {
useKeepPlaystreaksInSync({
appName,
runner,
getQuest: window.api.getQuest,
getQuests: window.api.getQuests,
getUserPlayStreak: window.api.getUserPlayStreak,
syncPlaySession: window.api.syncPlaySession
})
const url = OverlayState.renderState.browserGameUrl

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2993,10 +2993,10 @@
dependencies:
defer-to-connect "^2.0.0"

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

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

"@testing-library/dom@^7.31.2":
version "7.31.2"
Expand Down

0 comments on commit 9bb76b4

Please sign in to comment.