Skip to content

Commit

Permalink
Merge pull request #781 from WatWowMap/route-support
Browse files Browse the repository at this point in the history
feat: initial route support
  • Loading branch information
TurtIeSocks authored Aug 1, 2023
2 parents 501361f + 4d9add5 commit 30ad22b
Show file tree
Hide file tree
Showing 45 changed files with 1,010 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"i18n-ally.localesPaths": [
"public/base-locales"
"locales"
],
"i18n-ally.keystyle": "flat",
"[javascript]": {
Expand Down
12 changes: 11 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -626,5 +626,15 @@
"level_circles": "Level Indicators",
"min_level_circle": "Minimum Circle Level",
"mutation_auth_error": "Your request was unsuccessful due to not being logged in",
"submitted_by": "Submitted By"
"submitted_by": "Submitted By",
"reversible": "Reversible",
"version": "Version",
"route_tags": "Route Tags",
"routes": "Routes",
"route_type": "Route Type",
"routes_subtitle": "View in game routes and relevant information about them on the map",
"description": "Description",
"additional_info": "Additional Info",
"duration": "Duration",
"elevation": "Elevation"
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactmap",
"version": "1.21.1",
"version": "1.22.2",
"description": "React based frontend map.",
"main": "ReactMap.js",
"author": "TurtIeSocks <[email protected]>",
Expand Down Expand Up @@ -35,10 +35,10 @@
"yarn": "^1.22.x"
},
"devDependencies": {
"@sentry/vite-plugin": "^2.2.1",
"@sentry/vite-plugin": "2.2.1",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.0",
"@vitejs/plugin-react": "4.0.0",
"eslint": "^8.44.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.8.0",
Expand All @@ -50,9 +50,9 @@
"nodemon": "^2.0.22",
"prettier": "^2.8.8",
"rollup-plugin-delete": "^2.0.0",
"vite": "^4.3.9",
"vite-plugin-checker": "^0.6.0",
"vite-plugin-static-copy": "^0.16.0"
"vite": "4.3.9",
"vite-plugin-checker": "0.6.0",
"vite-plugin-static-copy": "0.16.0"
},
"dependencies": {
"@apollo/client": "^3.7.15",
Expand Down
Binary file added public/images/perms/routes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions server/scripts/configMigration.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ const mergeAuth = async () => {
allowedGuilds: obj?.allowedGuilds,
blockedGuilds: obj?.blockedGuilds,
allowedUsers: obj?.allowedUsers,
clientPrompt: obj?.clientPrompt,
})

const telegramObj = (obj, name) => ({
Expand Down
11 changes: 11 additions & 0 deletions server/src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"pokemon": 20,
"pokestops": 300,
"portals": 300,
"routes": 10000,
"scanAreas": 10000,
"scanCells": 10,
"submissionCells": 500,
Expand Down Expand Up @@ -133,6 +134,7 @@
"nests",
"pokestops",
"pokemon",
"routes",
"wayfarer",
"s2cells",
"scanAreas",
Expand Down Expand Up @@ -564,6 +566,9 @@
"portals": {
"enabled": false
},
"routes": {
"enabled": true
},
"scanAreas": {
"enabled": false
},
Expand Down Expand Up @@ -661,6 +666,7 @@
"allowedGuilds": [],
"blockedGuilds": [],
"allowedUsers": [],
"clientPrompt": "consent",
"thumbnailUrl": "https://user-images.githubusercontent.com/58572875/167069223-745a139d-f485-45e3-a25c-93ec4d09779c.png",
"trialPeriod": {
"start": {
Expand Down Expand Up @@ -835,6 +841,11 @@
"enabled": true,
"trialPeriodEligible": false,
"roles": []
},
"routes": {
"enabled": true,
"trialPeriodEligible": false,
"roles": []
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion server/src/configs/local.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"pokestop",
"scanCell",
"spawnpoint",
"weather"
"weather",
"route"
]
},
{
Expand Down Expand Up @@ -236,6 +237,11 @@
"enabled": true,
"trialPeriodEligible": false,
"roles": []
},
"routes": {
"enabled": true,
"trialPeriodEligible": false,
"roles": []
}
}
},
Expand Down
15 changes: 14 additions & 1 deletion server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { S2LatLng, S2RegionCoverer, S2LatLngRect } = require('nodes2ts')
const config = require('../services/config')
const Utility = require('../services/Utility')
const Fetch = require('../services/Fetch')
const buildDefaultFilters = require('../services/filters/builder/base')

