From 91acaf37eada2719503efa4662ddc91cdf129218 Mon Sep 17 00:00:00 2001 From: jmlee337 Date: Sun, 3 Mar 2024 23:03:05 +0900 Subject: [PATCH] use the return value of set mutation methods it seems to be consistent (unlike immediately fetching phaseGroup sets) --- src/main/ipc.ts | 8 +-- src/main/preload.ts | 7 +-- src/main/startgg.ts | 126 ++++++++++++++++++++++++++++++++----------- src/renderer/App.tsx | 45 +++++++++------- 4 files changed, 129 insertions(+), 57 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index f6fdb0d..aacc459 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -9,7 +9,7 @@ import { import Store from 'electron-store'; import { rm } from 'fs/promises'; import detectUsb from 'detect-usb'; -import { Output, Replay, StartggSet } from '../common/types'; +import { Output, Replay, Set, StartggSet } from '../common/types'; import { getEvent, getPhase, @@ -116,7 +116,7 @@ export default function setupIPCs(mainWindow: BrowserWindow): void { ipcMain.removeHandler('startSet'); ipcMain.handle( 'startSet', - async (event: IpcMainInvokeEvent, setId: number) => { + async (event: IpcMainInvokeEvent, setId: number): Promise => { if (!sggApiKey) { throw new Error('Please set start.gg API key'); } @@ -128,7 +128,7 @@ export default function setupIPCs(mainWindow: BrowserWindow): void { ipcMain.removeHandler('reportSet'); ipcMain.handle( 'reportSet', - async (event: IpcMainInvokeEvent, set: StartggSet) => { + async (event: IpcMainInvokeEvent, set: StartggSet): Promise => { if (!sggApiKey) { throw new Error('Please set start.gg API key'); } @@ -140,7 +140,7 @@ export default function setupIPCs(mainWindow: BrowserWindow): void { ipcMain.removeHandler('updateSet'); ipcMain.handle( 'updateSet', - async (event: IpcMainInvokeEvent, set: StartggSet) => { + async (event: IpcMainInvokeEvent, set: StartggSet): Promise => { if (!sggApiKey) { throw new Error('Please set start.gg API key'); } diff --git a/src/main/preload.ts b/src/main/preload.ts index e3ac04c..08a4b54 100644 --- a/src/main/preload.ts +++ b/src/main/preload.ts @@ -5,6 +5,7 @@ import { Phase, PhaseGroup, Replay, + Set, Sets, StartggSet, } from '../common/types'; @@ -43,10 +44,10 @@ const electronHandler = { ipcRenderer.invoke('getPhase', id), getPhaseGroup: (id: number): Promise => ipcRenderer.invoke('getPhaseGroup', id), - startSet: (id: number): Promise => ipcRenderer.invoke('startSet', id), - reportSet: (set: StartggSet): Promise => + startSet: (id: number): Promise => ipcRenderer.invoke('startSet', id), + reportSet: (set: StartggSet): Promise => ipcRenderer.invoke('reportSet', set), - updateSet: (set: StartggSet): Promise => + updateSet: (set: StartggSet): Promise => ipcRenderer.invoke('updateSet', set), getStartggKey: (): Promise => ipcRenderer.invoke('getStartggKey'), setStartggKey: (startggKey: string): Promise => diff --git a/src/main/startgg.ts b/src/main/startgg.ts index 02bcff9..71341bb 100644 --- a/src/main/startgg.ts +++ b/src/main/startgg.ts @@ -90,6 +90,33 @@ async function fetchGql(key: string, query: string, variables: any) { return json.data; } +function apiSetToSet(set: any): Set { + const slot1 = set.slots[0]; + const slot2 = set.slots[1]; + const entrant1Names = slot1.entrant.participants.map( + (participant: { gamerTag: string }) => participant.gamerTag, + ); + const entrant2Names = slot2.entrant.participants.map( + (participant: { gamerTag: string }) => participant.gamerTag, + ); + return { + id: set.id, + state: set.state, + fullRoundText: set.fullRoundText, + winnerId: set.winnerId, + entrant1Id: slot1.entrant.id, + entrant1Names, + entrant1Score: slot1.standing + ? slot1.standing.stats.score.displayValue + : null, + entrant2Id: slot2.entrant.id, + entrant2Names, + entrant2Score: slot2.standing + ? slot2.standing.stats.score.displayValue + : null, + }; +} + const PHASE_GROUP_QUERY = ` query PhaseGroupQuery($id: ID!, $page: Int) { phaseGroup(id: $id) { @@ -138,32 +165,7 @@ export async function getPhaseGroup(key: string, id: number): Promise { 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((set: any): Set => { - const slot1 = set.slots[0]; - const slot2 = set.slots[1]; - const entrant1Names = slot1.entrant.participants.map( - (participant: { gamerTag: string }) => participant.gamerTag, - ); - const entrant2Names = slot2.entrant.participants.map( - (participant: { gamerTag: string }) => participant.gamerTag, - ); - return { - id: set.id, - state: set.state, - fullRoundText: set.fullRoundText, - winnerId: set.winnerId, - entrant1Id: slot1.entrant.id, - entrant1Names, - entrant1Score: slot1.standing - ? slot1.standing.stats.score.displayValue - : null, - entrant2Id: slot2.entrant.id, - entrant2Names, - entrant2Score: slot2.standing - ? slot2.standing.stats.score.displayValue - : null, - }; - }); + .map(apiSetToSet); sets.push(...newSets); page += 1; @@ -185,31 +187,93 @@ const MARK_SET_IN_PROGRESS_MUTATION = ` mutation MarkSetInProgress($setId: ID!) { markSetInProgress(setId: $setId) { id + slots { + entrant { + id + participants { + gamerTag + } + } + standing { + stats { + score { + displayValue + } + } + } + } + state + fullRoundText + winnerId } } `; export async function startSet(key: string, setId: number) { - await fetchGql(key, MARK_SET_IN_PROGRESS_MUTATION, { setId }); + const data = await fetchGql(key, MARK_SET_IN_PROGRESS_MUTATION, { setId }); + return apiSetToSet(data.markSetInProgress); } const REPORT_BRACKET_SET_MUTATION = ` mutation ReportBracketSet($setId: ID!, $winnerId: ID, $isDQ: Boolean, $gameData: [BracketSetGameDataInput]) { reportBracketSet(setId: $setId, isDQ: $isDQ, winnerId: $winnerId, gameData: $gameData) { id + slots { + entrant { + id + participants { + gamerTag + } + } + standing { + stats { + score { + displayValue + } + } + } + } + state + fullRoundText + winnerId } } `; -export async function reportSet(key: string, set: StartggSet) { - await fetchGql(key, REPORT_BRACKET_SET_MUTATION, set); +export async function reportSet(key: string, set: StartggSet): Promise { + const data = await fetchGql(key, REPORT_BRACKET_SET_MUTATION, set); + return data.reportBracketSet + .filter( + (bracketSet: any) => + bracketSet.slots[0].entrant && bracketSet.slots[1].entrant, + ) + .map(apiSetToSet); } const UPDATE_BRACKET_SET_MUTATION = ` mutation UpdateBracketSet($setId: ID!, $winnerId: ID, $isDQ: Boolean, $gameData: [BracketSetGameDataInput]) { updateBracketSet(setId: $setId, isDQ: $isDQ, winnerId: $winnerId, gameData: $gameData) { id + slots { + entrant { + id + participants { + gamerTag + } + } + standing { + stats { + score { + displayValue + } + } + } + } + state + fullRoundText + winnerId } } `; -export async function updateSet(key: string, set: StartggSet) { - await fetchGql(key, UPDATE_BRACKET_SET_MUTATION, set); +export async function updateSet(key: string, set: StartggSet): Promise { + const data = await fetchGql(key, UPDATE_BRACKET_SET_MUTATION, set); + return apiSetToSet(data.updateBracketSet); } diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index bd3292e..4b397ab 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -239,6 +239,7 @@ function Hello() { id: number, phaseId: number, eventId: number, + updatedSets?: Map, ) => { let sets; try { @@ -262,6 +263,20 @@ 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 }); } @@ -486,20 +501,14 @@ function Hello() { const startSet = async (setId: number) => { try { - await window.electron.startSet(setId); - await new Promise((resolve) => { - // it seems that start.gg needs a moment to settle - // before the set will be reflected as completed. - setTimeout(resolve, 1000); - }); + const updatedSet = await window.electron.startSet(setId); await getPhaseGroup( selectedSetChain.phaseGroupId, selectedSetChain.phaseId, selectedSetChain.eventId, + new Map([[updatedSet.id, updatedSet]]), ); - const updatedSelectedSet = { ...selectedSet }; - updatedSelectedSet.state = 2; - setSelectedSet(updatedSelectedSet); + setSelectedSet(updatedSet); } catch (e: any) { showErrorDialog(e.toString()); } @@ -507,24 +516,22 @@ function Hello() { const reportSet = async (set: StartggSet, update: boolean) => { try { + const updatedSets = new Map(); if (update) { - await window.electron.updateSet(set); + const updatedSet = await window.electron.updateSet(set); + updatedSets.set(updatedSet.id, updatedSet); } else { - await window.electron.reportSet(set); + (await window.electron.reportSet(set)).forEach((updatedSet) => { + updatedSets.set(updatedSet.id, updatedSet); + }); } - await new Promise((resolve) => { - // it seems that start.gg needs a moment to settle - // before the set will be reflected as completed. - setTimeout(resolve, 1000); - }); await getPhaseGroup( selectedSetChain.phaseGroupId, selectedSetChain.phaseId, selectedSetChain.eventId, + updatedSets, ); - const updatedSelectedSet = { ...selectedSet }; - updatedSelectedSet.state = 3; - setSelectedSet(updatedSelectedSet); + setSelectedSet(updatedSets.get(set.setId)!); } catch (e: any) { showErrorDialog(e.toString()); }