Skip to content

Commit

Permalink
persist start.gg selected set chain
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Aug 14, 2024
1 parent 46f18b2 commit 0d062b8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 25 deletions.
20 changes: 19 additions & 1 deletion src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
getCurrentTournament,
getSelectedSet,
setSelectedSetId,
getSelectedSetChain,
setSelectedSetChain,
} from './startgg';
import { enforceReplays, getReplaysInDir, writeReplays } from './replay';
import {
Expand Down Expand Up @@ -234,11 +236,27 @@ export default function setupIPCs(mainWindow: BrowserWindow): void {
ipcMain.removeHandler('setSelectedSetId');
ipcMain.handle(
'setSelectedSetId',
async (event: IpcMainInvokeEvent, selectedSetId: number) => {
(event: IpcMainInvokeEvent, selectedSetId: number) => {
setSelectedSetId(selectedSetId);
},
);

ipcMain.removeHandler('getSelectedSetChain');
ipcMain.handle('getSelectedSetChain', getSelectedSetChain);

ipcMain.removeHandler('setSelectedSetChain');
ipcMain.handle(
'setSelectedSetChain',
(
event: IpcMainInvokeEvent,
eventId: number,
phaseId: number,
phaseGroupId: number,
) => {
setSelectedSetChain(eventId, phaseId, phaseGroupId);
},
);

ipcMain.removeHandler('getTournament');
ipcMain.handle(
'getTournament',
Expand Down
14 changes: 14 additions & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import {
Context,
CopySettings,
EnforceResult,
Event,
InvalidReplay,
Mode,
Output,
Phase,
PhaseGroup,
Replay,
ReportSettings,
Set,
Expand Down Expand Up @@ -54,6 +57,17 @@ const electronHandler = {
ipcRenderer.invoke('getCurrentTournament'),
getSelectedSet: (): Promise<Set | undefined> =>
ipcRenderer.invoke('getSelectedSet'),
getSelectedSetChain: (): Promise<{
event: Event | undefined;
phase: Phase | undefined;
phaseGroup: PhaseGroup | undefined;
}> => ipcRenderer.invoke('getSelectedSetChain'),
setSelectedSetChain: (
eventId: number,
phaseId: number,
phaseGroupId: number,
): Promise<void> =>
ipcRenderer.invoke('setSelectedSetChain', eventId, phaseId, phaseGroupId),
getTournament: (slug: string, recursive: boolean): Promise<void> =>
ipcRenderer.invoke('getTournament', slug, recursive),
getEvent: (id: number, recursive: boolean): Promise<void> =>
Expand Down
25 changes: 21 additions & 4 deletions src/main/startgg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const phaseGroupIdToEntrants = new Map<number, Entrant[]>();
const phaseGroupIdToSets = new Map<number, Sets>();
const idToSet = new Map<number, Set>();
let selectedSetId = 0;
let selectedPhaseGroupId = 0;
let selectedPhaseId = 0;
let selectedEventId = 0;
export function getCurrentTournament() {
if (!currentTournament) {
return undefined;
Expand Down Expand Up @@ -56,17 +59,31 @@ export function getCurrentTournament() {
}

export function getSelectedSet() {
if (!selectedSetId) {
return undefined;
}

return idToSet.get(selectedSetId);
}

export function setSelectedSetId(id: number) {
selectedSetId = id;
}

export function getSelectedSetChain() {
return {
event: idToEvent.get(selectedEventId),
phase: idToPhase.get(selectedPhaseId),
phaseGroup: idToPhaseGroup.get(selectedPhaseGroupId),
};
}

export function setSelectedSetChain(
eventId: number,
phaseId: number,
phaseGroupId: number,
) {
selectedEventId = eventId;
selectedPhaseId = phaseId;
selectedPhaseGroupId = phaseGroupId;
}

async function wrappedFetch(
input: URL | RequestInfo,
init?: RequestInit | undefined,
Expand Down
39 changes: 32 additions & 7 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ function Hello() {
const [dirInit, setDirInit] = useState(false);
const [copyDir, setCopyDir] = useState('');
const [selectedSet, setSelectedSet] = useState<Set>(EMPTY_SET);
const [selectedSetChain, setSelectedSetChain] = useState(
EMPTY_SELECTED_SET_CHAIN,
);
const [slug, setSlug] = useState('');
const [tournament, setTournament] = useState<Tournament>({
slug: '',
Expand Down Expand Up @@ -210,6 +213,7 @@ function Hello() {
const copyDirPromise = window.electron.getCopyDir();
const tournamentPromise = window.electron.getCurrentTournament();
const selectedSetPromise = window.electron.getSelectedSet();
const selectedSetChainPromise = window.electron.getSelectedSetChain();

// req network
const latestAppVersionPromise = window.electron.getLatestVersion();
Expand Down Expand Up @@ -243,6 +247,19 @@ function Hello() {
if (initSelectedSet) {
setSelectedSet(initSelectedSet);
}
const initSelectedSetChain = await selectedSetChainPromise;
const { event, phase, phaseGroup } = initSelectedSetChain;
if (event && phase && phaseGroup) {
setSelectedSetChain({
eventId: event.id,
eventName: event.name,
eventSlug: event.slug,
phaseId: phase.id,
phaseName: phase.name,
phaseGroupId: phaseGroup.id,
phaseGroupName: phaseGroup.name,
});
}

// req network
const errorMessages: string[] = [];
Expand Down Expand Up @@ -856,6 +873,11 @@ function Hello() {
setGettingTournament(true);
try {
await window.electron.getTournament(maybeSlug, initial && vlerkMode);
if (slug !== maybeSlug) {
setSelectedSet(EMPTY_SET);
setSelectedSetChain(EMPTY_SELECTED_SET_CHAIN);
await window.electron.setSelectedSetChain(0, 0, 0);
}
return true;
} catch (e: any) {
showErrorDialog([e.toString()]);
Expand All @@ -866,9 +888,6 @@ function Hello() {
};

// set controls
const [selectedSetChain, setSelectedSetChain] = useState(
EMPTY_SELECTED_SET_CHAIN,
);
const [selectedChallongeTournament, setSelectedChallongeTournament] =
useState({ name: '', slug: '' });
const selectSet = (set: Set) => {
Expand All @@ -888,7 +907,7 @@ function Hello() {
setOverrides(newOverrides);
setReplays(newReplays);
};
const selectStartggSet = (
const selectStartggSet = async (
set: Set,
phaseGroupId: number,
phaseGroupName: string,
Expand All @@ -908,6 +927,7 @@ function Hello() {
phaseGroupId,
phaseGroupName,
});
await window.electron.setSelectedSetChain(eventId, phaseId, phaseGroupId);
};
const selectChallongeSet = (
set: Set,
Expand Down Expand Up @@ -1633,10 +1653,13 @@ function Hello() {
searchSubstr={searchSubstr}
tournament={tournament}
vlerkMode={vlerkMode}
selectedEventId={selectedSetChain.eventId}
selectedPhaseId={selectedSetChain.phaseId}
selectedPhaseGroupId={selectedSetChain.phaseGroupId}
getEvent={(id: number) => getEvent(id)}
getPhase={(id: number) => getPhase(id)}
getPhaseGroup={(id: number) => getPhaseGroup(id)}
selectSet={(
selectSet={async (
set: Set,
phaseGroupId: number,
phaseGroupName: string,
Expand All @@ -1646,7 +1669,7 @@ function Hello() {
eventName: string,
eventSlug: string,
) => {
selectStartggSet(
await selectStartggSet(
set,
phaseGroupId,
phaseGroupName,
Expand Down Expand Up @@ -1988,10 +2011,12 @@ function Hello() {
latestAppVersion={latestAppVersion}
gotSettings={gotSettings}
mode={mode}
setMode={(newMode: Mode) => {
setMode={async (newMode: Mode) => {
setMode(newMode);
setSelectedSet(EMPTY_SET);
setSelectedSetChain(EMPTY_SELECTED_SET_CHAIN);
setSelectedChallongeTournament({ name: '', slug: '' });
await window.electron.setSelectedSetChain(0, 0, 0);
}}
startggApiKey={startggApiKey}
setStartggApiKey={setStartggApiKey}
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/ChallongeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export default function ChallongeView({
tournament.sets.pendingSets.length === 0 && (
<TiebreakerDialog
entrants={tournament.entrants}
selectSet={selectSet}
selectSet={async (set: Set) => {
selectSet(set);
}}
/>
)}
</div>
Expand Down
46 changes: 35 additions & 11 deletions src/renderer/StartggView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function SetView({
entrant1Names: NameWithHighlight[];
entrant2Names: NameWithHighlight[];
vlerkMode: boolean;
selectSet: () => void;
selectSet: () => Promise<void>;
}) {
return (
<ListItemButton
Expand Down Expand Up @@ -103,7 +103,7 @@ function PhaseGroupView({
searchSubstr: string;
vlerkMode: boolean;
getPhaseGroup: (id: number) => Promise<void>;
selectSet: (set: Set) => void;
selectSet: (set: Set) => Promise<void>;
showError: (error: string) => void;
}) {
const [getting, setGetting] = useState(false);
Expand Down Expand Up @@ -267,9 +267,7 @@ function PhaseGroupView({
phaseGroup.sets.pendingSets.length === 0 && (
<TiebreakerDialog
entrants={phaseGroup.entrants}
selectSet={(set: Set) => {
selectSet(set);
}}
selectSet={(set: Set) => selectSet(set)}
/>
)}
</Block>
Expand All @@ -288,6 +286,7 @@ function PhaseView({
tournamentSlug,
searchSubstr,
vlerkMode,
selectedPhaseGroupId,
getPhase,
getPhaseGroup,
selectSet,
Expand All @@ -301,9 +300,14 @@ function PhaseView({
tournamentSlug: string;
searchSubstr: string;
vlerkMode: boolean;
selectedPhaseGroupId: number;
getPhase: (id: number) => Promise<void>;
getPhaseGroup: (id: number) => Promise<void>;
selectSet: (set: Set, phaseGroupId: number, phaseGroupName: string) => void;
selectSet: (
set: Set,
phaseGroupId: number,
phaseGroupName: string,
) => Promise<void>;
showError: (error: string) => void;
}) {
const [getting, setGetting] = useState(false);
Expand Down Expand Up @@ -396,7 +400,10 @@ function PhaseView({
<PhaseGroupView
key={phaseGroup.id}
phaseGroup={phaseGroup}
initiallyOpen={phase.phaseGroups.length === 1}
initiallyOpen={
phase.phaseGroups.length === 1 ||
phaseGroup.id === selectedPhaseGroupId
}
isStartable={isStartable}
elevateStartButton={elevateStartButton}
eventId={eventId}
Expand Down Expand Up @@ -424,6 +431,8 @@ function EventView({
tournamentSlug,
searchSubstr,
vlerkMode,
selectedPhaseId,
selectedPhaseGroupId,
getEvent,
getPhase,
getPhaseGroup,
Expand All @@ -436,6 +445,8 @@ function EventView({
tournamentSlug: string;
searchSubstr: string;
vlerkMode: boolean;
selectedPhaseId: number;
selectedPhaseGroupId: number;
getEvent: (id: number) => Promise<void>;
getPhase: (id: number) => Promise<void>;
getPhaseGroup: (id: number) => Promise<void>;
Expand All @@ -445,7 +456,7 @@ function EventView({
phaseGroupName: string,
phaseId: number,
phaseName: string,
) => void;
) => Promise<void>;
showError: (error: string) => void;
}) {
const [getting, setGetting] = useState(false);
Expand Down Expand Up @@ -548,13 +559,16 @@ function EventView({
<PhaseView
key={phase.id}
phase={phase}
initiallyOpen={event.phases.length === 1}
initiallyOpen={
event.phases.length === 1 || phase.id === selectedPhaseId
}
isStartable={!event.isOnline}
elevateStartButton={elevateStartButton}
eventId={event.id}
tournamentSlug={tournamentSlug}
searchSubstr={searchSubstr}
vlerkMode={vlerkMode}
selectedPhaseGroupId={selectedPhaseGroupId}
getPhase={getPhase}
getPhaseGroup={getPhaseGroup}
selectSet={(
Expand Down Expand Up @@ -584,6 +598,9 @@ export default function StartggView({
searchSubstr,
tournament,
vlerkMode,
selectedEventId,
selectedPhaseId,
selectedPhaseGroupId,
getEvent,
getPhase,
getPhaseGroup,
Expand All @@ -593,6 +610,9 @@ export default function StartggView({
searchSubstr: string;
tournament: Tournament;
vlerkMode: boolean;
selectedEventId: number;
selectedPhaseId: number;
selectedPhaseGroupId: number;
getEvent: (id: number) => Promise<void>;
getPhase: (id: number) => Promise<void>;
getPhaseGroup: (id: number) => Promise<void>;
Expand All @@ -605,7 +625,7 @@ export default function StartggView({
eventId: number,
eventName: string,
eventSlug: string,
) => void;
) => Promise<void>;
}) {
const [error, setError] = useState('');
const [errorDialogOpen, setErrorDialogOpen] = useState(false);
Expand All @@ -620,11 +640,15 @@ export default function StartggView({
<EventView
key={event.id}
event={event}
initiallyOpen={tournament.events.length === 1}
initiallyOpen={
tournament.events.length === 1 || event.id === selectedEventId
}
elevateStartButton={elevateStartButton}
tournamentSlug={tournament.slug}
searchSubstr={searchSubstr}
vlerkMode={vlerkMode}
selectedPhaseId={selectedPhaseId}
selectedPhaseGroupId={selectedPhaseGroupId}
getEvent={getEvent}
getPhase={getPhase}
getPhaseGroup={getPhaseGroup}
Expand Down
Loading

0 comments on commit 0d062b8

Please sign in to comment.