2019-10-16 18:47:53 -04:00
const fs = require ( 'fs' )
2021-12-19 23:23:50 -05:00
const colors = require ( '../misc/icons/colors.json' ) ;
2019-10-15 22:42:47 -04:00
2019-10-16 18:47:53 -04:00
module . exports = async ( app , req , res , api , getLevels ) => {
2019-10-15 22:42:47 -04:00
2021-01-29 23:12:48 -05:00
if ( req . offline ) {
if ( ! api ) return res . redirect ( '/search/' + req . params . id )
2021-12-07 14:14:56 -05:00
else return res . sendError ( )
2021-01-29 23:12:48 -05:00
}
2020-11-07 19:20:44 -05:00
let username = getLevels || req . params . id
2021-08-01 19:22:35 -04:00
let probablyID
if ( username . endsWith ( "." ) && req . isGDPS ) {
username = username . slice ( 0 , - 1 )
probablyID = Number ( username )
}
2020-11-07 19:20:44 -05:00
let accountMode = ! req . query . hasOwnProperty ( "player" ) && Number ( req . params . id )
2021-01-21 17:15:31 -05:00
let foundID = app . userCache ( req . id , username )
2021-08-01 19:22:35 -04:00
let skipRequest = accountMode || foundID || probablyID
2021-01-17 00:05:06 -05:00
let searchResult ;
2020-09-10 09:02:40 -04:00
2020-11-07 19:20:44 -05: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)
2021-08-01 19:22:35 -04:00
req . gdRequest ( skipRequest ? "" : 'getGJUsers20' , skipRequest ? { } : { str : username , page : 0 } , function ( err1 , res1 , b1 ) {
2021-01-17 00:05:06 -05:00
if ( foundID ) searchResult = foundID [ 0 ]
2021-08-01 19:22:35 -04:00
else if ( accountMode || err1 || b1 == '-1' || b1 . startsWith ( "<" ) || ! b1 ) searchResult = probablyID ? username : req . params . id
2021-01-18 21:54:18 -05:00
else if ( ! req . isGDPS ) searchResult = app . parseResponse ( b1 . split ( "|" ) [ 0 ] ) [ 16 ]
2021-01-17 00:05:06 -05:00
else { // GDPS's return multiple users, GD no longer does this
let userResults = b1 . split ( "|" ) . map ( x => app . parseResponse ( x ) )
searchResult = userResults . find ( x => x [ 1 ] . toLowerCase ( ) == username . toLowerCase ( ) || x [ 2 ] == username ) || ""
if ( searchResult ) searchResult = searchResult [ 16 ]
}
2021-01-14 18:18:19 -05:00
2020-11-07 19:20:44 -05:00
if ( getLevels ) {
req . params . text = foundID ? foundID [ 1 ] : app . parseResponse ( b1 ) [ 2 ]
return app . run . search ( app , req , res )
}
2019-10-15 22:42:47 -04:00
2021-01-18 21:54:18 -05:00
req . gdRequest ( 'getGJUserInfo20' , { targetAccountID : searchResult } , function ( err2 , res2 , body ) {
2021-01-17 00:05:06 -05:00
let account = app . parseResponse ( body || "" )
2021-01-18 21:54:18 -05:00
let dumbGDPSError = req . isGDPS && ( ! account [ 16 ] || account [ 1 ] . toLowerCase ( ) == "undefined" )
2021-01-17 00:05:06 -05:00
2021-08-18 20:10:35 -04:00
if ( err2 || dumbGDPSError ) {
2019-10-15 22:42:47 -04:00
if ( ! api ) return res . redirect ( '/search/' + req . params . id )
2021-12-07 14:14:56 -05:00
else return res . sendError ( )
2019-10-15 22:42:47 -04:00
}
2020-11-07 19:20:44 -05:00
2021-01-21 17:15:31 -05:00
if ( ! foundID ) app . userCache ( req . id , account [ 16 ] , account [ 2 ] , account [ 1 ] )
2020-11-26 16:04:42 -05:00
2020-11-07 19:20:44 -05:00
let userData = {
2020-11-26 16:04:42 -05:00
username : account [ 1 ] || "[MISSINGNO.]" ,
2020-11-07 19:20:44 -05: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 16:04:42 -05:00
deathEffect : + account [ 48 ] || 1 ,
2020-11-07 19:20:44 -05:00
glow : account [ 28 ] == "1" ,
2021-12-19 23:23:50 -05:00
col1RGB : colors [ account [ 10 ] ] || colors [ "0" ] ,
col2RGB : colors [ account [ 11 ] ] || colors [ "3" ]
2019-10-15 22:42:47 -04:00
}
2020-11-07 19:20:44 -05:00
2021-12-07 11:06:33 -08:00
if ( api ) return res . status ( 200 ) . send ( userData )
2019-10-15 22:42:47 -04:00
2020-11-07 19:20:44 -05:00
else fs . readFile ( './html/profile.html' , 'utf8' , function ( err , data ) {
2019-10-15 22:42:47 -04: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 ] ) )
} )
2021-12-07 11:06:33 -08:00
return res . status ( 200 ) . send ( html )
2019-10-15 22:42:47 -04:00
} )
} )
} )
}