Skip to content

Commit

Permalink
Add message response on error + several bugfixes (LiveSplit#38)
Browse files Browse the repository at this point in the history
* Condense message response sending

* Add message response on caught error

Also add error for unrecognized message

* Fix: Default response to "-" when appropriate

Fixes several commands related to "current" or "previous" splits
throwing errors when ran before the timer is started

* Fix: Previous split name responds incorrectly

`getprevioussplitname` has (likely) never returned the name of the
previous split, it responded with the name of the current split

* Add alias `getcurrentsplittime` for `getcomparisonsplittime`

`getcurrentsplittime` is a more user-friendly name that also does not
introduce any additional confusion since the ongoing run cannot have a
"current split time," only a previous one.

* Add optional comparison parameter to
`getcomparisonsplittime`/`getcurrentsplittime`

* Protect `setsplitname` against finished runs

* Update syntax and "unrecognized command" messaging
  • Loading branch information
jhobz authored Mar 8, 2022
1 parent ee79a0b commit d367abe
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions UI/Components/ServerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void connection_MessageReceived(object sender, MessageEventArgs e)

private void ProcessMessage(String message, Connection clientConnection)
{
string response = null;
try
{
var args = message.Split(new [] { ' ' }, 2);
Expand Down Expand Up @@ -267,28 +268,39 @@ private void ProcessMessage(String message, Connection clientConnection)
else if (State.CurrentPhase == TimerPhase.Ended)
delta = State.Run.Last().SplitTime[State.CurrentTimingMethod] - State.Run.Last().Comparisons[comparison][State.CurrentTimingMethod];

var response = DeltaFormatter.Format(delta);
clientConnection.SendMessage(response);
// Defaults to "-" when delta is null, such as when State.CurrentPhase == TimerPhase.NotRunning
response = DeltaFormatter.Format(delta);
break;
}
case "getsplitindex":
{
var splitindex = State.CurrentSplitIndex;
var response = splitindex.ToString();
clientConnection.SendMessage(response);
response = splitindex.ToString();
break;
}
case "getcurrentsplitname":
{
var currentsplitname = State.CurrentSplit.Name;
clientConnection.SendMessage(currentsplitname);
if (State.CurrentSplit != null)
{
response = State.CurrentSplit.Name;
}
else
{
response = "-";
}
break;
}
case "getlastsplitname":
case "getprevioussplitname":
{
var previoussplitname = State.Run[State.CurrentSplitIndex].Name;
clientConnection.SendMessage(previoussplitname);
if (State.CurrentSplitIndex > 0)
{
response = State.Run[State.CurrentSplitIndex - 1].Name;
}
else
{
response = "-";
}
break;
}
case "getlastsplittime":
Expand All @@ -297,40 +309,48 @@ private void ProcessMessage(String message, Connection clientConnection)
if (State.CurrentSplitIndex > 0)
{
var time = State.Run[State.CurrentSplitIndex - 1].SplitTime[State.CurrentTimingMethod];
var response = SplitTimeFormatter.Format(time);
clientConnection.SendMessage(response);
response = SplitTimeFormatter.Format(time);
}
else
{
response = "-";
}
break;
}
case "getcurrentsplittime":
case "getcomparisonsplittime":
{
var time = State.CurrentSplit.Comparisons[State.CurrentComparison][State.CurrentTimingMethod];
var response = SplitTimeFormatter.Format(time);
clientConnection.SendMessage(response);
if (State.CurrentSplit != null)
{
var comparison = args.Length > 1 ? args[1] : State.CurrentComparison;
var time = State.CurrentSplit.Comparisons[comparison][State.CurrentTimingMethod];
response = SplitTimeFormatter.Format(time);
}
else
{
response = "-";
}
break;
}
case "getcurrentrealtime":
{
var response = SplitTimeFormatter.Format(State.CurrentTime.RealTime);
clientConnection.SendMessage(response);
response = SplitTimeFormatter.Format(State.CurrentTime.RealTime);
break;
}
case "getcurrentgametime":
{
var timingMethod = TimingMethod.GameTime;
if (!State.IsGameTimeInitialized)
timingMethod = TimingMethod.RealTime;
var response = SplitTimeFormatter.Format(State.CurrentTime[timingMethod]);
clientConnection.SendMessage(response);
response = SplitTimeFormatter.Format(State.CurrentTime[timingMethod]);
break;
}
case "getcurrenttime":
{
var timingMethod = State.CurrentTimingMethod;
if (timingMethod == TimingMethod.GameTime && !State.IsGameTimeInitialized)
timingMethod = TimingMethod.RealTime;
var response = SplitTimeFormatter.Format(State.CurrentTime[timingMethod]);
clientConnection.SendMessage(response);
response = SplitTimeFormatter.Format(State.CurrentTime[timingMethod]);
break;
}
case "getfinaltime":
Expand All @@ -340,8 +360,7 @@ private void ProcessMessage(String message, Connection clientConnection)
var time = (State.CurrentPhase == TimerPhase.Ended)
? State.CurrentTime[State.CurrentTimingMethod]
: State.Run.Last().Comparisons[comparison][State.CurrentTimingMethod];
var response = SplitTimeFormatter.Format(time);
clientConnection.SendMessage(response);
response = SplitTimeFormatter.Format(time);
break;
}
case "getbestpossibletime":
Expand All @@ -353,15 +372,13 @@ private void ProcessMessage(String message, Connection clientConnection)
else
comparison = args.Length > 1 ? args[1] : State.CurrentComparison;
var prediction = PredictTime(State, comparison);
var response = SplitTimeFormatter.Format(prediction);
clientConnection.SendMessage(response);
response = SplitTimeFormatter.Format(prediction);
break;
}
case "gettimerphase":
case "getcurrenttimerphase":
{
var response = State.CurrentPhase.ToString();
clientConnection.SendMessage(response);
response = State.CurrentPhase.ToString();
break;
}
case "setcomparison":
Expand All @@ -387,21 +404,30 @@ private void ProcessMessage(String message, Connection clientConnection)
title = options[1];
}

State.Run[index].Name = title;
State.Run.HasChanged = true;
if (index >= 0 && index < State.Run.Count)
{
State.Run[index].Name = title;
State.Run.HasChanged = true;
}

break;
}
default:
{
// perhaps an error should be returned for an unrecognized message?
break;
throw new Exception($"Unrecognized command: \"{command}\"");
}
}
}
catch (Exception ex)
{
response = "[Error]: " + ex.GetType() + ": " + ex.Message;
Log.Error(ex);
}

if (!string.IsNullOrEmpty(response))
{
clientConnection.SendMessage(response);
}
}

private void connection_Disconnected(object sender, EventArgs e)
Expand Down

0 comments on commit d367abe

Please sign in to comment.