@@ -7,12 +7,15 @@ import md5 from 'md5';
77import rp from 'request-promise' ;
88import sr from 'sync-request' ;
99import { NotFoundError , PrivateProfileError , UnauthorizedDeveloper } from './errors' ;
10+ import { Portals } from './util/enumerations' ;
1011
1112export default class API {
13+ /** @ignore */
1214 private serviceUrl : string = 'http://api.paladins.com/paladinsapi.svc' ;
15+ /** @ignore */
1316 private sessionCache : { [ key : string ] : any } = { } ;
1417
15- constructor ( private options : { [ key : string ] : any } = { } ) {
18+ constructor ( /** @ignore */ private options : { [ key : string ] : any } = { } ) {
1619 this . options = Util . mergeDefaults ( DefaultOptions , options ) ;
1720
1821 this . setupModule ( ) ;
@@ -138,7 +141,10 @@ export default class API {
138141
139142 /**
140143 * Get an array of players with the requested name.
144+ *
145+ * Will be removed in future releases. Please use {@link API.searchPlayers} for searching.
141146 *
147+ * @deprecated
142148 * @param {string } name
143149 * @returns {Promise<any> }
144150 * @memberof API
@@ -237,6 +243,18 @@ export default class API {
237243 return this . endpoint ( 'getmatchhistory' , [ playerId ] ) ;
238244 }
239245
246+ /**
247+ * Get the queue stats of a player.
248+ *
249+ * @param {number } playerId
250+ * @param {number } queueId
251+ * @returns {Promise<any> }
252+ * @memberof API
253+ */
254+ public getPlayerQueueStats ( playerId : number , queueId : number ) : Promise < any > {
255+ return this . endpoint ( 'getqueuestats' , [ playerId , null , null , null , queueId ] ) ;
256+ }
257+
240258 /**
241259 * Get the information for an ended match.
242260 *
@@ -248,6 +266,41 @@ export default class API {
248266 return this . endpoint ( 'getmodedetails' , [ matchId ] ) ;
249267 }
250268
269+ /**
270+ * Get details on multiple matches
271+ *
272+ * @param {number[] } matchIds
273+ * @param {boolean } [returnSorted=true] Makes each match sorted in the object. If you set this to false, it may improve performance when requesting many matches but it will return everything in a single array.
274+ * @returns {Promise<any> }
275+ * @memberof API
276+ */
277+ public getMatchModeDetailsBatch ( matchIds : number [ ] , returnSorted : boolean = true ) : Promise < any > {
278+ if ( returnSorted ) {
279+ return new Promise ( ( resolve , reject ) => {
280+ this . endpoint ( 'getmatchdetailsbatch' , [ matchIds . join ( ',' ) ] )
281+ . then ( ( data ) => {
282+ let sorted : { [ key : string ] : any [ ] } = { }
283+
284+ data . forEach ( ( matchPlayer : any ) => {
285+ if ( sorted [ matchPlayer [ 'Match' ] ] ) {
286+ sorted [ matchPlayer [ 'Match' ] ] . push ( matchPlayer ) ;
287+ } else {
288+ sorted [ matchPlayer [ 'Match' ] ] = [ ] ;
289+ sorted [ matchPlayer [ 'Match' ] ] . push ( matchPlayer ) ;
290+ }
291+ } ) ;
292+
293+ return resolve ( sorted ) ;
294+ } )
295+ . catch ( ( err ) => {
296+ return reject ( err ) ;
297+ } )
298+ } )
299+ } else {
300+ return this . endpoint ( 'getmatchdetailsbatch' , [ matchIds . join ( ',' ) ] ) ;
301+ }
302+ }
303+
251304 /**
252305 * Get match details from an ended match.
253306 *
@@ -280,6 +333,35 @@ export default class API {
280333 return this . endpoint ( 'getdataused' , [ ] , true ) ;
281334 }
282335
336+ /**
337+ * Do a general player search that returns more detail information on the players.
338+ *
339+ * @param {string } name
340+ * @param {boolean } [mapPortals=false] Map the portals to their general name. WARNING: This can severely affect performance if you are doing generic names because of Switch results.
341+ * @returns {Promise<any> }
342+ * @memberof API
343+ */
344+ public searchPlayers ( name : string , mapPortals : boolean = false ) : Promise < any > {
345+ if ( mapPortals ) {
346+ return new Promise ( ( resolve , reject ) => {
347+ this . endpoint ( 'searchplayers' , [ name ] )
348+ . then ( ( data ) => {
349+ data . forEach ( ( player : any ) => {
350+ player [ 'portal_name' ] = Portals [ player [ 'portal_id' ] ] ;
351+ } ) ;
352+
353+ return resolve ( data ) ;
354+ } )
355+ . catch ( ( err ) => {
356+ return reject ( err ) ;
357+ } )
358+ } )
359+ } else {
360+ return this . endpoint ( 'searchplayers' , [ name ] ) ;
361+ }
362+ }
363+
364+ /** @ignore */
283365 private endpoint ( endpoint : string , args : Array < any > , returnFirstElement : boolean = false ) : Promise < any > {
284366 let fArgs = < any > [ endpoint ] . concat ( args ) ;
285367 let url = this . buildUrl . apply ( this , fArgs ) ;
@@ -305,14 +387,17 @@ export default class API {
305387 } )
306388 }
307389
390+ /** @ignore */
308391 private getTimestamp ( ) {
309392 return moment ( ) . utc ( ) . format ( 'YYYYMMDDHHmmss' ) ;
310393 }
311394
395+ /** @ignore */
312396 private getSignature ( method : string ) {
313397 return md5 ( `${ this . options [ 'devId' ] } ${ method } ${ this . options [ 'authKey' ] } ${ this . getTimestamp ( ) } ` )
314398 }
315399
400+ /** @ignore */
316401 private setSession ( ) : string {
317402 let response = sr ( 'GET' , `${ this . getServiceUrl ( ) } /createsessionJson/${ this . options [ 'devId' ] } /${ this . getSignature ( 'createsession' ) } /${ this . getTimestamp ( ) } ` ) ;
318403 let body = JSON . parse ( response . body . toString ( ) ) ;
@@ -332,10 +417,12 @@ export default class API {
332417 return this . sessionCache [ 'sessionId' ] ;
333418 }
334419
420+ /** @ignore */
335421 private saveSessionCache ( ) {
336422 fs . writeFileSync ( path . resolve ( __dirname , 'cache' , 'session.json' ) , JSON . stringify ( this . sessionCache ) ) ;
337423 }
338424
425+ /** @ignore */
339426 private getSession ( ) : string {
340427 if ( this . sessionCache [ 'sessionId' ] == undefined || this . sessionCache [ 'sessionId' ] == null || this . sessionCache [ 'sessionId' ] . length < 1 ) {
341428 return this . setSession ( ) ;
@@ -344,6 +431,7 @@ export default class API {
344431 return this . sessionCache [ 'sessionId' ] ;
345432 }
346433
434+ /** @ignore */
347435 private buildUrl ( method : string , player ?: any , lang ?: number , matchId ?: number , champId ?: number , queue ?: number , tier ?: number , season ?: number , platform ?: number ) {
348436 let session = this . getSession ( ) ;
349437 let baseUrl = `${ this . getServiceUrl ( ) } /${ method } Json/${ this . options [ 'devId' ] } /${ this . getSignature ( method ) } /${ session } /${ this . getTimestamp ( ) } ` ;
@@ -383,12 +471,14 @@ export default class API {
383471 return baseUrl ;
384472 }
385473
474+ /** @ignore */
386475 private makeRequest ( url : string ) {
387476 return rp ( url ) . then ( ( r : any ) => {
388477 return r ;
389478 } ) ;
390479 }
391480
481+ /** @ignore */
392482 private setupModule ( ) {
393483 try {
394484 let data = fs . readFileSync ( path . resolve ( __dirname , 'cache' , 'session.json' ) ) ;
0 commit comments