Skip to content

Commit

Permalink
fix: more consistent gender filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Feb 12, 2024
1 parent 6935e89 commit a3b24cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
60 changes: 34 additions & 26 deletions server/src/services/filters/pokemon/Backend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-vars */
const config = require('@rm/config')
const { log, HELPERS } = require('@rm/logger')
const { KEYS, STANDARD, LEAGUES } = require('./constants')
const { KEYS, AND_KEYS, STANDARD, LEAGUES } = require('./constants')
const {
deepCompare,
between,
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = class PkmnBackend {
this.expertFilter = this.getCallback(id === 'global')
this.expertGlobal = this.getCallback(true)
this.isEqualToGlobal =
this.expertFilter.toString() === this.expertGlobal.toString()
this.expertFilter?.toString() === this.expertGlobal?.toString()
}

get keyArray() {
Expand Down Expand Up @@ -122,17 +122,6 @@ module.exports = class PkmnBackend {
orStr += `LC${filter.little.join('-')}`
}
}
if (this.perms.iv) {
if (this.mods.onlyZeroIv) {
if (orStr) orStr += '|'
orStr += `0`
}
if (this.mods.onlyHundoIv) {
if (orStr) orStr += '|'
orStr += `100`
}
}

if (andStr && !(andStr.startsWith('(') && andStr.endsWith(')')) && orStr) {
andStr = `(${andStr})`
}
Expand All @@ -149,7 +138,6 @@ module.exports = class PkmnBackend {
orStr,
merged,
})

return merged
}

Expand Down Expand Up @@ -299,10 +287,9 @@ module.exports = class PkmnBackend {
}
const results = /** @type {import('@rm/types').DnfFilter[]} */ ([])
if (
['iv', 'atk_iv', 'def_iv', 'sta_iv', 'cp', 'level', 'gender'].some((k) =>
this.filterKeys.has(k),
) &&
this.perms.iv
this.perms.iv &&
((this.filterKeys.has('gender') && this.filterKeys.size === 1) ||
AND_KEYS.some((k) => this.filterKeys.has(k)))
) {
results.push({
pokemon,
Expand Down Expand Up @@ -330,20 +317,37 @@ module.exports = class PkmnBackend {
if (this.perms.pvp) {
Object.entries(rest).forEach(([league, values]) => {
if (Array.isArray(values) && this.filterKeys.has(league)) {
results.push({
/** @type {import('@rm/types').DnfFilter} */
const pvpFilter = {
pokemon,
[`pvp_${league}`]: PkmnBackend.ensureSafe(
values,
STANDARD[league]?.[1],
),
})
}
if (this.filterKeys.has('gender')) {
pvpFilter.gender = { min: gender, max: gender }
}
results.push(pvpFilter)
}
})
}
if (this.filterKeys.has('xxs'))
results.push({ pokemon, size: { min: 1, max: 1 } })
if (this.filterKeys.has('xxl'))
results.push({ pokemon, size: { min: 5, max: 5 } })
if (this.filterKeys.has('xxs')) {
/** @type {import('@rm/types').DnfFilter} */
const xxsFilter = { pokemon, size: { min: 1, max: 1 } }
if (this.filterKeys.has('gender')) {
xxsFilter.gender = { min: gender, max: gender }
}
results.push(xxsFilter)
}
if (this.filterKeys.has('xxl')) {
/** @type {import('@rm/types').DnfFilter} */
const xxlFilter = { pokemon, size: { min: 5, max: 5 } }
if (this.filterKeys.has('gender')) {
xxlFilter.gender = { min: gender, max: gender }
}
results.push(xxlFilter)
}
return results
}

Expand All @@ -358,10 +362,13 @@ module.exports = class PkmnBackend {
) {
if (
!this.mods.onlyLinkGlobal ||
(this.mods.onlyHundoIv && pokemon.iv === 100) ||
(this.mods.onlyZeroIv && pokemon.iv === 0) ||
(this.pokemon === pokemon.pokemon_id && this.form === pokemon.form)
) {
if (
(this.mods.onlyHundoIv && pokemon.iv === 100) ||
(this.mods.onlyZeroIv && pokemon.iv === 0)
)
return true
if (!this.expertFilter || !this.expertGlobal) return true
if (this.expertFilter(pokemon)) {
return true
Expand All @@ -371,6 +378,7 @@ module.exports = class PkmnBackend {
}
}
}
log.trace(pokemon)
return false
}

Expand Down
12 changes: 6 additions & 6 deletions server/src/services/filters/pokemon/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ const LEVELS = config
.slice()
.sort((a, b) => a - b)

const BASE_KEYS = /** @type {const} */ ([
const AND_KEYS = /** @type {const} */ ([
'iv',
'cp',
'level',
'atk_iv',
'def_iv',
'sta_iv',
'gender',
'xxs',
'xxl',
'cp',
'level',
])

const BASE_KEYS = /** @type {const} */ ([...AND_KEYS, 'gender', 'xxs', 'xxl'])

const KEYS = /** @type {const} */ ([...BASE_KEYS, ...LEAGUES])

const MAD_KEY_MAP = /** @type {const} */ ({
Expand All @@ -47,6 +46,7 @@ const STANDARD = new PokemonFilter()
module.exports = {
LEVELS,
KEYS,
AND_KEYS,
IV_CALC,
LEAGUES,
LEVEL_CALC,
Expand Down

0 comments on commit a3b24cb

Please sign in to comment.