Skip to content

Commit

Permalink
Merge branch 'master' into pixel-update
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Jul 5, 2024
2 parents 58fa893 + ded3616 commit 9b24464
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 110 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"lint:fix": "npx @biomejs/biome lint --apply .",
"lint:fix-unsafe": "npx @biomejs/biome lint --apply-unsafe .",
"fmt": "npx dprint fmt",
"format": "npx dprint fmt",
"csslint": "npx stylelint Synergism.css",
"csslint:fix": "npx stylelint Synergism.css --fix",
"htmllint": "npx htmlhint index.html",
Expand Down
3 changes: 2 additions & 1 deletion src/BlueberryUpgrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { calculateAdditiveLuckMult, calculateAmbrosiaGenerationSpeed, calculateA
import { DynamicUpgrade } from './DynamicUpgrade'
import type { IUpgradeData } from './DynamicUpgrade'
import { exportData, saveFilename } from './ImportExport'
import { getQuarkBonus } from './Quark'
import { format, player } from './Synergism'
import type { Player } from './types/Synergism'
import { Alert, Confirm, Prompt } from './UpdateHTML'
Expand Down Expand Up @@ -667,7 +668,7 @@ export const blueberryUpgradeData: Record<
return baseCost * (Math.pow(level + 1, 2) - Math.pow(level, 2))
},
rewards: (n: number) => {
const val = 1 + (n * player.worlds.BONUS) / 100
const val = 1 + (n * getQuarkBonus()) / 100
return {
blueberryGeneration: val,
desc: String(
Expand Down
6 changes: 3 additions & 3 deletions src/Calculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { addTimers, automaticTools } from './Helper'
import { hepteractEffective } from './Hepteracts'
import { disableHotkeys, enableHotkeys } from './Hotkeys'
import { computeMetaBarLevel } from './PixelUpgrades'
import { quarkHandler } from './Quark'
import { getQuarkBonus, quarkHandler } from './Quark'
import { reset } from './Reset'
import { calculateSingularityDebuff } from './singularity'
import { getFastForwardTotalMultiplier } from './singularity'
Expand Down Expand Up @@ -2110,7 +2110,7 @@ export const getOcteractValueMultipliers = () => {
+player.singularityUpgrades.singOcteractGain5.getEffect().bonus,
// Patreon bonus
1
+ (player.worlds.BONUS / 100)
+ (getQuarkBonus() / 100)
* +player.singularityUpgrades.singOcteractPatreonBonus.getEffect().bonus,
// octeracts for dummies
1 + 0.2 * +player.octeractUpgrades.octeractStarter.getEffect().bonus,
Expand Down Expand Up @@ -2519,7 +2519,7 @@ export const calculateGoldenQuarkMultiplier = (computeMultiplier = false) => {

const arr = [
1 + Math.max(0, Math.log10(player.challenge15Exponent + 1) - 20) / 2, // Challenge 15 Exponent
1 + player.worlds.BONUS / 100, // Patreon Bonus
1 + getQuarkBonus() / 100, // Patreon Bonus
+player.singularityUpgrades.goldenQuarks1.getEffect().bonus, // Golden Quarks I
1 + 0.12 * player.cubeUpgrades[69], // Cookie Upgrade 19
+player.singularityChallenges.noSingularityUpgrades.rewards.goldenQuarks, // No Singularity Upgrades
Expand Down
4 changes: 2 additions & 2 deletions src/ImportExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { octeractGainPerSecond } from './Calculate'
import { testing, version } from './Config'
import { Synergism } from './Events'
import { addTimers } from './Helper'
import { quarkHandler } from './Quark'
import { getQuarkBonus, quarkHandler } from './Quark'
import { playerJsonSchema } from './saves/PlayerJsonSchema'
import { shopData } from './Shop'
import { singularityData } from './singularity'
Expand Down Expand Up @@ -240,7 +240,7 @@ export const exportSynergism = async (
const quarkData = quarkHandler()

let bonusGQMultiplier = 1
bonusGQMultiplier *= 1 + player.worlds.BONUS / 100
bonusGQMultiplier *= 1 + getQuarkBonus() / 100
bonusGQMultiplier *= player.highestSingularityCount >= 100
? 1 + player.highestSingularityCount / 50
: 1
Expand Down
21 changes: 15 additions & 6 deletions src/Login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import i18next from 'i18next'
import localforage from 'localforage'
import { DOMCacheGetOrSet } from './Cache/DOM'
import { QuarkHandler } from './Quark'
import { QuarkHandler, setQuarkBonus } from './Quark'
import { player } from './Synergism'
import { Alert } from './UpdateHTML'

Expand Down Expand Up @@ -86,19 +86,28 @@ export async function handleLogin () {
| SynergismDiscordUserAPIResponse
| SynergismPatreonUserAPIResponse

player.worlds = new QuarkHandler({
quarks: Number(player.worlds),
bonus: 100 * (1 + globalBonus / 100) * (1 + personalBonus / 100) - 100 // Multiplicative
})
setQuarkBonus(100 * (1 + globalBonus / 100) * (1 + personalBonus / 100) - 100)
player.worlds = new QuarkHandler(Number(player.worlds))

currentBonus.textContent = `Generous patrons give you a bonus of ${globalBonus}% more Quarks!`

const cookies = parseDocumentCookie()

if (cookies.id || cookies.patreonId) {
Alert('You may need to login to your account again for bonuses to apply! Thank you!')
}

if (cookies.id) document.cookie = 'id=;Max-Age=0'
if (cookies.patreonId) document.cookie = 'patreonId=;Max-Age=0'

if (location.hostname !== 'synergism.cc') {
// TODO: better error, make link clickable, etc.
subtabElement.textContent = 'Login is not available here, go to https://synergism.cc instead!'
} else if (parseDocumentCookie().id || parseDocumentCookie().patreonId) {
} else if (cookies.token) {
if (!member) {
console.log(response, globalBonus, member, personalBonus, document.cookie)
Alert('Your individual bonuses were not applied. Try refreshing the page!')
return
}

currentBonus.textContent +=
Expand Down
99 changes: 7 additions & 92 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 @@ -76,33 +37,23 @@ export const quarkHandler = () => {
}
}

let bonus = 0

export const setQuarkBonus = (newBonus: number) => bonus = newBonus
export const getQuarkBonus = () => bonus

export class QuarkHandler {
/** Global quark bonus */
public BONUS = 0
/** Quark amount */
private QUARKS = 0

private interval: ReturnType<typeof setInterval> | null = null

constructor ({ bonus, quarks }: { bonus?: number; quarks: number }) {
constructor (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. */
applyBonus (amount: number) {
const nonPatreon = calculateQuarkMultiplier()
return amount * (1 + (this.BONUS / 100)) * nonPatreon
return amount * (1 + (getQuarkBonus() / 100)) * nonPatreon
}

/** Subtracts quarks, as the name suggests. */
Expand All @@ -122,42 +73,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
6 changes: 3 additions & 3 deletions src/Synergism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ import { handleLogin } from './Login'
import { octeractData, OcteractUpgrade } from './Octeracts'
import { pixelData, PixelUpgrade } from './PixelUpgrades'
import { updatePlatonicUpgradeBG } from './Platonic'
import { QuarkHandler } from './Quark'
import { getQuarkBonus, QuarkHandler } from './Quark'
import { playerJsonSchema } from './saves/PlayerJsonSchema'
import { playerSchema } from './saves/PlayerSchema'
import { getFastForwardTotalMultiplier, singularityData, SingularityUpgrade } from './singularity'
Expand All @@ -181,7 +181,7 @@ import type { PlayerSave } from './types/LegacySynergism'

export const player: Player = {
firstPlayed: new Date().toISOString(),
worlds: new QuarkHandler({ quarks: 0, bonus: 0 }),
worlds: new QuarkHandler(0),
coins: new Decimal('1e2'),
coinsThisPrestige: new Decimal('1e2'),
coinsThisTranscension: new Decimal('1e2'),
Expand Down Expand Up @@ -5109,7 +5109,7 @@ export const resetCheck = async (
i18next.t('main.singularityMessage4', {
x: format(nextSingularityNumber),
y: format(calculateGoldenQuarkGain(), 2, true),
z: format(player.worlds.BONUS)
z: format(getQuarkBonus())
})
)
await Alert(i18next.t('main.singularityMessage5'))
Expand Down
4 changes: 2 additions & 2 deletions src/UpdateVisuals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { IMultiBuy } from './Cubes'
import type { hepteractTypes } from './Hepteracts'
import { hepteractTypeList } from './Hepteracts'
import { computeMetaBarLevel, LEVEL_REQ_ARR } from './PixelUpgrades'
import { quarkHandler } from './Quark'
import { getQuarkBonus, quarkHandler } from './Quark'
import { displayRuneInformation } from './Runes'
import { getShopCosts, isShopUpgradeUnlocked, shopData, shopUpgradeTypes } from './Shop'
import { getGoldenQuarkCost } from './singularity'
Expand Down Expand Up @@ -1343,7 +1343,7 @@ export const visualUpdateSettings = () => {
const maxExportQuarks = quarkData.capacity

let goldenQuarkMultiplier = 1
goldenQuarkMultiplier *= 1 + player.worlds.BONUS / 100
goldenQuarkMultiplier *= 1 + getQuarkBonus() / 100
goldenQuarkMultiplier *= player.highestSingularityCount >= 100
? 1 + player.highestSingularityCount / 50
: 1
Expand Down
2 changes: 1 addition & 1 deletion src/saves/PlayerSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const hepteractCraftSchema = (k: keyof Player['hepteractCrafts']) =>

export const playerSchema = z.object({
firstPlayed: z.string().datetime().optional().default(() => new Date().toISOString()),
worlds: z.number().transform((quarks) => new QuarkHandler({ quarks })),
worlds: z.number().transform((quarks) => new QuarkHandler(quarks)),
coins: decimalSchema,
coinsThisPrestige: decimalSchema,
coinsThisTranscension: decimalSchema,
Expand Down

0 comments on commit 9b24464

Please sign in to comment.