Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions server/src/models/Pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ module.exports = class Pokemon extends Model {
* @param {import("../types").DbContext} ctx
* @returns {{ filterMap: Record<string, PkmnFilter>, globalFilter: PkmnFilter }}
*/
static getFilters(perms, args, { SubModel: _, connection: __, ...ctx }) {
static getFilters(perms, args, ctx) {
const mods = {
onlyAreas: args.filters.onlyAreas || [],
...ctx,
...Object.fromEntries(
LEVELS.map((x) => [`onlyPvp${x}`, args.filters[`onlyPvp${x}`]]),
),
}
/** @type {Record<string, PkmnFilter>} */
const filterMap = {}

Object.entries(args.filters).forEach(([key, filter]) => {
Expand Down Expand Up @@ -120,7 +121,7 @@ module.exports = class Pokemon extends Model {
* @param {import("../types").Perms} perms
* @param {object} args
* @param {import("../types").DbContext} ctx
* @returns {Promise<import('../types').Pokemon[]>}
* @returns {Promise<Partial<import('../types').Pokemon>[]>}
*/
static async getAll(perms, args, ctx) {
const { iv: ivs, pvp, areaRestrictions } = perms
Expand Down Expand Up @@ -411,7 +412,7 @@ module.exports = class Pokemon extends Model {
* @param {import("../types").Perms} perms
* @param {object} args
* @param {import("../types").DbContext} ctx
* @returns {Promise<import('../types').Pokemon[]>}
* @returns {Promise<Partial<import('../types').Pokemon>[]>}
*/
static async getLegacy(perms, args, ctx) {
const { isMad, hasSize, hasHeight, mem, secret } = ctx
Expand Down Expand Up @@ -520,7 +521,7 @@ module.exports = class Pokemon extends Model {
isMad ? this.knex().fn.now() : ts,
)
.groupBy('pokemon_id', 'form')
.orderBy('pokemon_id', 'form'),
.orderBy(['pokemon_id', 'form']),
'GET',
secret,
)
Expand Down Expand Up @@ -559,7 +560,7 @@ module.exports = class Pokemon extends Model {
* @param {object} args
* @param {import("../types").DbContext} ctx
* @param {number} distance
* @returns {Promise<import('../types').Pokemon[]>}
* @returns {Promise<Partial<import('../types').Pokemon>[]>}
*/
static async search(perms, args, { isMad, mem, secret }, distance) {
const { search, locale, onlyAreas = [] } = args
Expand Down Expand Up @@ -627,10 +628,10 @@ module.exports = class Pokemon extends Model {
)
.map((poke) => ({
...poke,
iv: perms.iv && poke.iv ? poke.iv.toFixed(2) : null,
iv: perms.iv && poke.iv ? +poke.iv.toFixed(2) : null,
distance:
poke.distance ||
getDistance(
+getDistance(
point([poke.lon, poke.lat]),
point([args.lon, args.lat]),
{
Expand Down
32 changes: 17 additions & 15 deletions server/src/services/DbCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ module.exports = class DbCheck {
async getAll(model, perms, args, userId, method = 'getAll') {
try {
const data = await Promise.all(
this.models[model].map(async (source) =>
source.SubModel[method](perms, args, source, userId),
this.models[model].map(async ({ SubModel, ...source }) =>
SubModel[method](perms, args, source, userId),
),
)
return DbCheck.deDupeResults(data)
Expand All @@ -404,8 +404,8 @@ module.exports = class DbCheck {
*/
async getOne(model, id) {
const data = await Promise.all(
this.models[model].map(async (source) =>
source.SubModel.getOne(id, source),
this.models[model].map(async ({ SubModel, ...source }) =>
SubModel.getOne(id, source),
),
)
const cleaned = DbCheck.deDupeResults(data.filter(Boolean))
Expand All @@ -422,8 +422,8 @@ module.exports = class DbCheck {
*/
async search(model, perms, args, method = 'search') {
const data = await Promise.all(
this.models[model].map(async (source) =>
source.SubModel[method](
this.models[model].map(async ({ SubModel, ...source }) =>
SubModel[method](
perms,
args,
source,
Expand All @@ -450,13 +450,13 @@ module.exports = class DbCheck {
*/
async submissionCells(perms, args) {
const stopData = await Promise.all(
this.models.Pokestop.map(async (source) =>
source.SubModel.getSubmissions(perms, args, source),
this.models.Pokestop.map(async ({ SubModel, ...source }) =>
SubModel.getSubmissions(perms, args, source),
),
)
const gymData = await Promise.all(
this.models.Gym.map(async (source) =>
source.SubModel.getSubmissions(perms, args, source),
this.models.Gym.map(async ({ SubModel, ...source }) =>
SubModel.getSubmissions(perms, args, source),
),
)
return [DbCheck.deDupeResults(stopData), DbCheck.deDupeResults(gymData)]
Expand All @@ -483,8 +483,8 @@ module.exports = class DbCheck {
async query(model, method, ...args) {
if (Array.isArray(this.models[model])) {
const data = await Promise.all(
this.models[model].map(async (source) =>
source.SubModel[method](...args, source),
this.models[model].map(async ({ SubModel, ...source }) =>
SubModel[method](...args, source),
),
)
return DbCheck.deDupeResults(data)
Expand All @@ -502,8 +502,8 @@ module.exports = class DbCheck {
log.info(HELPERS.db, `Querying available for ${model}`)
try {
const results = await Promise.all(
this.models[model].map(async (source) =>
source.SubModel.getAvailable(source),
this.models[model].map(async ({ SubModel, ...source }) =>
SubModel.getAvailable(source),
),
)
log.info(HELPERS.db, `Setting available for ${model}`)
Expand Down Expand Up @@ -557,7 +557,9 @@ module.exports = class DbCheck {
async getFilterContext() {
if (this.models.Route) {
const results = await Promise.all(
this.models.Route.map((source) => source.SubModel.getFilterContext()),
this.models.Route.map(({ SubModel, ...source }) =>
SubModel.getFilterContext(source),
),
)
this.filterContext.Route.maxDistance = Math.max(
...results.map((result) => result.max_distance),
Expand Down
35 changes: 19 additions & 16 deletions server/src/services/filters/pokemon/Backend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */
const config = require('../../config')
const { KEYS, MAD_KEY_MAP, STANDARD, LEAGUES } = require('./constants')
const config = require('config')
const { KEYS, STANDARD, LEAGUES } = require('./constants')
const {
deepCompare,
between,
Expand All @@ -12,11 +12,14 @@ const { filterRTree } = require('../../functions/filterRTree')
const { Event, Pvp } = require('../../initialization')
const { log, HELPERS } = require('../../logger')

/** @type {import('../../../types').Config['api']['pvp']} */
const pvpConfig = config.get('api.pvp')

module.exports = class PkmnBackend {
/**
* @param {`${number}-${number}` | 'global'} id
* @param {import('./constants').PkmnFilter} filter
* @param {import('./constants').PkmnFilter} global
* @param {import("./Frontend")} filter
* @param {import("./Frontend")} global
* @param {object} perms
* @param {boolean} perms.pokemon
* @param {boolean} perms.iv
Expand Down Expand Up @@ -139,8 +142,8 @@ module.exports = class PkmnBackend {
}

/**
* @param {string} filter
* @returns {(pokemon?: import("../../../types").Pokemon) => boolean}
* @param {boolean} global
* @returns {(pokemon?: Partial<import("../../../types").Pokemon>) => boolean}
*/
getCallback(global = false) {
const filter = global ? this.global : this.filter
Expand All @@ -155,14 +158,14 @@ module.exports = class PkmnBackend {
}

/**
* @param {import("./constants").PkmnFilter} filter
* @param {import("./Frontend")} filter
* @returns {Set<(typeof import("./constants").KEYS)[number]>}
*/
getRelevantKeys(filter = this.filter) {
return new Set(
KEYS.filter(
(key) =>
(config.api.pvp.leagueObj[key] ? this.perms.pvp : this.perms.iv) &&
(pvpConfig.leagueObj[key] ? this.perms.pvp : this.perms.iv) &&
this.isActive(key, filter),
),
)
Expand Down Expand Up @@ -198,15 +201,15 @@ module.exports = class PkmnBackend {

const cpCheck =
this.mods.pvpV2 ||
config.api.pvp.reactMapHandlesPvp ||
entry.cp >= config.api.pvp.minCp[league]
pvpConfig.reactMapHandlesPvp ||
entry.cp >= pvpConfig.minCp[league]
if (!cpCheck) return false

const megaCheck = !entry.evolution || this.mods.onlyPvpMega
if (!megaCheck) return false

const capCheck =
this.mods.pvpV2 || config.api.pvp.reactMapHandlesPvp
this.mods.pvpV2 || pvpConfig.reactMapHandlesPvp
? entry.capped || this.mods[`onlyPvp${entry.cap}`]
: true
if (!capCheck) return false
Expand All @@ -216,8 +219,8 @@ module.exports = class PkmnBackend {

/**
* @param {typeof import("./constants").LEAGUES[number]} league
* @param {import("../../PvpWrapper").PokemonEntry[]} data
* @returns {{ best: number; filtered: import("../../PvpWrapper").PokemonEntry[]}}
* @param {import('../../../types').PvpEntry[]} data
* @returns {{ best: number; filtered: import('../../../types').PvpEntry[]}}
*/
getRanks(league, data) {
const filtered =
Expand Down Expand Up @@ -283,7 +286,7 @@ module.exports = class PkmnBackend {
}

/**
* @param {import('../../../types').Pokemon} pokemon
* @param {Partial<import('../../../types').Pokemon>} pokemon
* @return {boolean}
*/
valid(pokemon) {
Expand Down Expand Up @@ -312,13 +315,13 @@ module.exports = class PkmnBackend {
* @returns {{ cleanPvp: { [key in typeof LEAGUES[number]]?: number[] }, bestPvp: number }}
*/
buildPvp(pokemon, safeTs = Math.floor(Date.now() / 1000)) {
const parsed = config.api.pvp.reactMapHandlesPvp
const parsed = pvpConfig.reactMapHandlesPvp
? Pvp.resultWithCache(pokemon, safeTs)
: getParsedPvp(pokemon)
const cleanPvp = {}
let bestPvp = 4096
Object.keys(parsed).forEach((league) => {
if (config.api.pvp.leagueObj[league]) {
if (pvpConfig.leagueObj[league]) {
const { filtered, best } = this.getRanks(league, parsed[league])
if (filtered.length) {
cleanPvp[league] = filtered
Expand Down
23 changes: 0 additions & 23 deletions server/src/services/filters/pokemon/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,6 @@ const MAD_KEY_MAP = /** @type {const} */ ({
cp: 'cp',
})

/**
* @typedef {object} PkmnFilter
* @property {number[]} standard.iv
* @property {number[]} standard.level
* @property {number[]} standard.atk_iv
* @property {number[]} standard.def_iv
* @property {number[]} standard.sta_iv
* @property {number[]} standard.cp
* @property {number[]} standard.great
* @property {number[]} standard.ultra
* @property {number[]} standard.little
* @property {number} standard.gender
* @property {boolean} standard.xxs
* @property {boolean} standard.xxl
* @property {string} standard.size
* @property {string} standard.adv
* @property {boolean} standard.enabled
*/

/**
* @type {PkmnFilter}
*/

const STANDARD = new PokemonFilter()

module.exports = {
Expand Down
8 changes: 4 additions & 4 deletions server/src/services/functions/filterRTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { consolidateAreas } = require('./consolidateAreas')

/**
* Filters via RTree in place of MySQL query when using in memory data
* @template {{ lat: number, lon: number }} T
* @template {{ lat?: number, lon?: number }} T
* @param {T} item
* @param {string[]} areaRestrictions
* @param {string[]} onlyAreas
Expand All @@ -21,8 +21,8 @@ function filterRTree(item, areaRestrictions = [], onlyAreas = []) {

const foundFeatures = config.areas.myRTree
.search({
x: item.lon,
y: item.lat,
x: item.lon || 0,
y: item.lat || 0,
w: 0,
h: 0,
})
Expand All @@ -31,7 +31,7 @@ function filterRTree(item, areaRestrictions = [], onlyAreas = []) {
const foundInRtree =
foundFeatures.length &&
foundFeatures.some((feature) =>
pointInPolygon(point([item.lon, item.lat]), feature),
pointInPolygon(point([item.lon || 0, item.lat || 0]), feature),
)

return foundInRtree
Expand Down
4 changes: 4 additions & 0 deletions server/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export interface Pokemon {
first_seen_timestamp: number
expire_timestamp_verified: boolean
updated: number
pvp: { [league in (typeof LEAGUES)[number]]?: PvpEntry[] }
pvp_rankings_great_league?: PvpEntry[]
pvp_rankings_ultra_league?: PvpEntry[]
distance?: number
}

export interface AvailablePokemon {
Expand Down