Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config to set scanner cooldown by role #1014

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@
"pokemon": false,
"gmf": true,
"scanNextInstance": "scanNext",
"rules": [],
"scanNextDevice": "Device01",
"scanNextSleeptime": 5,
"userCooldownSeconds": 0,
Expand All @@ -643,6 +644,7 @@
"gmf": false,
"scanZoneMaxSize": 10,
"userCooldownSeconds": 0,
"rules": [],
"advancedScanZoneOptions": false,
"scanZoneRadius": {
"pokemon": 70,
Expand Down
8 changes: 8 additions & 0 deletions packages/config/lib/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ const applyMutations = (config) => {
}
Object.keys(config.scanner || {}).forEach((key) => {
config.scanner[key] = replaceBothAliases(config.scanner[key] || {})
config.scanner[key]?.rules?.forEach((rule) => {
rule.role = replaceAliases(rule.role)
})
if (config.scanner[key]?.rules) {
config.scanner[key].rulesObj = Object.fromEntries(
config.scanner[key]?.rules?.map((rule) => [rule.role, rule.cooldown]),
)
}
})

if (
Expand Down
1 change: 1 addition & 0 deletions packages/types/lib/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export interface Permissions extends BasePerms {
areaRestrictions: string[]
webhooks: string[]
trial: boolean
scannerCooldowns: Record<string, number>
}

export interface FilterId {
Expand Down
16 changes: 10 additions & 6 deletions server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,19 @@ const resolvers = {
gymRadius: scanner.scanZone.scanZoneRadius.gym,
spacing: scanner.scanZone.scanZoneSpacing,
maxSize: scanner.scanZone.scanZoneMaxSize,
cooldown: scanner.scanZone.userCooldownSeconds,
cooldown:
perms?.scannerCooldowns?.[mode] ??
scanner.scanZone.userCooldownSeconds,
refreshQueue: scanner.backendConfig.queueRefreshInterval,
enabled: scanner[mode].enabled,
}
: {
scannerType: scanner.backendConfig.platform,
showScanCount: scanner.scanNext.showScanCount,
showScanQueue: scanner.scanNext.showScanQueue,
cooldown: scanner.scanNext.userCooldownSeconds,
cooldown:
perms?.scannerCooldowns?.[mode] ??
scanner.scanNext.userCooldownSeconds,
refreshQueue: scanner.backendConfig.queueRefreshInterval,
enabled: scanner[mode].enabled,
}
Expand Down Expand Up @@ -616,11 +620,11 @@ const resolvers = {
(!req.session.cooldown || req.session.cooldown < Date.now())
) {
const validCoords = getValidCoords(category, data?.scanCoords, perms)

const cooldownSeconds =
perms?.scannerCooldowns?.[category] ||
config.getSafe(`scanner.${category}.userCooldownSeconds`)
const cooldown =
config.getSafe(`scanner.${category}.userCooldownSeconds`) *
validCoords.filter(Boolean).length *
1000 +
cooldownSeconds * validCoords.filter(Boolean).length * 1000 +
Date.now()
req.session.cooldown = cooldown
return scannerApi(
Expand Down
27 changes: 23 additions & 4 deletions server/src/services/DiscordClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class DiscordClient extends AuthClient {
blockedGuildNames: new Set(),
}
const scanner = config.getSafe('scanner')
perms.scannerCooldowns = {}

try {
const guilds = user.guilds?.map((guild) => guild.id) || []
if (
Expand All @@ -141,9 +143,12 @@ class DiscordClient extends AuthClient {
Object.keys(this.perms).forEach((key) => (perms[key] = true))
perms.admin = true
config.getSafe('webhooks').forEach((x) => permSets.webhooks.add(x.name))
Object.keys(scanner).forEach(
(x) => scanner[x]?.enabled && permSets.scanner.add(x),
)
Object.keys(scanner).forEach((x) => {
if (scanner[x]?.enabled) {
permSets.scanner.add(x)
perms.scannerCooldowns[x] = 0
}
})
this.log.debug(
`User ${user.username} (${user.id}) in allowed users list, skipping guild and role check.`,
)
Expand Down Expand Up @@ -195,7 +200,21 @@ class DiscordClient extends AuthClient {
(x) => permSets.webhooks.add(x),
)
scannerPerms(userRoles, 'discordRoles', trialActive).forEach(
(x) => permSets.scanner.add(x),
(x) => {
permSets.scanner.add(x)
perms.scannerCooldowns[x] = scanner[x].rules.reduce(
(acc, rule) => {
if (
userRoles.includes(rule?.role) &&
rule.cooldown < acc
) {
return rule.cooldown
}
return acc
},
scanner[x].userCooldownSeconds,
)
},
)
}
}),
Expand Down
18 changes: 18 additions & 0 deletions server/src/services/TelegramClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class TelegramClient extends AuthClient {
areaRestrictions: areaPerms(groups),
webhooks: webhookPerms(groups, 'telegramGroups', trialActive),
scanner: scannerPerms(groups, 'telegramGroups', trialActive),
scannerCooldowns: {},
},
}
if (newUserObj.perms.trial) {
Expand All @@ -115,6 +116,23 @@ class TelegramClient extends AuthClient {

if (this.strategy.allowedUsers?.includes(newUserObj.id)) {
newUserObj.perms.admin = true
Object.keys(newUserObj.perms.scanner).forEach((x) => {
newUserObj.perms.scannerCooldowns[x] = 0
})
} else {
const scanner = config.getSafe('scanner')

Object.keys(newUserObj.perms.scanner).forEach((mode) => {
newUserObj.perms.scannerCooldowns[mode] = scanner[mode].rules.reduce(
(acc, rule) => {
if (rule.cooldown < acc) {
return rule.cooldown
}
return acc
},
scanner[mode].userCooldownSeconds,
)
})
}
return newUserObj
}
Expand Down