Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@noah/mega-radio

MEGA Radio (the WAKE album) for members of the MEGA.dev Discord: sign in with Kody, verify with Discord, listen with repeat.

FollowForkOpen app

Files

stats.ts

32 lines · 1.4 KB · TypeScript
/**
 * Listen stats wording and rules, runtime-free (the player script mirrors the wording).
 * A play counts once 30 s have played or the track ends, each repeat loop included; one count per member
 * per track per track length at most.
 */
export const COUNT_AFTER_SECONDS = 30
export const ON_REPEAT_AFTER = 3

export type Plays = { total: number; mine: number }

/** "m:ss" to seconds (null when unknown). */
export function lengthSeconds(length: string | null | undefined): number | null {
	const m = /^(\d+):(\d{2})$/.exec(length ?? '')
	return m ? Number(m[1]) * 60 + Number(m[2]) : null
}

/**
 * Shortest gap between two counted plays of one track by one member.
 *
 * @param length - the track length "m:ss"
 * @returns milliseconds: 90% of the track length (drift allowance), at least 30 s
 */
export function minGapMs(length: string | null | undefined): number {
	const s = lengthSeconds(length) ?? COUNT_AFTER_SECONDS
	return Math.max(COUNT_AFTER_SECONDS, Math.floor(s * 0.9)) * 1000
}

/** Tooltip text, e.g. "7 plays by MEGA.dev members". */
export const playsTip = (p: Plays) => `${p.total} ${p.total === 1 ? 'play' : 'plays'} by MEGA.dev members${p.mine ? ` (${p.mine} ${p.mine === 1 ? 'is' : 'are'} yours)` : ''}`

/** " · you've got it on repeat (5)" once you've played it ON_REPEAT_AFTER+ times, else "". */
export const onRepeat = (mine: number) => (mine >= ON_REPEAT_AFTER ? ` · you’ve got it on repeat (${mine})` : '')