Skip to content

Commit

Permalink
Merge pull request #936 from WatWowMap/refine-available-checks
Browse files Browse the repository at this point in the history
Refine available checks
  • Loading branch information
TurtIeSocks authored Jan 26, 2024
2 parents 84d48c8 + 05261c1 commit 340c0ba
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 118 deletions.
1 change: 1 addition & 0 deletions packages/types/lib/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface DbCheckClass {
reactMapDb: null | number
filterContext: {
Route: { maxDistance: number; maxDuration: number }
Pokestop: { hasConfirmedInvasions: boolean }
}
}

Expand Down
2 changes: 0 additions & 2 deletions server/src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,6 @@
"forceTutorial": true,
"enableTutorial": true,
"enableUserProfile": true,
"enableQuestSetSelector": true,
"enableConfirmedInvasions": false,
"noScanAreaOverlay": false,
"scanAreaMenuHeight": 400,
"permImageDir": "images/perms",
Expand Down
5 changes: 1 addition & 4 deletions server/src/configs/local.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"startZoom": 12,
"minZoom": 10,
"maxZoom": 18
},
"misc": {
"enableQuestSetSelector": true
}
},
"database": {
Expand Down Expand Up @@ -323,4 +320,4 @@
"domain": "map_2.your_map.com"
}
]
}
}
2 changes: 1 addition & 1 deletion server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const resolvers = {
const data = {
questConditions: perms.quests ? Db.questConditions : {},
masterfile: { ...Event.masterfile, invasions: Event.invasions },
filters: buildDefaultFilters(perms, Db),
filters: buildDefaultFilters(perms),
audio: {
...config.getSafe('audio'),
styles: Event.uaudio,
Expand Down
21 changes: 18 additions & 3 deletions server/src/models/Pokestop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
stopValidDataLimit,
hideOldPokestops,
} = config.getSafe('api')
const map = config.getSafe('map')

const questProps = {
quest_type: true,
Expand Down Expand Up @@ -812,8 +811,7 @@ class Pokestop extends Model {
pokestop.quests.forEach((quest) => {
if (
quest.quest_reward_type &&
(!map.misc.enableQuestSetSelector ||
filters.onlyShowQuestSet === 'both' ||
(filters.onlyShowQuestSet === 'both' ||
(filters.onlyShowQuestSet === 'with_ar' && quest.with_ar) ||
(filters.onlyShowQuestSet === 'without_ar' && !quest.with_ar))
) {
Expand Down Expand Up @@ -1322,6 +1320,7 @@ class Pokestop extends Model {
'>=',
ts * (multiInvasionMs ? 1000 : 1),
)
.andWhereNot('incident.display_type', 9)
.groupBy('incident.character', 'incident.display_type')
.orderBy('incident.character', 'incident.display_type')
}
Expand All @@ -1336,6 +1335,7 @@ class Pokestop extends Model {
.distinct(isMad ? 'incident_grunt_type AS grunt_type' : 'grunt_type')
.where(isMad ? 'incident_grunt_type' : 'grunt_type', '>', 0)
.andWhere('incident_expire_timestamp', '>=', ts)
.andWhereNot('incident.display_type', 9)
.orderBy('grunt_type')
}
if (isMad && !hasMultiInvasions) {
Expand Down Expand Up @@ -1968,6 +1968,21 @@ class Pokestop extends Model {

return results.filter((x) => x.enabled && !x.deleted)
}

/**
* returns pokestop context
* @param {import('@rm/types').DbContext} ctx
* @returns {Promise<{ hasConfirmedInvasions: boolean }>}
*/
static async getFilterContext({ isMad, hasConfirmed }) {
if (isMad || !hasConfirmed) return { hasConfirmedInvasions: false }
const result = await this.query()
.from('incident')
.count('id', { as: 'total' })
.where('confirmed', 1)
.first()
return { hasConfirmedInvasions: result.total > 0 }
}
}

module.exports = Pokestop
25 changes: 18 additions & 7 deletions server/src/services/DbCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = class DbCheck {
this.historical = getCache('historical.json', {})
this.filterContext = getCache('filterContext.json', {
Route: { maxDistance: 0, maxDuration: 0 },
Pokestop: { hasConfirmedInvasions: false },
})
this.reactMapDb = null
this.connections = config
Expand Down Expand Up @@ -627,8 +628,8 @@ module.exports = class DbCheck {
* Builds filter context for all models
*/
async getFilterContext() {
try {
if (this.models.Route) {
if (this.models.Route) {
try {
const results = await Promise.all(
this.models.Route.map(({ SubModel, ...source }) =>
SubModel.getFilterContext(source),
Expand All @@ -642,12 +643,22 @@ module.exports = class DbCheck {
)
log.info(HELPERS.db, 'Updating filter context for routes')
await setCache('filterContext.json', this.filterContext)
} catch (e) {
log.error(
HELPERS.db,
'If you are using RDM, you likely do not have a routes table. Remove `route` from the `useFor` array in your config',
e,
)
}
} catch (e) {
log.error(
HELPERS.db,
'If you are using RDM, you likely do not have a routes table. Remove `route` from the `useFor` array in your config',
e,
}
if (this.models.Pokestop) {
const results = await Promise.all(
this.models.Pokestop.map(({ SubModel, ...source }) =>
SubModel.getFilterContext(source),
),
)
this.filterContext.Pokestop.hasConfirmedInvasions = results.some(
(result) => result.hasConfirmedInvasions,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/api/scannerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async function scannerApi(
}
const scannerResponse = await fetch(
`${payloadObj.url}${payloadObj.url.includes('?') ? '&' : '?'}username=${
user.username
user.username || user.id || 'a visitor'
}`,
{
...payloadObj.options,
Expand Down
5 changes: 5 additions & 0 deletions server/src/services/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const getCache = (fileName, fallback = null) => {
const data = JSON.parse(
fs.readFileSync(path.resolve(CACHE_DIR, fileName), 'utf-8'),
)
if (fallback) {
Object.entries(fallback).forEach(([key, value]) => {
if (!(key in data)) data[key] = value
})
}
log.info(HELPERS.cache, 'Loaded', fileName)
return data
} catch (e) {
Expand Down
29 changes: 14 additions & 15 deletions server/src/services/filters/builder/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
const config = require('@rm/config')

const { Db } = require('../../initialization')
const buildPokemon = require('./pokemon')
const buildPokestops = require('./pokestop')
const buildGyms = require('./gym')
Expand All @@ -26,12 +27,10 @@ const custom = new PokemonFilter(
)

/**
*
* @param {import("@rm/types").Permissions} perms
* @param {import("@rm/types").DbCheckClass} database
* @returns
*/
function buildDefaultFilters(perms, database) {
function buildDefaultFilters(perms) {
const stopReducer =
perms.pokestops || perms.lures || perms.quests || perms.invasions
const gymReducer = perms.gyms || perms.raids
Expand All @@ -40,7 +39,7 @@ function buildDefaultFilters(perms, database) {

return {
gyms:
gymReducer && database.models.Gym
gymReducer && Db.models.Gym
? {
enabled: defaultFilters.gyms.enabled,
allGyms: perms.gyms ? defaultFilters.gyms.enabled : undefined,
Expand All @@ -62,7 +61,7 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
nests:
perms.nests && database.models.Nest
perms.nests && Db.models.Nest
? {
enabled: defaultFilters.nests.enabled,
pokemon: defaultFilters.nests.pokemon,
Expand All @@ -73,7 +72,7 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
pokestops:
stopReducer && database.models.Pokestop
stopReducer && Db.models.Pokestop
? {
enabled: defaultFilters.pokestops.enabled,
allPokestops: perms.pokestops
Expand Down Expand Up @@ -104,7 +103,7 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
pokemon:
perms.pokemon && database.models.Pokemon
perms.pokemon && Db.models.Pokemon
? {
enabled: defaultFilters.pokemon.enabled,
easyMode: defaultFilters.pokemon.easyMode,
Expand All @@ -126,12 +125,12 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
routes:
perms.routes && database.models.Route
perms.routes && Db.models.Route
? {
enabled: defaultFilters.routes.enabled,
distance: [
0,
Math.ceil(database.filterContext.Route.maxDistance / 1000) + 1,
Math.ceil(Db.filterContext.Route.maxDistance / 1000) + 1,
],
standard: new BaseFilter(),
filter: {
Expand All @@ -140,7 +139,7 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
portals:
perms.portals && database.models.Portal
perms.portals && Db.models.Portal
? {
enabled: defaultFilters.portals.enabled,
standard: new BaseFilter(),
Expand All @@ -160,7 +159,7 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
submissionCells:
perms.submissionCells && database.models.Pokestop && database.models.Gym
perms.submissionCells && Db.models.Pokestop && Db.models.Gym
? {
enabled: defaultFilters.submissionCells.enabled,
rings: defaultFilters.submissionCells.rings,
Expand All @@ -180,15 +179,15 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
weather:
perms.weather && database.models.Weather
perms.weather && Db.models.Weather
? {
enabled: defaultFilters.weather.enabled,
standard: new BaseFilter(),
filter: { global: new BaseFilter() },
}
: undefined,
spawnpoints:
perms.spawnpoints && database.models.Spawnpoint
perms.spawnpoints && Db.models.Spawnpoint
? {
enabled: defaultFilters.spawnpoints.enabled,
standard: new BaseFilter(),
Expand All @@ -201,15 +200,15 @@ function buildDefaultFilters(perms, database) {
}
: undefined,
scanCells:
perms.scanCells && database.models.ScanCell
perms.scanCells && Db.models.ScanCell
? {
enabled: defaultFilters.scanCells.enabled,
standard: new BaseFilter(),
filter: { global: new BaseFilter() },
}
: undefined,
devices:
perms.devices && database.models.Device
perms.devices && Db.models.Device
? {
enabled: defaultFilters.devices.enabled,
standard: new BaseFilter(),
Expand Down
31 changes: 15 additions & 16 deletions server/src/services/filters/builder/pokemon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// @ts-check
/* eslint-disable no-restricted-syntax */
const config = require('@rm/config')

const { Event } = require('../../initialization')
const { Event, Db } = require('../../initialization')
const BaseFilter = require('../Base')

/**
Expand Down Expand Up @@ -33,20 +30,22 @@ function buildPokemon(defaults, base, custom) {
.map((e) => e.split('-')[1]),
])

for (const [i, pkmn] of Object.entries(Event.masterfile.pokemon)) {
for (const j of Object.keys(pkmn.forms)) {
pokemon.full[`${i}-${j}`] = base
pokemon.raids[`${i}-${j}`] = new BaseFilter(defaults.gyms.pokemon)
pokemon.quests[`${i}-${j}`] = new BaseFilter(defaults.pokestops.pokemon)
if (config.getSafe('map.misc.enableConfirmedInvasions')) {
pokemon.rocket[`a${i}-${j}`] = new BaseFilter(
Object.entries(Event.masterfile.pokemon).forEach(([id, pkmn]) => {
Object.keys(pkmn.forms).forEach((form) => {
pokemon.full[`${id}-${form}`] = base
pokemon.raids[`${id}-${form}`] = new BaseFilter(defaults.gyms.pokemon)
pokemon.quests[`${id}-${form}`] = new BaseFilter(
defaults.pokestops.pokemon,
)
if (Db.filterContext.Pokestop.hasConfirmedInvasions) {
pokemon.rocket[`a${id}-${form}`] = new BaseFilter(
defaults.pokestops.invasionPokemon,
)
}
pokemon.nests[`${i}-${j}`] = new BaseFilter(defaults.nests.allPokemon)
}
pokemon.nests[`${id}-${form}`] = new BaseFilter(defaults.nests.allPokemon)
})
if ('family' in pkmn) {
if (pkmn.family === +i) {
if (pkmn.family === +id) {
pokemon.quests[`c${pkmn.family}`] = new BaseFilter(
defaults.pokestops.candy,
)
Expand All @@ -57,12 +56,12 @@ function buildPokemon(defaults, base, custom) {
}
if ('tempEvolutions' in pkmn) {
energyAmounts.forEach((a) => {
pokemon.quests[`m${i}-${a}`] = new BaseFilter(
pokemon.quests[`m${id}-${a}`] = new BaseFilter(
defaults.pokestops.megaEnergy,
)
})
}
}
})
return pokemon
}

Expand Down
6 changes: 2 additions & 4 deletions server/src/services/filters/builder/pokestop.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-check
const config = require('@rm/config')

const BaseFilter = require('../Base')
const { Event } = require('../../initialization')
const { Event, Db } = require('../../initialization')

/**
*
Expand Down Expand Up @@ -72,7 +70,7 @@ function buildPokestops(perms, defaults) {
}
if (
avail.startsWith('a') &&
config.getSafe('map.misc.enableConfirmedInvasions')
Db.filterContext.Pokestop.hasConfirmedInvasions
) {
quests[avail] = new BaseFilter(defaults.invasionPokemon)
}
Expand Down
Loading

0 comments on commit 340c0ba

Please sign in to comment.