From 7dc7bf51c81c0382fcabea28428bcb013b00f94c Mon Sep 17 00:00:00 2001 From: sketchysocks <58572875+sketchysocks@users.noreply.github.com> Date: Mon, 7 Dec 2020 00:48:54 -0500 Subject: [PATCH 1/3] Db Config Fix - Accounts for how Extend handles arrays - Function sets default (nest/portals for ManualDb and the rest for primary if none are specified) - Should we add the sessions table in the customizable list? --- package-lock.json | 2 +- src/configs/default.json | 4 +-- src/data/map.js | 56 +++++++++++++++++++++++----------------- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index fd06c558..b65602cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mapjs", - "version": "1.0.0", + "version": "1.6.15", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/configs/default.json b/src/configs/default.json index dd44317a..872d40b9 100644 --- a/src/configs/default.json +++ b/src/configs/default.json @@ -97,7 +97,7 @@ "database": "rdmdb", "charset": "utf8mb4", "sessionTable": "sessions", - "useFor": ["pokemon", "pokestop", "gym", "spawnpoint", "device", "weather", "s2cell"] + "useFor": [] }, "manualdb": { "host": "127.0.0.1", @@ -106,7 +106,7 @@ "password": "pass123!", "database": "manualdb", "charset": "utf8mb4", - "useFor": ["portal", "nest"] + "useFor": [] } }, "discord": { diff --git a/src/data/map.js b/src/data/map.js index 56b5e236..8db54573 100644 --- a/src/data/map.js +++ b/src/data/map.js @@ -11,10 +11,20 @@ const MySQLConnector = require('../services/mysql.js'); const db = new MySQLConnector(config.db.scanner); const dbManual = new MySQLConnector(config.db.manualdb); -const dbOptions = config.db.scanner.useFor; const masterfile = require('../../static/data/masterfile.json'); +const dbSelection = (category) => { + let dbSelection; + if (config.db.scanner.useFor.length === 0 && config.db.manualdb.useFor.length === 0) { + dbSelection = category === 'nest' || category === 'portal' + ? dbManual : db; + } else { + dbSelection = config.db.scanner.useFor.includes(category) + ? db : dbManual; + } + return dbSelection; +}; const getPokemon = async (minLat, maxLat, minLon, maxLon, showPVP, showIV, updated, pokemonFilterExclude = null, pokemonFilterIV = null) => { const pokemonLookup = {}; @@ -113,7 +123,7 @@ const getPokemon = async (minLat, maxLat, minLon, maxLon, showPVP, showIV, updat FROM pokemon WHERE expire_timestamp >= UNIX_TIMESTAMP() AND lat >= ? AND lat <= ? AND lon >= ? AND lon <= ? AND updated > ? ${onlyVerifiedTimersSQL}`; const args = [minLat, maxLat, minLon, maxLon, updated]; - const results = await (dbOptions.includes('pokemon') ? db : dbManual).query(sql, args).catch(err => { + const results = await dbSelection('pokemon').query(sql, args).catch(err => { console.error('Failed to execute query:', sql, 'with arguments:', args, '\r\nError:', err); }); let pokemon = []; @@ -386,7 +396,7 @@ const getGyms = async (minLat, maxLat, minLon, maxLon, updated = 0, showRaids = sql += ' AND raid_end_timestamp IS NOT NULL AND raid_end_timestamp >= UNIX_TIMESTAMP()'; } - const results = await (dbOptions.includes('gym') ? db : dbManual).query(sql, args) + const results = await dbSelection('gym').query(sql, args) .catch(err => { if (err) { console.error('Failed to get gyms:', err); @@ -661,7 +671,7 @@ const getPokestops = async (minLat, maxLat, minLon, maxLon, updated = 0, showPok WHERE lat >= ? AND lat <= ? AND lon >= ? AND lon <= ? AND updated > ? AND deleted = false AND (false ${excludeTypeSQL} ${excludePokemonSQL} ${excludeEvolutionSQL} ${excludeItemSQL} ${excludePokestopSQL} ${excludeInvasionSQL}) `; - const results = await (dbOptions.includes('pokestop') ? db : dbManual).query(sql, args); + const results = await dbSelection('pokestop').query(sql, args); let pokestops = []; if (results && results.length > 0) { for (let i = 0; i < results.length; i++) { @@ -774,7 +784,7 @@ const getSpawnpoints = async (minLat, maxLat, minLon, maxLon, updated, spawnpoin `; let args = [minLat, maxLat, minLon, maxLon, updated]; - const results = await (dbOptions.includes('spawnpoint') ? db : dbManual).query(sql, args); + const results = await dbSelection('spawnpoint').query(sql, args); let spawnpoints = []; if (results && results.length > 0) { for (let i = 0; i < results.length; i++) { @@ -824,7 +834,7 @@ const getDevices = async (deviceFilterExclude = null) => { INNER JOIN instance ON device.instance_name = instance.name ${excludeDeviceSQL} `; - const results = await (dbOptions.includes('device') ? db : dbManual).query(sql); + const results = await dbSelection('device').query(sql); let devices = []; if (results && results.length > 0) { for (let i = 0; i < results.length; i++) { @@ -856,7 +866,7 @@ const getS2Cells = async (minLat, maxLat, minLon, maxLon, updated) => { WHERE center_lat >= ? AND center_lat <= ? AND center_lon >= ? AND center_lon <= ? AND updated > ? `; let args = [minLatReal, maxLatReal, minLonReal, maxLonReal, updated]; - const results = await (dbOptions.includes('s2cell') ? db : dbManual).query(sql, args); + const results = await dbSelection('s2cell').query(sql, args); let cells = []; if (results && results.length > 0) { for (let i = 0; i < results.length; i++) { @@ -1024,7 +1034,7 @@ const getWeather = async (minLat, maxLat, minLon, maxLon, updated, weatherFilter args.push(id); } - const results = await (dbOptions.includes('weather') ? db : dbManual).query(sql, args); + const results = await dbSelection('weather').query(sql, args); let weather = []; if (results && results.length > 0) { for (let i = 0; i < results.length; i++) { @@ -1106,7 +1116,7 @@ const getNests = async (minLat, maxLat, minLon, maxLon, nestFilterExclude = null args.push(excludedPokemon[i]); } - const results = await (dbOptions.includes('nest') ? db : dbManual).query(sql, args); + const results = await dbSelection('nest').query(sql, args); if (results && results.length > 0) { return results; } @@ -1141,7 +1151,7 @@ const getPortals = async (minLat, maxLat, minLon, maxLon, portalFilterExclude = WHERE lat >= ? AND lat <= ? AND lon >= ? AND lon <= ? ${sqlExcludeCreate} `; const args = [minLatReal, maxLatReal, minLonReal, maxLonReal]; - const results = await (dbOptions.includes('portal') ? db : dbManual).query(sql, args); + const results = await dbSelection('portal').query(sql, args); if (results && results.length > 0) { return results; } @@ -1152,7 +1162,7 @@ const getPortals = async (minLat, maxLat, minLon, maxLon, portalFilterExclude = const getSearchData = async (lat, lon, id, value, iconStyle) => { let sql = ''; let args = [lat, lon, lat]; - let useDb = true; + let useDb; let conditions = []; let sanitizedValue = sanitizer.sanitize(value); sanitizedValue = sanitizedValue.toLowerCase(); @@ -1226,7 +1236,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM pokestop WHERE ${conditions.join(' OR ') || 'FALSE'} `; - useDb = dbOptions.includes('pokestop'); + useDb = dbSelection('pokestop'); break; case 'search-nest': let ids = getPokemonIdsByName(sanitizedValue); @@ -1249,7 +1259,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM nests WHERE LOWER(name) LIKE '%${sanitizedValue}%' ${pokemonSQL} `; - useDb = dbOptions.includes('nest'); + useDb = dbSelection('nest'); break; case 'search-portal': sql = ` @@ -1258,7 +1268,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM ingress_portals WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbOptions.includes('portal'); + useDb = dbSelection('portal'); break; case 'search-gym': sql = ` @@ -1267,7 +1277,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM gym WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbOptions.includes('gym'); + useDb = dbSelection('gym'); break; case 'search-pokestop': sql = ` @@ -1276,13 +1286,11 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM pokestop WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbOptions.includes('pokestop'); + useDb = dbSelection('pokestop'); break; } sql += ` ORDER BY distance LIMIT ${config.searchMaxResults || 20}`; - let results = useDb - ? await db.query(sql, args) - : await dbManual.query(sql, args); + let results = useDb.query(sql, args); if (results && results.length > 0) { switch (id) { case 'search-reward': @@ -1416,7 +1424,7 @@ const getAvailableRaidBosses = async () => { AND raid_pokemon_id > 0 GROUP BY raid_pokemon_id, raid_pokemon_form `; - let result = await (dbOptions.includes('gym') ? db : dbManual).query(sql); + let result = await dbSelection('gym').query(sql); if (result) { return result.map(x => { return { id: x.raid_pokemon_id, form_id: x.raid_pokemon_form }; @@ -1427,19 +1435,19 @@ const getAvailableRaidBosses = async () => { const getAvailableQuests = async () => { let sql = 'SELECT quest_item_id AS id FROM pokestop WHERE quest_reward_type=2 GROUP BY quest_item_id'; - const itemResults = await (dbOptions.includes('pokestop') ? db : dbManual).query(sql); + const itemResults = await dbSelection('pokestop').query(sql); sql = `SELECT DISTINCT quest_pokemon_id AS id, json_extract(json_extract(quest_rewards, '$[*].info.form_id'), '$[0]') AS form FROM pokestop WHERE quest_reward_type=7`; - const pokemonResults = await (dbOptions.includes('pokestop') ? db : dbManual).query(sql); + const pokemonResults = await dbSelection('pokestop').query(sql); sql = ` SELECT DISTINCT json_extract(json_extract(quest_rewards, "$[*].info.pokemon_id"), "$[0]") AS id FROM pokestop WHERE quest_reward_type = 12 `; - const evoResults = await (dbOptions.includes('pokestop') ? db : dbManual).query(sql); + const evoResults = await dbSelection('pokestop').query(sql); return { pokemon: pokemonResults, items: itemResults.map(x => x.id), @@ -1453,7 +1461,7 @@ const getAvailableNestPokemon = async () => { FROM nests GROUP BY pokemon_id `; - let result = await (dbOptions.includes('nest') ? db : dbManual).query(sql); + let result = await dbSelection('nest').query(sql); if (result) { return result.map(x => x.pokemon_id); } From 92a87aed0fda9c00a1a7b6f5c3efdd22258db226 Mon Sep 17 00:00:00 2001 From: sketchysocks <58572875+sketchysocks@users.noreply.github.com> Date: Tue, 8 Dec 2020 22:26:42 -0500 Subject: [PATCH 2/3] Cleanup & Session Support - Bug fixes/cleanup - Now supporting sessions table --- src/configs/default.json | 1 + src/data/map.js | 12 ++++++------ src/services/session-store.js | 21 ++++++++++++--------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/configs/default.json b/src/configs/default.json index 872d40b9..b930c953 100644 --- a/src/configs/default.json +++ b/src/configs/default.json @@ -106,6 +106,7 @@ "password": "pass123!", "database": "manualdb", "charset": "utf8mb4", + "sessionTable": "sessions", "useFor": [] } }, diff --git a/src/data/map.js b/src/data/map.js index 8db54573..f23677a6 100644 --- a/src/data/map.js +++ b/src/data/map.js @@ -1236,7 +1236,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM pokestop WHERE ${conditions.join(' OR ') || 'FALSE'} `; - useDb = dbSelection('pokestop'); + useDb = 'pokestop'; break; case 'search-nest': let ids = getPokemonIdsByName(sanitizedValue); @@ -1259,7 +1259,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM nests WHERE LOWER(name) LIKE '%${sanitizedValue}%' ${pokemonSQL} `; - useDb = dbSelection('nest'); + useDb = 'nest'; break; case 'search-portal': sql = ` @@ -1268,7 +1268,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM ingress_portals WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbSelection('portal'); + useDb = 'portal'; break; case 'search-gym': sql = ` @@ -1277,7 +1277,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM gym WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbSelection('gym'); + useDb = 'gym'; break; case 'search-pokestop': sql = ` @@ -1286,11 +1286,11 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM pokestop WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbSelection('pokestop'); + useDb = 'pokestop'; break; } sql += ` ORDER BY distance LIMIT ${config.searchMaxResults || 20}`; - let results = useDb.query(sql, args); + let results = await dbSelection(useDb).query(sql, args); if (results && results.length > 0) { switch (id) { case 'search-reward': diff --git a/src/services/session-store.js b/src/services/session-store.js index 069cad46..33ff35b2 100644 --- a/src/services/session-store.js +++ b/src/services/session-store.js @@ -5,20 +5,23 @@ const MySQLStore = require('express-mysql-session')(session); const config = require('../services/config.js'); const MySQLConnector = require('../services/mysql.js'); -const db = new MySQLConnector(config.db.scanner); + +const { scanner, manualdb } = config.db; +const dbSelection = scanner.useFor.includes('session') ? scanner : manualdb; +const db = new MySQLConnector(dbSelection); // MySQL session store const sessionStore = new MySQLStore({ // Database server IP address/hostname - host: config.db.scanner.host, + host: dbSelection.host, // Database server listening port - port: config.db.scanner.port, + port: dbSelection.port, // Database username - user: config.db.scanner.username, + user: dbSelection.username, // Password for the above database user - password: config.db.scanner.password, + password: dbSelection.password, // Database name to save sessions table to - database: config.db.scanner.database, + database: dbSelection.database, // Whether or not to automatically check for and clear expired sessions: clearExpired: true, // How frequently expired sessions will be cleared; milliseconds: @@ -27,14 +30,14 @@ const sessionStore = new MySQLStore({ createDatabaseTable: true, // Set Sessions table name schema: { - tableName: config.db.scanner.sessionTable + tableName: dbSelection.sessionTable } }); const isValidSession = async (userId) => { let sql = ` SELECT session_id - FROM ${config.db.scanner.sessionTable} + FROM ${dbSelection.sessionTable} WHERE json_extract(data, '$.user_id') = ? AND expires >= UNIX_TIMESTAMP() @@ -46,7 +49,7 @@ const isValidSession = async (userId) => { const clearOtherSessions = async (userId, currentSessionId) => { let sql = ` - DELETE FROM ${config.db.scanner.sessionTable} + DELETE FROM ${dbSelection.sessionTable} WHERE json_extract(data, '$.user_id') = ? AND session_id != ? From d429372ea219e42fccb43b569016db5f7900fb83 Mon Sep 17 00:00:00 2001 From: TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com> Date: Wed, 9 Dec 2020 11:27:27 -0500 Subject: [PATCH 3/3] Use scanner db by default for sessions --- src/services/session-store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/session-store.js b/src/services/session-store.js index 33ff35b2..03d99a08 100644 --- a/src/services/session-store.js +++ b/src/services/session-store.js @@ -7,7 +7,7 @@ const config = require('../services/config.js'); const MySQLConnector = require('../services/mysql.js'); const { scanner, manualdb } = config.db; -const dbSelection = scanner.useFor.includes('session') ? scanner : manualdb; +const dbSelection = manualdb.useFor.includes('session') ? manualdb : scanner; const db = new MySQLConnector(dbSelection); // MySQL session store