fix: playtime count and custom games request on process watcher

This commit is contained in:
Zamitto
2025-10-30 07:35:49 -03:00
parent 1bd88e6c6e
commit a2ef0f304d
4 changed files with 41 additions and 13 deletions

View File

@@ -167,6 +167,8 @@ export class AchievementWatcherManager {
shop: GameShop, shop: GameShop,
objectId: string objectId: string
) { ) {
if (shop === "custom") return;
const gameKey = levelKeys.game(shop, objectId); const gameKey = levelKeys.game(shop, objectId);
if (this.alreadySyncedGames.get(gameKey)) return; if (this.alreadySyncedGames.get(gameKey)) return;

View File

@@ -3,6 +3,10 @@ import { HydraApi } from "../hydra-api";
import { gamesSublevel, levelKeys } from "@main/level"; import { gamesSublevel, levelKeys } from "@main/level";
export const createGame = async (game: Game) => { export const createGame = async (game: Game) => {
if (game.shop === "custom") {
return;
}
return HydraApi.post(`/profile/games`, { return HydraApi.post(`/profile/games`, {
objectId: game.objectId, objectId: game.objectId,
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds ?? 0), playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds ?? 0),

View File

@@ -6,6 +6,10 @@ export const updateGamePlaytime = async (
deltaInMillis: number, deltaInMillis: number,
lastTimePlayed: Date lastTimePlayed: Date
) => { ) => {
if (game.shop === "custom") {
return;
}
return HydraApi.put(`/profile/games/${game.remoteId}`, { return HydraApi.put(`/profile/games/${game.remoteId}`, {
playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000), playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000),
lastTimePlayed, lastTimePlayed,

View File

@@ -198,11 +198,6 @@ export const watchProcesses = async () => {
function onOpenGame(game: Game) { function onOpenGame(game: Game) {
const now = performance.now(); const now = performance.now();
AchievementWatcherManager.firstSyncWithRemoteIfNeeded(
game.shop,
game.objectId
);
gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), { gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), {
lastTick: now, lastTick: now,
firstTick: now, firstTick: now,
@@ -220,6 +215,13 @@ function onOpenGame(game: Game) {
}) })
.catch(() => {}); .catch(() => {});
if (game.shop === "custom") return;
AchievementWatcherManager.firstSyncWithRemoteIfNeeded(
game.shop,
game.objectId
);
if (game.remoteId) { if (game.remoteId) {
updateGamePlaytime( updateGamePlaytime(
game, game,
@@ -255,18 +257,20 @@ function onTickGame(game: Game) {
const delta = now - gamePlaytime.lastTick; const delta = now - gamePlaytime.lastTick;
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { const updatedGame: Game = {
...game, ...game,
playTimeInMilliseconds: (game.playTimeInMilliseconds ?? 0) + delta, playTimeInMilliseconds: (game.playTimeInMilliseconds ?? 0) + delta,
lastTimePlayed: new Date(), lastTimePlayed: new Date(),
}); };
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), updatedGame);
gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), { gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), {
...gamePlaytime, ...gamePlaytime,
lastTick: now, lastTick: now,
}); });
if (currentTick % TICKS_TO_UPDATE_API === 0) { if (currentTick % TICKS_TO_UPDATE_API === 0 && game.shop !== "custom") {
const deltaToSync = const deltaToSync =
now - now -
gamePlaytime.lastSyncTick + gamePlaytime.lastSyncTick +
@@ -279,19 +283,20 @@ function onTickGame(game: Game) {
gamePromise gamePromise
.then(() => { .then(() => {
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
...game, ...updatedGame,
unsyncedDeltaPlayTimeInMilliseconds: 0, unsyncedDeltaPlayTimeInMilliseconds: 0,
}); });
}) })
.catch(() => { .catch(() => {
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
...game, ...updatedGame,
unsyncedDeltaPlayTimeInMilliseconds: deltaToSync, unsyncedDeltaPlayTimeInMilliseconds: deltaToSync,
}); });
}) })
.finally(() => { .finally(() => {
gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), { gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), {
...gamePlaytime, ...gamePlaytime,
lastTick: now,
lastSyncTick: now, lastSyncTick: now,
}); });
}); });
@@ -299,11 +304,24 @@ function onTickGame(game: Game) {
} }
const onCloseGame = (game: Game) => { const onCloseGame = (game: Game) => {
const now = performance.now();
const gamePlaytime = gamesPlaytime.get( const gamePlaytime = gamesPlaytime.get(
levelKeys.game(game.shop, game.objectId) levelKeys.game(game.shop, game.objectId)
)!; )!;
gamesPlaytime.delete(levelKeys.game(game.shop, game.objectId)); gamesPlaytime.delete(levelKeys.game(game.shop, game.objectId));
const delta = now - gamePlaytime.lastTick;
const updatedGame: Game = {
...game,
playTimeInMilliseconds: (game.playTimeInMilliseconds ?? 0) + delta,
lastTimePlayed: new Date(),
};
gamesSublevel.put(levelKeys.game(game.shop, game.objectId), updatedGame);
if (game.shop === "custom") return;
if (game.remoteId) { if (game.remoteId) {
if (game.automaticCloudSync) { if (game.automaticCloudSync) {
CloudSync.uploadSaveGame( CloudSync.uploadSaveGame(
@@ -315,20 +333,20 @@ const onCloseGame = (game: Game) => {
} }
const deltaToSync = const deltaToSync =
performance.now() - now -
gamePlaytime.lastSyncTick + gamePlaytime.lastSyncTick +
(game.unsyncedDeltaPlayTimeInMilliseconds ?? 0); (game.unsyncedDeltaPlayTimeInMilliseconds ?? 0);
return updateGamePlaytime(game, deltaToSync, game.lastTimePlayed!) return updateGamePlaytime(game, deltaToSync, game.lastTimePlayed!)
.then(() => { .then(() => {
return gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { return gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
...game, ...updatedGame,
unsyncedDeltaPlayTimeInMilliseconds: 0, unsyncedDeltaPlayTimeInMilliseconds: 0,
}); });
}) })
.catch(() => { .catch(() => {
return gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { return gamesSublevel.put(levelKeys.game(game.shop, game.objectId), {
...game, ...updatedGame,
unsyncedDeltaPlayTimeInMilliseconds: deltaToSync, unsyncedDeltaPlayTimeInMilliseconds: deltaToSync,
}); });
}); });