import _ from 'lodash' ; import { SoundData , SoundId , SoundIdToNameMap , SoundName } from 'constants/sound' ; import { SettingsState } from 'store/ducks/settings' ; /**
 * Re-exports sound list.
 */ export { SoundId } ; /**
 * Sound manager.
 */ export default class Sound { class public static manager ( ) { if ( _ . isNil ( Sound . instance ) ) { Sound . instance = new Sound ( ) ; } return Sound . instance ; } private static instance : Sound ; private audios : Partial < Record < SoundName , HTMLAudioElement > > = { } ; private volumes : Partial < Record < SoundId , number > > = { } ; private delayBetweenThrottledSoundsInMs = 2000 ; private lastThrottledSoundTimestamp = Date . now ( ) ; class private constructor ( ) { _ . forEach ( SoundName , ( name ) => { this . audios [ name ] = new Audio ( SoundData [ name ] ) ; } ) ; } param settings public udpateVolumes ( settings ) { _ . forEach ( settings , ( { volume } , soundIdStr ) => { const soundId = parseInt ( soundIdStr , 10 ) ; this . volumes [ soundId ] = volume ; } ) ; } param delay public updateDelayBetweenThrottledSounds ( delay ) { this . delayBetweenThrottledSoundsInMs = delay * 1000 ; this . lastThrottledSoundTimestamp = Date . now ( ) ; } param sound return public getSoundDetails ( soundId ) { const soundName = SoundIdToNameMap [ soundId ] ; return { audio : _ . get ( this . audios , soundName ) , volume : _ . get ( this . volumes , soundId ) || 0 , } ; } param soundId param throttled public play ( soundId , throttled = false ) { if ( ! throttled ) { this . playSound ( soundId ) ; } else { const now = Date . now ( ) ; if ( now - this . lastThrottledSoundTimestamp > this . delayBetweenThrottledSoundsInMs ) { this . playSound ( soundId ) ; this . lastThrottledSoundTimestamp = now ; } } } param soundId see private async playSound ( soundId ) { const { audio , volume } = this . getSoundDetails ( soundId ) ; if ( ! _ . isNil ( audio ) ) { try { audio . volume = volume ; await audio . play ( ) ; } catch ( error ) { } } } }