Skip to content

Commit

Permalink
feat: add config to set scanner cooldown by role
Browse files Browse the repository at this point in the history
example config setup (scanZone accepts the same format)
 "scanNext": {
      ...
      // Discord roles that will be allowed to use scanNext
      "discordRoles": ["Supporter","Scanner", "Mod"],
      // Default cooldown if no role specific cooldown
     "userCooldownSeconds": 60,
      //List of role/cooldown pair shortest matching time will apply
     "discordRoleCooldown":[["Supporter",30],["Mod",0]],
      ...
}

Expected outcome:
Supporter: 30 second cooldown
Mod: 0 second cooldown
Scanner: will get the default 60 second cooldown.
Supporter & Mod role: the lower 0 second cooldown would apply.
  • Loading branch information
sherlocksometimes committed May 21, 2024
1 parent 8be85b6 commit 47cbbdb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/types/lib/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface Permissions {
scanner: string[]
areaRestrictions: string[]
webhooks: string[]
scannerCooldowns: string[]
}

export interface Waypoint {
Expand Down
4 changes: 3 additions & 1 deletion server/src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
"pokemon": false,
"gmf": true,
"scanNextInstance": "scanNext",
"rules": [],
"scanNextDevice": "Device01",
"scanNextSleeptime": 5,
"userCooldownSeconds": 0,
Expand All @@ -634,6 +635,7 @@
"gmf": false,
"scanZoneMaxSize": 10,
"userCooldownSeconds": 0,
"rules": [],
"advancedScanZoneOptions": false,
"scanZoneRadius": {
"pokemon": 70,
Expand Down Expand Up @@ -1025,4 +1027,4 @@
"tracesSampleRate": 0.1
}
}
}
}
16 changes: 10 additions & 6 deletions server/src/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,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.scanZone.userCooldownSeconds,
refreshQueue: scanner.backendConfig.queueRefreshInterval,
enabled: scanner[mode].enabled,
}
Expand Down Expand Up @@ -597,11 +601,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
28 changes: 24 additions & 4 deletions server/src/services/DiscordClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,23 @@ class DiscordClient {
webhooks: new Set(),
scanner: new Set(),
blockedGuildNames: new Set(),
rules: new Map(),
}
const scanner = config.getSafe('scanner')
perms.scannerCooldowns = {}

try {
const guilds = user.guilds.map((guild) => guild.id)
if (this.strategy.allowedUsers.includes(user.id)) {
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)
scanner[x].userCooldownSeconds = 0
}
})
log.info(
HELPERS.custom(this.rmStrategy, '#7289da'),
`User ${user.username} (${user.id}) in allowed users list, skipping guild and role check.`,
Expand Down Expand Up @@ -188,7 +194,21 @@ class DiscordClient {
(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
8 changes: 8 additions & 0 deletions server/src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ if (Array.isArray(config.webhooks)) {
}
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

0 comments on commit 47cbbdb

Please sign in to comment.