diff --git a/src/Quark.ts b/src/Quark.ts index ffb8702a1..64e523ab5 100644 --- a/src/Quark.ts +++ b/src/Quark.ts @@ -1,44 +1,5 @@ -/* Functions which Handle Quark Gains, */ - -import { DOMCacheGetOrSet } from './Cache/DOM' import { calculateCubeQuarkMultiplier, calculateQuarkMultiplier } from './Calculate' import { format, player } from './Synergism' -import { Alert } from './UpdateHTML' - -const getBonus = async (): Promise => { - if (!navigator.onLine) { - return null - } - if (document.visibilityState === 'hidden') { - return null - } - - try { - const r = await fetch('https://synergism-quarks.khafra.workers.dev/') - const j = await r.json() as { bonus: number } - - return j.bonus - } catch (e) { - console.log(`workers.dev: ${(e as Error).message}`) - } - - try { - const r = await fetch('https://api.github.com/gists/44be6ad2dcf0d44d6a29dffe1d66a84a', { - headers: { - Accept: 'application/vnd.github.v3+json' - } - }) - - const t = await r.json() as { files: Record } - const b = Number(t.files['SynergismQuarkBoost.txt'].content) - - return b - } catch (e) { - console.log(`GitHub Gist: ${(e as Error).message}`) - } - - return null -} export const quarkHandler = () => { let maxTime = 90000 // In Seconds @@ -82,21 +43,12 @@ export class QuarkHandler { /** Quark amount */ private QUARKS = 0 - private interval: ReturnType | null = null - constructor ({ bonus, quarks }: { bonus?: number; quarks: number }) { this.QUARKS = quarks if (bonus) { this.BONUS = bonus - } else { - void this.getBonus() } - - if (this.interval) clearInterval(this.interval) - - // although the values are cached for 15 mins, refresh every 5 - this.interval = setInterval(this.getBonus.bind(this), 60 * 1000 * 5) } /*** Calculates the number of quarks to give with the current bonus. */ @@ -122,42 +74,6 @@ export class QuarkHandler { return this } - async getBonus () { - const el = DOMCacheGetOrSet('currentBonus') - - if (location.hostname === 'synergism.cc') { - return - } - - if (localStorage.getItem('quarkBonus') !== null) { // is in cache - const { bonus, fetched } = JSON.parse(localStorage.getItem('quarkBonus')!) as { bonus: number; fetched: number } - if (Date.now() - fetched < 60 * 1000 * 15) { // cache is younger than 15 minutes - el.textContent = `Generous patrons give you a bonus of ${bonus}% more Quarks!` - return this.BONUS = bonus - } - } else if (!navigator.onLine) { - return el.textContent = 'Current Bonus: N/A% (offline)!' - } else if (document.hidden) { - return el.textContent = 'Current Bonus: N/A% (unfocused)!' - } - - const b = await getBonus() - - if (b === null) { - return - } else if (Number.isNaN(b) || typeof b !== 'number') { - return Alert('No bonus could be applied, a network error occurred! [Invalid Bonus] :(') - } else if (!Number.isFinite(b)) { - return Alert('No bonus could be applied, an error occurred. [Infinity] :(') - } else if (b < 0) { - return Alert('No bonus could be applied, an error occurred. [Zero] :(') - } - - el.textContent = `Generous patrons give you a bonus of ${b}% more Quarks!` - localStorage.setItem('quarkBonus', JSON.stringify({ bonus: b, fetched: Date.now() })) - this.BONUS = b - } - public toString (val: number): string { return format(Math.floor(this.applyBonus(val)), 0, true) }