Skip to content

Commit fc90c4a

Browse files
committed
Save volume settings to local storage
1 parent ceaf085 commit fc90c4a

File tree

4 files changed

+72
-369
lines changed

4 files changed

+72
-369
lines changed

agot-bg-game-server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"classnames": "^2.3.1",
3030
"css-loader": "^6.7.3",
3131
"dotenv": "^14.2.0",
32-
"emoji-picker-react": "^4.0.6",
32+
"emoji-picker-react": "^4.10.0",
3333
"eslint": "^8.7.0",
3434
"eslint-plugin-react": "^7.28.0",
3535
"express": "^4.17.2",

agot-bg-game-server/src/client/GameClient.ts

+57-30
Original file line numberDiff line numberDiff line change
@@ -42,107 +42,120 @@ export default class GameClient {
4242
chatClient: ChatClient = new ChatClient(this);
4343
sfxManager: SfxManager = new SfxManager(this);
4444

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+
4565
get muted(): boolean {
4666
if (!this.authenticatedUser) {
47-
throw new Error("Game client must have an authenticated user");
67+
throw new Error("Authenticated user required");
4868
}
4969

5070
return this.authenticatedUser.settings.muted;
5171
}
5272

5373
set muted(value: boolean) {
5474
if (!this.authenticatedUser) {
55-
throw new Error("Game client must have an authenticated user");
75+
throw new Error("Authenticated user required");
5676
}
5777

5878
this.authenticatedUser.settings.muted = value;
5979
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));
6681

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};
7083

7184
this.sfxManager.muteAll();
7285
} 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;
7788

78-
localStorage.removeItem('oldVolumes');
89+
localStorage.removeItem('oldVolumes'); // Todo: Remove this some day
90+
sessionStorage.removeItem('oldVolumes');
7991

8092
this.sfxManager.unmuteAll();
8193
}
8294

95+
localStorage.setItem('volumeSettings', JSON.stringify(this.currentVolumeSettings));
8396
this.authenticatedUser.syncSettings();
8497
}
8598

8699
get notificationsVolume(): number {
87100
if (!this.authenticatedUser) {
88-
throw new Error("Game client must have an authenticated user");
101+
throw new Error("Authenticated user required");
89102
}
90103

91104
return this.authenticatedUser.settings.notificationsVolume;
92105
}
93106

94107
set notificationsVolume(value: number) {
95108
if (!this.authenticatedUser) {
96-
throw new Error("Game client must have an authenticated user");
109+
throw new Error("Authenticated user required");
97110
}
98111

99112
this.authenticatedUser.settings.notificationsVolume = value;
100-
this.setCurrentMutedState(value);
113+
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();
101114

102115
this.authenticatedUser.syncSettings();
103116
}
104117

105118
get musicVolume(): number {
106119
if (!this.authenticatedUser) {
107-
throw new Error("Game client must have an authenticated user");
120+
throw new Error("Authenticated user required");
108121
}
109122

110123
return this.authenticatedUser.settings.musicVolume;
111124
}
112125

113126
set musicVolume(value: number) {
114127
if (!this.authenticatedUser) {
115-
throw new Error("Game client must have an authenticated user");
128+
throw new Error("Authenticated user required");
116129
}
117130

118131
this.authenticatedUser.settings.musicVolume = value;
119-
this.setCurrentMutedState(value);
132+
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();
120133

121134
this.authenticatedUser.syncSettings();
122135
}
123136

124137
get sfxVolume(): number {
125138
if (!this.authenticatedUser) {
126-
throw new Error("Game client must have an authenticated user");
139+
throw new Error("Authenticated user required");
127140
}
128141

129142
return this.authenticatedUser.settings.sfxVolume;
130143
}
131144

132145
set sfxVolume(value: number) {
133146
if (!this.authenticatedUser) {
134-
throw new Error("Game client must have an authenticated user");
147+
throw new Error("Authenticated user required");
135148
}
136149

137150
this.authenticatedUser.settings.sfxVolume = value;
138-
this.setCurrentMutedState(value);
151+
this.setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage();
139152

140153
this.authenticatedUser.syncSettings();
141154
}
142155

143156
get authenticatedPlayer(): Player | null {
144157
if (!this.authenticatedUser) {
145-
throw new Error("Game client must have an authenticated user");
158+
throw new Error("Authenticated user required");
146159
}
147160

148161
if (!this.entireGame || !(this.entireGame.childGameState instanceof IngameGameState)) {
@@ -156,16 +169,18 @@ export default class GameClient {
156169
}
157170
}
158171

159-
private setCurrentMutedState(value: number): void {
172+
private setCurrentMutedStateAndSaveVolumeSettingsToLocalStorage(): void {
160173
if (!this.authenticatedUser) {
161174
return;
162175
}
163176

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) {
167178
this.authenticatedUser.settings.muted = true;
179+
} else {
180+
this.authenticatedUser.settings.muted = false;
168181
}
182+
183+
localStorage.setItem('volumeSettings', JSON.stringify(this.currentVolumeSettings));
169184
}
170185

171186
constructor(authData: AuthData) {
@@ -307,6 +322,7 @@ export default class GameClient {
307322

308323
this.connectionState = ConnectionState.SYNCED;
309324
this.isReconnecting = false;
325+
this.loadVolumeSettingsFromLocalStorage();
310326
} else if (message.type == "new-private-chat-room") {
311327
if (this.entireGame == null) {
312328
return;
@@ -377,4 +393,15 @@ export default class GameClient {
377393
this.authenticatedUser = null;
378394
this.isReconnecting = false;
379395
}
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+
}
380407
}

agot-bg-game-server/src/client/chat-client/ChatComponent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default class ChatComponent extends Component<ChatComponentProps> {
174174
theme={Theme.DARK}
175175
autoFocusSearch={!isMobile}
176176
emojiStyle={isMobile ? EmojiStyle.NATIVE : EmojiStyle.APPLE}
177-
suggestedEmojisMode={SuggestionMode.FREQUENT}
177+
suggestedEmojisMode={SuggestionMode.RECENT}
178178
lazyLoadEmojis={true}
179179
onEmojiClick={(emoji) => {
180180
const input = document.getElementById(`chat-client-input-${this.channel.id}`) as HTMLInputElement;

0 commit comments

Comments
 (0)