Skip to content

Commit

Permalink
use the return value of set mutation methods #9
Browse files Browse the repository at this point in the history
it seems to be consistent (unlike immediately fetching phaseGroup sets)
  • Loading branch information
jmlee337 committed Mar 3, 2024
1 parent 6d13f4b commit 1c183e2
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Set> => {
if (!sggApiKey) {
throw new Error('Please set start.gg API key');
}
Expand All @@ -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<Set[]> => {
if (!sggApiKey) {
throw new Error('Please set start.gg API key');
}
Expand All @@ -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<Set> => {
if (!sggApiKey) {
throw new Error('Please set start.gg API key');
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Phase,
PhaseGroup,
Replay,
Set,
Sets,
StartggSet,
} from '../common/types';
Expand Down Expand Up @@ -43,10 +44,10 @@ const electronHandler = {
ipcRenderer.invoke('getPhase', id),
getPhaseGroup: (id: number): Promise<Sets> =>
ipcRenderer.invoke('getPhaseGroup', id),
startSet: (id: number): Promise<void> => ipcRenderer.invoke('startSet', id),
reportSet: (set: StartggSet): Promise<void> =>
startSet: (id: number): Promise<Set> => ipcRenderer.invoke('startSet', id),
reportSet: (set: StartggSet): Promise<Set[]> =>
ipcRenderer.invoke('reportSet', set),
updateSet: (set: StartggSet): Promise<void> =>
updateSet: (set: StartggSet): Promise<Set> =>
ipcRenderer.invoke('updateSet', set),
getStartggKey: (): Promise<string> => ipcRenderer.invoke('getStartggKey'),
setStartggKey: (startggKey: string): Promise<void> =>
Expand Down
126 changes: 95 additions & 31 deletions src/main/startgg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -138,32 +165,7 @@ export async function getPhaseGroup(key: string, id: number): Promise<Sets> {
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;
Expand All @@ -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<Set[]> {
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<Set> {
const data = await fetchGql(key, UPDATE_BRACKET_SET_MUTATION, set);
return apiSetToSet(data.updateBracketSet);
}
45 changes: 26 additions & 19 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ function Hello() {
id: number,
phaseId: number,
eventId: number,
updatedSets?: Map<number, Set>,
) => {
let sets;
try {
Expand All @@ -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 });
}
Expand Down Expand Up @@ -486,45 +501,37 @@ 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());
}
};

const reportSet = async (set: StartggSet, update: boolean) => {
try {
const updatedSets = new Map<number, Set>();
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());
}
Expand Down

0 comments on commit 1c183e2

Please sign in to comment.