Skip to content

Commit

Permalink
Merge pull request #929 from WatWowMap/rework-searching
Browse files Browse the repository at this point in the history
Rework searching
  • Loading branch information
TurtIeSocks authored Jan 19, 2024
2 parents 5fc811e + 50b06c3 commit 33178a2
Show file tree
Hide file tree
Showing 28 changed files with 1,167 additions and 637 deletions.
1 change: 1 addition & 0 deletions packages/locales/lib/human/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
"global_search_nests": "Enter Nest Pokémon Name...",
"global_search_raids": "Enter Raid Boss Name...",
"global_search_pokemon": "Enter Pokemon Name...",
"global_search_invasions": "Enter Grunt Type, Name, or Pokemon reward name...",
"raid_level_badges": "Raid Level Badges",
"options": "Options",
"profile": "Profile",
Expand Down
1 change: 1 addition & 0 deletions packages/logger/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const HELPERS = /** @type {const} */ ({
portals: chalk.hex('#795548')('[PORTALS]'),
route: chalk.hex('#607d8b')('[ROUTE]'),
routes: chalk.hex('#9e9e9e')('[ROUTES]'),
search: chalk.hex('#795548')('[SEARCH]'),

custom: (text = '', color = '#64b5f6') =>
chalk.hex(color)(`[${text.toUpperCase()}]`),
Expand Down
1 change: 1 addition & 0 deletions packages/types/lib/scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export interface Pokemon {
pvp_rankings_great_league?: PvpEntry[]
pvp_rankings_ultra_league?: PvpEntry[]
distance?: number
shiny?: boolean
}

export type FullPokemon = FullModel<Pokemon, PokemonModel>
Expand Down
5 changes: 4 additions & 1 deletion server/src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"gyms": true,
"portals": true,
"nests": true,
"pokemon": true
"pokemon": true,
"invasions": true
},
"queryUpdateHours": {
"pokemon": 0.17,
Expand Down Expand Up @@ -98,6 +99,8 @@
"portalUpdateLimit": 30,
"weatherCellLimit": 3,
"searchResultsLimit": 15,
"searchSoftKmLimit": 10,
"searchHardKmLimit": 100,
"nestHemisphere": "north",
"gymValidDataLimit": 30,
"hideOldGyms": false,
Expand Down
12 changes: 11 additions & 1 deletion server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ const resolvers = {
}
return []
},
searchInvasion: (_, args, { perms, Db }) => {
const { category, search } = args
if (perms?.[category] && /^[0-9\s\p{L}]+$/u.test(search)) {
if (!search || !search.trim()) {
return []
}
return Db.search('Pokestop', perms, args, 'searchInvasions')
}
return []
},
searchLure: (_, args, { perms, Db }) => {
const { category, search } = args
if (perms?.[category] && /^[0-9\s\p{L}]+$/u.test(search)) {
Expand All @@ -465,7 +475,7 @@ const resolvers = {
},
searchable: (_, __, { perms }) => {
const options = config.getSafe('api.searchable')
return Object.keys(options).filter((k) => options[k] && perms[k])
return Object.keys(options).filter((k) => perms[k] && options[k])
},
spawnpoints: (_, args, { perms, Db }) => {
if (perms?.spawnpoints) {
Expand Down
8 changes: 8 additions & 0 deletions server/src/graphql/typeDefs/index.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ type Query {
locale: String
onlyAreas: [String]
): [SearchLure]
searchInvasion(
search: String
category: String
lat: Float
lon: Float
locale: String
onlyAreas: [String]
): [SearchInvasion]
searchQuest(
search: String
category: String
Expand Down
18 changes: 18 additions & 0 deletions server/src/graphql/typeDefs/map.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ type SearchLure {
lure_expire_timestamp: Int
}

type SearchInvasion {
id: ID
name: String
url: String
lat: Float
lon: Float
distance: Float
grunt_type: Int
incident_expire_timestamp: Int
confirmed: Boolean
slot_1_pokemon_id: Int
slot_1_form: Int
slot_2_pokemon_id: Int
slot_2_form: Int
slot_3_pokemon_id: Int
slot_3_form: Int
}

type SearchQuest {
id: ID
name: String
Expand Down
14 changes: 12 additions & 2 deletions server/src/models/Gym.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class Gym extends Model {
}
}

static async search(perms, args, { isMad }, distance) {
static async search(perms, args, { isMad }, distance, bbox) {
const { areaRestrictions } = perms
const { onlyAreas = [], search } = args
const query = this.query()
Expand All @@ -471,6 +471,8 @@ class Gym extends Model {
'url',
distance,
])
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.whereRaw(`LOWER(name) LIKE '%${search}%'`)
.limit(searchResultsLimit)
.orderBy('distance')
Expand All @@ -485,7 +487,13 @@ class Gym extends Model {
return query
}

static async searchRaids(perms, args, { isMad, hasAlignment }, distance) {
static async searchRaids(
perms,
args,
{ isMad, hasAlignment },
distance,
bbox,
) {
const { search, locale, onlyAreas = [] } = args
const pokemonIds = Object.keys(Event.masterfile.pokemon).filter((pkmn) =>
i18next.t(`poke_${pkmn}`, { lng: locale }).toLowerCase().includes(search),
Expand All @@ -507,6 +515,8 @@ class Gym extends Model {
: 'raid_pokemon_evolution',
distance,
])
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.whereIn(isMad ? 'pokemon_id' : 'raid_pokemon_id', pokemonIds)
.limit(searchResultsLimit)
.orderBy('distance')
Expand Down
5 changes: 4 additions & 1 deletion server/src/models/Nest.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ class Nest extends Model {
* @param {object} args
* @param {import("@rm/types").DbContext} ctx
* @param {import('objection').Raw} distance
* @param {ReturnType<typeof import("server/src/services/functions/getBbox").getBboxFromCenter>} bbox
* @returns {Promise<FullNest[]>}
*/
static async search(perms, args, { isMad }, distance) {
static async search(perms, args, { isMad }, distance, bbox) {
const { search, locale, onlyAreas = [] } = args
const pokemonIds = Object.keys(Event.masterfile.pokemon).filter((pkmn) =>
i18next.t(`poke_${pkmn}`, { lng: locale }).toLowerCase().includes(search),
Expand All @@ -159,6 +160,8 @@ class Nest extends Model {
'pokemon_form AS nest_pokemon_form',
distance,
])
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.whereNotNull('pokemon_id')
.where((builder) => {
builder
Expand Down
20 changes: 15 additions & 5 deletions server/src/models/Pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,10 @@ class Pokemon extends Model {
* @param {object} args
* @param {import("@rm/types").DbContext} ctx
* @param {number} distance
* @param {ReturnType<typeof import("server/src/services/functions/getBbox").getBboxFromCenter>} bbox
* @returns {Promise<Partial<import("@rm/types").Pokemon>[]>}
*/
static async search(perms, args, { isMad, mem, secret }, distance) {
static async search(perms, args, { isMad, mem, secret }, distance, bbox) {
const { search, locale, onlyAreas = [] } = args
const pokemonIds = Object.keys(Event.masterfile.pokemon).filter((pkmn) =>
i18next.t(`poke_${pkmn}`, { lng: locale }).toLowerCase().includes(search),
Expand All @@ -578,6 +579,8 @@ class Pokemon extends Model {
const query = this.query()
.select(['pokemon_id', distance])
.whereIn('pokemon_id', pokemonIds)
.whereBetween(isMad ? 'latitude' : 'lat', [bbox.minLat, bbox.maxLat])
.andWhereBetween(isMad ? 'longitude' : 'lon', [bbox.minLon, bbox.maxLon])
.andWhere(
isMad ? 'disappear_time' : 'expire_timestamp',
'>=',
Expand Down Expand Up @@ -618,7 +621,15 @@ class Pokemon extends Model {
latitude: args.lat,
longitude: args.lon,
},
limit: searchResultsLimit * 4,
min: {
latitude: bbox.minLat,
longitude: bbox.minLon,
},
max: {
latitude: bbox.maxLat,
longitude: bbox.maxLon,
},
limit: searchResultsLimit,
searchIds: pokemonIds.map((id) => +id),
global: {},
filters: {},
Expand All @@ -627,11 +638,10 @@ class Pokemon extends Model {
'POST',
secret,
)
if (!results) return []
return results
.filter(
(item, i) =>
i < searchResultsLimit &&
(!mem || filterRTree(item, perms.areaRestrictions, onlyAreas)),
(item) => !mem || filterRTree(item, perms.areaRestrictions, onlyAreas),
)
.map((poke) => ({
...poke,
Expand Down
Loading

0 comments on commit 33178a2

Please sign in to comment.