generated from Distractic/LethalCompanyTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move static methods to a common Util class
- Loading branch information
1 parent
78a3388
commit cb6030c
Showing
4 changed files
with
55 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters