Skip to content

Commit

Permalink
transition start.gg set mutate to event
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Aug 14, 2024
1 parent 0d062b8 commit 69ea9b5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
42 changes: 37 additions & 5 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,24 +332,45 @@ export default function setupIPCs(mainWindow: BrowserWindow): void {
ipcMain.removeHandler('startSet');
ipcMain.handle(
'startSet',
async (event: IpcMainInvokeEvent, setId: number): Promise<Set> => {
async (event: IpcMainInvokeEvent, setId: number) => {
if (!sggApiKey) {
throw new Error('Please set start.gg API key');
}

return startSet(sggApiKey, setId);
const updatedSet = await startSet(sggApiKey, setId);
await getPhaseGroup(
sggApiKey,
getSelectedSetChain().phaseGroup!.id,
new Map([[updatedSet.id, updatedSet]]),
);
mainWindow.webContents.send(
'startggTournament',
getSelectedSet(),
getCurrentTournament(),
);
},
);

ipcMain.removeHandler('reportSet');
ipcMain.handle(
'reportSet',
async (event: IpcMainInvokeEvent, set: StartggSet): Promise<Set[]> => {
async (event: IpcMainInvokeEvent, set: StartggSet): Promise<Set> => {
if (!sggApiKey) {
throw new Error('Please set start.gg API key');
}

return reportSet(sggApiKey, set);
const updatedSets = await reportSet(sggApiKey, set);
await getPhaseGroup(
sggApiKey,
getSelectedSetChain().phaseGroup!.id,
updatedSets,
);
mainWindow.webContents.send(
'startggTournament',
getSelectedSet(),
getCurrentTournament(),
);
return updatedSets.get(set.setId)!;
},
);

Expand All @@ -361,7 +382,18 @@ export default function setupIPCs(mainWindow: BrowserWindow): void {
throw new Error('Please set start.gg API key');
}

return updateSet(sggApiKey, set);
const updatedSet = await updateSet(sggApiKey, set);
await getPhaseGroup(
sggApiKey,
getSelectedSetChain().phaseGroup!.id,
new Map([[updatedSet.id, updatedSet]]),
);
mainWindow.webContents.send(
'startggTournament',
getSelectedSet(),
getCurrentTournament(),
);
return updatedSet;
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ const electronHandler = {
ipcRenderer.invoke('getPhase', id, recursive),
getPhaseGroup: (id: number, updatedSets?: Map<number, Set>): Promise<void> =>
ipcRenderer.invoke('getPhaseGroup', id, updatedSets),
startSet: (id: number): Promise<Set> => ipcRenderer.invoke('startSet', id),
reportSet: (set: StartggSet): Promise<Set[]> =>
startSet: (id: number): Promise<void> => ipcRenderer.invoke('startSet', id),
reportSet: (set: StartggSet): Promise<Set> =>
ipcRenderer.invoke('reportSet', set),
updateSet: (set: StartggSet): Promise<Set> =>
ipcRenderer.invoke('updateSet', set),
Expand Down
5 changes: 3 additions & 2 deletions src/main/startgg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,15 +714,16 @@ const REPORT_BRACKET_SET_MUTATION = `
reportBracketSet(setId: $setId, isDQ: $isDQ, winnerId: $winnerId, gameData: $gameData) {${GQL_SET_INNER}}
}
`;
export async function reportSet(key: string, set: StartggSet): Promise<Set[]> {
export async function reportSet(key: string, set: StartggSet) {
const data = await fetchGql(key, REPORT_BRACKET_SET_MUTATION, set);
reportedSetIds.set(set.setId, true);
return data.reportBracketSet
const updatedSets = (data.reportBracketSet as any[])
.filter(
(bracketSet: any) =>
bracketSet.slots[0].entrant && bracketSet.slots[1].entrant,
)
.map(gqlSetToSet);
return new Map(updatedSets.map((updatedSet) => [updatedSet.id, updatedSet]));
}

const UPDATE_BRACKET_SET_MUTATION = `
Expand Down
21 changes: 5 additions & 16 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,7 @@ function Hello() {
setStartingSet(true);
try {
if (mode === Mode.STARTGG) {
const updatedSet = await window.electron.startSet(setId);
await getPhaseGroup(
selectedSetChain.phaseGroupId,
new Map([[updatedSet.id, updatedSet]]),
);
await window.electron.startSet(setId);
} else if (mode === Mode.CHALLONGE) {
const updatedSet = await window.electron.startChallongeSet(
selectedChallongeTournament.slug,
Expand All @@ -968,18 +964,11 @@ function Hello() {
};

const reportStartggSet = async (set: StartggSet, update: boolean) => {
const updatedSets = new Map<number, Set>();
if (update) {
const updatedSet = await window.electron.updateSet(set);
updatedSets.set(updatedSet.id, updatedSet);
} else {
(await window.electron.reportSet(set)).forEach((updatedSet) => {
updatedSets.set(updatedSet.id, updatedSet);
});
}
await getPhaseGroup(selectedSetChain.phaseGroupId, updatedSets);
const updatedSet = update
? await window.electron.updateSet(set)
: await window.electron.reportSet(set);
resetDq();
return updatedSets.get(set.setId);
return updatedSet;
};
const reportChallongeSet = async (
matchId: number,
Expand Down

0 comments on commit 69ea9b5

Please sign in to comment.