From fbd9446806ad3535435729179276ecca4df319d5 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sun, 2 May 2021 20:38:28 -0500 Subject: add `/constants` api to get max minions and fairy souls (#21) --- src/constants.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/constants.ts') 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 { +export async function fetchJSONConstant(filename: string): Promise { const file = await fetchFile(filename) try { return JSON.parse(file.content) @@ -229,3 +229,34 @@ export async function fetchMinions(): Promise { export async function addMinions(addingMinions: string[]): Promise { await constants.addJSONConstants('minions.json', addingMinions, 'minion') } + +interface constantValues { + max_minions?: number + max_fairy_souls?: number +} + +export async function fetchConstantValues(): Promise { + 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 -- cgit