Skip to content

Commit

Permalink
fix(client-electron): prefetch audio files
Browse files Browse the repository at this point in the history
fixes #190
  • Loading branch information
marcincichocki authored Sep 5, 2021
1 parent 53782b6 commit c25d23b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/electron/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { Accelerator, ipcRenderer as ipc, IpcRendererEvent } from 'electron';
import { ScreenshotDisplayOutput } from 'screenshot-desktop';
import { v4 as uuidv4 } from 'uuid';
import { SoundPlayerConfig } from '../worker/sound-player';

export enum WorkerStatus {
Disconnected,
Expand Down Expand Up @@ -45,13 +46,11 @@ export interface HistoryEntry {

export interface AppSettings
extends RobotSettings,
Required<SharpImageContainerConfig> {
Required<SharpImageContainerConfig>,
SoundPlayerConfig {
keyBind: Accelerator;
historySize: number;
preserveSourceOnSuccess: boolean;
soundEnabled: boolean;
errorSoundPath: string;
startSoundPath: string;
checkForUpdates: boolean;
autoUpdate: boolean;
minimizeToTray: boolean;
Expand Down
23 changes: 6 additions & 17 deletions src/electron/worker/autosolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { remove } from 'fs-extra';
import sharp from 'sharp';
import { v4 as uuidv4 } from 'uuid';
import { BreachProtocolSoundPlayer } from './sound-player';

export class BreachProtocolAutosolver {
private readonly uuid = uuidv4();
Expand All @@ -41,11 +42,12 @@ export class BreachProtocolAutosolver {

constructor(
private readonly settings: AppSettings,
private readonly robot: BreachProtocolRobot
private readonly robot: BreachProtocolRobot,
private readonly player: BreachProtocolSoundPlayer
) {}

async solve() {
this.notifyUser('start');
await this.player.play('start');

this.fileName = await this.robot.captureScreen();
this.recognitionResult = await this.recognize();
Expand Down Expand Up @@ -129,8 +131,8 @@ export class BreachProtocolAutosolver {
};
}

private reject() {
this.notifyUser('error');
private async reject() {
await this.player.play('error');

return this.finishWithStatus(BreachProtocolStatus.Rejected);
}
Expand All @@ -150,19 +152,6 @@ export class BreachProtocolAutosolver {
return this.toJSON();
}

private notifyUser(type: 'start' | 'error') {
if (this.settings.soundEnabled) {
const source =
type === 'start'
? this.settings.startSoundPath
: this.settings.errorSoundPath;

if (source) {
new Audio(source).play();
}
}
}

private removeSourceImage() {
remove(this.fileName);
this.fileName = null;
Expand Down
50 changes: 50 additions & 0 deletions src/electron/worker/sound-player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export interface SoundPlayerConfig {
soundEnabled: boolean;
errorSoundPath: string;
startSoundPath: string;
}

interface SoundPlayer {
play(id: string): Promise<void>;
}

type TrackId = 'start' | 'error';

export class BreachProtocolSoundPlayer implements SoundPlayer {
private readonly library = new Map<TrackId, HTMLAudioElement>([
['start', this.initAudio(this.config.startSoundPath)],
['error', this.initAudio(this.config.errorSoundPath)],
]);

constructor(private config: SoundPlayerConfig) {}

update({ startSoundPath, errorSoundPath, soundEnabled }: SoundPlayerConfig) {
if (this.config.startSoundPath !== startSoundPath) {
this.library.set('start', this.initAudio(startSoundPath));
this.config.startSoundPath = startSoundPath;
}

if (this.config.errorSoundPath !== errorSoundPath) {
this.library.set('error', this.initAudio(errorSoundPath));
this.config.errorSoundPath = errorSoundPath;
}

if (this.config.soundEnabled !== soundEnabled) {
this.config.soundEnabled = soundEnabled;
}
}

play(id: TrackId) {
if (!this.config.soundEnabled) return;

const audio = this.library.get(id);

if (!audio) return;

return audio.play();
}

private initAudio(path: string) {
return path ? new Audio(path) : null;
}
}
7 changes: 6 additions & 1 deletion src/electron/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { listDisplays, ScreenshotDisplayOutput } from 'screenshot-desktop';
import sharp from 'sharp';
import { NativeDialog } from '../common';
import { BreachProtocolAutosolver } from './autosolver';
import { BreachProtocolSoundPlayer } from './sound-player';

export class BreachProtocolWorker {
private disposeAsyncRequestListener: () => void = null;
Expand All @@ -46,6 +47,8 @@ export class BreachProtocolWorker {

private settings: AppSettings = ipc.sendSync('get-state').settings;

private readonly player = new BreachProtocolSoundPlayer(this.settings);

private status: WorkerStatus = WorkerStatus.Bootstrap;

private async loadAndSetActiveDisplay() {
Expand Down Expand Up @@ -149,7 +152,7 @@ export class BreachProtocolWorker {
this.updateStatus(WorkerStatus.Working);

const robot = this.getRobot();
const bpa = new BreachProtocolAutosolver(this.settings, robot);
const bpa = new BreachProtocolAutosolver(this.settings, robot, this.player);
const entry = await bpa.solve();

this.dispatch(new AddHistoryEntryAction(entry));
Expand All @@ -162,6 +165,8 @@ export class BreachProtocolWorker {
) {
if (type === ActionTypes.UPDATE_SETTINGS) {
this.settings = payload.settings;

this.player.update(this.settings);
}

if (type === ActionTypes.SET_STATUS) {
Expand Down

0 comments on commit c25d23b

Please sign in to comment.