This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathindex.ts
78 lines (62 loc) · 2.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import config from '../config/server'
import path from 'path'
import { Challenge, CleanedChallenge } from './types'
import { Provider, ProviderConstructor } from './Provider'
import { challUpdateEmitter, publishChallUpdate } from '../cache/challs'
import { EventEmitter } from 'events'
let provider: Provider
let challenges: Challenge[] = []
let cleanedChallenges: CleanedChallenge[] = []
let challengesMap = new Map<string, Challenge>()
let cleanedChallengesMap = new Map<string, CleanedChallenge>()
const cleanChallenge = (chall: Challenge): CleanedChallenge => {
const { files, description, author, points, id, name, category, sortWeight } = chall
return {
files,
description,
author,
points,
id,
name,
category,
sortWeight
}
}
const onUpdate = (newChallenges: Challenge[]): void => {
challenges = newChallenges
challengesMap = new Map(newChallenges.map(c => [c.id, c]))
cleanedChallenges = challenges.map(cleanChallenge)
cleanedChallengesMap = new Map(cleanedChallenges.map(c => [c.id, c]))
}
void import(path.join('../providers', config.challengeProvider.name))
.then(({ default: Provider }: { default: ProviderConstructor }): void => {
provider = new Provider(config.challengeProvider.options ?? {})
provider.on('update', onUpdate)
})
// FIXME: remove cast once cache is typed
;(challUpdateEmitter as EventEmitter).on('update', () => {
provider.forceUpdate()
})
export function getAllChallenges (): Challenge[] {
return challenges
}
export function getCleanedChallenges (): CleanedChallenge[] {
return cleanedChallenges
}
export function getChallenge (id: string): Challenge | undefined {
return challengesMap.get(id)
}
export function getCleanedChallenge (id: string): CleanedChallenge | undefined {
return cleanedChallengesMap.get(id)
}
export function resetCache (): void {
provider.forceUpdate()
}
export async function updateChallenge (chall: Challenge): Promise<void> {
await provider.updateChallenge(chall)
await publishChallUpdate()
}
export async function deleteChallenge (id: string): Promise<void> {
await provider.deleteChallenge(id)
await publishChallUpdate()
}