mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-27 12:51:03 +00:00
feat: refactor
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { AppUpdaterEvent } from "@types";
|
||||
import type { AppUpdaterEvent } from "@types";
|
||||
import { registerEvent } from "../register-event";
|
||||
import updater, { UpdateInfo } from "electron-updater";
|
||||
import { WindowManager } from "@main/services";
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
import type {
|
||||
AchievementData,
|
||||
GameShop,
|
||||
RemoteUnlockedAchievement,
|
||||
UnlockedAchievement,
|
||||
UserAchievement,
|
||||
} from "@types";
|
||||
import { registerEvent } from "../register-event";
|
||||
import {
|
||||
gameAchievementRepository,
|
||||
userPreferencesRepository,
|
||||
} from "@main/repository";
|
||||
import { getGameAchievementData } from "@main/services/achievements/get-game-achievement-data";
|
||||
import { HydraApi } from "@main/services";
|
||||
|
||||
const getAchievementLocalUser = async (shop: string, objectId: string) => {
|
||||
const cachedAchievements = await gameAchievementRepository.findOne({
|
||||
where: { objectId, shop },
|
||||
});
|
||||
|
||||
const achievementsData = await getGameAchievementData(objectId, shop);
|
||||
|
||||
const unlockedAchievements = JSON.parse(
|
||||
cachedAchievements?.unlockedAchievements || "[]"
|
||||
) as UnlockedAchievement[];
|
||||
|
||||
return achievementsData
|
||||
.map((achievementData) => {
|
||||
const unlockedAchiementData = unlockedAchievements.find(
|
||||
(localAchievement) => {
|
||||
return (
|
||||
localAchievement.name.toUpperCase() ==
|
||||
achievementData.name.toUpperCase()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
const icongray = achievementData.icongray.endsWith("/")
|
||||
? achievementData.icon
|
||||
: achievementData.icongray;
|
||||
|
||||
if (unlockedAchiementData) {
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: true,
|
||||
unlockTime: unlockedAchiementData.unlockTime,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: false,
|
||||
unlockTime: null,
|
||||
icongray: icongray,
|
||||
} as UserAchievement;
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.unlocked && !b.unlocked) return -1;
|
||||
if (!a.unlocked && b.unlocked) return 1;
|
||||
if (a.unlocked && b.unlocked) {
|
||||
return b.unlockTime! - a.unlockTime!;
|
||||
}
|
||||
return Number(a.hidden) - Number(b.hidden);
|
||||
});
|
||||
};
|
||||
|
||||
const getAchievementsRemoteUser = async (
|
||||
shop: string,
|
||||
objectId: string,
|
||||
userId: string
|
||||
) => {
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
|
||||
const achievementsData: AchievementData[] = await getGameAchievementData(
|
||||
objectId,
|
||||
shop
|
||||
);
|
||||
|
||||
const unlockedAchievements = await HydraApi.get<RemoteUnlockedAchievement[]>(
|
||||
`/users/${userId}/games/achievements`,
|
||||
{ shop, objectId, language: userPreferences?.language || "en" }
|
||||
);
|
||||
|
||||
return achievementsData
|
||||
.map((achievementData) => {
|
||||
const unlockedAchiementData = unlockedAchievements.find(
|
||||
(localAchievement) => {
|
||||
return (
|
||||
localAchievement.name.toUpperCase() ==
|
||||
achievementData.name.toUpperCase()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
const icongray = achievementData.icongray.endsWith("/")
|
||||
? achievementData.icon
|
||||
: achievementData.icongray;
|
||||
|
||||
if (unlockedAchiementData) {
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: true,
|
||||
unlockTime: unlockedAchiementData.unlockTime,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: false,
|
||||
unlockTime: null,
|
||||
icongray: icongray,
|
||||
} as UserAchievement;
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.unlocked && !b.unlocked) return -1;
|
||||
if (!a.unlocked && b.unlocked) return 1;
|
||||
if (a.unlocked && b.unlocked) {
|
||||
return b.unlockTime! - a.unlockTime!;
|
||||
}
|
||||
return Number(a.hidden) - Number(b.hidden);
|
||||
});
|
||||
};
|
||||
|
||||
export const getGameAchievements = async (
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
userId?: string
|
||||
): Promise<UserAchievement[]> => {
|
||||
if (!userId) {
|
||||
return getAchievementLocalUser(shop, objectId);
|
||||
}
|
||||
|
||||
return getAchievementsRemoteUser(shop, objectId, userId);
|
||||
};
|
||||
|
||||
const getGameAchievementsEvent = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
userId?: string
|
||||
): Promise<UserAchievement[]> => {
|
||||
return getGameAchievements(objectId, shop, userId);
|
||||
};
|
||||
|
||||
registerEvent("getGameAchievements", getGameAchievementsEvent);
|
||||
@@ -1,8 +1,6 @@
|
||||
import type { GameShop } from "@types";
|
||||
|
||||
import type { GameShop, GameStats } from "@types";
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import type { GameStats } from "@types";
|
||||
|
||||
const getGameStats = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { userPreferencesRepository } from "@main/repository";
|
||||
import { TrendingGame } from "@types";
|
||||
import type { TrendingGame } from "@types";
|
||||
|
||||
const getTrendingGames = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { convertSteamGameToCatalogueEntry } from "../helpers/search-games";
|
||||
import { CatalogueEntry } from "@types";
|
||||
import type { CatalogueEntry } from "@types";
|
||||
import { HydraApi } from "@main/services";
|
||||
|
||||
const searchGamesEvent = async (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { GameShop } from "@types";
|
||||
import type { GameShop } from "@types";
|
||||
import { Ludusavi } from "@main/services";
|
||||
import path from "node:path";
|
||||
import { backupsPath } from "@main/constants";
|
||||
|
||||
@@ -4,7 +4,7 @@ import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import * as tar from "tar";
|
||||
import crypto from "node:crypto";
|
||||
import { GameShop } from "@types";
|
||||
import type { GameShop } from "@types";
|
||||
import axios from "axios";
|
||||
import os from "node:os";
|
||||
import { backupsPath } from "@main/constants";
|
||||
|
||||
@@ -9,7 +9,6 @@ import "./catalogue/get-random-game";
|
||||
import "./catalogue/search-games";
|
||||
import "./catalogue/get-game-stats";
|
||||
import "./catalogue/get-trending-games";
|
||||
import "./catalogue/get-game-achievements";
|
||||
import "./hardware/get-disk-free-space";
|
||||
import "./library/add-game-to-library";
|
||||
import "./library/create-game-shortcut";
|
||||
@@ -50,6 +49,7 @@ import "./user/unblock-user";
|
||||
import "./user/get-user-friends";
|
||||
import "./user/get-user-stats";
|
||||
import "./user/report-user";
|
||||
import "./user/get-unlocked-achievements";
|
||||
import "./user/get-compared-unlocked-achievements";
|
||||
import "./profile/get-friend-requests";
|
||||
import "./profile/get-me";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { FriendRequest } from "@types";
|
||||
import type { FriendRequest } from "@types";
|
||||
|
||||
const getFriendRequests = async (
|
||||
_event: Electron.IpcMainInvokeEvent
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import * as Sentry from "@sentry/electron/main";
|
||||
import { HydraApi, logger } from "@main/services";
|
||||
import { ProfileVisibility, UserDetails } from "@types";
|
||||
import type { ProfileVisibility, UserDetails } from "@types";
|
||||
import {
|
||||
userAuthRepository,
|
||||
userSubscriptionRepository,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { UserNotLoggedInError } from "@shared";
|
||||
import { FriendRequestSync } from "@types";
|
||||
import type { FriendRequestSync } from "@types";
|
||||
|
||||
const syncFriendRequests = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
return HydraApi.get<FriendRequestSync>(`/profile/friend-requests/sync`).catch(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { FriendRequestAction } from "@types";
|
||||
import type { FriendRequestAction } from "@types";
|
||||
|
||||
const updateFriendRequest = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { UserNotLoggedInError } from "@shared";
|
||||
import { UserBlocks } from "@types";
|
||||
import type { UserBlocks } from "@types";
|
||||
|
||||
export const getBlockedUsers = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
|
||||
68
src/main/events/user/get-unlocked-achievements.ts
Normal file
68
src/main/events/user/get-unlocked-achievements.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import type { GameShop, UnlockedAchievement, UserAchievement } from "@types";
|
||||
import { registerEvent } from "../register-event";
|
||||
import { gameAchievementRepository } from "@main/repository";
|
||||
import { getGameAchievementData } from "@main/services/achievements/get-game-achievement-data";
|
||||
|
||||
export const getUnlockedAchievements = async (
|
||||
objectId: string,
|
||||
shop: GameShop
|
||||
): Promise<UserAchievement[]> => {
|
||||
const cachedAchievements = await gameAchievementRepository.findOne({
|
||||
where: { objectId, shop },
|
||||
});
|
||||
|
||||
const achievementsData = await getGameAchievementData(objectId, shop);
|
||||
|
||||
const unlockedAchievements = JSON.parse(
|
||||
cachedAchievements?.unlockedAchievements || "[]"
|
||||
) as UnlockedAchievement[];
|
||||
|
||||
return achievementsData
|
||||
.map((achievementData) => {
|
||||
const unlockedAchiementData = unlockedAchievements.find(
|
||||
(localAchievement) => {
|
||||
return (
|
||||
localAchievement.name.toUpperCase() ==
|
||||
achievementData.name.toUpperCase()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
const icongray = achievementData.icongray.endsWith("/")
|
||||
? achievementData.icon
|
||||
: achievementData.icongray;
|
||||
|
||||
if (unlockedAchiementData) {
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: true,
|
||||
unlockTime: unlockedAchiementData.unlockTime,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: false,
|
||||
unlockTime: null,
|
||||
icongray: icongray,
|
||||
} as UserAchievement;
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.unlocked && !b.unlocked) return -1;
|
||||
if (!a.unlocked && b.unlocked) return 1;
|
||||
if (a.unlocked && b.unlocked) {
|
||||
return b.unlockTime! - a.unlockTime!;
|
||||
}
|
||||
return Number(a.hidden) - Number(b.hidden);
|
||||
});
|
||||
};
|
||||
|
||||
const getGameAchievementsEvent = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
objectId: string,
|
||||
shop: GameShop
|
||||
): Promise<UserAchievement[]> => {
|
||||
return getUnlockedAchievements(objectId, shop);
|
||||
};
|
||||
|
||||
registerEvent("getUnlockedAchievements", getGameAchievementsEvent);
|
||||
@@ -1,7 +1,7 @@
|
||||
import { userAuthRepository } from "@main/repository";
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { UserFriends } from "@types";
|
||||
import type { UserFriends } from "@types";
|
||||
|
||||
export const getUserFriends = async (
|
||||
userId: string,
|
||||
|
||||
@@ -16,7 +16,7 @@ import { IsNull, Not } from "typeorm";
|
||||
const fileStats: Map<string, number> = new Map();
|
||||
const fltFiles: Map<string, Set<string>> = new Map();
|
||||
|
||||
const watchAchiievementsWindows = async () => {
|
||||
const watchAchievementsWindows = async () => {
|
||||
const games = await gameRepository.find({
|
||||
where: {
|
||||
isDeleted: false,
|
||||
@@ -53,12 +53,17 @@ const watchAchievementsWithWine = async () => {
|
||||
|
||||
if (games.length === 0) return;
|
||||
|
||||
// const user = app.getPath("home").split("/").pop()
|
||||
|
||||
// for (const game of games) {
|
||||
// }
|
||||
|
||||
// TODO: watch achievements with wine
|
||||
};
|
||||
|
||||
export const watchAchievements = async () => {
|
||||
if (process.platform === "win32") {
|
||||
return watchAchiievementsWindows();
|
||||
return watchAchievementsWindows();
|
||||
}
|
||||
|
||||
watchAchievementsWithWine();
|
||||
@@ -101,10 +106,9 @@ const compareFltFolder = async (game: Game, file: AchievementFile) => {
|
||||
}
|
||||
};
|
||||
|
||||
const compareFile = async (game: Game, file: AchievementFile) => {
|
||||
const compareFile = (game: Game, file: AchievementFile) => {
|
||||
if (file.type === Cracker.flt) {
|
||||
await compareFltFolder(game, file);
|
||||
return;
|
||||
return compareFltFolder(game, file);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -121,8 +125,7 @@ const compareFile = async (game: Game, file: AchievementFile) => {
|
||||
currentStat.mtimeMs
|
||||
);
|
||||
|
||||
await processAchievementFileDiff(game, file);
|
||||
return;
|
||||
return processAchievementFileDiff(game, file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,8 +139,9 @@ const compareFile = async (game: Game, file: AchievementFile) => {
|
||||
previousStat,
|
||||
currentStat.mtimeMs
|
||||
);
|
||||
await processAchievementFileDiff(game, file);
|
||||
return processAchievementFileDiff(game, file);
|
||||
} catch (err) {
|
||||
fileStats.set(file.filePath, -1);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,13 +3,13 @@ import {
|
||||
userPreferencesRepository,
|
||||
} from "@main/repository";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { AchievementData } from "@types";
|
||||
import type { AchievementData, GameShop } from "@types";
|
||||
import { UserNotLoggedInError } from "@shared";
|
||||
import { logger } from "../logger";
|
||||
|
||||
export const getGameAchievementData = async (
|
||||
objectId: string,
|
||||
shop: string
|
||||
shop: GameShop
|
||||
) => {
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
|
||||
@@ -6,11 +6,11 @@ import {
|
||||
import type { AchievementData, GameShop, UnlockedAchievement } from "@types";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { getGameAchievements } from "@main/events/catalogue/get-game-achievements";
|
||||
import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
|
||||
|
||||
const saveAchievementsOnLocal = async (
|
||||
objectId: string,
|
||||
shop: string,
|
||||
shop: GameShop,
|
||||
achievements: any[]
|
||||
) => {
|
||||
return gameAchievementRepository
|
||||
@@ -23,7 +23,7 @@ const saveAchievementsOnLocal = async (
|
||||
["objectId", "shop"]
|
||||
)
|
||||
.then(() => {
|
||||
return getGameAchievements(objectId, shop as GameShop)
|
||||
return getUnlockedAchievements(objectId, shop)
|
||||
.then((achievements) => {
|
||||
WindowManager.mainWindow?.webContents.send(
|
||||
`on-update-achievements-${objectId}-${shop}`,
|
||||
@@ -36,12 +36,12 @@ const saveAchievementsOnLocal = async (
|
||||
|
||||
export const mergeAchievements = async (
|
||||
objectId: string,
|
||||
shop: string,
|
||||
shop: GameShop,
|
||||
achievements: UnlockedAchievement[],
|
||||
publishNotification: boolean
|
||||
) => {
|
||||
const game = await gameRepository.findOne({
|
||||
where: { objectID: objectId, shop: shop as GameShop },
|
||||
where: { objectID: objectId, shop: shop },
|
||||
});
|
||||
|
||||
if (!game) return;
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
startTorrentClient as startRPCClient,
|
||||
} from "./torrent-client";
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { DownloadProgress } from "@types";
|
||||
import type { DownloadProgress } from "@types";
|
||||
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
||||
import { calculateETA } from "./helpers";
|
||||
import axios from "axios";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { SteamGame } from "@types";
|
||||
import type { SteamGame } from "@types";
|
||||
import { slice } from "lodash-es";
|
||||
import fs from "node:fs";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user