Skip to content

Commit

Permalink
Merge pull request #937 from WatWowMap/refactor-scan-on-demand
Browse files Browse the repository at this point in the history
refactor: scan on demand backend
  • Loading branch information
TurtIeSocks authored Jan 28, 2024
2 parents 72c6d52 + 92af316 commit 3310e77
Show file tree
Hide file tree
Showing 32 changed files with 563 additions and 347 deletions.
4 changes: 1 addition & 3 deletions ReactMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ const { build } = require('vite')
const { log, HELPERS } = require('@rm/logger')
const { generate } = require('@rm/masterfile')

const viteConfig = require('./vite.config')

generate(true).then(() =>
build(viteConfig({ mode: 'production', command: 'build' }))
build()
.then(() => log.info(HELPERS.build, 'React Map Compiled'))
.then(() => require('./server/src/index')),
)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
"i18next-http-backend": "^1.4.0",
"knex": "^2.4.2",
"leaflet": "1.9.4",
"leaflet.locatecontrol": "^0.79.0",
"lodash.debounce": "^4.0.8",
"leaflet.locatecontrol": "0.73.0",
"lodash": "^4.17.21",
"moment-timezone": "^0.5.43",
"morgan": "^1.10.0",
"mysql2": "^3.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"prettier:fix": "prettier --write \"**/*.{css,html,js,jsx,yml}\""
},
"dependencies": {
"config": "^3.3.9"
"config": "^3.3.10"
}
}
7 changes: 3 additions & 4 deletions packages/masterfile/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Object.entries(defaultRarity).forEach(([tier, pokemon]) => {
* @returns
*/
const generate = async (save = false, historicRarity = {}, dbRarity = {}) => {
log.info(HELPERS.masterfile, 'generating masterfile')
try {
if (!endpoint) throw new Error('No masterfile endpoint')

Expand Down Expand Up @@ -68,7 +69,7 @@ const generate = async (save = false, historicRarity = {}, dbRarity = {}) => {
}

if (save) {
fs.promises.writeFile(
await fs.promises.writeFile(
resolve(`${__dirname}/data/masterfile.json`),
JSON.stringify(newMf, null, 2),
'utf8',
Expand All @@ -87,9 +88,7 @@ const generate = async (save = false, historicRarity = {}, dbRarity = {}) => {
module.exports.generate = generate

if (require.main === module) {
generate(true).then(() =>
log.info(HELPERS.masterfile, 'Masterfile generated'),
)
generate(true).then(() => log.info(HELPERS.masterfile, 'OK'))
}

const read = () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/types/lib/general.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,20 @@ export interface RMSliderProps {
values: number[]
handleChange: RMSliderHandleChange
}

export type ScanTypes = 'scanNext' | 'scanZone'

export interface ScanOnDemandData {
typeName: ScanTypes
type: 'scan_next'
scanCoords?: [number, number][]
scanLocation?: [number, number]
scanSize?: number
cooldown?: number
}

export interface ScanOnDemandReq {
category: ScanTypes | 'getQueue'
method: HttpMethod
data?: ScanOnDemandData
}
2 changes: 1 addition & 1 deletion packages/types/lib/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export interface GqlContext {
user: string
transaction: Transaction
operation: 'query' | 'mutation'
startTime: number
startTime?: number
}

export interface Permissions {
Expand Down
9 changes: 7 additions & 2 deletions server/src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,14 @@
},
"scanner": {
"backendConfig": {
"platform": "rdm/mad",
"platform": "rdm/mad/custom",
"apiEndpoint": "http://ip:port/api/",
"headers": [],
"apiUsername": "username",
"apiPassword": "password",
"queueRefreshInterval": 5,
"sendDiscordMessage": true
"sendDiscordMessage": true,
"sendTelegramMessage": true
},
"scanNext": {
"enabled": false,
Expand Down Expand Up @@ -681,6 +682,10 @@
{
"name": "telegram",
"type": "telegram",
"logGroupId": "",
"scanNextLogGroupId": "",
"scanZoneLogGroupId": "",
"eventLogGroupId": "",
"enabled": false,
"botToken": "",
"groups": [],
Expand Down
82 changes: 47 additions & 35 deletions server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const config = require('@rm/config')

const buildDefaultFilters = require('../services/filters/builder/base')
const filterComponents = require('../services/functions/filterComponents')
const { filterRTree } = require('../services/functions/filterRTree')
const validateSelectedWebhook = require('../services/functions/validateSelectedWebhook')
const PoracleAPI = require('../services/api/Poracle')
const { geocoder } = require('../services/geocoder')
const scannerApi = require('../services/api/scannerApi')
const getPolyVector = require('../services/functions/getPolyVector')
const getPlacementCells = require('../services/functions/getPlacementCells')
const getTypeCells = require('../services/functions/getTypeCells')
const { getValidCoords } = require('../services/functions/getValidCoords')

/** @type {import("@apollo/server").ApolloServerOptions<import("@rm/types").GqlContext>['resolvers']} */
const resolvers = {
Expand Down Expand Up @@ -106,22 +106,8 @@ const resolvers = {
return !!results.length
},
/** @param {unknown} _ @param {{ mode: 'scanNext' | 'scanZone' }} args */
checkValidScan: (_, { mode, points }, { perms }) => {
if (perms?.scanner.includes(mode)) {
const areaRestrictions =
config.getSafe(`scanner.${mode}.${mode}AreaRestriction`) || []

const validPoints = points.map((point) =>
filterRTree(
{ lat: point[0], lon: point[1] },
perms.areaRestrictions,
areaRestrictions,
),
)
return validPoints
}
return []
},
checkValidScan: (_, { mode, points }, { perms }) =>
getValidCoords(mode, points, perms),
/** @param {unknown} _ @param {{ component: 'loginPage' | 'donationPage' | 'messageOfTheDay' }} args */
customComponent: (_, { component }, { perms, req, user }) => {
switch (component) {
Expand Down Expand Up @@ -521,16 +507,9 @@ const resolvers = {
}))
}
if (category === 'invasion') {
const { invasions } = Event.masterfile
result.invasion = result.invasion.map((x) => ({
...x,
real_grunt_id:
+Object.keys(invasions).find(
(key) =>
invasions[key]?.type?.toLowerCase() ===
x.grunt_type.toLowerCase() &&
invasions[key].gender === (x.gender || 1),
) || 0,
real_grunt_id: PoracleAPI.getRealGruntId(x, Event.invasions),
}))
}
if (category === 'raid') {
Expand All @@ -539,7 +518,6 @@ const resolvers = {
allMoves: x.move === 9000,
}))
}

return result
}
return {}
Expand Down Expand Up @@ -598,20 +576,33 @@ const resolvers = {
}
return {}
},
/** @param {unknown} _ @param {import('@rm/types').ScanOnDemandReq} args */
scanner: (_, args, { req, perms }) => {
const { category, method, data } = args
if (data?.cooldown) {
req.session.cooldown = Math.max(
req.session.cooldown || 0,
data.cooldown || 0,
)
req.session.save()
}
if (category === 'getQueue') {
return scannerApi(category, method, data, req?.user)
}
if (perms?.scanner?.includes(category)) {
return scannerApi(category, method, data, req?.user)
if (
perms?.scanner?.includes(category) &&
(!req.session.cooldown || req.session.cooldown < Date.now())
) {
const validCoords = getValidCoords(category, data?.scanCoords, perms)

const cooldown =
config.getSafe(`scanner.${category}.userCooldownSeconds`) *
validCoords.filter(Boolean).length *
1000 +
Date.now()
req.session.cooldown = cooldown
return scannerApi(
category,
method,
{
...data,
scanCoords: data.scanCoords?.filter((__, i) => validCoords[i]),
},
req?.user,
)
}
return {}
},
Expand Down Expand Up @@ -665,6 +656,27 @@ const resolvers = {
status,
data,
)
if (category === 'pokemon') {
result.pokemon = result.pokemon.map((x) => ({
...x,
allForms: !x.form,
pvpEntry: !!x.pvp_ranking_league,
xs: x.max_weight !== 9000000,
xl: x.min_weight !== 0,
}))
}
if (category === 'invasion') {
result.invasion = result.invasion.map((x) => ({
...x,
real_grunt_id: PoracleAPI.getRealGruntId(x, Event.invasions),
}))
}
if (category === 'raid') {
result.raid = result.raid.map((x) => ({
...x,
allMoves: x.move === 9000,
}))
}
return result
}
return {}
Expand Down
12 changes: 9 additions & 3 deletions server/src/graphql/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader')
const {
ApolloServerPluginDrainHttpServer,
} = require('@apollo/server/plugin/drainHttpServer')
const {
ApolloServerPluginLandingPageDisabled,
} = require('@apollo/server/plugin/disabled')

const config = require('@rm/config')

const { log, HELPERS } = require('@rm/logger')
Expand All @@ -28,7 +32,7 @@ async function startApollo(httpServer) {
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
introspection: config.getSafe('devOptions.enabled'),
introspection: config.getSafe('devOptions.graphiql'),
formatError: (e) => {
let customMessage = ''
if (
Expand Down Expand Up @@ -76,12 +80,14 @@ async function startApollo(httpServer) {
error: (...e) => log.error(HELPERS.gql, ...e),
},
plugins: [
ApolloServerPluginLandingPageDisabled(),
ApolloServerPluginDrainHttpServer({ httpServer }),
{
async requestDidStart(requestContext) {
requestContext.contextValue.startTime = Date.now()

log.debug(requestContext.request?.variables?.filters)
if (requestContext.request?.variables?.filters) {
log.debug(requestContext.request?.variables?.filters)
}
return {
async willSendResponse(context) {
const filterCount = Object.keys(
Expand Down
9 changes: 4 additions & 5 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ if (sentry.enabled || process.env.SENTRY_DSN) {
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
],
tracesSampleRate:
parseFloat(
process.env.SENTRY_TRACES_SAMPLE_RATE || sentry.tracesSampleRate,
) || 0.1,
+(process.env.SENTRY_TRACES_SAMPLE_RATE || sentry.tracesSampleRate) ||
0.1,
release: pkg.version,
})

Expand Down Expand Up @@ -215,10 +214,10 @@ app.use((err, req, res, next) => {
startApollo(httpServer).then((server) => {
app.use(
'/graphql',
cors(),
cors({ origin: '/' }),
json(),
expressMiddleware(server, {
context: ({ req, res }) => {
context: async ({ req, res }) => {
const perms = req.user ? req.user.perms : req.session.perms
const user = req?.user?.username || ''
const id = req?.user?.id || 0
Expand Down
10 changes: 6 additions & 4 deletions server/src/models/Pokestop.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ class Pokestop extends Model {
const general = []
const rocketPokemon = []
const displayTypes = []
let hasShowcase = false
// preps arrays for interested objects
Object.keys(args.filters).forEach((pokestop) => {
switch (pokestop.charAt(0)) {
case 'o':
break
case 'f':
case 'h':
// do nothing
hasShowcase = true
break
case 'd':
stardust.push(pokestop.slice(1).split('-')[0])
Expand Down Expand Up @@ -254,6 +255,7 @@ class Pokestop extends Model {
break
}
})
if (hasShowcase) displayTypes.push('9')

// builds the query
query.andWhere((stops) => {
Expand Down Expand Up @@ -705,8 +707,8 @@ class Pokestop extends Model {
.filter((event) =>
isMad && !hasMultiInvasions
? MADE_UP_MAD_INVASIONS.includes(event.grunt_type) ||
(!event.grunt_type && filters[`b${event.display_type}`])
: !event.grunt_type && filters[`b${event.display_type}`],
!event.grunt_type
: !event.grunt_type,
)
.map((event) => ({
event_expire_timestamp: event.incident_expire_timestamp,
Expand Down Expand Up @@ -739,7 +741,7 @@ class Pokestop extends Model {
]
: event.showcase_pokemon_type_id
? filters[`h${event.showcase_pokemon_type_id}`]
: true,
: filters[`b${event.display_type}`],
)
}
if (
Expand Down
5 changes: 4 additions & 1 deletion server/src/services/DiscordClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ class DiscordClient {
}
}

/** @param {import('discord.js').APIEmbed} embed @param {string} channel */
/**
* @param {import('discord.js').APIEmbed} embed
* @param {keyof DiscordClient['loggingChannels']} channel
*/
async sendMessage(embed, channel = 'main') {
const safeChannel = this.loggingChannels[channel]
if (!safeChannel || typeof embed !== 'object') {
Expand Down
Loading

0 comments on commit 3310e77

Please sign in to comment.