@@ -42,107 +42,120 @@ export default class GameClient {
42
42
chatClient : ChatClient = new ChatClient ( this ) ;
43
43
sfxManager : SfxManager = new SfxManager ( this ) ;
44
44
45
+ get currentVolumeSettings ( ) : { notifications : number , music : number , sfx : number } {
46
+ if ( ! this . authenticatedUser ) {
47
+ throw new Error ( "Authenticated user required" ) ;
48
+ }
49
+ return {
50
+ notifications : this . authenticatedUser . settings . notificationsVolume ,
51
+ music : this . authenticatedUser . settings . musicVolume ,
52
+ sfx : this . authenticatedUser . settings . sfxVolume
53
+ } ;
54
+ }
55
+
56
+ private set currentVolumeSettings ( value : { notifications : number , music : number , sfx : number } ) {
57
+ if ( ! this . authenticatedUser ) {
58
+ throw new Error ( "Authenticated user required" ) ;
59
+ }
60
+ this . authenticatedUser . settings . notificationsVolume = value . notifications ?? 1 ;
61
+ this . authenticatedUser . settings . musicVolume = value . music ?? 1 ;
62
+ this . authenticatedUser . settings . sfxVolume = value . sfx ?? 1 ;
63
+ }
64
+
45
65
get muted ( ) : boolean {
46
66
if ( ! this . authenticatedUser ) {
47
- throw new Error ( "Game client must have an authenticated user" ) ;
67
+ throw new Error ( "Authenticated user required " ) ;
48
68
}
49
69
50
70
return this . authenticatedUser . settings . muted ;
51
71
}
52
72
53
73
set muted ( value : boolean ) {
54
74
if ( ! this . authenticatedUser ) {
55
- throw new Error ( "Game client must have an authenticated user" ) ;
75
+ throw new Error ( "Authenticated user required " ) ;
56
76
}
57
77
58
78
this . authenticatedUser . settings . muted = value ;
59
79
if ( value == true ) {
60
- const oldVolumes = {
61
- notificationsVolume : this . authenticatedUser . settings . notificationsVolume ,
62
- musicVolume : this . authenticatedUser . settings . musicVolume ,
63
- sfxVolume : this . authenticatedUser . settings . sfxVolume
64
- } ;
65
- localStorage . setItem ( 'oldVolumes' , JSON . stringify ( oldVolumes ) ) ;
80
+ sessionStorage . setItem ( 'oldVolumes' , JSON . stringify ( this . currentVolumeSettings ) ) ;
66
81
67
- this . authenticatedUser . settings . notificationsVolume = 0 ;
68
- this . authenticatedUser . settings . musicVolume = 0 ;
69
- this . authenticatedUser . settings . sfxVolume = 0 ;
82
+ this . currentVolumeSettings = { notifications : 0 , music : 0 , sfx : 0 } ;
70
83
71
84
this . sfxManager . muteAll ( ) ;
72
85
} else {
73
- const oldVolumesFromStorage = JSON . parse ( localStorage . getItem ( 'oldVolumes' ) || '{}' ) ;
74
- this . authenticatedUser . settings . notificationsVolume = oldVolumesFromStorage . notificationsVolume ?? 1 ;
75
- this . authenticatedUser . settings . musicVolume = oldVolumesFromStorage . musicVolume ?? 1 ;
76
- this . authenticatedUser . settings . sfxVolume = oldVolumesFromStorage . sfxVolume ?? 1 ;
86
+ const oldVolumesFromStorage = JSON . parse ( sessionStorage . getItem ( 'oldVolumes' ) || '{}' ) ;
87
+ this . currentVolumeSettings = oldVolumesFromStorage ;
77
88
78
- localStorage . removeItem ( 'oldVolumes' ) ;
89
+ localStorage . removeItem ( 'oldVolumes' ) ; // Todo: Remove this some day
90
+ sessionStorage . removeItem ( 'oldVolumes' ) ;
79
91
80
92
this . sfxManager . unmuteAll ( ) ;
81
93
}
82
94
95
+ localStorage . setItem ( 'volumeSettings' , JSON . stringify ( this . currentVolumeSettings ) ) ;
83
96
this . authenticatedUser . syncSettings ( ) ;
84
97
}
85
98
86
99
get notificationsVolume ( ) : number {
87
100
if ( ! this . authenticatedUser ) {
88
- throw new Error ( "Game client must have an authenticated user" ) ;
101
+ throw new Error ( "Authenticated user required " ) ;
89
102
}
90
103
91
104
return this . authenticatedUser . settings . notificationsVolume ;
92
105
}
93
106
94
107
set notificationsVolume ( value : number ) {
95
108
if ( ! this . authenticatedUser ) {
96
- throw new Error ( "Game client must have an authenticated user" ) ;
109
+ throw new Error ( "Authenticated user required " ) ;
97
110
}
98
111
99
112
this . authenticatedUser . settings . notificationsVolume = value ;
100
- this . setCurrentMutedState ( value ) ;
113
+ this . setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage ( ) ;
101
114
102
115
this . authenticatedUser . syncSettings ( ) ;
103
116
}
104
117
105
118
get musicVolume ( ) : number {
106
119
if ( ! this . authenticatedUser ) {
107
- throw new Error ( "Game client must have an authenticated user" ) ;
120
+ throw new Error ( "Authenticated user required " ) ;
108
121
}
109
122
110
123
return this . authenticatedUser . settings . musicVolume ;
111
124
}
112
125
113
126
set musicVolume ( value : number ) {
114
127
if ( ! this . authenticatedUser ) {
115
- throw new Error ( "Game client must have an authenticated user" ) ;
128
+ throw new Error ( "Authenticated user required " ) ;
116
129
}
117
130
118
131
this . authenticatedUser . settings . musicVolume = value ;
119
- this . setCurrentMutedState ( value ) ;
132
+ this . setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage ( ) ;
120
133
121
134
this . authenticatedUser . syncSettings ( ) ;
122
135
}
123
136
124
137
get sfxVolume ( ) : number {
125
138
if ( ! this . authenticatedUser ) {
126
- throw new Error ( "Game client must have an authenticated user" ) ;
139
+ throw new Error ( "Authenticated user required " ) ;
127
140
}
128
141
129
142
return this . authenticatedUser . settings . sfxVolume ;
130
143
}
131
144
132
145
set sfxVolume ( value : number ) {
133
146
if ( ! this . authenticatedUser ) {
134
- throw new Error ( "Game client must have an authenticated user" ) ;
147
+ throw new Error ( "Authenticated user required " ) ;
135
148
}
136
149
137
150
this . authenticatedUser . settings . sfxVolume = value ;
138
- this . setCurrentMutedState ( value ) ;
151
+ this . setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage ( ) ;
139
152
140
153
this . authenticatedUser . syncSettings ( ) ;
141
154
}
142
155
143
156
get authenticatedPlayer ( ) : Player | null {
144
157
if ( ! this . authenticatedUser ) {
145
- throw new Error ( "Game client must have an authenticated user" ) ;
158
+ throw new Error ( "Authenticated user required " ) ;
146
159
}
147
160
148
161
if ( ! this . entireGame || ! ( this . entireGame . childGameState instanceof IngameGameState ) ) {
@@ -156,16 +169,18 @@ export default class GameClient {
156
169
}
157
170
}
158
171
159
- private setCurrentMutedState ( value : number ) : void {
172
+ private setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage ( ) : void {
160
173
if ( ! this . authenticatedUser ) {
161
174
return ;
162
175
}
163
176
164
- if ( value > 0 ) {
165
- this . authenticatedUser . settings . muted = false ;
166
- } else if ( this . musicVolume == 0 && this . notificationsVolume == 0 && this . sfxVolume == 0 ) {
177
+ if ( this . musicVolume == 0 && this . notificationsVolume == 0 && this . sfxVolume == 0 ) {
167
178
this . authenticatedUser . settings . muted = true ;
179
+ } else {
180
+ this . authenticatedUser . settings . muted = false ;
168
181
}
182
+
183
+ localStorage . setItem ( 'volumeSettings' , JSON . stringify ( this . currentVolumeSettings ) ) ;
169
184
}
170
185
171
186
constructor ( authData : AuthData ) {
@@ -307,6 +322,7 @@ export default class GameClient {
307
322
308
323
this . connectionState = ConnectionState . SYNCED ;
309
324
this . isReconnecting = false ;
325
+ this . loadVolumeSettingsFromLocalStorage ( ) ;
310
326
} else if ( message . type == "new-private-chat-room" ) {
311
327
if ( this . entireGame == null ) {
312
328
return ;
@@ -377,4 +393,15 @@ export default class GameClient {
377
393
this . authenticatedUser = null ;
378
394
this . isReconnecting = false ;
379
395
}
396
+
397
+ private loadVolumeSettingsFromLocalStorage ( ) : void {
398
+ const item = localStorage . getItem ( 'volumeSettings' ) ;
399
+ if ( ! item ) {
400
+ return ;
401
+ }
402
+
403
+ const volumeSettings = JSON . parse ( item ) ;
404
+ this . currentVolumeSettings = volumeSettings ;
405
+ this . setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage ( ) ;
406
+ }
380
407
}
0 commit comments