Skip to content

Commit a1cfb57

Browse files
committed
fix: optimize dynamax code
1 parent 5b7b58f commit a1cfb57

File tree

14 files changed

+157
-64
lines changed

14 files changed

+157
-64
lines changed

packages/locales/lib/human/en.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,16 @@
788788
"individual_filters": "Partially Filtered",
789789
"data_limit_reached": "You have requested too much data recently and are on cooldown until {{until}}",
790790
"unknown_station": "Unknown Power Spot",
791-
"exclude_battle": "Exclude Battle",
791+
"exclude_battle": "Exclude Max Battle",
792792
"station": "Power Spot",
793793
"stations": "Power Spots",
794794
"stations_options": "Power Spot Options",
795795
"all_stations": "All Power Spots",
796-
"search_battles": "Search Battles",
796+
"search_battles": "Search Max Battles",
797797
"started": "Started",
798798
"ended": "Ended",
799-
"global_search_stations": "Search Power Spots"
799+
"global_search_stations": "Search Power Spots",
800+
"station_timers": "Power Spot Timers",
801+
"station_opacity": "Power Spot Opacity",
802+
"max_battles": "Max Battles"
800803
}

server/src/filters/builder/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function buildDefaultFilters(perms) {
122122
battleTier: perms.dynamax
123123
? defaultFilters.stations.battleTier
124124
: undefined,
125-
battles: perms.stations
125+
maxBattles: perms.stations
126126
? defaultFilters.stations.battles
127127
: undefined,
128128
filter: pokemon.stations,

server/src/graphql/typeDefs/map.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ type Search {
7070
costume: Int
7171
shiny: Int
7272
iv: Float
73+
battle_pokemon_id: Int
74+
battle_pokemon_form: Int
75+
battle_pokemon_gender: Int
76+
battle_pokemon_costume: Int
77+
# battle_pokemon_evolution: Int
78+
battle_pokemon_alignment: Int
7379
}
7480

7581
type SearchLure {

server/src/models/Station.js

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// @ts-check
22
const { Model } = require('objection')
33
const config = require('@rm/config')
4+
const i18next = require('i18next')
45

56
const { getAreaSql } = require('../utils/getAreaSql')
67
const { getEpoch } = require('../utils/getClientTime')
8+
const { state } = require('../services/state')
79

810
class Station extends Model {
911
static get tableName() {
@@ -19,7 +21,7 @@ class Station extends Model {
1921
*/
2022
static async getAll(perms, args, { isMad }) {
2123
const { areaRestrictions } = perms
22-
const { onlyAreas } = args.filters
24+
const { onlyAreas, onlyAllStations, onlyMaxBattles } = args.filters
2325
const ts = getEpoch()
2426

2527
const select = [
@@ -38,8 +40,9 @@ class Station extends Model {
3840
.andWhere('end_time', '>', ts)
3941
// .where('is_inactive', false)
4042

41-
if (perms.dynamax) {
43+
if (perms.dynamax && onlyMaxBattles) {
4244
select.push(
45+
'is_battle_available',
4346
'battle_level',
4447
'battle_pokemon_id',
4548
'battle_pokemon_form',
@@ -48,35 +51,37 @@ class Station extends Model {
4851
'battle_pokemon_alignment',
4952
)
5053

51-
const battleBosses = new Set()
52-
const battleForms = new Set()
53-
const battleLevels = new Set()
54-
55-
Object.keys(args.filters).forEach((key) => {
56-
switch (key.charAt(0)) {
57-
case 'o':
58-
break
59-
case 'j':
60-
battleLevels.add(key.slice(1))
61-
break
62-
default:
63-
{
64-
const [id, form] = key.split('-')
65-
if (id) battleBosses.add(id)
66-
if (form) battleForms.add(form)
67-
}
68-
break
54+
if (!onlyAllStations) {
55+
const battleBosses = new Set()
56+
const battleForms = new Set()
57+
const battleLevels = new Set()
58+
59+
Object.keys(args.filters).forEach((key) => {
60+
switch (key.charAt(0)) {
61+
case 'o':
62+
break
63+
case 'j':
64+
battleLevels.add(key.slice(1))
65+
break
66+
default:
67+
{
68+
const [id, form] = key.split('-')
69+
if (id) battleBosses.add(id)
70+
if (form) battleForms.add(form)
71+
}
72+
break
73+
}
74+
})
75+
76+
if (battleBosses.size) {
77+
query.andWhere('battle_pokemon_id', 'in', [...battleBosses])
78+
}
79+
if (battleForms.size) {
80+
query.andWhere('battle_pokemon_form', 'in', [...battleForms])
81+
}
82+
if (battleLevels.size) {
83+
query.andWhere('battle_level', 'in', [...battleLevels])
6984
}
70-
})
71-
72-
if (battleBosses.size) {
73-
query.andWhere('battle_pokemon_id', 'in', [...battleBosses])
74-
}
75-
if (battleForms.size) {
76-
query.andWhere('battle_pokemon_form', 'in', [...battleForms])
77-
}
78-
if (battleLevels.size) {
79-
query.andWhere('battle_level', 'in', [...battleLevels])
8085
}
8186
}
8287

@@ -137,19 +142,46 @@ class Station extends Model {
137142
*/
138143
static async search(perms, args, { isMad }, distance, bbox) {
139144
const { areaRestrictions } = perms
140-
const { onlyAreas = [], search = '' } = args
145+
const { onlyAreas = [], search = '', locale } = args
141146
const { searchResultsLimit, stationUpdateLimit } = config.getSafe('api')
142147

148+
const pokemonIds = Object.keys(state.event.masterfile.pokemon).filter(
149+
(pkmn) =>
150+
i18next
151+
.t(`poke_${pkmn}`, { lng: locale })
152+
.toLowerCase()
153+
.includes(search),
154+
)
155+
156+
const select = ['id', 'name', 'lat', 'lon', distance]
157+
if (perms.dynamax) {
158+
select.push(
159+
'battle_level',
160+
'battle_pokemon_id',
161+
'battle_pokemon_form',
162+
'battle_pokemon_costume',
163+
'battle_pokemon_gender',
164+
'battle_pokemon_alignment',
165+
)
166+
}
167+
143168
const query = this.query()
144-
.select(['name', 'id', 'lat', 'lon', distance])
145-
.whereILike('name', `%${search}%`)
169+
.select(select)
146170
.whereBetween('lat', [bbox.minLat, bbox.maxLat])
147171
.andWhereBetween('lon', [bbox.minLon, bbox.maxLon])
148172
.andWhere(
149173
'updated',
150174
'>',
151175
Date.now() / 1000 - stationUpdateLimit * 60 * 60 * 24,
152176
)
177+
.andWhere((builder) => {
178+
if (perms.stations) {
179+
builder.orWhereILike('name', `%${search}%`)
180+
}
181+
if (perms.dynamax) {
182+
builder.orWhereIn('battle_pokemon_id', pokemonIds)
183+
}
184+
})
153185
.limit(searchResultsLimit)
154186
.orderBy('distance')
155187
if (!getAreaSql(query, areaRestrictions, onlyAreas, isMad)) {

server/src/ui/clientOptions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ function clientOptions(perms) {
6060
},
6161
stations: {
6262
clustering: { type: 'bool', perm: ['stations', 'dynamax'] },
63-
battleTimers: { type: 'bool', perm: ['dynamax'] },
64-
battleOpacity: { type: 'bool', perm: ['dynamax'] },
65-
opacityTenMinutes: { type: 'number', perm: ['dynamax'] },
66-
opacityFiveMinutes: { type: 'number', perm: ['dynamax'] },
67-
opacityOneMinute: { type: 'number', perm: ['dynamax'] },
63+
stationTimers: { type: 'bool', perm: ['stations', 'dynamax'] },
64+
stationOpacity: { type: 'bool', perm: ['stations', 'dynamax'] },
65+
opacityTenMinutes: { type: 'number', perm: ['stations', 'dynamax'] },
66+
opacityFiveMinutes: { type: 'number', perm: ['stations', 'dynamax'] },
67+
opacityOneMinute: { type: 'number', perm: ['stations', 'dynamax'] },
6868
enableStationPopupCoords: map.misc.enableStationPopupCoordsSelector
6969
? { type: 'bool', perm: ['stations', 'dynamax'] }
7070
: undefined,

server/src/ui/drawer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function drawer(req, perms) {
6767
(perms.stations || perms.dynamax) && state.db.models.Station
6868
? {
6969
allStations: perms.stations || BLOCKED,
70-
battles: perms.dynamax || BLOCKED,
70+
maxBattles: perms.dynamax || BLOCKED,
7171
}
7272
: BLOCKED,
7373
pokemon:

src/features/drawer/Extras.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function ExtrasComponent({ category, subItem }) {
2727
case 'admin':
2828
return <AdminDrawer subItem={subItem} />
2929
case 'stations':
30-
return subItem === 'battles' && <StationsDrawer />
30+
return subItem === 'maxBattles' && <StationsDrawer />
3131
default:
3232
return null
3333
}

src/features/drawer/Stations.jsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
import * as React from 'react'
3+
import Box from '@mui/material/Box'
34
// import ListItem from '@mui/material/ListItem'
45
// import ListItemText from '@mui/material/ListItemText'
56
// import MenuItem from '@mui/material/MenuItem'
@@ -52,14 +53,16 @@ import { SelectorListMemo } from './components/SelectorList'
5253
// }
5354

5455
function StationsQuickSelect() {
55-
const enabled = useStorage((s) => !!s.filters?.stations?.battles)
56+
const enabled = useStorage((s) => !!s.filters?.stations?.maxBattles)
5657
return (
5758
<CollapsibleItem open={enabled}>
58-
<SelectorListMemo
59-
category="stations"
60-
label="search_battles"
61-
height={350}
62-
/>
59+
<Box px={2}>
60+
<SelectorListMemo
61+
category="stations"
62+
label="search_battles"
63+
height={350}
64+
/>
65+
</Box>
6366
</CollapsibleItem>
6467
)
6568
}

src/features/search/OptionImage.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function Misc() {
175175
return <Img src={miscIcon} alt={searchTab} maxHeight={45} maxWidth={45} />
176176
}
177177

178-
/** @param {Partial<import('@rm/types').Quest & { id: string }> | { id: string, url?: string } | Partial<import('@rm/types').Pokemon> | Partial<import('@rm/types').Gym> | Partial<import('@rm/types').Pokestop> | { id: string, nest_pokemon_id: number, nest_pokemon_form?: number } | Partial<import('@rm/types').Invasion> & { id: string }} props */
178+
/** @param {Partial<import('@rm/types').Quest & { id: string }> | { id: string, url?: string } | Partial<import('@rm/types').Pokemon> | Partial<import('@rm/types').Gym> | Partial<import('@rm/types').Pokestop> | { id: string, nest_pokemon_id: number, nest_pokemon_form?: number } | Partial<import('@rm/types').Invasion> & { id: string } | Partial<import('@rm/types').Station} props */
179179
function OptionImage(props) {
180180
if ('url' in props && props.url) return <FortImage url={props.url} />
181181
if ('quest_reward_type' in props) return <QuestImage {...props} />
@@ -184,6 +184,15 @@ function OptionImage(props) {
184184
if ('lure_id' in props) return <LureImage {...props} />
185185
if ('nest_pokemon_id' in props) return <NestImage {...props} />
186186
if ('grunt_type' in props) return <InvasionImage {...props} />
187+
if ('battle_pokemon_id' in props && props.battle_pokemon_id)
188+
return (
189+
<PokemonImage
190+
pokemon_id={props.battle_pokemon_id}
191+
form={props.battle_pokemon_form}
192+
gender={props.battle_pokemon_gender}
193+
costume={props.battle_pokemon_costume}
194+
/>
195+
)
187196
return <Misc />
188197
}
189198

src/features/station/StationPopup.jsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,19 @@ function StationMedia({
182182
),
183183
)
184184
const stationImage = useMemory((s) => s.Icons.getStation(true))
185-
const pokemon = useMemory((s) => {
186-
if (!battle_pokemon_id) return null
185+
const types = useMemory((s) => {
186+
if (!battle_pokemon_id) return []
187187
const poke = s.masterfile.pokemon[battle_pokemon_id]
188-
if (poke?.forms?.[battle_pokemon_form]) {
189-
return poke.forms[battle_pokemon_form]
188+
if (poke?.forms?.[battle_pokemon_form]?.types) {
189+
return poke.forms[battle_pokemon_form]?.types || []
190190
}
191-
return poke
191+
return poke?.types || []
192192
})
193193

194194
return battle_pokemon_id ? (
195195
<CardMedia>
196196
<Box className="popup-card-media">
197-
<Box className="flex-center">
197+
<Box className="flex-center" py={2}>
198198
<Img
199199
src={monImage}
200200
alt={t(`${battle_pokemon_id}-${battle_pokemon_form}`)}
@@ -203,10 +203,12 @@ function StationMedia({
203203
/>
204204
</Box>
205205
<Stack alignItems="center" justifyContent="center" spacing={2}>
206-
{pokemon?.types?.map((type) => (
206+
{types.map((type) => (
207207
<PokeType key={type} id={type} size="medium" />
208208
))}
209-
<GenderIcon gender={battle_pokemon_gender} fontSize="medium" />
209+
{!!battle_pokemon_gender && (
210+
<GenderIcon gender={battle_pokemon_gender} fontSize="medium" />
211+
)}
210212
</Stack>
211213
</Box>
212214
</CardMedia>
@@ -228,6 +230,7 @@ function StationMedia({
228230
*/
229231
function StationContent({
230232
battle_pokemon_id,
233+
is_battle_available,
231234
battle_pokemon_form,
232235
battle_pokemon_costume,
233236
battle_level,
@@ -240,9 +243,14 @@ function StationContent({
240243
<CardContent sx={{ p: 0 }}>
241244
<Stack alignItems="center" justifyContent="center" spacing={1}>
242245
{!!battle_level && (
243-
<Rating value={battle_level} max={Math.max(5, battle_level)} />
246+
<Rating
247+
value={battle_level}
248+
max={Math.max(5, battle_level)}
249+
readOnly
250+
size="large"
251+
/>
244252
)}
245-
{!!battle_pokemon_id && (
253+
{!!is_battle_available && (
246254
<Box textAlign="center">
247255
<Typography variant="h6">
248256
{t(`poke_${battle_pokemon_id}`)}

0 commit comments

Comments
 (0)