2019-01-10 13:03:07 +01:00
|
|
|
const Core = require('./core'),
|
2019-10-16 06:56:33 +02:00
|
|
|
MinecraftVanilla = require('./minecraftvanilla'),
|
2021-02-12 18:07:50 +01:00
|
|
|
MinecraftBedrock = require('./minecraftbedrock'),
|
2019-10-16 06:56:33 +02:00
|
|
|
Gamespy3 = require('./gamespy3');
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2021-02-12 18:07:50 +01:00
|
|
|
/*
|
|
|
|
Vanilla servers respond to minecraftvanilla only
|
|
|
|
Some modded vanilla servers respond to minecraftvanilla and gamespy3, or gamespy3 only
|
|
|
|
Some bedrock servers respond to gamespy3 only
|
|
|
|
Some bedrock servers respond to minecraftbedrock only
|
|
|
|
Unsure if any bedrock servers respond to gamespy3 and minecraftbedrock
|
|
|
|
*/
|
|
|
|
|
2019-01-07 01:52:03 +01:00
|
|
|
class Minecraft extends Core {
|
2019-01-12 11:43:36 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.srvRecord = "_minecraft._tcp";
|
|
|
|
}
|
2019-01-10 13:03:07 +01:00
|
|
|
async run(state) {
|
2019-10-16 06:56:33 +02:00
|
|
|
const promises = [];
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2019-10-16 06:56:33 +02:00
|
|
|
const vanillaResolver = new MinecraftVanilla();
|
|
|
|
vanillaResolver.options = this.options;
|
|
|
|
vanillaResolver.udpSocket = this.udpSocket;
|
|
|
|
promises.push((async () => {
|
|
|
|
try { return await vanillaResolver.runOnceSafe(); } catch(e) {}
|
|
|
|
})());
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2021-02-12 18:07:50 +01:00
|
|
|
const gamespyResolver = new Gamespy3();
|
|
|
|
gamespyResolver.options = {
|
2019-10-16 06:56:33 +02:00
|
|
|
...this.options,
|
|
|
|
encoding: 'utf8',
|
|
|
|
};
|
2021-02-12 18:07:50 +01:00
|
|
|
gamespyResolver.udpSocket = this.udpSocket;
|
|
|
|
promises.push((async () => {
|
|
|
|
try { return await gamespyResolver.runOnceSafe(); } catch(e) {}
|
|
|
|
})());
|
|
|
|
|
|
|
|
const bedrockResolver = new MinecraftBedrock();
|
|
|
|
bedrockResolver.options = this.options;
|
2019-10-16 06:56:33 +02:00
|
|
|
bedrockResolver.udpSocket = this.udpSocket;
|
|
|
|
promises.push((async () => {
|
|
|
|
try { return await bedrockResolver.runOnceSafe(); } catch(e) {}
|
|
|
|
})());
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2021-02-12 18:07:50 +01:00
|
|
|
const [ vanillaState, gamespyState, bedrockState ] = await Promise.all(promises);
|
2015-01-18 08:38:45 +01:00
|
|
|
|
2019-10-16 06:56:33 +02:00
|
|
|
state.raw.vanilla = vanillaState;
|
2021-02-12 18:07:50 +01:00
|
|
|
state.raw.gamespy = gamespyState;
|
2019-10-16 06:56:33 +02:00
|
|
|
state.raw.bedrock = bedrockState;
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2021-02-12 18:07:50 +01:00
|
|
|
if (!vanillaState && !gamespyState && !bedrockState) {
|
2019-11-11 10:04:51 +01:00
|
|
|
throw new Error('No protocols succeeded');
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:07:50 +01:00
|
|
|
// Ordered from least worth to most worth (player names / etc)
|
|
|
|
if (bedrockState) {
|
|
|
|
if (bedrockState.name) state.name = bedrockState.name;
|
|
|
|
if (bedrockState.maxplayers) state.maxplayers = bedrockState.maxplayers;
|
|
|
|
if (bedrockState.players) state.players = bedrockState.players;
|
|
|
|
if (bedrockState.map) state.map = bedrockState.map;
|
|
|
|
}
|
2019-10-16 06:56:33 +02:00
|
|
|
if (vanillaState) {
|
|
|
|
try {
|
|
|
|
let name = '';
|
|
|
|
const description = vanillaState.raw.description;
|
|
|
|
if (typeof description === 'string') {
|
|
|
|
name = description;
|
|
|
|
}
|
|
|
|
if (!name && typeof description === 'object' && description.text) {
|
|
|
|
name = description.text;
|
|
|
|
}
|
|
|
|
if (!name && typeof description === 'object' && description.extra) {
|
|
|
|
name = description.extra.map(part => part.text).join('');
|
|
|
|
}
|
|
|
|
state.name = name;
|
|
|
|
} catch(e) {}
|
|
|
|
if (vanillaState.maxplayers) state.maxplayers = vanillaState.maxplayers;
|
|
|
|
if (vanillaState.players) state.players = vanillaState.players;
|
2019-01-10 13:03:07 +01:00
|
|
|
}
|
2021-02-12 18:07:50 +01:00
|
|
|
if (gamespyState) {
|
|
|
|
if (gamespyState.name) state.name = gamespyState.name;
|
|
|
|
if (gamespyState.maxplayers) state.maxplayers = gamespyState.maxplayers;
|
2021-02-12 18:36:51 +01:00
|
|
|
if (gamespyState.players.length) state.players = gamespyState.players;
|
|
|
|
else if (gamespyState.raw.numplayers) state.players = parseInt(gamespyState.raw.numplayers);
|
2019-10-16 06:56:33 +02:00
|
|
|
}
|
|
|
|
// remove dupe spaces from name
|
|
|
|
state.name = state.name.replace(/\s+/g, ' ');
|
|
|
|
// remove color codes from name
|
|
|
|
state.name = state.name.replace(/\u00A7./g, '');
|
2017-08-09 12:32:09 +02:00
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 13:12:44 +01:00
|
|
|
module.exports = Minecraft;
|