-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathGMCommand.js
79 lines (79 loc) · 3.15 KB
/
GMCommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Constructor
var Command = function(Name, Level, Execute) {
this.Name = Name;
this.Level = Level;
this.Execute = Execute;
this.Alias = function(Name) {
return new Command(Name, this.Level, this.Execute);
}
}
// Need to inherit from Comand here.
// Command.prototype = {
// }
var GMCommands = function() {
// TODO: Use object for storing commands in rather than array.
// Add the Command Object refrence to this object so other modules can use it if needed
this.Command = Command;
this.Commands = [];
// Adds support for Commands to be added from other modules
this.AddCommand = function(command) {
for(var i = 0; i < this.Commands.length; i++) {
if(this.Commands[i].Name == command.Name) {
this.Commands.splice(i, 1);
}
}
this.Commands.push(command);
}
// this.RemoveCommand = function(name)
// {
// // Find the command by name
// // Remove it from the Commands by using splice trick.
// }
this.Start = function() {
if(typeof(gmscripts) === 'undefined') gmscripts = new vmscript('commands', 'commands');
}
this.GetCommandInfo = function() {
// Get the command info from db for setting levels of commands dynamically?
}
this.getCommand = function(CommandName) {
var command = null;
for(var i = 0; i < this.Commands.length; i++) {
if(this.Commands[i].Name == CommandName) {
command = this.Commands[i];
break;
}
}
return command;
}
this.Execute = function(string, client) {
if(string == "") return;
// Get CommandName
// String split for first space if no space found use whole string
var indexofSpace = string.indexOf(' ');
var CommandName = indexofSpace > -1 ? string.substr(0, indexofSpace).toLowerCase() : string;
var CommandText = indexofSpace > -1 ? string.substr(indexofSpace + 1) : "";
//console.log('GMCommand String: '+CommandName);
//console.log('GMCommand Text: '+CommandText);
// Check for the command here
var command = this.getCommand(CommandName);
if(command) {
if(client.account.Level < command.Level) {
// Cannot execute tell client too bad
client.sendInfoMessage('account.Level too low for this command');
return;
}
try {
command.Execute.call(this, CommandText, client);
} catch(ex) {
dumpError(ex);
if(client.account.Level >= 80) {
client.sendInfoMessage(ex.toString());
}
}
} else {
client.sendInfoMessage('Invalid Command');
}
// Check if character level is high enough
}
}
module.exports = new GMCommands();