@@ -24,18 +24,30 @@ interface SoundSettings {
2424 type : string ;
2525 level : number ;
2626 tournament_level ?: number ;
27+ tournamentLevel ?: number ;
2728}
2829
29- const soundSettings = getPageProp < { sound_settings : SoundSettings } > ( 'current_user' , {
30- sound_settings : { type : 'standard' , level : 5 } ,
30+ const defaultSoundSettings : SoundSettings = { type : 'standard' , level : 5 } ;
31+ const initialSoundSettings = getPageProp < { sound_settings : SoundSettings } > ( 'current_user' , {
32+ sound_settings : defaultSoundSettings ,
3133} ) . sound_settings ;
32- const soundType = soundSettings . type ;
33- const defaultSoundLevel = soundSettings . level * 0.1 ;
34- const tournamentSoundLevel = isUndefined ( soundSettings . tournament_level )
35- ? defaultSoundLevel
36- : soundSettings . tournament_level * 0.1 ;
3734
38- const audio = ( type : string = soundType , volume : number = defaultSoundLevel ) =>
35+ let currentSoundSettings = { ...defaultSoundSettings , ...initialSoundSettings } ;
36+
37+ const getSoundType = ( ) => currentSoundSettings . type ;
38+ const getDefaultSoundLevel = ( ) => currentSoundSettings . level * 0.1 ;
39+ const getTournamentSoundLevel = ( ) => {
40+ const tournamentLevel =
41+ currentSoundSettings . tournamentLevel ?? currentSoundSettings . tournament_level ;
42+
43+ return isUndefined ( tournamentLevel ) ? getDefaultSoundLevel ( ) : tournamentLevel * 0.1 ;
44+ } ;
45+
46+ const configureSound = ( settings : SoundSettings ) => {
47+ currentSoundSettings = { ...defaultSoundSettings , ...settings } ;
48+ } ;
49+
50+ const audio = ( type : string = getSoundType ( ) , volume : number = getDefaultSoundLevel ( ) ) =>
3951 new Howl ( {
4052 src : audioPaths [ type as keyof typeof audioPaths ] ,
4153 sprite : ( audioConfigs [ type as keyof typeof audioConfigs ] as { sprite ?: unknown } )
@@ -48,7 +60,7 @@ const getAssetPlayer = (path: string) => {
4860 if ( ! assetPlayers [ path ] ) {
4961 assetPlayers [ path ] = new Howl ( {
5062 src : path ,
51- volume : defaultSoundLevel ,
63+ volume : getDefaultSoundLevel ( ) ,
5264 } ) ;
5365 }
5466
@@ -58,27 +70,27 @@ const getAssetPlayer = (path: string) => {
5870const sound = {
5971 play : ( type : string , soundLevel ?: number ) => {
6072 const isMute = JSON . parse ( ( localStorage . getItem ( 'ui_mute_sound' ) || false ) as string ) ;
73+ if ( getSoundType ( ) === 'silent' || isMute ) return ;
6174 const soundEffect = audio ( ) ;
62- if ( soundType === 'silent' || isMute ) return ;
63- Howler . volume ( isUndefined ( soundLevel ) ? defaultSoundLevel : soundLevel ) ;
75+ Howler . volume ( isUndefined ( soundLevel ) ? getDefaultSoundLevel ( ) : soundLevel ) ;
6476 soundEffect . play ( type ) ;
6577 } ,
6678 playAsset : ( path : string , soundLevel ?: number ) => {
6779 const isMute = JSON . parse ( ( localStorage . getItem ( 'ui_mute_sound' ) || false ) as string ) ;
68- if ( soundType === 'silent' || isMute ) return ;
69- Howler . volume ( isUndefined ( soundLevel ) ? defaultSoundLevel : soundLevel ) ;
80+ if ( getSoundType ( ) === 'silent' || isMute ) return ;
81+ Howler . volume ( isUndefined ( soundLevel ) ? getDefaultSoundLevel ( ) : soundLevel ) ;
7082 getAssetPlayer ( path ) . play ( ) ;
7183 } ,
7284 playTournamentAsset : ( path : string ) => {
7385 const isMute = JSON . parse ( ( localStorage . getItem ( 'ui_mute_sound' ) || false ) as string ) ;
74- if ( soundType === 'silent' || isMute ) return ;
75- Howler . volume ( tournamentSoundLevel ) ;
86+ if ( getSoundType ( ) === 'silent' || isMute ) return ;
87+ Howler . volume ( getTournamentSoundLevel ( ) ) ;
7688 const player = getAssetPlayer ( path ) ;
7789 player . volume ( 1 ) ;
7890 player . play ( ) ;
7991 } ,
8092 stop : ( ) => Howler . stop ( ) ,
81- toggle : ( volume : number = defaultSoundLevel ) => {
93+ toggle : ( volume : number = getDefaultSoundLevel ( ) ) => {
8294 Howler . volume ( volume ) ;
8395 } ,
8496} ;
@@ -98,5 +110,5 @@ const createPlayer = () => ({
98110 stop : ( ) => Howler . stop ( ) ,
99111} ) ;
100112
101- export { createPlayer } ;
113+ export { configureSound , createPlayer } ;
102114export default sound ;
0 commit comments