@@ -1111,9 +1111,23 @@ function startNewPlaySession(appName: string) {
1111
1111
return prevStartTime
1112
1112
}
1113
1113
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 ) {
1115
1129
if ( ! Object . hasOwn ( gamePlaySessionStartTimes , appName ) ) {
1116
- return
1130
+ return BigInt ( 0 )
1117
1131
}
1118
1132
1119
1133
// 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) {
1130
1144
sessionPlaytimeInMinutes + BigInt ( tsStore . get ( `${ appName } .totalPlayed` , 0 ) )
1131
1145
tsStore . set ( `${ appName } .totalPlayed` , Number ( totalPlaytime ) )
1132
1146
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
-
1141
1147
return sessionPlaytimeInMs
1142
1148
}
1143
1149
1144
1150
ipcMain . handle (
1145
1151
'syncPlaySession' ,
1146
1152
async ( e , appName : string , runner : Runner ) => {
1147
- await syncPlaySession ( appName , runner )
1153
+ const sessionPlaytimeInMs = syncPlaySession ( appName )
1154
+ await postPlaySession ( appName , runner , sessionPlaytimeInMs )
1148
1155
}
1149
1156
)
1150
1157
@@ -1317,8 +1324,6 @@ ipcMain.handle(
1317
1324
// Update playtime and last played date
1318
1325
const finishedPlayingDate = new Date ( )
1319
1326
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 )
1322
1327
1323
1328
if ( runner === 'gog' ) {
1324
1329
await updateGOGPlaytime ( appName , startPlayingDate , finishedPlayingDate )
@@ -1327,6 +1332,12 @@ ipcMain.handle(
1327
1332
await addRecentGame ( game )
1328
1333
1329
1334
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
+ */
1330
1341
sendFrontendMessage ( 'gameStatusUpdate' , {
1331
1342
appName,
1332
1343
runner,
@@ -1339,6 +1350,12 @@ ipcMain.handle(
1339
1350
status : 'syncing-saves'
1340
1351
} )
1341
1352
1353
+ sendFrontendMessage ( 'gameStatusUpdate' , {
1354
+ appName,
1355
+ runner,
1356
+ status : 'done'
1357
+ } )
1358
+
1342
1359
logInfo ( `Uploading saves for ${ title } ` , LogPrefix . Backend )
1343
1360
try {
1344
1361
await gameManagerMap [ runner ] . syncSaves (
@@ -1362,6 +1379,10 @@ ipcMain.handle(
1362
1379
status : 'done'
1363
1380
} )
1364
1381
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
+
1365
1386
trackEvent ( {
1366
1387
event : 'Game Closed' ,
1367
1388
properties : {
0 commit comments