-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCommand.cs
162 lines (141 loc) · 4.79 KB
/
Command.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Data.Models.Client;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
using static SharedLibraryCore.Server;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace SharedLibraryCore
{
/// <summary>
/// Abstract class for command
/// </summary>
public abstract class Command : IManagerCommand
{
protected readonly CommandConfiguration _config;
protected readonly ITranslationLookup _translationLookup;
private string alias;
protected ILogger logger;
private string name;
private EFClient.Permission permission;
private Game[] supportedGames;
public Command(CommandConfiguration config, ITranslationLookup layout)
{
_config = config;
_translationLookup = layout;
SupportedGames = new Game[0];
}
/// <summary>
/// Helper property to determine the number of required args
/// </summary>
public int RequiredArgumentCount => Arguments.Count(c => c.Required);
/// <summary>
/// Argument list for the command
/// </summary>
public CommandArgument[] Arguments { get; protected set; } = new CommandArgument[0];
/// <summary>
/// Executes the command
/// </summary>
/// <param name="gameEvent"></param>
/// <returns></returns>
public abstract Task ExecuteAsync(GameEvent gameEvent);
/// <summary>
/// Specifies the name and string that triggers the command
/// </summary>
public string Name
{
get => name;
protected set
{
try
{
name = _config?.Commands[GetType().Name].Name ?? value;
}
catch (KeyNotFoundException)
{
name = value;
}
}
}
/// <summary>
/// Specifies the command description
/// </summary>
public string Description { get; protected set; }
/// <summary>
/// Helper property to provide the syntax of the command
/// </summary>
public string Syntax =>
$"{_translationLookup["COMMAND_HELP_SYNTAX"]} {_config.CommandPrefix ?? "!"}{Alias} {string.Join(" ", Arguments.Select(a => $"<{(a.Required ? "" : _translationLookup["COMMAND_HELP_OPTIONAL"] + " ")}{a.Name}>"))}";
/// <summary>
/// Alternate name for this command to be executed by
/// </summary>
public string Alias
{
get => alias;
protected set
{
try
{
alias = _config?.Commands[GetType().Name].Alias ?? value;
}
catch (KeyNotFoundException)
{
alias = value;
}
}
}
/// <summary>
/// Indicates if the command requires a target to execute on
/// </summary>
public bool RequiresTarget { get; protected set; }
/// <summary>
/// Minimum permission level to execute command
/// </summary>
public EFClient.Permission Permission
{
get => permission;
protected set
{
if (_config is null)
{
permission = value;
return;
}
if (_config.Commands.TryGetValue(GetType().Name, out var byClassName))
{
permission = byClassName.MinimumPermission;
return;
}
if (_config.Commands.TryGetValue(this.CommandConfigNameForType(), out var byCommandName))
{
permission = byCommandName.MinimumPermission;
return;
}
permission = value;
}
}
public Game[] SupportedGames
{
get => supportedGames;
protected set
{
try
{
var savedGames = _config?.Commands[GetType().Name].SupportedGames;
supportedGames = savedGames?.Length != 0 ? savedGames : value;
}
catch (KeyNotFoundException)
{
supportedGames = value;
}
}
}
/// <summary>
/// indicates if this command allows impersonation (run as)
/// </summary>
public bool AllowImpersonation { get; set; }
public bool IsBroadcast { get; set; }
}
}