1
- /* Functions which Handle Quark Gains, */
2
-
3
- import { DOMCacheGetOrSet } from './Cache/DOM'
4
1
import { calculateCubeQuarkMultiplier , calculateQuarkMultiplier } from './Calculate'
5
2
import { format , player } from './Synergism'
6
- import { Alert } from './UpdateHTML'
7
-
8
- const getBonus = async ( ) : Promise < null | number > => {
9
- if ( ! navigator . onLine ) {
10
- return null
11
- }
12
- if ( document . visibilityState === 'hidden' ) {
13
- return null
14
- }
15
-
16
- try {
17
- const r = await fetch ( 'https://synergism-quarks.khafra.workers.dev/' )
18
- const j = await r . json ( ) as { bonus : number }
19
-
20
- return j . bonus
21
- } catch ( e ) {
22
- console . log ( `workers.dev: ${ ( e as Error ) . message } ` )
23
- }
24
-
25
- try {
26
- const r = await fetch ( 'https://api.github.com/gists/44be6ad2dcf0d44d6a29dffe1d66a84a' , {
27
- headers : {
28
- Accept : 'application/vnd.github.v3+json'
29
- }
30
- } )
31
-
32
- const t = await r . json ( ) as { files : Record < string , { content : string } > }
33
- const b = Number ( t . files [ 'SynergismQuarkBoost.txt' ] . content )
34
-
35
- return b
36
- } catch ( e ) {
37
- console . log ( `GitHub Gist: ${ ( e as Error ) . message } ` )
38
- }
39
-
40
- return null
41
- }
42
3
43
4
export const quarkHandler = ( ) => {
44
5
let maxTime = 90000 // In Seconds
@@ -82,21 +43,12 @@ export class QuarkHandler {
82
43
/** Quark amount */
83
44
private QUARKS = 0
84
45
85
- private interval : ReturnType < typeof setInterval > | null = null
86
-
87
46
constructor ( { bonus, quarks } : { bonus ?: number ; quarks : number } ) {
88
47
this . QUARKS = quarks
89
48
90
49
if ( bonus ) {
91
50
this . BONUS = bonus
92
- } else {
93
- void this . getBonus ( )
94
51
}
95
-
96
- if ( this . interval ) clearInterval ( this . interval )
97
-
98
- // although the values are cached for 15 mins, refresh every 5
99
- this . interval = setInterval ( this . getBonus . bind ( this ) , 60 * 1000 * 5 )
100
52
}
101
53
102
54
/*** Calculates the number of quarks to give with the current bonus. */
@@ -122,42 +74,6 @@ export class QuarkHandler {
122
74
return this
123
75
}
124
76
125
- async getBonus ( ) {
126
- const el = DOMCacheGetOrSet ( 'currentBonus' )
127
-
128
- if ( location . hostname === 'synergism.cc' ) {
129
- return
130
- }
131
-
132
- if ( localStorage . getItem ( 'quarkBonus' ) !== null ) { // is in cache
133
- const { bonus, fetched } = JSON . parse ( localStorage . getItem ( 'quarkBonus' ) ! ) as { bonus : number ; fetched : number }
134
- if ( Date . now ( ) - fetched < 60 * 1000 * 15 ) { // cache is younger than 15 minutes
135
- el . textContent = `Generous patrons give you a bonus of ${ bonus } % more Quarks!`
136
- return this . BONUS = bonus
137
- }
138
- } else if ( ! navigator . onLine ) {
139
- return el . textContent = 'Current Bonus: N/A% (offline)!'
140
- } else if ( document . hidden ) {
141
- return el . textContent = 'Current Bonus: N/A% (unfocused)!'
142
- }
143
-
144
- const b = await getBonus ( )
145
-
146
- if ( b === null ) {
147
- return
148
- } else if ( Number . isNaN ( b ) || typeof b !== 'number' ) {
149
- return Alert ( 'No bonus could be applied, a network error occurred! [Invalid Bonus] :(' )
150
- } else if ( ! Number . isFinite ( b ) ) {
151
- return Alert ( 'No bonus could be applied, an error occurred. [Infinity] :(' )
152
- } else if ( b < 0 ) {
153
- return Alert ( 'No bonus could be applied, an error occurred. [Zero] :(' )
154
- }
155
-
156
- el . textContent = `Generous patrons give you a bonus of ${ b } % more Quarks!`
157
- localStorage . setItem ( 'quarkBonus' , JSON . stringify ( { bonus : b , fetched : Date . now ( ) } ) )
158
- this . BONUS = b
159
- }
160
-
161
77
public toString ( val : number ) : string {
162
78
return format ( Math . floor ( this . applyBonus ( val ) ) , 0 , true )
163
79
}
0 commit comments