Skip to content

Commit

Permalink
feat: more nest viewing options
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Aug 19, 2024
1 parent 51d3384 commit 959116d
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 118 deletions.
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@
"allPokemon": true,
"onlyShowAvailable": true,
"avgFilter": [0, 100],
"avgSliderStep": 1
"avgSliderStep": 1,
"active": "active"
},
"pokestops": {
"enabled": false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"nodemonConfig": {
"watch": [
"server/src/**/*.js",
"server/src/configs/default.json",
"config/default.json",
"packages/**/*"
],
"ignore": [
Expand Down
3 changes: 2 additions & 1 deletion server/src/filters/builder/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ function buildDefaultFilters(perms) {
enabled: defaultFilters.nests.enabled,
onlyShowAvailable: defaultFilters.nests.onlyShowAvailable,
pokemon: defaultFilters.nests.pokemon,
polygons: defaultFilters.nests.polygons,
avgFilter: defaultFilters.nests.avgFilter,
polygons: defaultFilters.nests.polygons,
standard: new BaseFilter(),
filter: pokemon.nests,
active: defaultFilters.nests.active,
}
: undefined,
pokestops:
Expand Down
67 changes: 40 additions & 27 deletions server/src/models/Nest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-check
/* eslint-disable no-nested-ternary */
const { Model } = require('objection')
const i18next = require('i18next')
const config = require('@rm/config')
Expand Down Expand Up @@ -28,18 +27,29 @@ class Nest extends Model {
static async getAll(perms, args, { polygon }) {
const { areaRestrictions } = perms
const { minLat, minLon, maxLat, maxLon, filters } = args
const pokemon = []
Object.keys(filters).forEach((pkmn) => {
if (!pkmn.startsWith('o')) {
pokemon.push(pkmn.split('-')[0])
}
})
const query = this.query()
.select(['*', 'nest_id AS id'])
.whereNotNull('pokemon_id')
// .whereNotNull('pokemon_id')
.whereBetween('lat', [minLat, maxLat])
.andWhereBetween('lon', [minLon, maxLon])
.whereIn('pokemon_id', pokemon)

const pokemon = []
if (filters.onlyPokemon) {
Object.keys(filters).forEach((pkmn) => {
if (!pkmn.startsWith('o')) {
pokemon.push(pkmn.split('-')[0])
}
})
}

if (pokemon.length) {
query.whereIn('pokemon_id', pokemon)
}
if (filters.onlyActive === 'inactive') {
query.where('active', false)
} else if (filters.onlyActive === 'active') {
query.where('active', true)
}
if (
!config
.getSafe('defaultFilters.nests.avgFilter')
Expand Down Expand Up @@ -91,24 +101,27 @@ class Nest extends Model {
static secondaryFilter(queryResults, filters, polygon) {
const returnedResults = []
queryResults.forEach((pkmn) => {
if (pkmn.pokemon_form == 0 || pkmn.pokemon_form === null) {
const formId =
state.event.masterfile.pokemon[pkmn.pokemon_id].defaultFormId
if (formId) pkmn.pokemon_form = formId
}
if (filters[`${pkmn.pokemon_id}-${pkmn.pokemon_form}`]) {
pkmn.polygon_path = polygon
? typeof pkmn.polygon === 'string' && pkmn.polygon
? pkmn.polygon
: JSON.stringify(
pkmn.polygon || { type: 'Polygon', coordinates: [] },
)
: JSON.stringify({
type: 'Polygon',
coordinates: JSON.parse(pkmn.polygon_path || '[]').map((line) =>
line.map((point) => [point[1], point[0]]),
),
})
pkmn.polygon_path = polygon
? typeof pkmn.polygon === 'string' && pkmn.polygon
? pkmn.polygon
: JSON.stringify(pkmn.polygon || { type: 'Polygon', coordinates: [] })
: JSON.stringify({
type: 'Polygon',
coordinates: JSON.parse(pkmn.polygon_path || '[]').map((line) =>
line.map((point) => [point[1], point[0]]),
),
})

if (pkmn.pokemon_id && filters.onlyPokemon) {
if (pkmn.pokemon_form == 0 || pkmn.pokemon_form === null) {
const formId =
state.event.masterfile.pokemon[pkmn.pokemon_id].defaultFormId
if (formId) pkmn.pokemon_form = formId
}
if (filters[`${pkmn.pokemon_id}-${pkmn.pokemon_form}`]) {
returnedResults.push(pkmn)
}
} else {
returnedResults.push(pkmn)
}
})
Expand Down
12 changes: 6 additions & 6 deletions server/src/services/DbCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = class DbCheck extends Logger {
this.endpoints[i] = schema
return null
}
const logger = new Logger('knex', schema.database)
const { log } = new Logger('knex', schema.database)
return knex({
client: 'mysql2',
connection: {
Expand All @@ -88,12 +88,12 @@ module.exports = class DbCheck extends Logger {
conn.query('SET time_zone="+00:00";', (err) => done(err, conn)),
},
log: {
warn: (message) => logger.warn(message),
error: (message) => logger.error(message),
warn: (message) => log.warn(message),
error: (message) => log.error(message),
debug: (message) =>
logger[
config.getSafe('devOptions.queryDebug') ? 'info' : 'debug'
](message),
log[config.getSafe('devOptions.queryDebug') ? 'info' : 'debug'](
message,
),
enableColors: true,
},
})
Expand Down
3 changes: 2 additions & 1 deletion server/src/ui/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function generateUi(req, perms) {
perms.nests && state.db.models.Nest
? {
pokemon: true,
polygons: true,
sliders: {
secondary: [
{
Expand All @@ -48,6 +47,8 @@ function generateUi(req, perms) {
},
],
},
polygons: true,
active: true,
}
: BLOCKED,
pokestops:
Expand Down
24 changes: 15 additions & 9 deletions server/src/utils/reloadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,21 @@ async function reloadConfig() {
const invalid = changed.filter((key) => NO_RELOAD.has(key))

/** @param {string} key */
const print = (key) =>
log.info(
TAGS.config,
`'${key}' -`,
'old:',
dlv(oldWithoutAreas, key),
'new:',
newConfig.getSafe(key),
)
const print = (key) => {
let newValue
let oldValue
try {
oldValue = dlv(oldWithoutAreas, key)
} catch {
// do nothing
}
try {
newValue = newConfig.get(key)
} catch {
// do nothing
}
log.info(TAGS.config, `'${key}' -`, 'old:', oldValue, 'new:', newValue)
}

if (valid.length) {
log.info(TAGS.config, 'updating the following config values:')
Expand Down
4 changes: 2 additions & 2 deletions src/components/inputs/MultiSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function MultiSelector({ items, value, disabled, onClick, tKey }) {
* @param {{
* field: T,
* defaultValue?: V,
* onClick?: (oldValue: ReturnType<typeof useDeepStore>[0], newValue: V) => void
* onClick?: (oldValue: V, newValue: V) => void
* allowNone?: boolean
* } & Omit<import('@rm/types').MultiSelectorProps<V>, 'value' | 'onClick'>} props
* @returns
Expand All @@ -50,7 +50,7 @@ export function MultiSelectorStore({
}) {
const [value, setValue] = useDeepStore(field, defaultValue)

/** @type {(o: typeof value, n: V) => import('@mui/material').ButtonProps['onClick']} */
/** @type {(o: V, n: V) => import('@mui/material').ButtonProps['onClick']} */
const onClickWrapper = React.useCallback(
(oldValue, newValue) => () => {
// @ts-ignore // TODO: fix this
Expand Down
2 changes: 1 addition & 1 deletion src/features/drawer/S2Cells.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const S2Cells = () => {
sx={{ mx: 'auto', width: '90%' }}
value={safe}
renderValue={(selected) =>
Array.isArray(selected) ? selected.join(', ') : selected
Array.isArray(selected) ? selected.join(', ') : `${selected}`
}
multiple
onChange={({ target }) =>
Expand Down
5 changes: 4 additions & 1 deletion src/features/drawer/components/Section.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ const DrawerSection = ({ category }) => {
category === 'wayfarer' || category === 'admin'
return (
<React.Fragment key={`${category}${subItem}`}>
{!(category === 'nests' && subItem === 'sliders') && (
{!(
category === 'nests' &&
(subItem === 'sliders' || subItem === 'active')
) && (
<BoolToggle
// @ts-ignore
field={`filters.${
Expand Down
19 changes: 19 additions & 0 deletions src/features/drawer/nests/ActiveNests.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as React from 'react'
import ListItem from '@mui/material/ListItem'

import { MultiSelectorStore } from '@components/inputs/MultiSelector'
import { useStorage } from '@store/useStorage'
import { CollapsibleItem } from '../components/CollapsibleItem'

const ITEMS = ['active', 'all', 'inactive']

export function ActiveNests() {
const show = useStorage((s) => !!s.filters?.nests?.polygons)
return (
<CollapsibleItem open={show}>
<ListItem>
<MultiSelectorStore items={ITEMS} field="filters.nests.active" />
</ListItem>
</CollapsibleItem>
)
}
20 changes: 12 additions & 8 deletions src/features/drawer/nests/AvgSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import * as React from 'react'
import ListItem from '@mui/material/ListItem'

import { useMemory } from '@store/useMemory'
import { useDeepStore } from '@store/useStorage'
import { useDeepStore, useStorage } from '@store/useStorage'
import { SliderTile } from '@components/inputs/SliderTile'
import { CollapsibleItem } from '../components/CollapsibleItem'

const BaseNestSlider = () => {
const slider = useMemory((s) => s.ui.nests?.sliders?.secondary?.[0])
const show = useStorage((s) => !!s.filters?.nests?.pokemon)
const [filters, setFilters] = useDeepStore(`filters.nests.avgFilter`)
if (!filters || !slider) return null
return (
<ListItem>
<SliderTile
slide={slider}
handleChange={(_, values) => setFilters(values)}
values={filters}
/>
</ListItem>
<CollapsibleItem open={show}>
<ListItem>
<SliderTile
slide={slider}
handleChange={(_, values) => setFilters(values)}
values={filters}
/>
</ListItem>
</CollapsibleItem>
)
}

Expand Down
3 changes: 3 additions & 0 deletions src/features/drawer/nests/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import * as React from 'react'

import { NestSlider } from './AvgSlider'
import { NestQuickSelect } from './NestSelector'
import { ActiveNests } from './ActiveNests'

function BaseNestDrawer({ subItem }) {
switch (subItem) {
case 'sliders':
return <NestSlider />
case 'pokemon':
return <NestQuickSelect />
case 'active':
return <ActiveNests />
default:
return null
}
Expand Down
Loading

0 comments on commit 959116d

Please sign in to comment.