Skip to content

Commit

Permalink
Improved consumables (#644)
Browse files Browse the repository at this point in the history
* info-all

* spacing

* fixup

* fixup

---------

Co-authored-by: Kevin Bunn <[email protected]>
  • Loading branch information
KhafraDev and Pseudonian authored Feb 2, 2025
1 parent 261cd0b commit d3d907f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<meta property="og:description" content="The greatest idle game of all time. This is the best idle game ever.">

<link rel="icon" href="favicon.ico" type="image/x-icon" sizes="16x16">
<link rel="canonical" href="https://synergism.cc/" />
<link rel="canonical" href="https://synergism.cc/">

<link rel="stylesheet" href="Synergism.css">
<title>Synergism</title>
Expand Down
47 changes: 36 additions & 11 deletions src/Login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const activeConsumables: Record<PseudoCoinConsumableNames, number> = {
HAPPY_HOUR_BELL: 0
}

export let happyHourEndTime = 0
export const allConsumableTimes: Record<PseudoCoinConsumableNames, Array<number>> = {
HAPPY_HOUR_BELL: []
}

const messageSchema = z.preprocess(
(data, ctx) => {
Expand Down Expand Up @@ -75,6 +77,15 @@ const messageSchema = z.preprocess(
}).array(),
tips: z.number().int().nonnegative()
}),
z.object({
type: z.literal('info-all'),
active: z.object({
name: z.string(),
internalName: z.string(),
endsAt: z.number().int()
}).array(),
tips: z.number().int().nonnegative()
}),
/** Received after the *user* successfully redeems a consumable. */
z.object({ type: z.literal('thanks') }),
/** Received when a user is tipped */
Expand Down Expand Up @@ -330,6 +341,13 @@ const queue: string[] = []
const exponentialBackoff = [5000, 15000, 30000, 60000]
let tries = 0

function resetConsumables () {
for (const key in activeConsumables) {
activeConsumables[key as PseudoCoinConsumableNames] = 0
allConsumableTimes[key as PseudoCoinConsumableNames].length = 0 // Specifically for info-all
}
}

function handleWebSocket () {
assert(!ws || ws.readyState === WebSocket.CLOSED, 'WebSocket has been set and is not closed')

Expand All @@ -344,6 +362,7 @@ function handleWebSocket () {
Notification(
'Could not re-establish your connection. Consumables and events related to Consumables will not work.'
)
resetConsumables()
}
})

Expand All @@ -355,6 +374,7 @@ function handleWebSocket () {
}

queue.length = 0
sendToWebsocket(JSON.stringify({ type: 'info-all' }))
})

ws.addEventListener('message', (ev) => {
Expand All @@ -363,6 +383,7 @@ function handleWebSocket () {

if (data.type === 'error') {
Notification(data.message, 5_000)
resetConsumables()
} else if (data.type === 'consumed') {
activeConsumables[data.consumable as PseudoCoinConsumableNames]++
Notification(`Someone redeemed a(n) ${data.consumable}!`)
Expand All @@ -382,10 +403,22 @@ function handleWebSocket () {
ends = Math.max(ends, endsAt)
}

happyHourEndTime = ends
Notification(message)
}

tips = data.tips
} else if (data.type === 'info-all') { // new, needs to be checked
resetConsumables() // So that we can get an accurate count each time
if (data.active.length !== 0) {
let message = 'The following consumables are active:\n'

for (const { internalName, name, endsAt } of data.active) {
activeConsumables[internalName as PseudoCoinConsumableNames]++
allConsumableTimes[internalName as PseudoCoinConsumableNames].push(endsAt)
message += `${name}, until ${endsAt}\n`
}

Notification(message)
updateEventsPage(ends)
}

tips = data.tips
Expand All @@ -405,14 +438,6 @@ function handleWebSocket () {
})
}

function updateEventsPage (endsAt: number) {
const amount = document.getElementById('consumableEventBonus')!
const timer = document.getElementById('consumableEventTimer')!

timer.textContent = new Date(endsAt).toLocaleString()
amount.textContent = `${Object.values(activeConsumables).reduce((a, b) => a + b, 0)}`
}

export function sendToWebsocket (message: string) {
if (ws?.readyState !== WebSocket.OPEN) {
queue.push(message)
Expand Down
12 changes: 10 additions & 2 deletions src/UpdateVisuals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type { IMultiBuy } from './Cubes'
import { BuffType, consumableEventBuff, eventBuffType, getEvent, getEventBuff } from './Event'
import type { hepteractTypes } from './Hepteracts'
import { hepteractTypeList } from './Hepteracts'
import { activeConsumables, happyHourEndTime } from './Login'
import { activeConsumables, allConsumableTimes, type PseudoCoinConsumableNames } from './Login'
import { PCoinUpgradeEffects } from './PseudoCoinUpgrades'
import { getQuarkBonus, quarkHandler } from './Quark'
import { displayRuneInformation } from './Runes'
Expand Down Expand Up @@ -1809,6 +1809,14 @@ export const visualUpdateShop = () => {
} Quarks Each`
}

export const constructConsumableTimes = (p: PseudoCoinConsumableNames) => {
const msg: string[] = []
for (const time of allConsumableTimes[p]) {
msg.push(timeReminingHours(new Date(time)))
}
return msg.join(', ')
}

export const visualUpdateEvent = () => {
const event = getEvent()
if (event !== null) {
Expand All @@ -1835,7 +1843,7 @@ export const visualUpdateEvent = () => {
}
const { HAPPY_HOUR_BELL } = activeConsumables
if (HAPPY_HOUR_BELL > 0) {
DOMCacheGetOrSet('consumableEventTimer').textContent = timeReminingHours(new Date(happyHourEndTime))
DOMCacheGetOrSet('consumableEventTimer').textContent = constructConsumableTimes('HAPPY_HOUR_BELL')
DOMCacheGetOrSet('consumableEventBonus').textContent = `${HAPPY_HOUR_BELL}`

for (let i = 0; i < eventBuffType.length; i++) {
Expand Down

0 comments on commit d3d907f

Please sign in to comment.