aboutsummaryrefslogtreecommitdiff
path: root/src-ui/src/main.ts
blob: 6eaee030d4def21b899777ca284244d050d62087 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { invoke } from "@tauri-apps/api";
import { listen, Event as TauriEvent } from "@tauri-apps/api/event";
import { open } from '@tauri-apps/api/dialog';
import { appDir } from '@tauri-apps/api/path';

const $ = document.querySelector.bind(document);

// Stores the overall state of the application
var globalState = {
    gamepath: "",
    installed_northstar_version: "",
    current_view: "" // Note sure if this is the right way to do it
}

document.addEventListener("DOMContentLoaded", async function () {
    // get the elements
    const helloEl = $("div.hello")! as HTMLElement;
    let counterButtonEl = $("counter-button") as HTMLElement;
    let counterResultEl = $("counter-result") as HTMLElement;
    let pingEl = $("backend-ping")! as HTMLElement;
    let panicButtonEl = $("panic-button") as HTMLElement;
    let installLocationHolderEl = $("install-location-holder") as HTMLElement;
    let versionNumberHolderEl = $("version-number-holder") as HTMLElement;
    let omniButtonEl = document.getElementById("omni-button") as HTMLElement;

    // listen backend-ping event (from Tauri Rust App)
    listen("backend-ping", function (evt: TauriEvent<any>) {
        pingEl.classList.add("on");
        setTimeout(function () {
            pingEl.classList.remove("on");
        }, 500);
    })

    // omni button click
    omniButtonEl.addEventListener("click", async function () {
        // Check if Titanfall2 install path as found
        let install_location = await invoke("find_game_install_location_caller") as string;
        if (!(install_location && install_location.length > 0)) {
            alert("Titanfall2 install not found");
            // Open a selection dialog for directories
            const selected = await open({
                directory: true,
                multiple: false,
                defaultPath: await appDir(),
            });
            if (Array.isArray(selected)) {
                // user selected multiple directories
                alert("Please only select a single directory");
            } else if (selected === null) {
                // user cancelled the selection
            } else {
                // user selected a single directory
                alert(selected);


                // TODO Verify if valid Titanfall2 install location
                let is_valid_titanfall2_install = await invoke("verify_install_location", { game_path: selected }) as boolean;
                if (is_valid_titanfall2_install) {
                    globalState.gamepath = selected;

                    // Update omni-button
                    omniButtonEl.textContent = "Install";

                    // Check for Northstar install
                    let northstar_version_number = await invoke("get_northstar_version_number_caller") as string;
                    if (northstar_version_number && northstar_version_number.length > 0) {
                        globalState.installed_northstar_version = northstar_version_number;
                        omniButtonEl.textContent = `Play (${northstar_version_number})`;
                        // Check for updated Northstar
                        let northstar_is_outdated = await invoke("check_is_northstar_outdated") as boolean;
                        if (northstar_is_outdated) {
                            omniButtonEl.textContent = "Update";
                        }
                    }
                }
                else {
                    // Not valid Titanfall2 install
                    alert("Not a valid Titanfall2 install");
                }
            }
            return;
        }

        alert("TODO");
    });

    // counter button click
    counterButtonEl.addEventListener("pointerup", async function () {
        const result = await invoke("add_count", { num: 1 }) as string;
        counterResultEl.textContent = result;
    });

    // hello click
    helloEl.addEventListener("pointerup", async function () {
        const result = await invoke("hello_world") as string;
        helloEl.textContent = result;
        setTimeout(function () {
            helloEl.textContent = "Click again";
        }, 1000);
    })

    // panic button click
    panicButtonEl.addEventListener("pointerup", async function () {
        await invoke("force_panic");
        alert("Never should have been able to get here!");
    });

    // Run the following on initial page load
    // Get version number
    let version_number_string = await invoke("get_version_number") as string;
    versionNumberHolderEl.textContent = version_number_string;

    // Get install location
    let install_location = await invoke("find_game_install_location_caller") as string;
    // Change omni-button content based on whether game install was found
    if (install_location && install_location.length > 0) {
        omniButtonEl.textContent = "Install";
        installLocationHolderEl.textContent = install_location;
        globalState.gamepath = install_location;

        // Check installed Northstar version if found
        let northstar_version_number = await invoke("get_northstar_version_number_caller") as string;
        if (northstar_version_number && northstar_version_number.length > 0) {
            globalState.installed_northstar_version = northstar_version_number;
            omniButtonEl.textContent = `Play (${northstar_version_number})`;
            let northstar_is_outdated = await invoke("check_is_northstar_outdated") as boolean;
            if (northstar_is_outdated) {
                omniButtonEl.textContent = "Update";
            }
        }
        console.log(globalState);
    }
    else {
        omniButtonEl.textContent = "Find Titanfall2 install location";
    }
})