TextRPG/commands/ConsoleManager.cs

65 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using TextRPG.Commands;
namespace TextRPG.Commands
{
public static class ConsoleManager
{
private static Dictionary<string, Command> commands = new Dictionary<string, Command>(StringComparer.OrdinalIgnoreCase);
static ConsoleManager()
{
// Register all commands
RegisterCommand(new HelpCommand());
RegisterCommand(new ClearCommand());
RegisterCommand(new TimeCommand());
RegisterCommand(new StatusCommand());
RegisterCommand(new InventoryCommand());
RegisterCommand(new TakeCommand());
RegisterCommand(new DropCommand());
RegisterCommand(new HealCommand());
}
public static void RegisterCommand(Command command)
{
commands[command.Name] = command;
}
public static Command GetCommand(string name)
{
return commands.ContainsKey(name) ? commands[name] : null;
}
public static IEnumerable<Command> GetAllCommands()
{
return commands.Values;
}
public static bool Execute(string input, Action<string> outputCallback)
{
string[] parts = input.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0) return false;
string commandName = parts[0];
string[] args = parts.Skip(1).ToArray();
if (commands.ContainsKey(commandName))
{
try
{
commands[commandName].Execute(args, outputCallback);
}
catch (Exception e)
{
outputCallback($"Error: {e.Message}");
}
return true;
}
return false;
}
}
}