Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align web socket commands with LiveView server #397

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/ui/TimerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TimingMethod, TimeSpan, LayoutStateRefMut,
} from "../livesplit-core";
import { Option } from "../util/OptionUtil";
import { formatTimeForServer } from "../util/TimeUtil";
import DragUpload from "./DragUpload";
import AutoRefresh from "../util/AutoRefresh";
import AutoRefreshLayout from "../layout/AutoRefreshLayout";
Expand Down Expand Up @@ -252,13 +253,16 @@ export class TimerView extends React.Component<Props, State> {
if (typeof e.data === "string") {
const [command, ...args] = e.data.split(" ");
switch (command) {
case "start": this.start(); break;
case "starttimer": this.start(); break;
case "getcurrenttime": this.getTime(); break;
case "getcurrenttimerphase": this.getTimerPhase(); break;
case "split": this.split(); break;
case "splitorstart": this.splitOrStart(); break;
case "reset": this.reset(); break;
case "togglepause": this.togglePauseOrStart(); break;
case "undo": this.undoSplit(); break;
case "skip": this.skipSplit(); break;
case "pause": this.togglePauseOrStart(); break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this to either stay togglepause or for pause to be a (deprecated) alias.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good idea, I'll fix that up 👍

case "resume": this.togglePauseOrStart(); break;
case "unsplit": this.undoSplit(); break;
case "skipsplit": this.skipSplit(); break;
case "initgametime": this.initializeGameTime(); break;
case "setgametime": this.setGameTime(args[0]); break;
case "setloadingtimes": this.setLoadingTimes(args[0]); break;
Expand All @@ -276,6 +280,27 @@ export class TimerView extends React.Component<Props, State> {
};
}

private getTime() {
console.log(`Fetching game time`);
this.readWith((t) => {
console.log(`Read time: ${formatTimeForServer(t.currentTime().realTime()!.totalSeconds(), false)}`);
this.connection?.send(formatTimeForServer(t.currentTime().realTime()!.totalSeconds(), false));
});
}

private getTimerPhase() {
this.readWith((t) => {
let result = "";
switch (t.currentPhase()) {
case 0: result = "NotRunning"; break;
case 1: result = "Running"; break;
case 2: result = "Ended"; break;
case 3: result = "Paused"; break;
}
this.connection?.send(result);
});
}

private writeWith<T>(action: (timer: TimerRefMut) => T): T {
return this.props.timer.writeWith(action);
}
Expand Down
10 changes: 10 additions & 0 deletions src/util/TimeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ export function formatLeaderboardTime(totalSeconds: number, hideMilliseconds: bo
}`;
}
}

export function formatTimeForServer(totalSeconds: number, hideMilliseconds: boolean): string {
let t = formatLeaderboardTime(totalSeconds, hideMilliseconds);
let parts = t.split(":");
if (parts[0].length < 2) {
return "0" + parts[0] + ":" + parts[1].slice(0, parts[1].length - 1);
} else {
return parts[0] + ":" + parts[1].slice(0, parts[1].length - 1);
}
}