Files
elysium/apps/web/src/utils/sound.ts
T
hikari a36c8e72a5
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 1m3s
CI / Lint, Build & Test (push) Successful in 1m8s
feat: error handling, logging, analytics, OG tags, and sticky sidebar (#44)
## Summary

- Add comprehensive try/catch error handling across all API routes, middleware, and the Hono global error handler, piping every unhandled error to the `@nhcarrigan/logger` service to prevent silent crashes and unhandled Promise rejections
- Add a `logError` utility on the frontend that forwards errors through the overridden `console.error` to the backend telemetry endpoint; apply it to every silent `catch {}` block in the game context, sound, notification, and clipboard utilities, and wrap the React tree in an `ErrorBoundary`
- Add Plausible analytics, Open Graph + Twitter Card meta tags, Tree-Nation widget, and Google Ads to `index.html`
- Make the game sidebar sticky with a `--resource-bar-height` CSS custom property offset so it stays viewport-height without overlapping the resource bar; reset sticky behaviour in the mobile responsive override

## Test plan

- [ ] Lint passes: `pnpm lint`
- [ ] Build passes: `pnpm build`
- [ ] Verify errors thrown in API routes appear in the logger service rather than crashing the process
- [ ] Verify frontend errors appear in the `/api/fe/error` backend log
- [ ] Verify Open Graph tags render correctly when sharing the URL
- [ ] Verify Plausible analytics fires on page load
- [ ] Verify Tree-Nation badge renders in the sidebar
- [ ] Verify sidebar stays fixed while the main content scrolls on desktop
- [ ] Verify mobile layout is unaffected

 This issue was created with help from Hikari~ 🌸

Reviewed-on: #44
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
2026-03-09 19:54:42 -07:00

113 lines
3.1 KiB
TypeScript

/**
* @file Sound effect utilities using the Web Audio API.
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { logError } from "./logError.js";
type SoundEvent =
| "achievement"
| "apotheosis"
| "bossVictory"
| "prestige"
| "questCompleted"
| "questFailed"
| "transcendence";
interface SoundPattern {
frequencies: Array<number>;
gain: number;
noteDuration: number;
type: OscillatorType;
}
// eslint-disable-next-line @typescript-eslint/naming-convention -- intentional constant name
const SOUND_PATTERNS: Record<SoundEvent, SoundPattern> = {
achievement: {
frequencies: [ 523, 659, 784, 1047 ],
gain: 0.3,
noteDuration: 0.12,
type: "triangle",
},
apotheosis: {
frequencies: [ 1047, 880, 784, 659, 523 ],
gain: 0.35,
noteDuration: 0.25,
type: "sine",
},
bossVictory: {
frequencies: [ 523, 784, 1047 ],
gain: 0.4,
noteDuration: 0.18,
type: "square",
},
prestige: {
frequencies: [ 392, 523, 659, 784 ],
gain: 0.35,
noteDuration: 0.15,
type: "sawtooth",
},
questCompleted: {
frequencies: [ 523, 659 ],
gain: 0.25,
noteDuration: 0.15,
type: "sine",
},
questFailed: {
frequencies: [ 392, 330, 261 ],
gain: 0.25,
noteDuration: 0.18,
type: "triangle",
},
transcendence: {
frequencies: [ 261, 329, 392, 523 ],
gain: 0.3,
noteDuration: 0.3,
type: "sine",
},
};
// eslint-disable-next-line @typescript-eslint/init-declarations -- lazily initialised on first use
let audioContext: AudioContext | undefined;
const getAudioContext = (): AudioContext => {
if (audioContext === undefined) {
audioContext = new AudioContext();
}
return audioContext;
};
/**
* Plays a sound effect for a given game event using the Web Audio API.
* @param event - The game event to play a sound for.
*/
const playSound = (event: SoundEvent): void => {
try {
const context = getAudioContext();
const pattern = SOUND_PATTERNS[event];
for (const [ index, frequency ] of pattern.frequencies.entries()) {
const oscillator = context.createOscillator();
const gainNode = context.createGain();
oscillator.connect(gainNode);
gainNode.connect(context.destination);
oscillator.type = pattern.type;
oscillator.frequency.value = frequency;
const noteOffset = index * pattern.noteDuration;
const startTime = context.currentTime + noteOffset;
const endTime = startTime + pattern.noteDuration;
gainNode.gain.setValueAtTime(0, startTime);
gainNode.gain.linearRampToValueAtTime(pattern.gain, startTime + 0.01);
gainNode.gain.linearRampToValueAtTime(0, endTime);
oscillator.start(startTime);
oscillator.stop(endTime);
}
} catch (error_: unknown) {
logError("play_sound", error_);
// Silently ignore — audio may not be available in all environments
}
};
export type { SoundEvent };
export { playSound };