-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (51 loc) · 1.63 KB
/
app.js
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
function isValidIpv4Addr(ip) {
return /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/.test(ip);
}
function decodeUTF8(str) {
return new TextDecoder('utf-8').decode(new Uint8Array(str.split('').map(function(c) {
return c.charCodeAt(0);
})));
}
new Vue({
el: '#app',
data: {
directIp: '',
search: '',
servers: [
{ version: "...", name: "Loading...", players: 0, maxplayers: "0", ip: "0", port: 0 },
],
},
computed: {
filteredServers() {
return this.servers.filter(server => {
return server.name.toLowerCase().includes(this.search.toLowerCase());
});
}
},
mounted() {
axios
.get('https://mtasa.com/api/')
.then(({ data: servers }) => {
const orderedServersByPlayers = servers.sort((a, b) => a.players > b.players ? -1 : 1);
orderedServersByPlayers.forEach(server => { server.name = decodeUTF8(server.name); });
this.servers = orderedServersByPlayers;
});
},
methods: {
openServer(server) {
window.location.href = `mtasa://${server.ip}:${server.port}`;
},
connectToIp() {
const [ip, port] = this.directIp.split(':');
if (!isValidIpv4Addr(ip)) {
alert('This isn\'t a valid IP');
return;
}
if (!port) {
alert('You\'re missing the port');
return;
}
window.location.href = `mtasa://${this.directIp}`;
}
}
})