From d367abe00f08fb57983017495ca97c89da772c5f Mon Sep 17 00:00:00 2001 From: Jeff Hobson Date: Tue, 8 Mar 2022 11:09:42 -0500 Subject: [PATCH] Add message response on error + several bugfixes (#38) * 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 --- UI/Components/ServerComponent.cs | 84 +++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/UI/Components/ServerComponent.cs b/UI/Components/ServerComponent.cs index 6ba2f52..0c1a32c 100644 --- a/UI/Components/ServerComponent.cs +++ b/UI/Components/ServerComponent.cs @@ -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); @@ -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": @@ -297,22 +309,32 @@ 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": @@ -320,8 +342,7 @@ private void ProcessMessage(String message, Connection clientConnection) 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": @@ -329,8 +350,7 @@ private void ProcessMessage(String message, Connection clientConnection) 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": @@ -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": @@ -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": @@ -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)