2016-07-05 19:36:29 +00:00
var mcpe _ping = require ( 'mcpe-ping-fixed' ) ;
2015-11-02 07:42:11 +00:00
var mcpc _ping = require ( 'mc-ping-updated' ) ;
2015-11-02 04:56:08 +00:00
2020-03-30 06:31:35 +00:00
var logger = require ( './logger' ) ;
2015-11-09 00:34:17 +00:00
var util = require ( './util' ) ;
2015-11-03 07:40:09 +00:00
2015-11-02 08:24:55 +00:00
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
2016-03-02 03:09:38 +00:00
function pingMinecraftPC ( host , port , timeout , callback , version ) {
2015-11-09 00:34:17 +00:00
var startTime = util . getCurrentTimeMs ( ) ;
2015-11-02 04:56:08 +00:00
2015-11-26 01:29:00 +00:00
mcpc _ping ( host , port , function ( err , res ) {
if ( err ) {
callback ( err , null ) ;
} else {
2019-09-05 21:15:44 +00:00
// Remap our JSON into our custom structure.
var favicon ;
// Ensure the returned favicon is a data URI
if ( res . favicon . indexOf ( 'data:image/' ) === 0 ) {
favicon = res . favicon ;
}
2015-11-26 01:29:00 +00:00
callback ( null , {
players : {
2020-03-30 06:31:35 +00:00
online : capPlayerCount ( host , parseInt ( res . players . online ) ) ,
2019-09-05 21:15:44 +00:00
max : parseInt ( res . players . max )
2015-11-26 01:29:00 +00:00
} ,
2019-09-05 21:15:44 +00:00
version : parseInt ( res . version . protocol ) ,
2015-11-26 01:29:00 +00:00
latency : util . getCurrentTimeMs ( ) - startTime ,
2019-09-05 21:15:44 +00:00
favicon
2015-11-26 01:29:00 +00:00
} ) ;
}
2016-03-02 03:09:38 +00:00
} , timeout , version ) ;
2015-11-02 07:04:49 +00:00
}
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
2015-11-02 06:57:30 +00:00
function pingMinecraftPE ( host , port , timeout , callback ) {
2015-11-09 00:34:17 +00:00
var startTime = util . getCurrentTimeMs ( ) ;
2015-11-26 01:29:00 +00:00
mcpe _ping ( host , port || 19132 , function ( err , res ) {
if ( err ) {
callback ( err , null ) ;
} else {
// Remap our JSON into our custom structure.
callback ( err , {
players : {
2020-03-30 06:31:35 +00:00
online : capPlayerCount ( host , parseInt ( res . currentPlayers ) ) ,
2015-11-26 01:29:00 +00:00
max : parseInt ( res . maxPlayers )
} ,
version : res . version ,
latency : util . getCurrentTimeMs ( ) - startTime
} ) ;
}
} , timeout ) ;
2015-11-02 06:57:30 +00:00
}
2020-03-30 06:31:35 +00:00
// player count can be up to 1^32-1, which is a massive scale and destroys browser performance when rendering graphs
// Artificially cap and warn to prevent propogating garbage
function capPlayerCount ( host , playerCount ) {
const maxPlayerCount = 250000 ;
if ( playerCount !== Math . min ( playerCount , maxPlayerCount ) ) {
logger . log ( 'warn' , '%s returned a player count of %d, Minetrack has capped it to %d to prevent browser performance issues with graph rendering. If this is in error, please edit maxPlayerCount in ping.js!' , host , playerCount , maxPlayerCount ) ;
return maxPlayerCount ;
} else if ( playerCount !== Math . max ( playerCount , 0 ) ) {
logger . log ( 'warn' , '%s returned an invalid player count of %d, setting to 0.' , host , playerCount ) ;
return 0 ;
}
return playerCount ;
}
2016-03-02 03:09:38 +00:00
exports . ping = function ( host , port , type , timeout , callback , version ) {
2015-11-02 04:56:08 +00:00
if ( type === 'PC' ) {
2016-06-09 03:26:32 +00:00
util . unfurlSRV ( host , port , function ( host , port ) {
pingMinecraftPC ( host , port || 25565 , timeout , callback , version ) ;
} )
2015-11-02 04:56:08 +00:00
} else if ( type === 'PE' ) {
2015-11-02 06:57:30 +00:00
pingMinecraftPE ( host , port || 19132 , timeout , callback ) ;
2015-11-02 04:56:08 +00:00
} else {
throw new Error ( 'Unsupported type: ' + type ) ;
}
2016-03-02 03:09:38 +00:00
} ;