/**
* @typedef {(parent: unknown, args: object, context: import('../types').GqlContext) => unknown} Resolver
Expand All @@ -28,7 +29,7 @@ const resolvers = {
return {
...available,
masterfile: { ...Event.masterfile, invasions: Event.invasions },
filters: Utility.buildDefaultFilters(perms, available, Db.models),
filters: buildDefaultFilters(perms, available, Db),
}
},
backup: (_, args, { req, perms, Db }) => {
Expand Down Expand Up @@ -141,6 +142,18 @@ const resolvers = {
}
return {}
},
route: (_, args, { perms, Db }) => {
if (perms?.routes) {
return Db.query('Route', 'getOne', args.id)
}
return {}
},
routes: (_, args, { perms, Db }) => {
if (perms?.routes) {
return Db.query('Route', 'getAll', perms, args)
}
return []
},
s2cells: (_, args, { perms }) => {
if (perms?.s2cells) {
const { onlyCells } = args.filters
Expand Down
30 changes: 30 additions & 0 deletions server/src/graphql/scannerTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,34 @@ module.exports = gql`
status: String
message: String
}
type Waypoint {
lat_degrees: Float
lng_degrees: Float
elevation_in_meters: Float
}
type Route {
id: ID
name: String
description: String
distance_meters: Int
duration_seconds: Int
start_fort_id: String
start_lat: Float
start_lon: Float
start_image: String
end_fort_id: String
end_lat: Float
end_lon: Float
end_image: String
image: String
image_border_color: String
reversible: Boolean
tags: [String]
type: Int
updated: Int
version: Int
waypoints: [Waypoint]
}
`
8 changes: 8 additions & 0 deletions server/src/graphql/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ module.exports = gql`
maxLon: Float
filters: JSON
): [Weather]
route(id: ID): Route
routes(
minLat: Float
maxLat: Float
minLon: Float
maxLon: Float
filters: JSON
): [Route]
webhook(category: String, status: String, name: String): Poracle
scanner(category: String, method: String, data: JSON): ScannerApi
}
Expand Down
1 change: 1 addition & 0 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ connection.migrate.latest().then(async () => {
await Db.getDbContext()
await Promise.all([
Db.historicalRarity(),
Db.getFilterContext(),
Event.setAvailable('gyms', 'Gym', Db),
Event.setAvailable('pokestops', 'Pokestop', Db),
Event.setAvailable('pokemon', 'Pokemon', Db),
Expand Down
105 changes: 105 additions & 0 deletions server/src/models/Route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const { Model } = require('objection')
const getAreaSql = require('../services/functions/getAreaSql')

const GET_ALL_SELECT = /** @type {const} */ ([
'id',
'start_lat',
'start_lon',
'end_lat',
'end_lon',
'waypoints',
'image_border_color',
'reversible',
])

