Skip to content

Commit 23b0c38

Browse files
committed
chore: code review
1 parent 1d5f336 commit 23b0c38

File tree

4 files changed

+95
-26
lines changed

4 files changed

+95
-26
lines changed

packages/types/lib/server.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface DbContext {
4646
hasShowcaseForm: boolean
4747
hasShowcaseType: boolean
4848
hasStationedGmax: boolean
49+
hasPokemonShinyStats?: boolean
4950
connection?: number
5051
}
5152

server/src/models/Pokemon.js

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -442,33 +442,68 @@ class Pokemon extends Model {
442442
* @returns {import('knex').Knex | null}
443443
*/
444444
static getStatsKnex(preferredConnection = null) {
445+
return this.getStatsHandle(preferredConnection)?.knex ?? null
446+
}
447+
448+
/**
449+
* @param {number | null | undefined} preferredConnection
450+
* @returns {{ knex: import('knex').Knex, source?: import('@rm/types').DbContext & { connection: number } } | null}
451+
*/
452+
static getStatsHandle(preferredConnection = null) {
445453
const dbManager = state.db
446454
if (!dbManager) return null
447455
const { connections } = dbManager
448-
if (!Array.isArray(dbManager.models?.Spawnpoint)) {
456+
if (!connections?.length) return null
457+
458+
const spawnSources = dbManager.models?.Spawnpoint
459+
if (Array.isArray(spawnSources) && spawnSources.length) {
460+
let candidate = null
461+
449462
if (typeof preferredConnection === 'number') {
450-
return connections?.[preferredConnection] ?? null
463+
candidate = spawnSources.find(
464+
(source) =>
465+
source.connection === preferredConnection &&
466+
source.hasPokemonShinyStats &&
467+
connections?.[source.connection],
468+
)
469+
}
470+
471+
if (!candidate) {
472+
candidate = spawnSources.find(
473+
(source) =>
474+
source.hasPokemonShinyStats && connections?.[source.connection],
475+
)
476+
}
477+
478+
if (!candidate && typeof preferredConnection === 'number') {
479+
candidate = spawnSources.find(
480+
(source) =>
481+
source.connection === preferredConnection &&
482+
connections?.[source.connection],
483+
)
484+
}
485+
486+
if (!candidate) {
487+
candidate = spawnSources.find(
488+
(source) => connections?.[source.connection],
489+
)
490+
}
491+
492+
if (candidate) {
493+
const knexInstance = connections?.[candidate.connection]
494+
if (knexInstance) {
495+
return { knex: knexInstance, source: candidate }
496+
}
451497
}
452498
return null
453499
}
454-
const spawnSources = dbManager.models.Spawnpoint
500+
455501
if (typeof preferredConnection === 'number') {
456-
const direct = spawnSources.find(
457-
({ connection }) => connection === preferredConnection,
458-
)
459-
if (direct) {
460-
return connections?.[direct.connection] ?? null
502+
const knexInstance = connections?.[preferredConnection]
503+
if (knexInstance) {
504+
return { knex: knexInstance }
461505
}
462506
}
463-
const fallback = spawnSources.find(
464-
({ connection }) => connections?.[connection],
465-
)
466-
if (fallback) {
467-
return connections?.[fallback.connection] ?? null
468-
}
469-
if (typeof preferredConnection === 'number') {
470-
return connections?.[preferredConnection] ?? null
471-
}
472507
return null
473508
}
474509

@@ -477,7 +512,17 @@ class Pokemon extends Model {
477512
* @returns {boolean}
478513
*/
479514
static supportsShinyStats(ctx) {
480-
return Boolean(this.getStatsKnex(ctx.connection) || !ctx.mem)
515+
const statsHandle = this.getStatsHandle(ctx?.connection)
516+
if (!statsHandle?.knex) {
517+
return false
518+
}
519+
const flag =
520+
typeof statsHandle.source?.hasPokemonShinyStats === 'boolean'
521+
? statsHandle.source.hasPokemonShinyStats
522+
: typeof ctx?.hasPokemonShinyStats === 'boolean'
523+
? ctx.hasPokemonShinyStats
524+
: false
525+
return flag
481526
}
482527

483528
/**
@@ -495,7 +540,8 @@ class Pokemon extends Model {
495540

496541
let knexInstance = statsKnex || null
497542
if (!knexInstance) {
498-
knexInstance = this.getStatsKnex(preferredConnection)
543+
const statsHandle = this.getStatsHandle(preferredConnection)
544+
knexInstance = statsHandle?.knex ?? null
499545
}
500546
if (!knexInstance) {
501547
try {
@@ -707,7 +753,17 @@ class Pokemon extends Model {
707753
* @returns {Promise<import("@rm/types").PokemonShinyStats | null>}
708754
*/
709755
static async getShinyStats(_perms, args, ctx) {
710-
if (!this.supportsShinyStats(ctx)) {
756+
const statsHandle = this.getStatsHandle(ctx?.connection)
757+
if (!statsHandle?.knex) {
758+
return null
759+
}
760+
const hasStats =
761+
typeof statsHandle.source?.hasPokemonShinyStats === 'boolean'
762+
? statsHandle.source.hasPokemonShinyStats
763+
: typeof ctx?.hasPokemonShinyStats === 'boolean'
764+
? ctx.hasPokemonShinyStats
765+
: false
766+
if (!hasStats) {
711767
return null
712768
}
713769
const pokemonId = Number.parseInt(`${args.pokemon_id}`, 10)
@@ -719,12 +775,17 @@ class Pokemon extends Model {
719775
try {
720776
const stats = await this.fetchShinyStats(
721777
[key],
722-
this.getStatsKnex(ctx.connection),
723-
ctx.connection,
778+
statsHandle.knex,
779+
statsHandle.source?.connection ?? ctx.connection,
724780
)
725781
return stats.get(key) || null
726782
} catch (e) {
727783
log.error(TAGS.pokemon, 'Failed to fetch shiny stats', e)
784+
if (statsHandle.source && e?.code === 'ER_NO_SUCH_TABLE') {
785+
statsHandle.source.hasPokemonShinyStats = false
786+
} else if (ctx && e?.code === 'ER_NO_SUCH_TABLE') {
787+
ctx.hasPokemonShinyStats = false
788+
}
728789
return null
729790
}
730791
}

server/src/services/DbManager.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ class DbManager extends Logger {
185185
.columnInfo()
186186
.then((columns) => ['shortcode' in columns])
187187

188+
let hasPokemonShinyStats
189+
try {
190+
hasPokemonShinyStats = await schema.schema.hasTable('pokemon_shiny_stats')
191+
} catch (e) {
192+
hasPokemonShinyStats = false
193+
}
194+
188195
return {
189196
isMad,
190197
pvpV2,
@@ -207,6 +214,7 @@ class DbManager extends Logger {
207214
hasShowcaseType,
208215
hasStationedGmax,
209216
hasShortcode,
217+
hasPokemonShinyStats,
210218
}
211219
}
212220

@@ -227,6 +235,7 @@ class DbManager extends Logger {
227235
// Add support for HTTP authentication
228236
httpAuth: this.endpoints[i].httpAuth,
229237
pvpV2: true,
238+
hasPokemonShinyStats: false,
230239
}
231240

232241
Object.entries(this.models).forEach(([category, sources]) => {

src/features/pokemon/PokemonPopup.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ export function PokemonPopup({ pokemon, iconUrl, isTutorial = false }) {
7676
const hasLeagues = cleanPvp ? Object.keys(cleanPvp) : []
7777
const hasStats = iv || cp
7878

79-
const supportsShinyStats = useMemory(
80-
(s) => s.featureFlags.supportsShinyStats,
81-
)
79+
const supportsShinyStats = useMemory((s) => s.featureFlags.supportsShinyStats)
8280
const shinyKey = React.useMemo(
8381
() => `${pokemon.pokemon_id}-${pokemon.form ?? 0}`,
8482
[pokemon.pokemon_id, pokemon.form],
@@ -135,7 +133,7 @@ export function PokemonPopup({ pokemon, iconUrl, isTutorial = false }) {
135133
return () => {
136134
isActive = false
137135
}
138-
}, [supportsShinyStats, shinyStats, shinyKey, loadShinyStats, pokemon.pokemon_id, pokemon.form])
136+
}, [supportsShinyStats, shinyStats, shinyKey, loadShinyStats])
139137

140138
useAnalytics(
141139
'Popup',

0 commit comments

Comments
 (0)