Skip to content

Commit

Permalink
feat!: Changes to API and Vanillin Shell to allow for commands to acc…
Browse files Browse the repository at this point in the history
…ept input
  • Loading branch information
Computerdores committed Apr 3, 2024
1 parent c92c121 commit dbe84a0
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 33 deletions.
13 changes: 13 additions & 0 deletions AdvancedTerminalAPI/CommandResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Computerdores;

public struct CommandResult {
public string output;
public bool clearScreen;
public bool success;

public CommandResult(string output, bool clearScreen, bool success) {
this.output = output;
this.clearScreen = clearScreen;
this.success = success;
}
}
8 changes: 5 additions & 3 deletions AdvancedTerminalAPI/ICommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Computerdores;
using System;

public interface ICommand {
namespace Computerdores;

public interface ICommand : ICloneable {
public string GetName();

public string PredictArguments(string partialArgumentsText);

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal);
public CommandResult Execute(string input, ITerminal terminal, out bool wantsMoreInput);
}
11 changes: 11 additions & 0 deletions AdvancedTerminalAPI/Vanillin/ASimpleCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Computerdores.Vanillin;

public abstract class ASimpleCommand {

public CommandResult Execute(string input, ITerminal terminal, out bool wantsMoreInput) {
wantsMoreInput = false;
return Execute(input, terminal);
}

protected abstract CommandResult Execute(string input, ITerminal terminal);
}
8 changes: 5 additions & 3 deletions AdvancedTerminalAPI/Vanillin/AcessibleObjectCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Computerdores.Vanillin;

