Skip to content

Commit

Permalink
fix: move static methods to a common Util class
Browse files Browse the repository at this point in the history
  • Loading branch information
Computerdores committed Apr 1, 2024
1 parent 78a3388 commit cb6030c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
44 changes: 10 additions & 34 deletions AdvancedTerminalAPI/Commands/SwitchCommand.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
using System.Collections.Generic;
using System.Linq;

namespace Computerdores.Commands;
namespace Computerdores.Commands;

public class SwitchCommand : ICommand {
public string GetName() {
return "switch";
}
public string GetName() => "switch";

public string PredictArguments(string partialArgumentsText) { // untested
int index = GetPlayerIndexByName(partialArgumentsText);
int index = Util.GetPlayerIndexByName(partialArgumentsText);
return index != -1 ? StartOfRound.Instance.mapScreen.radarTargets[index].name : partialArgumentsText;
}

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
int index = GetPlayerIndexByName(finalArgumentsText);
if (index != -1 || finalArgumentsText == "") {
if (index != -1) {
StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(index);
} else {
StartOfRound.Instance.mapScreen.SwitchRadarTargetForward(true);
}
return ("Switching Radar cam view.\n\n", true, true);
}
return ("", false, false);
}

private static int GetPlayerIndexByName(string name) {
// Note: I didn't come up with this logic
// I just reimplemented the base game name detection in a more efficient way (see: CheckForPlayerNameCommand)
if (name.Length < 3) return -1;
var playerNames = new List<string>(
StartOfRound.Instance.mapScreen.radarTargets.Select(element => element.name.ToLower())
);
name = name.ToLower();
for (var i = 0; i < playerNames.Count; i++) {
if (playerNames[i] == name) return i;
}
for (var i = 0; i < playerNames.Count; i++) {
if (playerNames[i].StartsWith(name[..3])) return i;
int index = Util.GetPlayerIndexByName(finalArgumentsText);
if (index == -1 && finalArgumentsText != "") return ("", false, false);
if (index != -1) {
StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(index);
} else {
StartOfRound.Instance.mapScreen.SwitchRadarTargetForward(true);
}
return -1;
return ("Switching Radar cam view.\n\n", true, true);
}
}
17 changes: 1 addition & 16 deletions AdvancedTerminalAPI/Commands/ViewCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using JetBrains.Annotations;

namespace Computerdores.Commands;

Expand All @@ -11,24 +10,10 @@ public class ViewCommand : ICommand {
}

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
TerminalNode node = FindNode(terminal.GetDriver().VanillaTerminal, "view",
TerminalNode node = Util.FindNode(terminal.GetDriver().VanillaTerminal, "view",
finalArgumentsText.Split(' ').First());
if (node == null) return ("", false, false);
terminal.GetDriver().VanillaTerminal.LoadTerminalImage(node);
return (node.displayText, node.clearPreviousText, true);
}

private static TerminalNode FindNode(Terminal vanillaTerm, string verb, [CanBeNull] string noun) {
// find fitting verb
TerminalKeyword tVerb = vanillaTerm.terminalNodes.allKeywords.
FirstOrDefault(v => v.isVerb && v.word == verb);
if (tVerb == null && verb.Length >= 3) tVerb = vanillaTerm.terminalNodes.allKeywords.
FirstOrDefault(v => v.isVerb && v.word.StartsWith(verb[..3]));
if (tVerb == null || noun == null) return tVerb != null ? tVerb.specialKeywordResult : null; // if no noun given, return node of verb
// find fitting noun
CompatibleNoun tNoun = tVerb.compatibleNouns?.FirstOrDefault(n => n.noun.word == noun);
if (tNoun == null && noun.Length >= 3) tNoun = tVerb.compatibleNouns?.
FirstOrDefault(n => n.noun.word.StartsWith(noun[..3]));
return tNoun?.result;
}
}
42 changes: 42 additions & 0 deletions AdvancedTerminalAPI/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

namespace Computerdores;

public static class Util {
public static TerminalNode GetSpecialNode(Terminal vanillaTerminal, int nodeIndex) {
return vanillaTerminal.terminalNodes.specialNodes[nodeIndex];
}

public static TerminalNode FindNode(Terminal vanillaTerm, string verb, [CanBeNull] string noun) {
// find fitting verb
TerminalKeyword tVerb = vanillaTerm.terminalNodes.allKeywords.
FirstOrDefault(v => v.isVerb && v.word == verb);
if (tVerb == null && verb.Length >= 3) tVerb = vanillaTerm.terminalNodes.allKeywords.
FirstOrDefault(v => v.isVerb && v.word.StartsWith(verb[..3]));
if (tVerb == null || noun == null) return tVerb != null ? tVerb.specialKeywordResult : null; // if no noun given, return node of verb
// find fitting noun
CompatibleNoun tNoun = tVerb.compatibleNouns?.FirstOrDefault(n => n.noun.word == noun);
if (tNoun == null && noun.Length >= 3) tNoun = tVerb.compatibleNouns?.
FirstOrDefault(n => n.noun.word.StartsWith(noun[..3]));
return tNoun?.result;
}

public static int GetPlayerIndexByName(string name) {
// Note: I didn't come up with this logic
// I just reimplemented the base game name detection in a more efficient way (see: CheckForPlayerNameCommand)
if (name.Length < 3) return -1;
var playerNames = new List<string>(
StartOfRound.Instance.mapScreen.radarTargets.Select(element => element.name.ToLower())
);
name = name.ToLower();
for (var i = 0; i < playerNames.Count; i++) {
if (playerNames[i] == name) return i;
}
for (var i = 0; i < playerNames.Count; i++) {
if (playerNames[i].StartsWith(name[..3])) return i;
}
return -1;
}
}
7 changes: 2 additions & 5 deletions AdvancedTerminalAPI/VanillinTerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public class VanillinTerminal : ITerminal {
return keys.Length != 1 ? null : _commands[keys[0]];
}

private string SpecialText(int i) => GetSpecialNode(i).displayText; // purely for convenience
private TerminalNode GetSpecialNode(int nodeIndex) => GetSpecialNode(_driver.VanillaTerminal, nodeIndex);
private static TerminalNode GetSpecialNode(Terminal vanillaTerminal, int nodeIndex) {
return vanillaTerminal.terminalNodes.specialNodes[nodeIndex];
}
// purely for convenience
private string SpecialText(int i) => Util.GetSpecialNode(_driver.VanillaTerminal, i).displayText;
}

0 comments on commit cb6030c

Please sign in to comment.