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 18, 2017
1 parent e641979 commit a716b14
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions Mage/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,39 @@

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
protected bool SetupCompleted = false;

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

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


//
// Static
protected virtual List<string> StaticTopics
{
get { return null; }
get { return new List<string> {}; }
}

// 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
public void SetupStaticData(Action<Exception> cb)
{
Logger.Info("Setting up static data");
Expand Down Expand Up @@ -67,28 +77,46 @@ 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; }

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

private Dictionary<string, MageCommandAction> commandHandlerActions;
private Dictionary<string, MageCommandFunction> commandHandlerFuncs;

private void AssertSetupCompleted()
{
get { return null; }
if (SetupCompleted == false)
{
throw new Exception("This 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();

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 @@ -126,17 +154,12 @@ public void SetupUsercommands(Action<Exception> cb)
commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();

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

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

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

0 comments on commit a716b14

Please sign in to comment.