Skip to content

Commit

Permalink
Throw if module is not setup or cmd missing
Browse files Browse the repository at this point in the history
  - Transform CommandPrefix and Commands into abstracts; this should
    help provide a more comprehensive experience to newcomers
  - Track whether a module was setup or not, and throw an exception
    when you try to call a command on a non-setup module
  - Throw an exception if a user command is not found

Fixes #63
  • Loading branch information
stelcheck committed Aug 22, 2017
1 parent e641979 commit a95ce2b
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions Mage/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,45 @@

namespace Wizcorp.MageSDK.MageClient
{

public class Module<T> : Singleton<T> where T : class, new()
using MageCommandAction = Action<JObject, Action<Exception, JToken>>;
using MageCommandFunction = Func<JObject, UserCommandStatus>;

public abstract class Module<T> : Singleton<T> where T : class, new()
{
//
// Tells us if the module was set up
private bool _SetupCompleted = false;
public bool SetupCompleted {
get { return _SetupCompleted; }
private set { _SetupCompleted = value; }
}


// Mage singleton accessor
protected Mage Mage
{
get { return Mage.Instance; }
}

// Contextualized logger
protected Logger Logger
{
get { return Mage.Logger(GetType().Name); }
}


//
// List of static topics to load during setup
protected virtual List<string> StaticTopics
{
get { return null; }
}

// Static data container
public JToken StaticData;

// Static data setup
//
// Note that topics are not tied to MAGE modules; they are
// essentially global to the MAGE server instance. This is
// simply a convenience function for loading data at setup time.
public void SetupStaticData(Action<Exception> cb)
{
Logger.Info("Setting up static data");
Expand Down Expand Up @@ -67,28 +83,50 @@ public void SetupStaticData(Action<Exception> cb)
}


//
protected virtual string CommandPrefix
{
get { return null; }
}
// The module name as defined on the remote MAGE server
protected abstract string CommandPrefix { get; }

// The list of available user commands on the remote MAGE server
protected abstract List<string> Commands { get; }

protected virtual List<string> Commands
private Dictionary<string, MageCommandAction> commandHandlerActions;
private Dictionary<string, MageCommandFunction> commandHandlerFuncs;

private void AssertSetupCompleted()
{
get { return null; }
if (SetupCompleted == false)
{
throw new Exception("Module was not setup: " + CommandPrefix);
}
}

private Dictionary<string, Action<JObject, Action<Exception, JToken>>> commandHandlerActions;
private Dictionary<string, Func<JObject, UserCommandStatus>> commandHandlerFuncs;
private M GetCommand<M>(Dictionary<string, M> list, string commandName) {
AssertSetupCompleted();

if (list == null) {
throw new Exception("Module does not define any user commands: " + CommandPrefix);
}

var command = list[commandName];

if (command == null)
{
throw new Exception("User command not found: " + CommandPrefix + "." + commandName);
}

return command;
}

public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb)
{
commandHandlerActions[commandName](arguments, cb);
var action = GetCommand(commandHandlerActions, commandName);
action(arguments, cb);
}

public UserCommandStatus Command(string commandName, JObject arguments)
{
return commandHandlerFuncs[commandName](arguments);
var action = GetCommand(commandHandlerFuncs, commandName);
return action(arguments);
}

private void RegisterCommand(string command)
Expand Down Expand Up @@ -123,20 +161,21 @@ public void SetupUsercommands(Action<Exception> cb)
{
Logger.Info("Setting up usercommands");

commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();

if (Commands == null)
{
if (Commands == null) {
SetupCompleted = true;
cb(null);
return;
}

commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();

foreach (string command in Commands)
{
RegisterCommand(command);
}

SetupCompleted = true;
cb(null);
}
}
Expand Down

0 comments on commit a95ce2b

Please sign in to comment.