mirror of
https://github.com/Such-Software/wownerogue.git
synced 2026-12-03 20:34:01 +01:00
Begin payment logic and tests
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* AudioAlerts: optional user-enabled audio notifications for key events
|
||||
* - Payment confirmed (game about to start soon / queued)
|
||||
* - Game start (focus attention)
|
||||
* Persists preference in localStorage and uses Web Audio or HTMLAudio fallback.
|
||||
*/
|
||||
const AudioAlerts = {
|
||||
_enabled: false,
|
||||
_initialized: false,
|
||||
_ctx: null,
|
||||
_gain: null,
|
||||
_volume: 0.4,
|
||||
_lastPlay: 0,
|
||||
_cooldownMs: 1500,
|
||||
|
||||
init() {
|
||||
if (this._initialized) return;
|
||||
this._initialized = true;
|
||||
// Load preference
|
||||
try {
|
||||
const saved = localStorage.getItem('wow_audioAlerts');
|
||||
this._enabled = saved === '1';
|
||||
} catch(_) {}
|
||||
const toggle = document.getElementById('audioAlertsToggle');
|
||||
if (toggle) {
|
||||
toggle.checked = this._enabled;
|
||||
toggle.addEventListener('change', () => {
|
||||
this._enabled = !!toggle.checked;
|
||||
try { localStorage.setItem('wow_audioAlerts', this._enabled ? '1':'0'); } catch(_) {}
|
||||
if (this._enabled) this._lazyInitContext();
|
||||
});
|
||||
}
|
||||
|
||||
// Hook into socket events (if SocketHandlers already patched later)
|
||||
if (typeof SocketHandlers !== 'undefined') {
|
||||
this._patchSocketHandlers();
|
||||
}
|
||||
},
|
||||
|
||||
_patchSocketHandlers() {
|
||||
if (SocketHandlers._audioAlertsPatched) return; // idempotent
|
||||
SocketHandlers._audioAlertsPatched = true;
|
||||
const origPaymentConfirmed = SocketHandlers.onPaymentConfirmed;
|
||||
const origGameStart = SocketHandlers.onGameStart;
|
||||
SocketHandlers.onPaymentConfirmed = function(data) {
|
||||
if (origPaymentConfirmed) origPaymentConfirmed.call(SocketHandlers, data);
|
||||
AudioAlerts.playPattern('payment');
|
||||
};
|
||||
SocketHandlers.onGameStart = function(data) {
|
||||
if (origGameStart) origGameStart.call(SocketHandlers, data);
|
||||
AudioAlerts.playPattern('start');
|
||||
};
|
||||
},
|
||||
|
||||
_lazyInitContext() {
|
||||
if (this._ctx) return;
|
||||
try {
|
||||
const AudioCtx = window.AudioContext || window.webkitAudioContext;
|
||||
if (!AudioCtx) return;
|
||||
this._ctx = new AudioCtx();
|
||||
this._gain = this._ctx.createGain();
|
||||
this._gain.gain.value = this._volume;
|
||||
this._gain.connect(this._ctx.destination);
|
||||
} catch (e) {
|
||||
console.warn('Audio context init failed:', e);
|
||||
}
|
||||
},
|
||||
|
||||
playPattern(type) {
|
||||
if (!this._enabled) return;
|
||||
const now = Date.now();
|
||||
if (now - this._lastPlay < this._cooldownMs) return; // basic cooldown
|
||||
this._lastPlay = now;
|
||||
this._lazyInitContext();
|
||||
if (!this._ctx) return;
|
||||
|
||||
if (this._ctx.state === 'suspended') {
|
||||
// Attempt resume on user gesture contexts; safe to call
|
||||
this._ctx.resume().catch(()=>{});
|
||||
}
|
||||
|
||||
// Simple beep patterns
|
||||
const pattern = type === 'start' ? [440, 660, 880] : [440, 660];
|
||||
const dur = 0.18;
|
||||
let t = this._ctx.currentTime + 0.01;
|
||||
pattern.forEach((freq) => {
|
||||
const osc = this._ctx.createOscillator();
|
||||
const gainNode = this._ctx.createGain();
|
||||
osc.frequency.value = freq;
|
||||
osc.type = 'sine';
|
||||
gainNode.gain.setValueAtTime(0.0001, t);
|
||||
gainNode.gain.linearRampToValueAtTime(this._volume, t + 0.01);
|
||||
gainNode.gain.exponentialRampToValueAtTime(0.0001, t + dur);
|
||||
osc.connect(gainNode).connect(this._gain || this._ctx.destination);
|
||||
osc.start(t);
|
||||
osc.stop(t + dur + 0.02);
|
||||
t += dur * 0.5; // slight overlap for chord-like effect
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = AudioAlerts;
|
||||
}
|
||||
Reference in New Issue
Block a user