Skip to content

Commit a716b14

Browse files
committed
Throw if module is not setup or cmd missing
- 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
1 parent e641979 commit a716b14

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

Mage/Module.cs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,39 @@
88

99
namespace Wizcorp.MageSDK.MageClient
1010
{
11-
12-
public class Module<T> : Singleton<T> where T : class, new()
11+
using MageCommandAction = Action<JObject, Action<Exception, JToken>>;
12+
using MageCommandFunction = Func<JObject, UserCommandStatus>;
13+
14+
public abstract class Module<T> : Singleton<T> where T : class, new()
1315
{
14-
//
16+
// Tells us if the module was set up
17+
protected bool SetupCompleted = false;
18+
19+
// Mage singleton accessor
1520
protected Mage Mage
1621
{
1722
get { return Mage.Instance; }
1823
}
1924

25+
// Contextualized logger
2026
protected Logger Logger
2127
{
2228
get { return Mage.Logger(GetType().Name); }
2329
}
2430

25-
26-
//
31+
// Static
2732
protected virtual List<string> StaticTopics
2833
{
29-
get { return null; }
34+
get { return new List<string> {}; }
3035
}
3136

37+
// Static data container
3238
public JToken StaticData;
3339

40+
// Static data setup
41+
// Note that topics are not tied to MAGE modules; they are
42+
// essentially global to the MAGE server instance. This is
43+
// simply a convenience function for
3444
public void SetupStaticData(Action<Exception> cb)
3545
{
3646
Logger.Info("Setting up static data");
@@ -67,28 +77,46 @@ public void SetupStaticData(Action<Exception> cb)
6777
}
6878

6979

70-
//
71-
protected virtual string CommandPrefix
72-
{
73-
get { return null; }
74-
}
80+
// The module name as defined on the remote MAGE server
81+
protected abstract string CommandPrefix { get; }
7582

76-
protected virtual List<string> Commands
83+
// The list of available user commands on the remote MAGE server
84+
protected abstract List<string> Commands { get; }
85+
86+
private Dictionary<string, MageCommandAction> commandHandlerActions;
87+
private Dictionary<string, MageCommandFunction> commandHandlerFuncs;
88+
89+
private void AssertSetupCompleted()
7790
{
78-
get { return null; }
91+
if (SetupCompleted == false)
92+
{
93+
throw new Exception("This module was not setup: " + CommandPrefix);
94+
}
7995
}
8096

81-
private Dictionary<string, Action<JObject, Action<Exception, JToken>>> commandHandlerActions;
82-
private Dictionary<string, Func<JObject, UserCommandStatus>> commandHandlerFuncs;
97+
private M GetCommand<M>(Dictionary<string, M> list, string commandName) {
98+
AssertSetupCompleted();
99+
100+
var command = list[commandName];
101+
102+
if (command == null)
103+
{
104+
throw new Exception("User command not found: " + CommandPrefix + "." + commandName);
105+
}
106+
107+
return command;
108+
}
83109

84110
public void Command(string commandName, JObject arguments, Action<Exception, JToken> cb)
85111
{
86-
commandHandlerActions[commandName](arguments, cb);
112+
var action = GetCommand(commandHandlerActions, commandName);
113+
action(arguments, cb);
87114
}
88115

89116
public UserCommandStatus Command(string commandName, JObject arguments)
90117
{
91-
return commandHandlerFuncs[commandName](arguments);
118+
var action = GetCommand(commandHandlerFuncs, commandName);
119+
return action(arguments);
92120
}
93121

94122
private void RegisterCommand(string command)
@@ -126,17 +154,12 @@ public void SetupUsercommands(Action<Exception> cb)
126154
commandHandlerActions = new Dictionary<string, Action<JObject, Action<Exception, JToken>>>();
127155
commandHandlerFuncs = new Dictionary<string, Func<JObject, UserCommandStatus>>();
128156

129-
if (Commands == null)
130-
{
131-
cb(null);
132-
return;
133-
}
134-
135157
foreach (string command in Commands)
136158
{
137159
RegisterCommand(command);
138160
}
139161

162+
SetupCompleted = true;
140163
cb(null);
141164
}
142165
}

0 commit comments

Comments
 (0)