Skip to content

Commit

Permalink
fixup! always use synergism.cc for quark bonus
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Jul 3, 2024
1 parent c2601f3 commit 50646bf
Showing 1 changed file with 0 additions and 84 deletions.
84 changes: 0 additions & 84 deletions src/Quark.ts
Original file line number Diff line number Diff line change
@@ -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<null | number> => {
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<string, { content: string }> }
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
Expand Down Expand Up @@ -82,21 +43,12 @@ export class QuarkHandler {
/** Quark amount */
private QUARKS = 0

private interval: ReturnType<typeof setInterval> | 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. */
Expand All @@ -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)
}
Expand Down

0 comments on commit 50646bf

Please sign in to comment.