Skip to content

Commit

Permalink
don't rely on CALL_ORDER sort to be neatly partitioned #11
Browse files Browse the repository at this point in the history
also filter specifically on completed sets (state === 3) since there are actually many different states other than 1, 2, 3

also pipe updatedSets all the way down to account for sets that were previously "empty"
  • Loading branch information
jmlee337 committed Mar 4, 2024
1 parent 8e0fc83 commit 3fcdd21
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
8 changes: 6 additions & 2 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ export default function setupIPCs(mainWindow: BrowserWindow): void {
ipcMain.removeHandler('getPhaseGroup');
ipcMain.handle(
'getPhaseGroup',
async (event: IpcMainInvokeEvent, id: number) => {
async (
event: IpcMainInvokeEvent,
id: number,
updatedSets?: Map<number, Set>,
) => {
if (!sggApiKey) {
throw new Error('Please set start.gg API key');
}

return getPhaseGroup(sggApiKey, id);
return getPhaseGroup(sggApiKey, id, updatedSets);
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const electronHandler = {
ipcRenderer.invoke('getEvent', id),
getPhase: (id: number): Promise<PhaseGroup[]> =>
ipcRenderer.invoke('getPhase', id),
getPhaseGroup: (id: number): Promise<Sets> =>
ipcRenderer.invoke('getPhaseGroup', id),
getPhaseGroup: (id: number, updatedSets?: Map<number, Set>): Promise<Sets> =>
ipcRenderer.invoke('getPhaseGroup', id, updatedSets),
startSet: (id: number): Promise<Set> => ipcRenderer.invoke('startSet', id),
reportSet: (set: StartggSet): Promise<Set[]> =>
ipcRenderer.invoke('reportSet', set),
Expand Down
38 changes: 24 additions & 14 deletions src/main/startgg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function apiSetToSet(set: any): Set {
const PHASE_GROUP_QUERY = `
query PhaseGroupQuery($id: ID!, $page: Int) {
phaseGroup(id: $id) {
sets(page: $page, perPage: 52, sortType: CALL_ORDER, filters: {hideEmpty: true}) {
sets(page: $page, perPage: 52, sortType: CALL_ORDER) {
pageInfo {
totalPages
}
Expand Down Expand Up @@ -156,31 +156,41 @@ const PHASE_GROUP_QUERY = `
// 2139098 the-off-season-2-2 1-000-melee-doubles
// state {1: not started, 2: started, 3: completed}
// sort: completed reverse chronological, then call order
export async function getPhaseGroup(key: string, id: number): Promise<Sets> {
export async function getPhaseGroup(
key: string,
id: number,
updatedSets: Map<number, Set> = new Map(),
): Promise<Sets> {
let page = 1;
let nextData;
const sets: Set[] = [];
do {
// eslint-disable-next-line no-await-in-loop
nextData = await fetchGql(key, PHASE_GROUP_QUERY, { id, page });
const newSets: Set[] = nextData.phaseGroup.sets.nodes
.filter((set: any) => set.slots[0].entrant && set.slots[1].entrant)
.map(apiSetToSet);
.filter(
(set: any) =>
(set.slots[0].entrant && set.slots[1].entrant) ||
updatedSets.has(set.id),
)
.map((set: any) =>
updatedSets.has(set.id) ? updatedSets.get(set.id) : apiSetToSet(set),
);
sets.push(...newSets);

page += 1;
} while (page <= nextData.phaseGroup.sets.pageInfo.totalPages);

const partIndex = sets.findIndex(
(set: any) => set.state === 1 || set.state === 2,
);
if (partIndex === -1) {
return { pendingSets: [], completedSets: sets };
}
return {
pendingSets: sets.slice(partIndex),
completedSets: sets.slice(0, partIndex),
};
const pendingSets: Set[] = [];
const completedSets: Set[] = [];
sets.forEach((set) => {
if (set.state === 3) {
completedSets.push(set);
} else {
pendingSets.push(set);
}
});
return { pendingSets, completedSets };
}

const MARK_SET_IN_PROGRESS_MUTATION = `
Expand Down
16 changes: 1 addition & 15 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function Hello() {
) => {
let sets;
try {
sets = await window.electron.getPhaseGroup(id);
sets = await window.electron.getPhaseGroup(id, updatedSets);
} catch (e: any) {
showErrorDialog(e.toString());
return;
Expand All @@ -263,20 +263,6 @@ function Hello() {
(phaseGroup) => phaseGroup.id === id,
);
if (editPhaseGroup) {
if (updatedSets) {
for (let i = 0; i < sets.completedSets.length; i += 1) {
const updatedSet = updatedSets.get(sets.completedSets[i].id);
if (updatedSet) {
sets.completedSets[i] = updatedSet;
}
}
for (let i = 0; i < sets.pendingSets.length; i += 1) {
const updatedSet = updatedSets.get(sets.pendingSets[i].id);
if (updatedSet) {
sets.pendingSets[i] = updatedSet;
}
}
}
editPhaseGroup.sets = sets;
setTournament({ ...tournament });
}
Expand Down

0 comments on commit 3fcdd21

Please sign in to comment.