aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/ui/src/stores/game.svelte.ts
blob: 504d1083f8f305b233ef16f52df9002ab3ac23ae (plain) (blame)
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
76
77
78
import { invoke } from "@tauri-apps/api/core";
import type { Version } from "../types";
import { uiState } from "./ui.svelte";
import { authState } from "./auth.svelte";
import { instancesState } from "./instances.svelte";

export class GameState {
  versions = $state<Version[]>([]);
  selectedVersion = $state("");

  constructor() {
    // Constructor intentionally empty
    // Instance switching handled in App.svelte with $effect
  }

  get latestRelease() {
    return this.versions.find((v) => v.type === "release");
  }

  async loadVersions(instanceId?: string) {
    const id = instanceId || instancesState.activeInstanceId;
    if (!id) {
      this.versions = [];
      return;
    }

    try {
      this.versions = await invoke<Version[]>("get_versions", {
        instanceId: id,
      });
      // Don't auto-select version here - let BottomBar handle version selection
      // based on installed versions only
    } catch (e) {
      console.error("Failed to fetch versions:", e);
      uiState.setStatus("Error fetching versions: " + e);
    }
  }

  async startGame() {
    if (!authState.currentAccount) {
      alert("Please login first!");
      authState.openLoginModal();
      return;
    }

    if (!this.selectedVersion) {
      alert("Please select a version!");
      return;
    }

    if (!instancesState.activeInstanceId) {
      alert("Please select an instance first!");
      uiState.setView("instances");
      return;
    }

    uiState.setStatus("Preparing to launch " + this.selectedVersion + "...");
    console.log(
      "Invoking start_game for version:",
      this.selectedVersion,
      "instance:",
      instancesState.activeInstanceId,
    );
    try {
      const msg = await invoke<string>("start_game", {
        instanceId: instancesState.activeInstanceId,
        versionId: this.selectedVersion,
      });
      console.log("Response:", msg);
      uiState.setStatus(msg);
    } catch (e) {
      console.error(e);
      uiState.setStatus("Error: " + e);
    }
  }
}

export const gameState = new GameState();