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
Allow users to set specific cooldown by roles
  • Loading branch information
sherlocksometimes committed May 21, 2024
1 parent 8be85b6 commit 7151b22
Show file tree
Hide file tree
Showing 6 changed files with 63 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: Record<string, number>
}

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.scanNext.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
27 changes: 23 additions & 4 deletions server/src/services/DiscordClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,20 @@ class DiscordClient {
blockedGuildNames: new Set(),
}
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)
perms.scannerCooldowns[x] = 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 +193,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
18 changes: 18 additions & 0 deletions server/src/services/TelegramClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,28 @@ class TelegramClient {
areaRestrictions: areaPerms(groups),
webhooks: webhookPerms(groups, 'telegramGroups', trialActive),
scanner: scannerPerms(groups, 'telegramGroups', trialActive),
scannerCooldowns: {},
},
}
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
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 7151b22

Please sign in to comment.