aboutsummaryrefslogtreecommitdiff
path: root/src/app/js/launcher.js
blob: 77d90457eef4138d69e84ddb5e5dd0cafdd5761b (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const popups = require("./popups");
const markdown = require("marked").parse;

let launcher = {};

var servercount;
var playercount;
var masterserver;

// changes the main page, this is the tabs in the sidebar
launcher.change_page = (page) => {
	let btns = document.querySelectorAll(".gamesContainer button");
	let pages = document.querySelectorAll(".mainContainer .contentContainer");

	for (let i = 0; i < pages.length; i++) {
		pages[i].classList.add("hidden");
	}

	for (let i = 0; i < btns.length; i++) {
		btns[i].classList.add("inactive");
	}

	pages[page].classList.remove("hidden");
	btns[page].classList.remove("inactive");
	bgHolder.setAttribute("bg", page);
}; launcher.change_page(1)

launcher.format_release = (notes) => {
	if (! notes) {return ""}

	let content = "";

	if (notes.length === 1) {
		content = notes[0];
	} else {
		for (let release of notes) {
			if (release.prerelease) {continue}
			let new_content = 
				// release date
				new Date(release.published_at).toLocaleString() +
				"\n" +

				// release name
				`# ${release.name}` +
				"\n\n" +

				// actual release text/body
				release.body +
				"\n\n\n";

			content +=
				"<div class='release-block'>\n"
					+ markdown(new_content, {breaks: true}) + "\n" +
				"</div>";
		}
	
		content = content.replaceAll(/\@(\S+)/g, `<a href="https://github.com/$1">@$1</a>`);
	}

	return markdown(content, {
		breaks: true
	});
}

// sets content of `div` to a single release block with centered text
// inside it, the text being `lang(lang_key)`
launcher.error = (div, lang_key) => {
	div.innerHTML =
		"<div class='release-block'>" +
			"<p><center>" +
				lang(lang_key) +
			"</center></p>" +
		"</div>";
}

// updates the Viper release notes
ipcRenderer.on("vp-notes", (event, response) => {
	if (! response) {
		return launcher.error(
			vpReleaseNotes,
			"request.no_vp_release_notes"
		)
	}

	vpReleaseNotes.innerHTML = launcher.format_release(response);
});

// updates the Northstar release notes
ipcRenderer.on("ns-notes", (event, response) => {
	if (! response) {
		return launcher.error(
			nsRelease,
			"request.no_ns_release_notes"
		)
	}

	nsRelease.innerHTML = launcher.format_release(response);
});

launcher.load_vp_notes = async () => {
	ipcRenderer.send("get-vp-notes");	
}; launcher.load_vp_notes();

launcher.load_ns_notes = async () => {
	ipcRenderer.send("get-ns-notes");
}; launcher.load_ns_notes();

// TODO: We gotta make this more automatic instead of switch statements
// it's both not pretty, but adding more sections requires way too much
// effort, compared to how it should be.
launcher.show_vp = (section) => {
	if (!["main", "release", "info", "credits"].includes(section)) throw new Error("unknown vp section");
	vpMainBtn.removeAttribute("active");
	vpReleaseBtn.removeAttribute("active");
	vpInfoBtn.removeAttribute("active");

	vpMain.classList.add("hidden");
	vpReleaseNotes.classList.add("hidden");
	vpInfo.classList.add("hidden");

	switch(section) {
		case "main":
			vpMainBtn.setAttribute("active", "");
			vpMain.classList.remove("hidden");
			break;
		case "release":
			vpReleaseBtn.setAttribute("active", "");
			vpReleaseNotes.classList.remove("hidden");
			break;
		case "info":
			vpInfoBtn.setAttribute("active", "");
			vpInfo.classList.remove("hidden");
			break;
	}
}

launcher.show_ns = (section) => {
	if (! ["main", "release", "mods"].includes(section)) {
		throw new Error("unknown ns section");
	}

	nsMainBtn.removeAttribute("active");
	nsModsBtn.removeAttribute("active");
	nsReleaseBtn.removeAttribute("active");

	nsMain.classList.add("hidden");
	nsMods.classList.add("hidden");
	nsRelease.classList.add("hidden");

	switch(section) {
		case "main":
			nsMainBtn.setAttribute("active", "");
			nsMain.classList.remove("hidden");
			break;
		case "mods":
			nsModsBtn.setAttribute("active", "");
			nsMods.style.display = "block";
			nsMods.classList.remove("hidden");
			break;
		case "release":
			nsReleaseBtn.setAttribute("active", "");
			nsRelease.classList.remove("hidden");
			break;
	}
}

// changes the active section on the currently active
// `.contentContainer` in the direction specified
//
// `direction` can be: left or right
launcher.relative_section = (direction) => {
	// prevent switching section if a popup is open
	if (popups.open_list().length) {
		return;
	}

	// the `.contentMenu` in the currently active tab
	let active_menu = document.querySelector(
		".contentContainer:not(.hidden) .contentMenu"
	)

	// get the currently active section
	let active_section = active_menu.querySelector("[active]");

	// no need to do anything, if there's somehow no active section
	if (! active_section) {return}

	// these will be filled out
	let prev_section, next_section;

	// get list of all the sections
	let sections = active_menu.querySelectorAll("li");

	for (let i = 0; i < sections.length; i++) {
		if (sections[i] != active_section) {
			continue;
		}

		// make `next_section` be the next element in `sections`
		next_section = sections[i + 1];

		// if we're at the first iteration, use the last element in
		// `sections` as the previous section, otherwise make it the
		// element before this iteration
		if (i == 0) {
			prev_section = sections[sections.length - 1];
		} else {
			prev_section = sections[i - 1];
		}
	}

	let new_section;

	// if we're going left, and a previous section was found, click it
	if (direction == "left" && prev_section) {
		new_section = prev_section;
	} else if (direction == "right") {
		// click the next section, if one was found, otherwise just
		// assume that the first section is the next section, as the
		// active section is likely just the last section, so we wrap
		// around instead
		if (next_section) {
			new_section = next_section;
		} else if (sections[0]) {
			new_section = sections[0];
		}
	}

	if (new_section) {
		new_section.click();

		// if there's an active selection, we select the new section, as
		// that selection may be in a section that's now hidden
		if (document.querySelector(".active-selection")) {
			navigate.selection(new_section);
		}
	}
}

launcher.check_servers = async () => {
	serverstatus.classList.add("checking");

	try {
		let host = "northstar.tf";
		let path = "/client/servers";

		// ask the masterserver for the list of servers, if this has
		// been done recently, it'll simply return the cached version
		let servers = JSON.parse(
			await request(host, path, "ns-servers", false)
		)

		masterserver = true;

		playercount = 0;
		servercount = servers.length;

		for (let i = 0; i < servers.length; i++) {
			playercount += servers[i].playerCount
		}
	}catch (err) {
		playercount = 0;
		servercount = 0;
		masterserver = false;
	}

	serverstatus.classList.remove("checking");

	if (servercount == 0 || ! servercount || ! playercount) {masterserver = false}

	let playerstr = lang("gui.server.players");
	if (playercount == 1) {
		playerstr = lang("gui.server.player");
	}

	if (masterserver) {
		serverstatus.classList.add("up");
		serverstatus.classList.remove("down");
		serverstatus.innerHTML = `${servercount} ${lang("gui.server.servers")} - ${playercount} ${playerstr}`;
	} else {
		serverstatus.classList.add("down");
		serverstatus.classList.remove("up");
		serverstatus.innerHTML = lang("gui.server.offline");

	}
}; launcher.check_servers()

// refreshes every 5 minutes
setInterval(() => {
	launcher.check_servers();
}, 300000)

module.exports = launcher;