Skip to content

Commit

Permalink
Update how splits are loaded from the leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
wooferzfg committed Jun 14, 2020
1 parent 70b6ecf commit 9473772
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
26 changes: 19 additions & 7 deletions src/ui/LiveSplit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export enum MenuKind {
type Menu =
{ kind: MenuKind.Timer } |
{ kind: MenuKind.Splits } |
{ kind: MenuKind.RunEditor, editor: RunEditor, splitsKey?: number } |
{ kind: MenuKind.RunEditor, editor: RunEditor, splitsKey?: number, persistChanges: boolean } |
{ kind: MenuKind.Layout } |
{ kind: MenuKind.LayoutEditor, editor: LayoutEditor } |
{ kind: MenuKind.SettingsEditor, config: HotkeyConfig } |
Expand Down Expand Up @@ -365,13 +365,25 @@ export class LiveSplit extends React.Component<Props, State> {
this.setLayout(layout);
}

public openRunEditor({ splitsKey, run }: EditingInfo) {
public openRunEditor({ splitsKey, persistChanges, run }: EditingInfo) {
const editor = expect(
RunEditor.new(run),
"The Run Editor should always be able to be opened.",
);
this.setState({
menu: { kind: MenuKind.RunEditor, editor, splitsKey },
menu: { kind: MenuKind.RunEditor, editor, splitsKey, persistChanges },
sidebarOpen: false,
});
}

public replaceRunEditor(editor: RunEditor) {
this.setState({
menu: {
kind: MenuKind.RunEditor,
editor,
splitsKey: undefined,
persistChanges: true,
},
sidebarOpen: false,
});
}
Expand All @@ -380,16 +392,16 @@ export class LiveSplit extends React.Component<Props, State> {
if (this.state.menu.kind !== MenuKind.RunEditor) {
panic("No Run Editor to close");
}
const { editor, splitsKey } = this.state.menu;
const { editor, persistChanges, splitsKey } = this.state.menu;
const run = editor.close();
if (save) {
if (splitsKey == null) {
if (persistChanges) {
Storage.storeRunAndDispose(run, splitsKey);
} else {
assertNull(
this.writeWith((t) => t.setRun(run)),
"The Run Editor should always return a valid Run.",
);
} else {
Storage.storeRunAndDispose(run, splitsKey);
}
} else {
run.dispose();
Expand Down
27 changes: 22 additions & 5 deletions src/ui/RunEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface State {

interface Callbacks {
renderViewWithSidebar(renderedView: JSX.Element, sidebarContent: JSX.Element): JSX.Element,
replaceRunEditor(editor: LiveSplit.RunEditor): void,
closeRunEditor(save: boolean): void,
}

Expand Down Expand Up @@ -1871,6 +1872,18 @@ export class RunEditor extends React.Component<Props, State> {
}

private async downloadSplits<T>(apiRun: Run<T>, apiUri: string) {
// FIXME: Determine whether there are any unsaved changes in the Run Editor
if (!confirm(
"Are you sure you want to load these splits? Any unsaved changes will be lost.",
)) {
return;
}

this.setState({
...this.state,
isLoading: true,
});

const baseUri = "https://splits.io/api/v3/runs/";
assert(apiUri.startsWith(baseUri), "Unexpected Splits.io URL");
const splitsId = apiUri.slice(baseUri.length);
Expand All @@ -1881,11 +1894,12 @@ export class RunEditor extends React.Component<Props, State> {
const platformListDownload = downloadPlatformList();
const regionListDownload = downloadRegionList();
const gameInfoDownload = downloadGameInfo(gameName);

await gameInfoDownload;
await platformListDownload;
await regionListDownload;
const run = await runDownload;
// TODO Race Condition with the Run Editor closing (and probably others)

run.with((run) => {
const newEditor = LiveSplit.RunEditor.new(run);
if (newEditor !== null) {
Expand All @@ -1896,10 +1910,7 @@ export class RunEditor extends React.Component<Props, State> {
apiRun,
);

// TODO Oh no, not internal pointer stuff
this.props.editor.dispose();
this.props.editor.ptr = newEditor.ptr;

this.props.callbacks.replaceRunEditor(newEditor);
this.update();
} else {
toast.error("The downloaded splits are not suitable for being edited.");
Expand All @@ -1908,6 +1919,12 @@ export class RunEditor extends React.Component<Props, State> {
} catch (_) {
toast.error("Failed to download the splits.");
}

this.setState({
...this.state,
isLoading: false,
});
this.update(Tab.RealTime);
}

private toggleExpandLeaderboardRow(rowIndex: number) {
Expand Down
15 changes: 12 additions & 3 deletions src/ui/SplitsSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import DragUpload from "./DragUpload";
import { ContextMenuTrigger, ContextMenu, MenuItem } from "react-contextmenu";

export interface EditingInfo {
splitsKey?: number,
splitsKey: number | undefined,
persistChanges: boolean,
run: Run,
}

Expand Down Expand Up @@ -179,7 +180,11 @@ export class SplitsSelection extends React.Component<Props, State> {
}
});
if (run !== null) {
this.props.callbacks.openRunEditor({ run });
this.props.callbacks.openRunEditor({
run,
splitsKey: this.props.openedSplitsKey,
persistChanges: false,
});
} else {
toast.error("You can't edit your run while the timer is running.");
}
Expand Down Expand Up @@ -265,7 +270,11 @@ export class SplitsSelection extends React.Component<Props, State> {

private async editSplits(splitsKey: number) {
const run = await this.getRunFromKey(splitsKey);
this.props.callbacks.openRunEditor({ splitsKey, run });
this.props.callbacks.openRunEditor({
splitsKey,
persistChanges: true,
run,
});
}

private async copySplits(key: number) {
Expand Down

0 comments on commit 9473772

Please sign in to comment.