public class AccessibleObjectCommand : ICommand {
public class AccessibleObjectCommand : ASimpleCommand, ICommand {
private readonly string _name;

public AccessibleObjectCommand(string name) {
Expand All @@ -11,10 +11,12 @@ public class AccessibleObjectCommand : ICommand {

public string PredictArguments(string partialArgumentsText) => partialArgumentsText;

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
protected override CommandResult Execute(string input, ITerminal terminal) {
Terminal vT = terminal.GetDriver().VanillaTerminal;
vT.CallFunctionInAccessibleTerminalObject(_name);
vT.PlayBroadcastCodeEffect();
return (Util.GetSpecialNode(vT, 19).displayText, true, true);
return new CommandResult(Util.GetSpecialNode(vT, 19).displayText, true, true);
}

public object Clone() => new AccessibleObjectCommand(_name);
}
8 changes: 5 additions & 3 deletions AdvancedTerminalAPI/Vanillin/HelpCommand.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
namespace Computerdores.Vanillin;

public class HelpCommand : ICommand {
public class HelpCommand : ASimpleCommand, ICommand {
public string GetName() => "help";

public string PredictArguments(string partialArgumentsText) {
return partialArgumentsText;
}

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
protected override CommandResult Execute(string input, ITerminal terminal) {
Terminal vT = terminal.GetDriver().VanillaTerminal;
return (Util.GetSpecialNode(vT, 13).displayText.
return new CommandResult(Util.GetSpecialNode(vT, 13).displayText.
Replace("[numberOfItemsOnRoute]",
vT.numberOfItemsInDropship > 0
? $"{vT.numberOfItemsInDropship} purchased items on route."
: ""
),
true, true);
}

public object Clone() => new HelpCommand();
}
8 changes: 5 additions & 3 deletions AdvancedTerminalAPI/Vanillin/OtherCommand.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
namespace Computerdores.Vanillin;

public class OtherCommand : ICommand {
public class OtherCommand : ASimpleCommand, ICommand {
public string GetName() => "other";

public string PredictArguments(string partialArgumentsText) {
return partialArgumentsText;
}

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
return (
protected override CommandResult Execute(string input, ITerminal terminal) {
return new CommandResult(
Util.FindNoun(terminal.GetDriver().VanillaTerminal, "other").specialKeywordResult.displayText,
true, true
);
}

public object Clone() => new OtherCommand();
}
8 changes: 5 additions & 3 deletions AdvancedTerminalAPI/Vanillin/ScanCommand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace Computerdores.Vanillin;

public class ScanCommand : ICommand {
public class ScanCommand : ASimpleCommand, ICommand {
public string GetName() => "scan";

public string PredictArguments(string partialArgumentsText) => partialArgumentsText;

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
protected override CommandResult Execute(string input, ITerminal terminal) {
Terminal vT = terminal.GetDriver().VanillaTerminal;
TerminalNode node = Util.FindNoun(terminal.GetDriver().VanillaTerminal, "scan").specialKeywordResult;
return (vT.TextPostProcess(node.displayText, node), true, true);
return new CommandResult(vT.TextPostProcess(node.displayText, node), true, true);
}

public object Clone() => new ScanCommand();
}
15 changes: 10 additions & 5 deletions AdvancedTerminalAPI/Vanillin/SwitchCommand.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
namespace Computerdores.Vanillin;

public class SwitchCommand : ICommand {
public class SwitchCommand : ASimpleCommand, ICommand {
public string GetName() => "switch";

public string PredictArguments(string partialArgumentsText) { // untested
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 = Util.GetPlayerIndexByName(finalArgumentsText);
if (index == -1 && finalArgumentsText != "") return ("", false, false);
protected override CommandResult Execute(string input, ITerminal terminal) {
int index = Util.GetPlayerIndexByName(input);
if (index == -1 && input != "") return new CommandResult("", false, false);
if (index != -1) {
StartOfRound.Instance.mapScreen.SwitchRadarTargetAndSync(index);
} else {
StartOfRound.Instance.mapScreen.SwitchRadarTargetForward(true);
}
return (Util.FindNoun(terminal.GetDriver().VanillaTerminal, "switch").specialKeywordResult.displayText, true, true);
return new CommandResult(
Util.FindNoun(terminal.GetDriver().VanillaTerminal, "switch").specialKeywordResult.displayText,
true, true
);
}

public object Clone() => new SwitchCommand();
}
35 changes: 30 additions & 5 deletions AdvancedTerminalAPI/Vanillin/VanillinTerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class VanillinTerminal : ITerminal {

private static ManualLogSource Log => Plugin.Log;

private ICommand _currentCommand;

public void RegisterDriver(InputFieldDriver driver) {
_driver = driver;
_driver.OnSubmit += OnSubmit;
Expand Down Expand Up @@ -51,15 +53,15 @@ public class VanillinTerminal : ITerminal {
}
}

private void OnSubmit(string text) {
private void OnSubmit1(string text) {
string[] words = text.Split(' ');
string arguments = words.Length == 1 ? "" : words.Skip(1).Join(delimiter: " ");
ICommand nCommand = FindCommand(words[0]);
if (nCommand is {} command) {
Log.LogInfo($"Found Command for word: '{words[0]}'");
(string outp, bool clear, bool success) = command.Execute(arguments, this);
if (success) {
_driver.DisplayText(outp, clear);
CommandResult result = command.Execute(arguments, this, out bool more);
if (result.success) {
_driver.DisplayText(result.output, result.clearScreen);
} else {
Log.LogInfo($"Command execution failed for input: '{text}'");
_driver.DisplayText(SpecialText(11), true);
Expand All @@ -69,11 +71,34 @@ public class VanillinTerminal : ITerminal {
_driver.DisplayText(SpecialText(10), true);
}
}

private void OnSubmit(string text) {
string input = text;
if (_currentCommand == null) {
string[] words = text.Split(' ');
input = words.Length == 1 ? "" : words.Skip(1).Join(delimiter: " ");
_currentCommand = (ICommand)FindCommand(words[0])?.Clone();
}
if (_currentCommand is {} command) {
Log.LogInfo($"Executing Command ({_currentCommand.GetName()}) for input : '{text}'");
CommandResult result = command.Execute(input, this, out bool more);
if (result.success) {
_driver.DisplayText(result.output, result.clearScreen);
} else {
Log.LogInfo($"Command execution failed for input ({_currentCommand.GetName()}): '{text}'");
_driver.DisplayText(SpecialText(11), true);
}
if (!more) _currentCommand = null;
} else {
Log.LogInfo($"Did not find Command for input: '{text}'");
_driver.DisplayText(SpecialText(10), true);
}
}

private void OnEnterTerminal(bool firstTime) {
ICommand welcomeCommand = firstTime ? FindCommand("welcome") : FindCommand("help");
Log.LogInfo("Entering Terminal"+(firstTime ? " for the first time" : "")+".");
_driver.DisplayText(welcomeCommand?.Execute("", this).output, true);
_driver.DisplayText(welcomeCommand?.Execute("", this, out bool more).output, true);
}


Expand Down
12 changes: 7 additions & 5 deletions AdvancedTerminalAPI/Vanillin/ViewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace Computerdores.Vanillin;

public class ViewCommand : ICommand {
public class ViewCommand : ASimpleCommand, ICommand {
public string GetName() => "view";

public string PredictArguments(string partialArgumentsText) {
throw new System.NotImplementedException(); // TODO
}

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

public object Clone() => new ViewCommand();
}
8 changes: 5 additions & 3 deletions AdvancedTerminalAPI/Vanillin/WelcomeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Computerdores.Vanillin;

public class WelcomeCommand : ICommand {
public class WelcomeCommand : ASimpleCommand, ICommand {
public string GetName() => "welcome";

public string PredictArguments(string partialArgumentsText) {
return partialArgumentsText;
}

public (string output, bool clearScreen, bool success) Execute(string finalArgumentsText, ITerminal terminal) {
return (Util.GetSpecialNode(terminal.GetDriver().VanillaTerminal, 1).displayText
protected override CommandResult Execute(string input, ITerminal terminal) {
return new CommandResult(Util.GetSpecialNode(terminal.GetDriver().VanillaTerminal, 1).displayText
.Replace("[currentDay]", DateTime.Now.DayOfWeek.ToString()), true, true);
}

public object Clone() => new WelcomeCommand();
}

0 comments on commit dbe84a0

Please sign in to comment.