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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Random utility functions that are not related to Hypixel
export function undashUuid(uuid: string): string {
return uuid.replace(/-/g, '').toLowerCase()
}
export function jsonToQuery(data): string {
return Object.entries(data || {}).map(e => e.join('=')).join('&')
}
export function shuffle<T>(a: T[]): T[] {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[a[i], a[j]] = [a[j], a[i]]
}
return a
}
export const minecraftColorCodes: { [key: string]: string } = {
'0': '#000000',
'1': '#0000be',
'2': '#00be00',
'3': '#00bebe',
'4': '#be0000', // red
'5': '#be00be',
'6': '#ffaa00', // gold
'7': '#bebebe',
'8': '#3f3f3f',
'9': '#3f3ffe',
'a': '#3ffe3f',
'b': '#3ffefe',
'c': '#fe3f3f', // light red
'd': '#fe3ffe',
'e': '#fefe3f',
'f': '#ffffff',
'black': '#000000',
'dark_blue': '#0000be',
'dark_green': '#00be00',
'dark_aqua': '#00bebe',
'dark_red': '#be0000', // red
'dark_purple': '#be00be',
'gold': '#ffaa00', // gold
'gray': '#bebebe',
'dark_gray': '#3f3f3f',
'blue': '#3f3ffe',
'green': '#3ffe3f',
'aqua': '#3ffefe',
'red': '#fe3f3f', // light red
'light_purple': '#fe3ffe',
'yellow': '#fefe3f',
'white': '#ffffff',
}
/**
* Converts a color name to the code
* For example: blue -> 9
* @param colorName The name of the color (blue, red, aqua, etc)
*/
export function colorCodeFromName(colorName: string): string | null {
const hexColor = minecraftColorCodes[colorName.toLowerCase()]
for (const key in minecraftColorCodes) {
const value = minecraftColorCodes[key]
if (key.length === 1 && value === hexColor)
return key
}
return null
}
export function letterFromColorCode(colorCode: string): string | null {
for (const [key, value] of Object.entries(minecraftColorCodes)) {
if (value === colorCode)
return key
}
return null
}
export async function sleep(ms: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, ms))
}
/** Returns whether a string is a UUID4 (Minecraft uuid) */
export function isUuid(string: string) {
return undashUuid(string).length === 32
}
/**
* Get a level for an amount of total xp
* @param xp The xp we're finding the level for
* @param xpTable The list of required xp values for each level, starting at 1
*/
export function levelFromXpTable(xp: number, xpTable: number[]) {
const skillLevel = [...xpTable].reverse().findIndex(levelXp => xp >= levelXp)
return skillLevel === -1 ? 0 : xpTable.length - skillLevel
}
// https://stackoverflow.com/a/51365037
export type RecursivePartial<T> = {
[P in keyof T]?:
T[P] extends (infer U)[] ? RecursivePartial<U>[] :
T[P] extends object ? RecursivePartial<T[P]> :
T[P]
}
let caches: Map<string, {
data: any
isFetching: boolean
nextUpdate: Date
}> = new Map()
export async function withCache<T>(key: string, ttl: number | ((arg: T) => Date), task: () => Promise<T>): Promise<T> {
if (caches.get(key)?.data && caches.get(key)!.nextUpdate > new Date())
return caches.get(key)!.data
// if it's currently fetching the election data and it doesn't have it,
// wait until we do have the election data
if (caches.get(key)?.isFetching && !caches.get(key)?.data) {
await new Promise(resolve => {
const interval = setInterval(() => {
if (caches.get(key)?.data) {
clearInterval(interval)
resolve(caches.get(key)!.data)
}
}, 100)
})
}
caches.set(key, {
...(caches.get(key) ?? { data: undefined, nextUpdate: new Date(0) }),
isFetching: true,
})
const data = await task()
caches.set(key, {
...caches.get(key)!,
isFetching: false,
})
if (!data) return undefined as any
caches.set(key, {
...caches.get(key)!,
data
})
const nextUpdate = typeof ttl === 'number' ? new Date(Date.now() + ttl) : ttl(data)
caches.set(key, {
...caches.get(key)!,
nextUpdate
})
return data
}
|