summaryrefslogtreecommitdiff
path: root/src/utils/gameTargets.ts
blob: 8922e98782bdf6b62a5fbe65cade8991e682d145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { GameConfigEntry, InstalledGame, WorkaroundApp } from "../api/lsfgApi";

export type GameSource = "steam" | "nonSteam" | "unknown";
export type KnownGameSource = Exclude<GameSource, "unknown">;

export interface GameTarget extends InstalledGame {
  configured: boolean;
  source: GameSource;
}

export function sourceFromNonSteam(nonSteam: boolean): KnownGameSource {
  return nonSteam ? "nonSteam" : "steam";
}

export function getTargetSource(
  appid: string,
  installedGames: InstalledGame[],
  workaroundApps: WorkaroundApp[],
): GameSource {
  const workaround = workaroundApps.find((item) => item.appid === appid);
  if (workaround) return sourceFromNonSteam(workaround.non_steam);

  const installed = installedGames.find((game) => game.appid === appid);
  return installed ? sourceFromNonSteam(installed.nonSteam) : "unknown";
}

export function mergeGameTargets(
  configs: GameConfigEntry[],
  installedGames: InstalledGame[],
  workaroundApps: WorkaroundApp[],
  runningGame: GameTarget | null = null,
): GameTarget[] {
  const configuredIds = new Set(configs.map((game) => game.appid));
  const targets = installedGames.map((game) => {
    const source = getTargetSource(game.appid, installedGames, workaroundApps);
    return {
      ...game,
      nonSteam: source === "nonSteam",
      source,
      configured: configuredIds.has(game.appid),
    };
  });

  for (const game of configs) {
    if (targets.some((target) => target.appid === game.appid)) continue;
    const source = getTargetSource(game.appid, installedGames, workaroundApps);
    targets.push({
      appid: game.appid,
      name: game.profile || `App ${game.appid}`,
      nonSteam: source === "nonSteam",
      source,
      configured: true,
    });
  }

  if (
    runningGame
    && !targets.some((target) => target.appid === runningGame.appid)
    && (runningGame.configured || runningGame.source !== "unknown")
  ) {
    targets.unshift(runningGame);
  }

  return targets;
}

export function targetsForSource(targets: GameTarget[], source: KnownGameSource): GameTarget[] {
  return targets.filter((target) => target.source === source || (target.source === "unknown" && target.configured));
}

export function sourceLabel(source: GameSource): string {
  if (source === "nonSteam") return "Non-Steam";
  if (source === "steam") return "Steam";
  return "Unknown source";
}