aboutsummaryrefslogtreecommitdiff
path: root/src/app/js/update.js
blob: 034f3e8d457532a2f2d3c3bee1e748dcf6905630 (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
137
138
139
140
141
142
143
144
145
const ipcRenderer = require("electron").ipcRenderer;

const lang = require("../../lang");

// updates version numbers
ipcRenderer.on("version", (event, versions) => {
	vpversion.innerText = versions.vp;
	nsversion.innerText = versions.ns;
	tf2Version.innerText = versions.tf2;

	if (versions.ns == "unknown") {
		let buttons = document.querySelectorAll(".modbtns button");

		for (let i = 0; i < buttons.length; i++) {
			buttons[i].disabled = true;
		}

		// since Northstar is not installed, we cannot launch it
		update.ns.should_install = true;
		playNsBtn.innerText = lang("gui.install");
	}
}); ipcRenderer.send("get-version");

// when an update is available it'll ask the user about it
ipcRenderer.on("update-available", () => {
	if (confirm(lang("gui.update.available"))) {
		ipcRenderer.send("update-now");
	}
})

// frontend part of updating Northstar
ipcRenderer.on("ns-update-event", (event, options) => {
	let key = options.key;
	if (typeof options == "string") {
		key = options;
	}

	// updates text in update button to `lang(key)`
	let update_btn = () => {
		document.getElementById("update")
			.innerText = `(${lang(key)})`;
	}

	switch(key) {
		case "cli.update.uptodate_short":
		case "cli.update.no_internet":
			// initial value
			let delay = 0;

			// get current time
			let now = new Date().getTime();

			// check if `update.ns.last_checked` was less than 500ms
			// since now, this variable is set when `update.ns()` is
			// called
			if (now - update.ns.last_checked < 500) {
				// if less than 500ms has passed, set `delay` to the
				// amount of milliseconds missing until we've hit that
				// 500ms threshold
				delay = 500 - (now - update.ns.last_checked);
			}

			// wait `delay`ms
			setTimeout(() => {
				// set buttons accordingly
				update_btn();
				set_buttons(true);
				update.ns.progress(false);
				playNsBtn.innerText = lang("gui.launch");
			}, delay)

			break;
		default:
			update_btn();

			if (options.progress) {
				update.ns.progress(options.progress);
			}

			if (options.btn_text) {
				playNsBtn.innerText = options.btn_text;
			}

			set_buttons(false);
			break;
	}
})

let update = {
	// deletes install and update cache
	delete_cache: () => {
		ipcRenderer.send("delete-install-cache");
	},

	// updates Northstar, `force_update` forcefully updates Northstar,
	// causing it to update, even if its already up-to-date
	ns: (force_update) => {
		update.ns.last_checked = new Date().getTime();
		ipcRenderer.send("update-northstar", force_update);
		update.ns.should_install = false;
	}
}

// should Northstar be updated?
update.ns.should_install = false;

// when was the last time we checked for a Northstar update
update.ns.last_checked = new Date().getTime();

// `percent` should be a number between 0 to 100, if it's `false` it'll
// reset it back to nothing instantly, with no animation
update.ns.progress = (percent) => {
	// reset button progress
	if (percent === false) {
		document.querySelector(".contentContainer #nsMain .playBtn")
			.style.setProperty("--progress", "unset");

		return;
	}

	percent = parseInt(percent);

	// make sure we're dealing with a number
	if (isNaN(percent) || typeof percent !== "number") {
		return false;
	}

	// limit percent, while this barely has a difference, if you were to
	// set a very high number, the CSS would then use a very high
	// number, not great.
	if (percent > 100) {
		percent = 100;
	} else if (percent < 0) {
		percent = 0;
	}

	// invert number to it works in the CSS
	percent = 100 - percent;

	// set the CSS progress variable
	document.querySelector(".contentContainer #nsMain .playBtn")
		.style.setProperty("--progress", percent + "%");
}

module.exports = update;