From 2ea6285744b8dc859cf12618d2e700f44bc01512 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:45:37 -0300 Subject: [PATCH 1/3] feat: add language migration --- src/main/knex-client.ts | 3 ++- .../20240913213944_update_user_language.ts | 13 +++++++++++++ src/renderer/src/main.tsx | 7 +++++-- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/main/migrations/20240913213944_update_user_language.ts diff --git a/src/main/knex-client.ts b/src/main/knex-client.ts index 031760f6..9d045e3a 100644 --- a/src/main/knex-client.ts +++ b/src/main/knex-client.ts @@ -2,12 +2,13 @@ import knex, { Knex } from "knex"; import { databasePath } from "./constants"; import { Hydra2_0_3 } from "./migrations/20240830143811_Hydra_2_0_3"; import { RepackUris } from "./migrations/20240830143906_RepackUris"; +import { UpdateUserLanguage } from "./migrations/20240913213944_update_user_language"; export type HydraMigration = Knex.Migration & { name: string }; class MigrationSource implements Knex.MigrationSource { getMigrations(): Promise { - return Promise.resolve([Hydra2_0_3, RepackUris]); + return Promise.resolve([Hydra2_0_3, RepackUris, UpdateUserLanguage]); } getMigrationName(migration: HydraMigration): string { return migration.name; diff --git a/src/main/migrations/20240913213944_update_user_language.ts b/src/main/migrations/20240913213944_update_user_language.ts new file mode 100644 index 00000000..3297eb0d --- /dev/null +++ b/src/main/migrations/20240913213944_update_user_language.ts @@ -0,0 +1,13 @@ +import type { HydraMigration } from "@main/knex-client"; +import type { Knex } from "knex"; + +export const UpdateUserLanguage: HydraMigration = { + name: "UpdateUserLanguage", + up: async (knex: Knex) => { + await knex("user_preferences") + .update("language", "pt-BR") + .where("language", "pt"); + }, + + down: async (_knex: Knex) => {}, +}; diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index 2377bf7c..3cf1b9a1 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -41,8 +41,11 @@ i18n escapeValue: false, }, }) - .then(() => { - window.electron.updateUserPreferences({ language: i18n.language }); + .then(async () => { + const userPreferences = await window.electron.getUserPreferences(); + if (!userPreferences?.language) { + window.electron.updateUserPreferences({ language: i18n.language }); + } }); ReactDOM.createRoot(document.getElementById("root")!).render( From f439b6809c71f0b57b65ab3c1446f4ac378ce1f7 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Fri, 13 Sep 2024 20:30:54 -0300 Subject: [PATCH 2/3] feat: add needsAuth: false to /games/download --- src/main/services/library-sync/create-game.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index 54c4e866..0b991bed 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -4,10 +4,14 @@ import { gameRepository } from "@main/repository"; import { logger } from "../logger"; export const createGame = async (game: Game) => { - HydraApi.post("/games/download", { - objectId: game.objectID, - shop: game.shop, - }).catch((err) => { + HydraApi.post( + "/games/download", + { + objectId: game.objectID, + shop: game.shop, + }, + { needsAuth: false } + ).catch((err) => { logger.error("Failed to create game download", err); }); From 608a53e8df549212bc13da530e868281eb4f9b3a Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Fri, 13 Sep 2024 21:02:59 -0300 Subject: [PATCH 3/3] feat: accumulate ticks when request to update playtime fails --- .../events/library/add-game-to-library.ts | 2 +- .../events/torrenting/start-game-download.ts | 2 +- src/main/services/library-sync/create-game.ts | 18 +++++----- .../library-sync/update-game-playtime.ts | 4 +-- src/main/services/process-watcher.ts | 36 ++++++++++--------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/main/events/library/add-game-to-library.ts b/src/main/events/library/add-game-to-library.ts index 2eba4a71..13a7e5e0 100644 --- a/src/main/events/library/add-game-to-library.ts +++ b/src/main/events/library/add-game-to-library.ts @@ -54,7 +54,7 @@ const addGameToLibrary = async ( const game = await gameRepository.findOne({ where: { objectID } }); - createGame(game!); + createGame(game!).catch(() => {}); }); }; diff --git a/src/main/events/torrenting/start-game-download.ts b/src/main/events/torrenting/start-game-download.ts index ada05a09..90350f4a 100644 --- a/src/main/events/torrenting/start-game-download.ts +++ b/src/main/events/torrenting/start-game-download.ts @@ -97,7 +97,7 @@ const startGameDownload = async ( }, }); - createGame(updatedGame!); + createGame(updatedGame!).catch(() => {}); await downloadQueueRepository.delete({ game: { id: updatedGame!.id } }); await downloadQueueRepository.insert({ game: { id: updatedGame!.id } }); diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index 0b991bed..396ddbdd 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -15,19 +15,17 @@ export const createGame = async (game: Game) => { logger.error("Failed to create game download", err); }); - HydraApi.post(`/profile/games`, { + return HydraApi.post(`/profile/games`, { objectId: game.objectID, playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds), shop: game.shop, lastTimePlayed: game.lastTimePlayed, - }) - .then((response) => { - const { id: remoteId, playTimeInMilliseconds, lastTimePlayed } = response; + }).then((response) => { + const { id: remoteId, playTimeInMilliseconds, lastTimePlayed } = response; - gameRepository.update( - { objectID: game.objectID }, - { remoteId, playTimeInMilliseconds, lastTimePlayed } - ); - }) - .catch(() => {}); + gameRepository.update( + { objectID: game.objectID }, + { remoteId, playTimeInMilliseconds, lastTimePlayed } + ); + }); }; diff --git a/src/main/services/library-sync/update-game-playtime.ts b/src/main/services/library-sync/update-game-playtime.ts index 5cfc4103..28c3bed3 100644 --- a/src/main/services/library-sync/update-game-playtime.ts +++ b/src/main/services/library-sync/update-game-playtime.ts @@ -6,8 +6,8 @@ export const updateGamePlaytime = async ( deltaInMillis: number, lastTimePlayed: Date ) => { - HydraApi.put(`/profile/games/${game.remoteId}`, { + return HydraApi.put(`/profile/games/${game.remoteId}`, { playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000), lastTimePlayed, - }).catch(() => {}); + }); }; diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 080f1efc..64b14f4d 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -70,9 +70,9 @@ function onOpenGame(game: Game) { }); if (game.remoteId) { - updateGamePlaytime(game, 0, new Date()); + updateGamePlaytime(game, 0, new Date()).catch(() => {}); } else { - createGame({ ...game, lastTimePlayed: new Date() }); + createGame({ ...game, lastTimePlayed: new Date() }).catch(() => {}); } } @@ -93,20 +93,22 @@ function onTickGame(game: Game) { }); if (currentTick % TICKS_TO_UPDATE_API === 0) { - if (game.remoteId) { - updateGamePlaytime( - game, - now - gamePlaytime.lastSyncTick, - game.lastTimePlayed! - ); - } else { - createGame(game); - } + const gamePromise = game.remoteId + ? updateGamePlaytime( + game, + now - gamePlaytime.lastSyncTick, + game.lastTimePlayed! + ) + : createGame(game); - gamesPlaytime.set(game.id, { - ...gamePlaytime, - lastSyncTick: now, - }); + gamePromise + .then(() => { + gamesPlaytime.set(game.id, { + ...gamePlaytime, + lastSyncTick: now, + }); + }) + .catch(() => {}); } } @@ -119,8 +121,8 @@ const onCloseGame = (game: Game) => { game, performance.now() - gamePlaytime.firstTick, game.lastTimePlayed! - ); + ).catch(() => {}); } else { - createGame(game); + createGame(game).catch(() => {}); } };