mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-22 02:13:59 +00:00
feat: sync library
This commit is contained in:
@@ -14,6 +14,10 @@ export class HydraApi {
|
||||
expirationTimestamp: 0,
|
||||
};
|
||||
|
||||
static isLoggedIn() {
|
||||
return this.userAuth.authToken !== "";
|
||||
}
|
||||
|
||||
static async handleExternalAuth(auth: string) {
|
||||
const { payload } = url.parse(auth, true).query;
|
||||
|
||||
@@ -140,4 +144,9 @@ export class HydraApi {
|
||||
await this.revalidateAccessTokenIfExpired();
|
||||
return this.instance.patch(url, data, this.getAxiosConfig());
|
||||
}
|
||||
|
||||
static async delete(url: string) {
|
||||
await this.revalidateAccessTokenIfExpired();
|
||||
return this.instance.delete(url, this.getAxiosConfig());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,12 @@ import { IsNull, Not } from "typeorm";
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { getProcesses } from "@main/helpers";
|
||||
import { WindowManager } from "./window-manager";
|
||||
import { HydraApi } from "./hydra-api";
|
||||
|
||||
const gamesPlaytime = new Map<number, number>();
|
||||
const gamesPlaytime = new Map<
|
||||
number,
|
||||
{ lastTick: number; firstTick: number }
|
||||
>();
|
||||
|
||||
export const watchProcesses = async () => {
|
||||
const games = await gameRepository.find({
|
||||
@@ -37,7 +41,9 @@ export const watchProcesses = async () => {
|
||||
|
||||
if (gameProcess) {
|
||||
if (gamesPlaytime.has(game.id)) {
|
||||
const zero = gamesPlaytime.get(game.id) ?? 0;
|
||||
const gamePlaytime = gamesPlaytime.get(game.id)!;
|
||||
|
||||
const zero = gamePlaytime.lastTick;
|
||||
const delta = performance.now() - zero;
|
||||
|
||||
if (WindowManager.mainWindow) {
|
||||
@@ -48,12 +54,57 @@ export const watchProcesses = async () => {
|
||||
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
|
||||
lastTimePlayed: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
gamesPlaytime.set(game.id, performance.now());
|
||||
gamesPlaytime.set(game.id, {
|
||||
...gamePlaytime,
|
||||
lastTick: performance.now(),
|
||||
});
|
||||
} else {
|
||||
if (game.remoteId) {
|
||||
HydraApi.put(`/games/${game.remoteId}`, {
|
||||
playTimeDeltaInMilliseconds: 0,
|
||||
lastTimePlayed: new Date(),
|
||||
});
|
||||
} else {
|
||||
HydraApi.post("/games", {
|
||||
objectId: game.objectID,
|
||||
playTimeInMilliseconds: Math.round(game.playTimeInMilliseconds),
|
||||
shop: game.shop,
|
||||
lastTimePlayed: new Date(),
|
||||
}).then((response) => {
|
||||
const { id: remoteId } = response.data;
|
||||
gameRepository.update({ objectID: game.objectID }, { remoteId });
|
||||
});
|
||||
}
|
||||
|
||||
gamesPlaytime.set(game.id, {
|
||||
lastTick: performance.now(),
|
||||
firstTick: performance.now(),
|
||||
});
|
||||
}
|
||||
} else if (gamesPlaytime.has(game.id)) {
|
||||
const gamePlaytime = gamesPlaytime.get(game.id)!;
|
||||
gamesPlaytime.delete(game.id);
|
||||
|
||||
if (game.remoteId) {
|
||||
HydraApi.put(`/games/${game.remoteId}`, {
|
||||
playTimeInMilliseconds: Math.round(
|
||||
performance.now() - gamePlaytime.firstTick
|
||||
),
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
});
|
||||
} else {
|
||||
HydraApi.post("/games", {
|
||||
objectId: game.objectID,
|
||||
playTimeInMilliseconds: Math.round(game.playTimeInMilliseconds),
|
||||
shop: game.shop,
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
}).then((response) => {
|
||||
const { id: remoteId } = response.data;
|
||||
gameRepository.update({ objectID: game.objectID }, { remoteId });
|
||||
});
|
||||
}
|
||||
|
||||
if (WindowManager.mainWindow) {
|
||||
WindowManager.mainWindow.webContents.send("on-game-close", game.id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user