@@ -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 }
0 commit comments