Skip to content

Commit

Permalink
fix: trial fallbacks for local strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Aug 17, 2024
1 parent 1824f65 commit 0744d1c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/types/lib/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { UiconsIndex } from 'uicons.js'
import { Props as ImgProps } from '@components/Img'

import type { CustomComponent } from './blocks'
import config = require('server/src/configs/default.json')
import config = require('../../../config/default.json')
import example = require('../../../config/local.example.json')

import type { Schema } from './server'
Expand All @@ -22,7 +22,7 @@ type BaseConfig = typeof config
type ExampleConfig = typeof example

export type ConfigAreas = Awaited<
ReturnType<typeof import('server/src/services/areas')['loadLatestAreas']>
ReturnType<(typeof import('server/src/services/areas'))['loadLatestAreas']>
>

export type Config<Client extends boolean = false> = DeepMerge<
Expand All @@ -33,7 +33,7 @@ export type Config<Client extends boolean = false> = DeepMerge<
version: string
locales: string[]
localeStatus: ReturnType<
typeof import('@rm/locales/lib/utils')['getStatus']
(typeof import('@rm/locales/lib/utils'))['getStatus']
>
hasCustom: boolean
title: string
Expand Down
8 changes: 4 additions & 4 deletions server/src/services/EventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ class EventManager extends Logger {
async chatLog(channel, embed, strategy) {
if (strategy) {
if (strategy in this.authClients) {
return this.authClients[strategy].sendMessage(embed, channel)
return this.authClients[strategy]?.sendMessage(embed, channel)
}
this.log.warn(`Strategy ${strategy} not found in authClients`)
} else {
await Promise.allSettled(
Object.values(this.authClients).map(async (client) =>
client.sendMessage(embed, channel),
client?.sendMessage(embed, channel),
),
)
}
Expand All @@ -152,15 +152,15 @@ class EventManager extends Logger {
clearTrialTimers() {
this.log.info('clearing trial timers')
Object.values(this.authClients).forEach((client) =>
client.trialManager.end(),
client?.trialManager?.end(),
)
}

async cleanupTrials() {
this.log.info('cleaning up session for possibly expired trials')
await Promise.allSettled(
Object.values(this.authClients).map((client) =>
client.trialManager.cleanup(),
client?.trialManager?.cleanup(),
),
)
}
Expand Down
12 changes: 11 additions & 1 deletion server/src/services/Trial.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
const { Logger, log } = require('@rm/logger')
const config = require('@rm/config')

const { Timer } = require('./Timer')
const state = require('./state')
Expand All @@ -11,7 +12,16 @@ class Trial extends Logger {

this._name = strategy.name
this._type = strategy.type
this._trial = strategy.trialPeriod
/** @type {import("@rm/types").StrategyConfig['trialPeriod']} */
this._trial = config.util.extendDeep(
{
start: null,
end: null,
intervalHours: 0,
roles: [],
},
strategy.trialPeriod,
)

this._forceActive = false

Expand Down
10 changes: 5 additions & 5 deletions server/src/services/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const serverState = {
try {
if (this.event.authClients[name]) {
// Clear any existing trials before we reinitialize
this.event.authClients[name].trialManager.end()
this.event.authClients[name]?.trialManager?.end()
}
const buildStrategy = fs.existsSync(
path.resolve(__dirname, `../strategies/${name}.js`),
Expand All @@ -62,14 +62,14 @@ const serverState = {
getTrialStatus(strategy) {
if (strategy) {
if (strategy in this.event.authClients) {
return this.event.authClients[strategy].trialManager.status()
return this.event.authClients[strategy]?.trialManager?.status() ?? null
}
throw new Error(`Strategy ${strategy} not found`)
} else {
return Object.fromEntries(
Object.entries(this.event.authClients).map(([k, v]) => [
k,
v.trialManager.status(),
v.trialManager?.status() ?? null,
]),
)
}
Expand All @@ -81,13 +81,13 @@ const serverState = {
setTrials(active, strategy) {
if (strategy) {
if (strategy in this.event.authClients) {
this.event.authClients[strategy].trialManager.setActive(active)
this.event.authClients[strategy]?.trialManager?.setActive(active)
} else {
throw new Error(`Strategy ${strategy} not found`)
}
} else {
Object.values(this.event.authClients).forEach((client) => {
client.trialManager.setActive(active)
client?.trialManager?.setActive(active)
})
}
},
Expand Down

0 comments on commit 0744d1c

Please sign in to comment.