@@ -14,7 +14,7 @@ const config = require('@rm/config')
1414const { getAreaSql } = require ( '../utils/getAreaSql' )
1515const { filterRTree } = require ( '../utils/filterRTree' )
1616const { fetchJson } = require ( '../utils/fetchJson' )
17- const { applyManualIdFilter, parseManualIds } = require ( '../utils/manualFilter' )
17+ const { applyManualIdFilter } = require ( '../utils/manualFilter' )
1818const {
1919 IV_CALC ,
2020 LEVEL_CALC ,
@@ -131,7 +131,6 @@ class Pokemon extends Model {
131131 const { onlyIvOr, onlyHundoIv, onlyZeroIv, onlyAreas = [ ] } = args . filters
132132 const { hasSize, hasHeight, isMad, mem, secret, httpAuth, pvpV2 } = ctx
133133 const { filterMap, globalFilter } = this . getFilters ( perms , args , ctx )
134- const manualIds = parseManualIds ( args . filters . onlyManualId )
135134
136135 let queryPvp = config
137136 . getSafe ( 'api.pvp.leagues' )
@@ -148,8 +147,16 @@ class Pokemon extends Model {
148147 if ( ! noPokemonSelect ) return [ ]
149148 }
150149
150+ const manualIdRaw =
151+ typeof args . filters . onlyManualId === 'string' ||
152+ typeof args . filters . onlyManualId === 'number'
153+ ? args . filters . onlyManualId
154+ : null
155+
151156 const query = this . query ( )
152157
158+ let manualId = null
159+
153160 const pokemonIds = [ ]
154161 const pokemonForms = [ ]
155162 Object . values ( filterMap ) . forEach ( ( filter ) => {
@@ -176,8 +183,8 @@ class Pokemon extends Model {
176183 '>=' ,
177184 isMad ? this . knex ( ) . fn . now ( ) : ts ,
178185 )
179- applyManualIdFilter ( query , {
180- manualIds ,
186+ manualId = applyManualIdFilter ( query , {
187+ manualId : manualIdRaw ,
181188 latColumn : isMad ? 'pokemon.latitude' : 'lat' ,
182189 lonColumn : isMad ? 'pokemon.longitude' : 'lon' ,
183190 idColumn : isMad ? 'pokemon.encounter_id' : 'id' ,
@@ -242,13 +249,15 @@ class Pokemon extends Model {
242249 if ( onlyHundoIv && ivs ) {
243250 ivOr . orWhere ( isMad ? raw ( IV_CALC ) : 'iv' , 100 )
244251 }
245- if ( manualIds . length ) {
246- ivOr . orWhereIn ( isMad ? 'pokemon.encounter_id' : 'id' , manualIds )
252+ if ( manualId !== null ) {
253+ ivOr . orWhereIn ( isMad ? 'pokemon.encounter_id' : 'id' , [ manualId ] )
247254 }
248255 } )
249256 if ( ! getAreaSql ( query , areaRestrictions , onlyAreas , isMad , 'pokemon' ) ) {
250257 return [ ]
251258 }
259+ } else {
260+ manualId = manualIdRaw
252261 }
253262
254263 const filters = mem
@@ -301,27 +310,23 @@ class Pokemon extends Model {
301310 httpAuth ,
302311 )
303312
304- if ( mem && manualIds . length ) {
313+ if ( mem && manualId !== null ) {
305314 const loadedIds = Array . isArray ( results )
306315 ? new Set ( results . map ( ( pkmn ) => `${ pkmn . id } ` ) )
307316 : new Set ( )
308- const missingManuals = manualIds . filter ( ( id ) => ! loadedIds . has ( `${ id } ` ) )
309- if ( missingManuals . length ) {
310- const manualResults = await Promise . all (
311- missingManuals . map ( ( id ) =>
312- this . evalQuery (
313- `${ mem } /api/pokemon/id/${ id } ` ,
314- null ,
315- 'GET' ,
316- secret ,
317- httpAuth ,
318- ) . catch ( ( ) => null ) ,
319- ) ,
320- )
321- const validManuals = manualResults . filter ( Boolean )
322- results = Array . isArray ( results )
323- ? [ ...results , ...validManuals ]
324- : validManuals
317+ if ( ! loadedIds . has ( `${ manualId } ` ) ) {
318+ const manualResult = await this . evalQuery (
319+ `${ mem } /api/pokemon/id/${ manualId } ` ,
320+ null ,
321+ 'GET' ,
322+ secret ,
323+ httpAuth ,
324+ ) . catch ( ( ) => null )
325+ if ( manualResult ) {
326+ results = Array . isArray ( results )
327+ ? [ ...results , manualResult ]
328+ : [ manualResult ]
329+ }
325330 }
326331 }
327332
@@ -368,7 +373,7 @@ class Pokemon extends Model {
368373 isMad ? this . knex ( ) . fn . now ( ) : ts ,
369374 )
370375 applyManualIdFilter ( pvpQuery , {
371- manualIds ,
376+ manualId ,
372377 latColumn : isMad ? 'pokemon.latitude' : 'lat' ,
373378 lonColumn : isMad ? 'pokemon.longitude' : 'lon' ,
374379 idColumn : isMad ? 'pokemon.encounter_id' : 'id' ,
@@ -682,6 +687,11 @@ class Pokemon extends Model {
682687 const { isMad, hasSize, hasHeight, mem, secret, httpAuth } = ctx
683688 const ts = Math . floor ( Date . now ( ) / 1000 )
684689 const { filterMap, globalFilter } = this . getFilters ( perms , args , ctx )
690+ const manualIdRaw =
691+ typeof args . filters . onlyManualId === 'string' ||
692+ typeof args . filters . onlyManualId === 'number'
693+ ? args . filters . onlyManualId
694+ : null
685695 const queryLimits = config . getSafe ( 'api.queryLimits' )
686696
687697 if ( ! perms . iv && ! perms . pvp ) {
@@ -721,7 +731,18 @@ class Pokemon extends Model {
721731 ) {
722732 return [ ]
723733 }
724-
734+ const manualId = applyManualIdFilter ( query , {
735+ manualId : manualIdRaw ,
736+ latColumn : isMad ? 'pokemon.latitude' : 'lat' ,
737+ lonColumn : isMad ? 'pokemon.longitude' : 'lon' ,
738+ idColumn : isMad ? 'pokemon.encounter_id' : 'id' ,
739+ bounds : {
740+ minLat : args . minLat ,
741+ maxLat : args . maxLat ,
742+ minLon : args . minLon ,
743+ maxLon : args . maxLon ,
744+ } ,
745+ } )
725746 const filters = mem
726747 ? Object . values ( filterMap ) . flatMap ( ( filter ) => filter . buildApiFilter ( ) )
727748 : [ ]
@@ -749,24 +770,18 @@ class Pokemon extends Model {
749770 httpAuth ,
750771 )
751772
752- if ( mem && args . filters . onlyManualId ) {
753- const manualIds = parseManualIds ( args . filters . onlyManualId )
754- if ( manualIds . length ) {
755- const loaded = new Set ( results . map ( ( pkmn ) => `${ pkmn . id } ` ) )
756- const missingManuals = manualIds . filter ( ( id ) => ! loaded . has ( `${ id } ` ) )
757- if ( missingManuals . length ) {
758- const manualResults = await Promise . all (
759- missingManuals . map ( ( id ) =>
760- this . evalQuery (
761- `${ mem } /api/pokemon/id/${ id } ` ,
762- null ,
763- 'GET' ,
764- secret ,
765- httpAuth ,
766- ) . catch ( ( ) => null ) ,
767- ) ,
768- )
769- results = [ ...results , ...manualResults . filter ( ( pkmn ) => pkmn ) ]
773+ if ( mem && manualId !== null ) {
774+ const loaded = new Set ( results . map ( ( pkmn ) => `${ pkmn . id } ` ) )
775+ if ( ! loaded . has ( `${ manualId } ` ) ) {
776+ const manualResult = await this . evalQuery (
777+ `${ mem } /api/pokemon/id/${ manualId } ` ,
778+ null ,
779+ 'GET' ,
780+ secret ,
781+ httpAuth ,
782+ ) . catch ( ( ) => null )
783+ if ( manualResult ) {
784+ results = [ ...results , manualResult ]
770785 }
771786 }
772787 }
0 commit comments