Skip to content

Commit 361be53

Browse files
[FIX] Update Game status after finishing playing and not logged in (#1162)
* [FIX] Update Game status after finishing playing and not logged in * chore: version and pr comments * await syncPlaySession only on syncPlaySession handle (#1165) * add comment explaining gameStatusUpdate done --------- Co-authored-by: Flavio F Lima <flavioislima@users.noreply.github.com> Co-authored-by: Brett <27568879+BrettCleary@users.noreply.github.com>
1 parent 2c723a7 commit 361be53

File tree

3 files changed

+68
-42
lines changed

3 files changed

+68
-42
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyperplay",
3-
"version": "0.22.0",
3+
"version": "0.22.1",
44
"private": true,
55
"main": "build/main/main.js",
66
"homepage": "./",

src/backend/main.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,23 @@ function startNewPlaySession(appName: string) {
11111111
return prevStartTime
11121112
}
11131113

1114-
async function syncPlaySession(appName: string, runner: Runner) {
1114+
async function postPlaySession(
1115+
appName: string,
1116+
runner: Runner,
1117+
sessionPlaytimeInMs: bigint
1118+
) {
1119+
const game = gameManagerMap[runner].getGameInfo(appName)
1120+
const { hyperPlayListing } = await gameIsEpicForwarderOnHyperPlay(game)
1121+
await postPlaySessionTime(
1122+
hyperPlayListing?.project_id || appName,
1123+
// round up to prevent session time loss
1124+
parseInt(((sessionPlaytimeInMs + BigInt(1000)) / BigInt(1000)).toString())
1125+
)
1126+
}
1127+
1128+
function syncPlaySession(appName: string) {
11151129
if (!Object.hasOwn(gamePlaySessionStartTimes, appName)) {
1116-
return
1130+
return BigInt(0)
11171131
}
11181132

11191133
// reset the time counter and start new session slightly before ending current session to prevent time loss
@@ -1130,21 +1144,14 @@ async function syncPlaySession(appName: string, runner: Runner) {
11301144
sessionPlaytimeInMinutes + BigInt(tsStore.get(`${appName}.totalPlayed`, 0))
11311145
tsStore.set(`${appName}.totalPlayed`, Number(totalPlaytime))
11321146

1133-
const game = gameManagerMap[runner].getGameInfo(appName)
1134-
const { hyperPlayListing } = await gameIsEpicForwarderOnHyperPlay(game)
1135-
await postPlaySessionTime(
1136-
hyperPlayListing?.project_id || appName,
1137-
// round up to prevent session time loss
1138-
parseInt(((sessionPlaytimeInMs + BigInt(1000)) / BigInt(1000)).toString())
1139-
)
1140-
11411147
return sessionPlaytimeInMs
11421148
}
11431149

11441150
ipcMain.handle(
11451151
'syncPlaySession',
11461152
async (e, appName: string, runner: Runner) => {
1147-
await syncPlaySession(appName, runner)
1153+
const sessionPlaytimeInMs = syncPlaySession(appName)
1154+
await postPlaySession(appName, runner, sessionPlaytimeInMs)
11481155
}
11491156
)
11501157

@@ -1317,8 +1324,6 @@ ipcMain.handle(
13171324
// Update playtime and last played date
13181325
const finishedPlayingDate = new Date()
13191326
tsStore.set(`${appName}.lastPlayed`, finishedPlayingDate.toISOString())
1320-
// Playtime of this session in minutes. Uses hrtime for monotonic timer not subject to clock drift or sync errors
1321-
const sessionPlaytimeInMs = await syncPlaySession(appName, runner)
13221327

13231328
if (runner === 'gog') {
13241329
await updateGOGPlaytime(appName, startPlayingDate, finishedPlayingDate)
@@ -1327,6 +1332,12 @@ ipcMain.handle(
13271332
await addRecentGame(game)
13281333

13291334
if (autoSyncSaves && isOnline()) {
1335+
/**
1336+
* @dev It sets to done, so the GlobalState knows that the game session stopped.
1337+
* Then it changes the status to syncing-saves. Then It sets to done again.
1338+
* Otherwise it would count the Syncing Saves time (which can be long depending on the game) as playing time as well.
1339+
* done is not only the state for stopping playing but for finishing any other process that came before.
1340+
*/
13301341
sendFrontendMessage('gameStatusUpdate', {
13311342
appName,
13321343
runner,
@@ -1339,6 +1350,12 @@ ipcMain.handle(
13391350
status: 'syncing-saves'
13401351
})
13411352

1353+
sendFrontendMessage('gameStatusUpdate', {
1354+
appName,
1355+
runner,
1356+
status: 'done'
1357+
})
1358+
13421359
logInfo(`Uploading saves for ${title}`, LogPrefix.Backend)
13431360
try {
13441361
await gameManagerMap[runner].syncSaves(
@@ -1362,6 +1379,10 @@ ipcMain.handle(
13621379
status: 'done'
13631380
})
13641381

1382+
// Playtime of this session in milliseconds. Uses hrtime for monotonic timer not subject to clock drift or sync errors
1383+
const sessionPlaytimeInMs = syncPlaySession(appName)
1384+
postPlaySession(appName, runner, sessionPlaytimeInMs)
1385+
13651386
trackEvent({
13661387
event: 'Game Closed',
13671388
properties: {

src/backend/utils/quests.ts

+33-28
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,41 @@ export async function postPlaySessionTime(
77
appName: string,
88
playSessionInSeconds: number
99
) {
10-
logInfo(
11-
`Posting play session for project id ${appName}. Time played in seconds was ${playSessionInSeconds}`,
12-
LogPrefix.HyperPlay
13-
)
14-
const cookieString = await getPartitionCookies({
15-
partition: 'persist:auth',
16-
url: DEV_PORTAL_URL
17-
})
18-
const response = await fetch(`${DEV_PORTAL_URL}api/v1/quests/playStreak`, {
19-
method: 'POST',
20-
headers: {
21-
Cookie: cookieString
22-
},
23-
body: JSON.stringify({
24-
project_id: appName,
25-
play_session_in_seconds: playSessionInSeconds
10+
try {
11+
logInfo(
12+
`Posting play session for project id ${appName}. Time played in seconds was ${playSessionInSeconds}`,
13+
LogPrefix.HyperPlay
14+
)
15+
const cookieString = await getPartitionCookies({
16+
partition: 'persist:auth',
17+
url: DEV_PORTAL_URL
2618
})
27-
})
28-
if (!response.ok) {
29-
throw await response.text()
19+
const response = await fetch(`${DEV_PORTAL_URL}api/v1/quests/playStreak`, {
20+
method: 'POST',
21+
headers: {
22+
Cookie: cookieString
23+
},
24+
body: JSON.stringify({
25+
project_id: appName,
26+
play_session_in_seconds: playSessionInSeconds
27+
})
28+
})
29+
if (!response.ok) {
30+
throw await response.text()
31+
}
32+
const resultJson = await response.json()
33+
logInfo(
34+
`Posted playstreak playsession. response: ${JSON.stringify(
35+
resultJson,
36+
null,
37+
4
38+
)}`,
39+
LogPrefix.HyperPlay
40+
)
41+
} catch (error) {
42+
logInfo(`Error in postPlaySessionTime: ${error}`, LogPrefix.HyperPlay)
43+
throw error
3044
}
31-
const resultJson = await response.json()
32-
logInfo(
33-
`Posted playstreak playsession. response: ${JSON.stringify(
34-
resultJson,
35-
null,
36-
4
37-
)}`,
38-
LogPrefix.HyperPlay
39-
)
4045
}
4146

4247
export async function checkG7ConnectionStatus() {

0 commit comments

Comments
 (0)