Skip to content

Commit

Permalink
Merge pull request #1822 from gereon77/fix-autoplay
Browse files Browse the repository at this point in the history
Use timers and a fake click to make autoplay work again
  • Loading branch information
gereon77 authored Feb 8, 2024
2 parents 8f716b1 + 370ac07 commit 6b894ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ export default class CombatComponent extends Component<GameStateComponentProps<C
this.props.mapControls.modifyUnitsOnMap.push(this.modifyUnitsOnMapCallback = () => this.modifyUnitsOnMap());
this.props.mapControls.modifyOrdersOnMap.push(this.modifyOrdersOnMapCallback = () => this.modifyOrdersOnMap());

this.props.gameClient.sfxManager.playCombatSound(this.props.gameState.attacker.id);
window.setTimeout(() => {
document.body.click();
window.setTimeout(() => {
this.props.gameClient.sfxManager.playCombatSound(this.props.gameState.attacker.id);
}, 100);
}, 100);
}

componentWillUnmount(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export default class GameEndedComponent extends Component<GameStateComponentProp
}

componentDidMount(): void {
this.props.gameClient.sfxManager.playGotTheme();
window.setTimeout(() => {
document.body.click();
window.setTimeout(() => {
this.props.gameClient.sfxManager.playGotTheme();
}, 100);
}, 100);
}
}
66 changes: 34 additions & 32 deletions agot-bg-game-server/src/client/utils/SfxManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,50 +175,50 @@ class SfxManager {
}, fadeOutInterval);
}

playNotificationSound(): void {
playNotificationSound(): Promise<void> {
if (this.gameClient.muted) {
return;
return Promise.resolve();
}

this.playNotification(notificationSound, this.gameClient.notificationsVolume);
return this.playNotification(notificationSound, this.gameClient.notificationsVolume);
}

playVoteNotificationSound(): void {
playVoteNotificationSound(): Promise<void> {
if (this.gameClient.muted) {
return;
return Promise.resolve();
}

this.playNotification(voteSound, this.gameClient.notificationsVolume);
return this.playNotification(voteSound, this.gameClient.notificationsVolume);
}

playNewMessageReceivedSound(): void {
playNewMessageReceivedSound(): Promise<void> {
if (this.gameClient.muted) {
return;
return Promise.resolve();
}

this.playNotification(ravenCallSound, this.gameClient.notificationsVolume);
return this.playNotification(ravenCallSound, this.gameClient.notificationsVolume);
}

playGotTheme(): void {
playGotTheme(): Promise<void> {
if (this.gameClient.musicMuted) {
return;
return Promise.resolve();
}

this.playMusic(introSound, this.gameClient.musicVolume, false);
return this.playMusic(introSound, this.gameClient.musicVolume, false);
}

playCombatSound(attackerId?: string): void {
playCombatSound(attackerId?: string): Promise<void> {
if (this.gameClient.musicMuted) {
return;
return Promise.resolve();
}

const sound = attackerId ? houseThemes.tryGet(attackerId, combatSound) : combatSound;
this.playMusic(sound, this.gameClient.musicVolume, false);
return this.playMusic(sound, this.gameClient.musicVolume, false);
}

playSoundWhenClickingMarchOrder(region: Region): void {
playSoundWhenClickingMarchOrder(region: Region): Promise<void> {
if (this.gameClient.musicMuted) {
return;
return Promise.resolve();
}

const army = region.units.values;
Expand All @@ -240,12 +240,12 @@ class SfxManager {
? soundsForShips
: soundsForFootmenOnly;

this.playRandomMusic(files, this.gameClient.musicVolume, true);
return this.playRandomMusic(files, this.gameClient.musicVolume, true);
}

playSoundForLogEvent(log: GameLogData): void {
playSoundForLogEvent(log: GameLogData): Promise<void> {
if (this.gameClient.musicMuted) {
return;
return Promise.resolve();
}

switch(log.type) {
Expand All @@ -256,55 +256,57 @@ class SfxManager {
case "killed-after-combat": {
const units = log.killed.map(ut => unitTypes.get(ut));
if (units.includes(dragon)) {
this.playRandomMusic(soundsWhenDragonsAreDestroyed, this.gameClient.musicVolume, false);
return this.playRandomMusic(soundsWhenDragonsAreDestroyed, this.gameClient.musicVolume, false);
} else {
this.playRandomMusic(soundsWhenUnitsAreDestroyedBySwords, this.gameClient.musicVolume, false);
return this.playRandomMusic(soundsWhenUnitsAreDestroyedBySwords, this.gameClient.musicVolume, false);
}
break;
}
case "immediatly-killed-after-combat": {
const killed = _.concat(log.killedBecauseCantRetreat, log.killedBecauseWounded);
const units = killed.map(ut => unitTypes.get(ut));
if (units.includes(dragon)) {
this.playRandomMusic(soundsWhenDragonsAreDestroyed, this.gameClient.musicVolume, false);
return this.playRandomMusic(soundsWhenDragonsAreDestroyed, this.gameClient.musicVolume, false);
}
break;
}
case "retreat-casualties-suffered": {
const units = log.units.map(ut => unitTypes.get(ut));
if (units.includes(dragon)) {
this.playRandomMusic(soundsWhenDragonsAreDestroyed, this.gameClient.musicVolume, false);
return this.playRandomMusic(soundsWhenDragonsAreDestroyed, this.gameClient.musicVolume, false);
}
break;
}
}

return Promise.resolve();
}

private playRandomMusic(files: string[], volume: number, withCurrentPlayingCheck: boolean): void {
private playRandomMusic(files: string[], volume: number, withCurrentPlayingCheck: boolean): Promise<void> {
const randomFile = pickRandom(files);
if (!randomFile) {
return;
return Promise.resolve();
}
this.playMusic(randomFile, volume, withCurrentPlayingCheck);
return this.playMusic(randomFile, volume, withCurrentPlayingCheck);
}

private playMusic(file: string, volume: number, withCurrentPlayingCheck: boolean): void {
private playMusic(file: string, volume: number, withCurrentPlayingCheck: boolean): Promise<void> {
if (withCurrentPlayingCheck && this.currentlyPlayingMusic.length > 0) {
return;
return Promise.resolve();
}
const audio = new Audio(file);
this.currentlyPlayingMusic.push(audio);
audio.onended = () => _.pull(this.currentlyPlayingMusic, audio);
audio.volume = volume;
audio.play();
return audio.play();
}

private playNotification(file: string, volume: number): void {
private playNotification(file: string, volume: number): Promise<void> {
const audio = new Audio(file);
this.currentlyPlayingNotifications.push(audio);
audio.onended = () => _.pull(this.currentlyPlayingNotifications, audio);
audio.volume = volume;
audio.play();
return audio.play();
}
}

Expand Down

0 comments on commit 6b894ae

Please sign in to comment.