summaryrefslogtreecommitdiffstatshomepage
path: root/ui/src/stores/game.svelte.ts
blob: 0af3dafd75f000f4c3ac7fb3ce130b43db8d4751 (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
import { invoke } from "@tauri-apps/api/core";
import type { Version } from "../types";
import { uiState } from "./ui.svelte";
import { authState } from "./auth.svelte";

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

  async loadVersions() {
    try {
      this.versions = await invoke<Version[]>("get_versions");
      if (this.versions.length > 0) {
        const latest = this.versions.find((v) => v.type === "release");
        this.selectedVersion = latest ? latest.id : this.versions[0].id;
      }
    } 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;
    }

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

export const gameState = new GameState();