apieceoffruit

CommandTerminal

Sep 7th, 2020
865
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace CommandSystem
  2. {
  3.     public class CommandTerminal
  4.     {
  5.  
  6.         public CommandTerminal(TerminalDisplay display,TerminalCommands commands) : this(new Commander(),display,commands) { }
  7.  
  8.         public CommandTerminal(Commander commander,TerminalDisplay display,TerminalCommands commands)
  9.         {
  10.             Display = display;
  11.             Commands = commands;
  12.             _commander = commander;
  13.         }
  14.  
  15.  
  16.         public Commander Commander => _commander;
  17.  
  18.         public TerminalDisplay Display { get; }
  19.         protected TerminalCommands Commands { get; }
  20.  
  21.         public void Enter(object sender, string message) => ProcessInput(sender,message);
  22.  
  23.         public void Enter(string message) => Enter(Sender, message);
  24.  
  25.         protected virtual void ProcessInput(object sender, string message)
  26.         {            
  27.             var cmd = Commands.FindCommandFor(message);
  28.             if(Exists(cmd))
  29.                 Execute(cmd);
  30.             else
  31.                 DisplayMessage(sender,message);
  32.         }
  33.  
  34.  
  35.         protected virtual void DisplayMessage(object sender, string message) => Display.Send(sender, message);
  36.  
  37.         protected virtual void Execute(Command cmd) => _commander.Execute(cmd);
  38.  
  39.         bool Exists(Command cmd) => cmd != NoCommand.Instance;
  40.  
  41.         public void SetDefaultSender(object sender) => _sender = sender;
  42.  
  43.         private object Sender => _sender ?? SYSTEM;
  44.  
  45.         private object _sender;
  46.         private const string SYSTEM = "SYSTEM";
  47.         private Commander _commander;
  48.     }
  49. }
RAW Paste Data