class Route extends Model {
static get tableName() {
return 'route'
}

/**
* Returns the bare essentials for displaying on the map
* @param {import('../types').Permissions} perms
* @param {object} args
* @param {import('../types').DbContext} ctx
* @returns
*/
static async getAll(perms, args, ctx) {
const { areaRestrictions } = perms
const { onlyAreas, onlyDistance } = args.filters

const distanceInMeters = (onlyDistance || [0.5, 100]).map((x) => x * 1000)

const query = this.query()
.select(GET_ALL_SELECT)
.whereBetween('start_lat', [args.minLat, args.maxLat])
.andWhereBetween('start_lon', [args.minLon, args.maxLon])
.andWhereBetween('distance_meters', distanceInMeters)
.union((qb) => {
qb.select(GET_ALL_SELECT)
.whereBetween('end_lat', [args.minLat, args.maxLat])
.andWhereBetween('end_lon', [args.minLon, args.maxLon])
.andWhereBetween('distance_meters', distanceInMeters)
.from('route')

getAreaSql(qb, areaRestrictions, onlyAreas, ctx.isMad, 'route')
})

if (!getAreaSql(query, areaRestrictions, onlyAreas, ctx.isMad, 'route')) {
return []
}
const results = await query

return results.map((result) => {
if (typeof result.waypoints === 'string') {
result.waypoints = JSON.parse(result.waypoints)
} else if (result.waypoints === null) {
result.waypoints = []
}
return result
})
}

/**
* Returns the full route after querying it, generally from the Popup
* @param {number} id
*/
static async getOne(id) {
const result = await this.query().findById(id)
if (typeof result.waypoints === 'string') {
result.waypoints = JSON.parse(result.waypoints)
} else if (result.waypoints === null) {
result.waypoints = []
}
if (typeof result.tags === 'string') {
result.tags = JSON.parse(result.tags)
} else if (result.tags === null) {
result.tags = []
}
if (typeof result.image === 'string') {
result.image = result.image.replace('http://', 'https://')
}
if (typeof result.start_image === 'string') {
result.start_image = result.start_image.replace('http://', 'https://')
}
if (typeof result.end_image === 'string') {
result.end_image = result.end_image.replace('http://', 'https://')
}
return result
}

/**
* returns route context
* @returns {{ max_distance: number, max_duration: number }}
*/
static async getFilterContext() {
const result = await this.query()
.max('distance_meters AS max_distance')
.max('duration_seconds AS max_duration')
.first()

return result
}
}

module.exports = Route
2 changes: 2 additions & 0 deletions server/src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Pokestop = require('./Pokestop')
const Pokemon = require('./Pokemon')
const Portal = require('./Portal')
const Ring = require('./Ring')
const Route = require('./Route')
const ScanCell = require('./ScanCell')
const Session = require('./Session')
const Spawnpoint = require('./Spawnpoint')
Expand All @@ -30,6 +31,7 @@ const scannerModels = {
Pokestop,
Pokemon,
Portal,
Route,
ScanCell,
Spawnpoint,
Weather,
Expand Down
18 changes: 10 additions & 8 deletions server/src/routes/authRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ strategies.forEach((strategy, i) => {
strategy.type === 'discord' || strategy.type === 'telegram' ? 'get' : 'post'
if (strategy.enabled) {
const name = strategy.name ?? `${strategy.type}-${i}`
router[method](
`/${name}`,
passport.authenticate(name, {
failureRedirect: '/',
successRedirect: '/',
}),
)
const callbackOptions = {}
const authenticateOptions = {
failureRedirect: '/',
successRedirect: '/',
}
if (strategy.type === 'discord') {
callbackOptions.prompt = strategy.clientPrompt
}
router[method](`/${name}`, passport.authenticate(name, authenticateOptions))
router[method](`/${name}/callback`, async (req, res, next) =>
passport.authenticate(name, async (err, user, info) => {
passport.authenticate(name, callbackOptions, async (err, user, info) => {
if (err) {
return next(err)
}
Expand Down
5 changes: 3 additions & 2 deletions server/src/routes/rootRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Fetch = require('../services/Fetch')
const { Event, Db } = require('../services/initialization')
const { version } = require('../../../package.json')
const { log, HELPERS } = require('../services/logger')
const buildDefaultFilters = require('../services/filters/builder/base')

const rootRouter = express.Router()

Expand Down Expand Up @@ -287,10 +288,10 @@ rootRouter.get('/api/settings', async (req, res, next) => {
}
}

serverSettings.defaultFilters = Utility.buildDefaultFilters(
serverSettings.defaultFilters = buildDefaultFilters(
serverSettings.user.perms,
serverSettings.available,
Db.models,
Db,
)

// Backup in case there are Pokemon/Quests/Raids etc that are not in the masterfile
Expand Down
Loading

0 comments on commit 30ad22b

Please sign in to comment.