diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-05-02 20:38:28 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-02 20:38:28 -0500 |
commit | fbd9446806ad3535435729179276ecca4df319d5 (patch) | |
tree | 83b3ea672dac268417d93ac236eafc3c6fd3debe /src/constants.ts | |
parent | fd7991ba7318b32027db8dbe9160490e8652b199 (diff) | |
download | skyblock-api-fbd9446806ad3535435729179276ecca4df319d5.tar.gz skyblock-api-fbd9446806ad3535435729179276ecca4df319d5.tar.bz2 skyblock-api-fbd9446806ad3535435729179276ecca4df319d5.zip |
add `/constants` api to get max minions and fairy souls (#21)
Diffstat (limited to 'src/constants.ts')
-rw-r--r-- | src/constants.ts | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/constants.ts b/src/constants.ts index 14d94cd..52c104c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -124,7 +124,7 @@ async function editFile(file: GithubFile, message: string, newContent: string): }) } -export async function fetchJSONConstant(filename: string): Promise<string[]> { +export async function fetchJSONConstant(filename: string): Promise<any> { const file = await fetchFile(filename) try { return JSON.parse(file.content) @@ -229,3 +229,34 @@ export async function fetchMinions(): Promise<string[]> { export async function addMinions(addingMinions: string[]): Promise<void> { await constants.addJSONConstants('minions.json', addingMinions, 'minion') } + +interface constantValues { + max_minions?: number + max_fairy_souls?: number +} + +export async function fetchConstantValues(): Promise<constantValues> { + return await constants.fetchJSONConstant('values.json') +} + +export async function setConstantValues(newValues: constantValues) { + let file: GithubFile = await fetchFile('values.json') + if (!file.path) return + let oldValues: constantValues + try { + oldValues = JSON.parse(file.content) + } catch { + // invalid json, set it as an empty array + oldValues = {} + } + const updatedStats = { ...oldValues, ...newValues } + + // there's not actually any new stats, just return + // TODO: optimize this? might be fine already though, idk + if (JSON.stringify(updatedStats) === JSON.stringify(oldValues)) return + + const commitMessage = 'Update values' + try { + await editFile(file, commitMessage, JSON.stringify(updatedStats, null, 2)) + } catch {} +}
\ No newline at end of file |