Skip to content

Commit

Permalink
feat: pass consoleNick or broadcaster connect code to comm file when …
Browse files Browse the repository at this point in the history
…spectating (#320)

* feat: pass consoleNick to comm file when mirroring

* feat: pass name to comm file when spectating

* address comments
  • Loading branch information
NikhilNarayana authored Jun 7, 2022
1 parent c1c4694 commit 12832d5
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/broadcast/spectate.worker.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ export async function createSpectateWorker(dolphinManager: DolphinManager): Prom
const errorMessage = err instanceof Error ? err.message : err;
void ipc_spectateErrorOccurredEvent.main!.trigger({ errorMessage });
});
worker.getSpectateDetailsObservable().subscribe(({ playbackId, filePath }) => {
worker.getSpectateDetailsObservable().subscribe(({ playbackId, filePath, broadcasterName }) => {
const replayComm: ReplayCommunication = {
mode: "mirror",
replay: filePath,
gameStation: broadcasterName,
};
dolphinManager.launchPlaybackDolphin(playbackId, replayComm).catch(log.error);
});
Expand Down
10 changes: 5 additions & 5 deletions src/broadcast/spectate.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Methods {
getLogObservable(): Observable<string>;
getErrorObservable(): Observable<Error | string>;
getBroadcastListObservable(): Observable<BroadcasterItem[]>;
getSpectateDetailsObservable(): Observable<{ playbackId: string; filePath: string }>;
getSpectateDetailsObservable(): Observable<{ playbackId: string; filePath: string; broadcasterName: string }>;
getReconnectObservable(): Observable<Record<never, never>>;
}

Expand All @@ -30,7 +30,7 @@ const spectateManager = new SpectateManager();
const logSubject = new Subject<string>();
const errorSubject = new Subject<Error | string>();
const broadcastListSubject = new Subject<BroadcasterItem[]>();
const spectateDetailsSubject = new Subject<{ playbackId: string; filePath: string }>();
const spectateDetailsSubject = new Subject<{ playbackId: string; filePath: string; broadcasterName: string }>();
const reconnectSubject = new Subject<Record<never, never>>();

// Forward the events to the renderer
Expand All @@ -46,8 +46,8 @@ spectateManager.on(SpectateEvent.ERROR, async (err: Error | string) => {
errorSubject.next(err);
});

spectateManager.on(SpectateEvent.NEW_FILE, async (playbackId: string, filePath: string) => {
spectateDetailsSubject.next({ playbackId, filePath });
spectateManager.on(SpectateEvent.NEW_FILE, async (playbackId: string, filePath: string, broadcasterName: string) => {
spectateDetailsSubject.next({ playbackId, filePath, broadcasterName });
});

spectateManager.on(SpectateEvent.RECONNECT, async () => {
Expand Down Expand Up @@ -87,7 +87,7 @@ const methods: WorkerSpec = {
getBroadcastListObservable(): Observable<BroadcasterItem[]> {
return Observable.from(broadcastListSubject);
},
getSpectateDetailsObservable(): Observable<{ playbackId: string; filePath: string }> {
getSpectateDetailsObservable(): Observable<{ playbackId: string; filePath: string; broadcasterName: string }> {
return Observable.from(spectateDetailsSubject);
},
getReconnectObservable(): Observable<Record<never, never>> {
Expand Down
30 changes: 18 additions & 12 deletions src/broadcast/spectateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ const generatePlaybackId = (broadcastId: string) => `spectate-${broadcastId}`;
* Dealing with dolphin related details should be handled elsewhere.
*/
export class SpectateManager extends EventEmitter {
private broadcastInfo: Record<string, BroadcastInfo> = {};
private openBroadcasts: Record<string, BroadcastInfo> = {};
private availableBroadcasts: Record<string, BroadcasterItem> = {};
private wsConnection: connection | null = null;

constructor() {
super();
}

private async _playFile(filePath: string, playbackId: string) {
this.emit(SpectateEvent.NEW_FILE, playbackId, filePath);
private async _playFile(filePath: string, playbackId: string, broadcasterName: string) {
this.emit(SpectateEvent.NEW_FILE, playbackId, filePath, broadcasterName);
}

private _handleEvents(obj: { type: string; broadcastId: string; cursor: string; events: any[] }) {
const events = obj.events ?? [];
const broadcastId: string = obj.broadcastId;

const broadcastInfo = this.broadcastInfo[broadcastId];
const broadcastInfo = this.openBroadcasts[broadcastId];
if (!broadcastInfo) {
// We've stopped watching this broadcast already
return;
Expand Down Expand Up @@ -110,7 +111,7 @@ export class SpectateManager extends EventEmitter {
this.wsConnection = connection;

// Reconnect to all the broadcasts that we were already watching
Object.entries(this.broadcastInfo).forEach(([broadcastId, info]) => {
Object.entries(this.openBroadcasts).forEach(([broadcastId, info]) => {
const watchMsg: { type: string; broadcastId: string; startCursor?: string } = {
type: "watch-broadcast",
broadcastId,
Expand Down Expand Up @@ -158,6 +159,10 @@ export class SpectateManager extends EventEmitter {
case "list-broadcasts-resp": {
const broadcasts: BroadcasterItem[] = obj.broadcasts ?? [];
this.emit(SpectateEvent.BROADCAST_LIST_UPDATE, broadcasts);
this.availableBroadcasts = {};
broadcasts.forEach((broadcast) => {
this.availableBroadcasts[broadcast.id] = broadcast;
});
break;
}
case "events": {
Expand Down Expand Up @@ -204,10 +209,10 @@ export class SpectateManager extends EventEmitter {
}

// End the file writing and remove from the map
const info = this.broadcastInfo[broadcastId];
const info = this.openBroadcasts[broadcastId];
if (info) {
info.fileWriter.endCurrentFile();
delete this.broadcastInfo[broadcastId];
delete this.openBroadcasts[broadcastId];
}
}

Expand All @@ -224,7 +229,7 @@ export class SpectateManager extends EventEmitter {
throw new Error("No websocket connection");
}

const existingBroadcasts = Object.keys(this.broadcastInfo);
const existingBroadcasts = Object.keys(this.openBroadcasts);
if (existingBroadcasts.includes(broadcastId)) {
// We're already watching this broadcast!
this.emit(SpectateEvent.LOG, `We are already watching the selected broadcast`);
Expand All @@ -248,11 +253,12 @@ export class SpectateManager extends EventEmitter {
folderPath: targetPath,
});

const broadcasterName = this.availableBroadcasts[broadcastId].name;
slpFileWriter.on(SlpFileWriterEvent.NEW_FILE, (currFilePath) => {
this._playFile(currFilePath, dolphinPlaybackId).catch(console.warn);
this._playFile(currFilePath, dolphinPlaybackId, broadcasterName).catch(console.warn);
});

this.broadcastInfo[broadcastId] = {
this.openBroadcasts[broadcastId] = {
broadcastId,
cursor: "",
fileWriter: slpFileWriter,
Expand All @@ -270,11 +276,11 @@ export class SpectateManager extends EventEmitter {
// Play an empty file such that we just launch into the waiting for game screen, this is
// used to clear out any previous file that we were reading for. The file will get updated
// by the fileWriter
this._playFile("", dolphinPlaybackId).catch(console.warn);
this._playFile("", dolphinPlaybackId, broadcasterName).catch(console.warn);
}

public handleClosedDolphin(playbackId: string) {
const broadcastInfo = Object.values(this.broadcastInfo).find((info) => info.dolphinId === playbackId);
const broadcastInfo = Object.values(this.openBroadcasts).find((info) => info.dolphinId === playbackId);
if (!broadcastInfo) {
// This is not one of the spectator dolphin instances
return;
Expand Down
3 changes: 2 additions & 1 deletion src/console/mirror.worker.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ export async function createMirrorWorker(dolphinManager: DolphinManager): Promis
ipc_consoleMirrorErrorMessageEvent.main!.trigger({ message }).catch(log.error);
});

worker.getMirrorDetailsObservable().subscribe(({ playbackId, filePath, isRealtime }) => {
worker.getMirrorDetailsObservable().subscribe(({ playbackId, filePath, isRealtime, nickname }) => {
const replayComm: ReplayCommunication = {
mode: "mirror",
isRealTimeMode: isRealtime,
replay: filePath,
gameStation: nickname,
};
dolphinManager.launchPlaybackDolphin(playbackId, replayComm).catch(log.error);
});
Expand Down
30 changes: 24 additions & 6 deletions src/console/mirror.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ interface Methods {
dolphinClosed(playbackId: string): Promise<void>;
getLogObservable(): Observable<string>;
getErrorObservable(): Observable<Error | string>;
getMirrorDetailsObservable(): Observable<{ playbackId: string; filePath: string; isRealtime: boolean }>;
getMirrorDetailsObservable(): Observable<{
playbackId: string;
filePath: string;
isRealtime: boolean;
nickname?: string;
}>;
getMirrorStatusObservable(): Observable<{ ip: string; info: Partial<ConsoleMirrorStatusUpdate> }>;
}

Expand All @@ -28,7 +33,12 @@ const mirrorManager = new MirrorManager();

const logSubject = new Subject<string>();
const errorSubject = new Subject<Error | string>();
const mirrorDetailsSubject = new Subject<{ playbackId: string; filePath: string; isRealtime: boolean }>();
const mirrorDetailsSubject = new Subject<{
playbackId: string;
filePath: string;
isRealtime: boolean;
nickname?: string;
}>();
const mirrorStatusSubject = new Subject<{ ip: string; info: Partial<ConsoleMirrorStatusUpdate> }>();

// Forward the events to the renderer
Expand All @@ -40,9 +50,12 @@ mirrorManager.on(MirrorEvent.ERROR, async (error: Error | string) => {
errorSubject.next(error);
});

mirrorManager.on(MirrorEvent.NEW_FILE, async (playbackId: string, filePath: string, isRealtime: boolean) => {
mirrorDetailsSubject.next({ playbackId, filePath, isRealtime });
});
mirrorManager.on(
MirrorEvent.NEW_FILE,
async (playbackId: string, filePath: string, isRealtime: boolean, nickname?: string) => {
mirrorDetailsSubject.next({ playbackId, filePath, isRealtime, nickname: nickname });
},
);

mirrorManager.on(
MirrorEvent.MIRROR_STATUS_CHANGE,
Expand Down Expand Up @@ -79,7 +92,12 @@ const methods: WorkerSpec = {
getErrorObservable(): Observable<Error | string> {
return Observable.from(errorSubject);
},
getMirrorDetailsObservable(): Observable<{ playbackId: string; filePath: string; isRealtime: boolean }> {
getMirrorDetailsObservable(): Observable<{
playbackId: string;
filePath: string;
isRealtime: boolean;
nickname?: string;
}> {
return Observable.from(mirrorDetailsSubject);
},
getMirrorStatusObservable(): Observable<{ ip: string; info: Partial<ConsoleMirrorStatusUpdate> }> {
Expand Down
8 changes: 7 additions & 1 deletion src/console/mirrorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,13 @@ export class MirrorManager extends EventEmitter {
}

private async _playFile(filePath: string, playbackId: string) {
return this.emit(MirrorEvent.NEW_FILE, playbackId, filePath, this.mirrors[playbackId].isRealtime);
return this.emit(
MirrorEvent.NEW_FILE,
playbackId,
filePath,
this.mirrors[playbackId].isRealtime,
this.mirrors[playbackId].nickname,
);
}

public async handleClosedDolphin(playbackId: string) {
Expand Down
1 change: 1 addition & 0 deletions src/console/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface MirrorConfig {
enableRelay: boolean;
autoSwitcherSettings?: AutoSwitcherSettings;
useNicknameFolders: boolean;
nickname?: string;
}

export interface MirrorDetails extends MirrorConfig {
Expand Down
1 change: 1 addition & 0 deletions src/dolphin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ReplayCommunication {
isRealTimeMode?: boolean; // default false; keeps dolphin fairly close to real time (about 2-3 frames); only relevant in mirror mode
shouldResync?: boolean; // default true; disables the resync functionality
rollbackDisplayMethod?: "off" | "normal" | "visible"; // default off; normal shows like a player experienced it, visible shows ALL frames (normal and rollback)
gameStation?: string;
queue?: ReplayQueueItem[];
}

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/containers/Console/SavedConnectionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const SavedConnectionItem: React.FC<SavedConnectionItemProps> = ({
isRealtime: conn.isRealtime,
enableRelay: conn.enableRelay,
useNicknameFolders: conn.useNicknameFolders,
nickname,
};

// Add OBS config if necessary
Expand All @@ -71,7 +72,7 @@ export const SavedConnectionItem: React.FC<SavedConnectionItemProps> = ({
}

await consoleService.connectToConsoleMirror(config);
}, [consoleService, connection]);
}, [consoleService, connection, nickname]);
const onMirror = React.useCallback(() => {
consoleService.startMirroring(connection.ipAddress).catch(showError);
}, [consoleService, connection, showError]);
Expand Down

0 comments on commit 12832d5

Please sign in to comment.