2019-10-16 19:47:53 -03:00
const request = require ( 'request' )
const fs = require ( 'fs' )
2019-10-15 23:42:47 -03:00
2019-10-16 19:47:53 -03:00
module . exports = async ( app , req , res , api , getLevels ) => {
2019-10-15 23:42:47 -03:00
2020-09-10 10:02:40 -03:00
if ( app . offline ) return res . send ( "-1" )
2020-11-07 21:20:44 -03:00
let username = getLevels || req . params . id
let accountMode = ! req . query . hasOwnProperty ( "player" ) && Number ( req . params . id )
let foundID = app . accountCache [ username . toLowerCase ( ) ]
let skipRequest = accountMode || foundID
2020-09-10 10:02:40 -03:00
2020-11-07 21:20:44 -03:00
// if you're searching by account id, an intentional error is caused to skip the first request to the gd servers. see i pulled a sneaky on ya. (fuck callbacks man)
request . post ( skipRequest ? "" : app . endpoint + 'getGJUsers20.php' , skipRequest ? { } : req . gdParams ( { str : username } ) , function ( err1 , res1 , b1 ) {
let searchResult = foundID ? foundID [ 0 ] : ( accountMode || err1 || b1 == '-1' || b1 . startsWith ( "<!" ) || ! b1 ) ? req . params . id : app . parseResponse ( b1 ) [ 16 ]
2019-11-10 18:18:52 -03:00
2020-11-07 21:20:44 -03:00
if ( getLevels ) {
req . params . text = foundID ? foundID [ 1 ] : app . parseResponse ( b1 ) [ 2 ]
return app . run . search ( app , req , res )
}
2019-10-15 23:42:47 -03:00
2020-11-01 17:29:32 -03:00
request . post ( app . endpoint + 'getGJUserInfo20.php' , req . gdParams ( { targetAccountID : searchResult } ) , function ( err2 , res2 , body ) {
2019-10-15 23:42:47 -03:00
2019-11-10 18:18:52 -03:00
if ( err2 || body == '-1' || ! body ) {
2019-10-15 23:42:47 -03:00
if ( ! api ) return res . redirect ( '/search/' + req . params . id )
else return res . send ( "-1" )
}
let account = app . parseResponse ( body )
2020-11-07 21:20:44 -03:00
if ( ! foundID && app . config . cacheAccountIDs ) app . accountCache [ username . toLowerCase ( ) ] = [ account [ 16 ] , account [ 2 ] ]
2020-11-26 18:04:42 -03:00
2020-11-07 21:20:44 -03:00
let userData = {
2020-11-26 18:04:42 -03:00
username : account [ 1 ] || "[MISSINGNO.]" ,
2020-11-07 21:20:44 -03:00
playerID : account [ 2 ] ,
accountID : account [ 16 ] ,
rank : + account [ 30 ] ,
stars : + account [ 3 ] ,
diamonds : + account [ 46 ] ,
coins : + account [ 13 ] ,
userCoins : + account [ 17 ] ,
demons : + account [ 4 ] ,
cp : + account [ 8 ] ,
friendRequests : account [ 19 ] == "0" ,
messages : account [ 18 ] == "0" ? "all" : account [ 18 ] == "1" ? "friends" : "off" ,
commentHistory : account [ 50 ] == "0" ? "all" : account [ 50 ] == "1" ? "friends" : "off" ,
moderator : + account [ 49 ] ,
youtube : account [ 20 ] || null ,
twitter : account [ 44 ] || null ,
twitch : account [ 45 ] || null ,
icon : + account [ 21 ] ,
ship : + account [ 22 ] ,
ball : + account [ 23 ] ,
ufo : + account [ 24 ] ,
wave : + account [ 25 ] ,
robot : + account [ 26 ] ,
spider : + account [ 43 ] ,
col1 : + account [ 10 ] ,
col2 : + account [ 11 ] ,
2020-11-26 18:04:42 -03:00
deathEffect : + account [ 48 ] || 1 ,
2020-11-07 21:20:44 -03:00
glow : account [ 28 ] == "1" ,
2019-10-15 23:42:47 -03:00
}
2020-11-07 21:20:44 -03:00
if ( api ) return res . send ( userData )
2019-10-15 23:42:47 -03:00
2020-11-07 21:20:44 -03:00
else fs . readFile ( './html/profile.html' , 'utf8' , function ( err , data ) {
2019-10-15 23:42:47 -03:00
let html = data ;
let variables = Object . keys ( userData )
variables . forEach ( x => {
let regex = new RegExp ( ` \\ [ \\ [ ${ x . toUpperCase ( ) } \\ ] \\ ] ` , "g" )
html = html . replace ( regex , app . clean ( userData [ x ] ) )
} )
return res . send ( html )
} )
} )
} )
}