aboutsummaryrefslogtreecommitdiff
path: root/src-vue/src/plugins/store.ts
blob: 14b9641575bfada3902d5ba4d557673d4486c363 (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
import { createStore } from 'vuex';
import { listen, Event as TauriEvent } from "@tauri-apps/api/event";
import {Tabs} from "../utils/Tabs";
import {invoke} from "@tauri-apps/api";
import {GameInstall} from "../utils/GameInstall";
import {ReleaseCanal} from "../utils/ReleaseCanal";
import { ElNotification } from 'element-plus';

export const store = createStore({
    state () {
        return {
            current_tab: Tabs.PLAY,
            developer_mode: false,
            game_path: "this/is/the/game/path",

            installed_northstar_version: "Unknown version",

            northstar_is_running: false,
            origin_is_running: false
        }
    },
    mutations: {
        toggleDeveloperMode(state) {
            state.developer_mode = !state.developer_mode;

            // Reset tab when closing dev mode.
            if (!state.developer_mode) {
                store.commit('updateCurrentTab', Tabs.PLAY);
            }
        },
        initialize(state) {
            _initializeApp(state);
            // _checkForFlightCoreUpdates(state);
            _initializeListeners(state);
        },
        updateCurrentTab(state: any, newTab: Tabs) {
            state.current_tab = newTab;
        },
        launchGame(state: any) {
            // TODO update installation if release track was switched
            // TODO install northstar if it wasn't detected
            // Show an error message if Origin is not running.
            if (!state.origin_is_running) {
                ElNotification({
                    title: 'Origin is not running',
                    message: "Northstar cannot launch while you're not authenticated with Origin.",
                    type: 'warning',
                    position: 'bottom-right'
                });
            }

            // TODO launch game
        }
    }
});

/**
 * This is called when application root component has been mounted.
 * It invokes all Rust methods that are needed to initialize UI.
 */
async function _initializeApp(state: any) {
    const result: GameInstall = await invoke("find_game_install_location_caller")
        .catch((err) => {
            // Gamepath not found or other error
            console.error(err);
            alert(err);
        });
    state.game_path = result.game_path;

    // Check installed Northstar version if found
    await _get_northstar_version_number(state);
}

// TODO
async function _checkForFlightCoreUpdates(state: any) {
    // Get version number
    let version_number_string = await invoke("get_version_number") as string;
    // Check if up-to-date
    let flightcore_is_outdated = await invoke("check_is_flightcore_outdated_caller") as boolean;
}

/**
 * This registers callbacks listening to events from Rust-backend.
 * Those events include Origin and Northstar running state.
 */
function _initializeListeners(state: any) {
    listen("origin-running-ping", function (evt: TauriEvent<any>) {
        state.origin_is_running = evt.payload as boolean;
    });

    listen("northstar-running-ping", function (evt: TauriEvent<any>) {
        state.northstar_is_running = evt.payload as boolean;
    });
}

/**
 * This retrieves Northstar version tag, and stores it in application 
 * state, for it to be displayed in UI.
 */
async function _get_northstar_version_number(state: any) {
    let northstar_version_number: string = await invoke("get_northstar_version_number_caller", { gamePath: state.game_path });
    if (northstar_version_number && northstar_version_number.length > 0) {
        state.installed_northstar_version = northstar_version_number;

        await invoke("check_is_northstar_outdated", { gamePath: state.game_path, northstarPackageName: ReleaseCanal.RELEASE })
            .then((message) => {
                console.log(message);
            })
            .catch((error) => {
                console.error(error);
                alert(error);
            });
    }
}