-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cs
More file actions
313 lines (282 loc) · 9.82 KB
/
Command.cs
File metadata and controls
313 lines (282 loc) · 9.82 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rift.ModernRift.Core
{
/// <summary>
/// Runs a list of Directives when a specific string is typed into the console
/// </summary>
public class Command
{
/// <summary>
/// The text that needs to be entered to trigger the command
/// </summary>
internal string Data;
/// <summary>
/// The list of directives to execute when the command is executed
/// </summary>
public List<Directive> Directives;
/// <summary>
/// The description of the command
/// </summary>
public string Description;
/// <param name="data">The text that needs to be entered to trigger the command</param>
/// <param name="directives">The list of directives to be executed when the command is run</param>
public Command(string data, params Directive[] directives)
{
Directives = new();
foreach (Directive d in directives)
{
Directives.Add(d);
}
Data = data;
Description = "No description provided";
}
/// <param name="data">The text that needs to be enterd to trigger the command</param>
/// <param name="description">The description of the command</param>
/// <param name="directives">The list of directives to be executed when the command is run</param>
public Command(string data, string description, params Directive[] directives)
{
Directives = new();
foreach (Directive d in directives)
{
Directives.Add(d);
}
Data = data;
Description = description;
}
/// <summary>
/// Returns the value of Data
/// </summary>
/// <returns>The text that needs to be entered to trigger the command</returns>
public string Get()
{
return Data;
}
/// <summary>
/// Returns the value of Description
/// </summary>
/// <returns>The description of the command</returns>
public string GetDescription()
{
return Description;
}
/// <summary>
/// Execute all of the directives in sequence
/// </summary>
private void Execute()
{
foreach(Directive directive in Directives)
{
directive.Execute();
}
}
/// <summary>
/// Compare a prompt to the required text, and execute the directives if the two match
/// </summary>
/// <param name="command">The text to compare the required text to; usually something typed by the user.</param>
public void HandlePrompt(string command)
{
string relevantCommand = command;
if (relevantCommand == Data.ToLower().Trim())
{
Execute();
}
}
/// <summary>
/// Change the description of the command
/// </summary>
/// <param name="newDescription">What the description should be changed to</param>
public void UpdateDescription(string newDescription)
{
Description = newDescription;
}
/// <summary>
/// Add the command to the engine
/// </summary>
public void Add()
{
Engine.GameReference.AddCommand(this);
}
}
/// <summary>
/// Command that safely stops the program
/// </summary>
public class QuitCommand : Command
{
/// <summary>
/// What should be typed if the user wants the command to be something other than "quit"
/// </summary>
private string alias;
public QuitCommand() : base("quit", "Safely stops the program.", new QuitDirective())
{
alias = "quit";
}
/// <param name="alias">What should be typed to execute the command</param>
public QuitCommand(string alias) : base(alias, "Safely stops the program.", new QuitDirective()) {
this.alias = alias;
}
/// <summary>
/// Change what should be typed to execute the command
/// </summary>
/// <param name="alias">What should be typed to execute the command</param>
/// <exception cref="InvalidAliasException">You are trying to set two different commands to have the same alias, or are trying to set the alias to null or ""</exception>
public void UpdateAlias(string alias)
{
foreach (Command c in Engine.GameReference.Commands)
{
if (alias == c.Get())
{
if (c.GetType() != typeof(QuitCommand))
{
throw new InvalidAliasException("Two different commands cannot have the same alias.");
}
}
}
if (alias != "")
{
this.alias = alias;
Data = alias;
} else
{
throw new InvalidAliasException("You cannot set the alias of QuitCommand to be null.");
}
}
}
/// <summary>
/// Lists all of the commands in the game along with the descriptions of the commands
/// </summary>
public class HelpCommand : Command
{
/// <summary>
/// What should be typed in order to execute the command
/// </summary>
private string alias;
/// <summary>
/// Whether this command should be able to be executed by the user
/// </summary>
private bool enabled = true;
public HelpCommand() : base("help", "Writes a brief description of each command.", new HelpDirective())
{
alias = "help";
}
/// <param name="alias">What should be typed to execute the command</param>
public HelpCommand(string alias) : base(alias, "Writes a brief description of each command.", new HelpDirective())
{
this.alias = alias;
}
/// <summary>
/// Change what should be typed to execute the command
/// </summary>
/// <param name="alias">What should be typed to execute the command</param>
/// <exception cref="InvalidAliasException">You are trying to set two different commands to have the same alias</exception>
public void UpdateAlias(string alias)
{
foreach (Command c in Engine.GameReference.Commands)
{
if (alias == c.Get())
{
if (c.GetType() != typeof(HelpCommand))
{
throw new InvalidAliasException("Two different commands cannot have the same alias.");
}
}
}
if (alias != null)
{
this.alias = alias;
Data = alias;
HelpDirective.enabled = true;
enabled = true;
}
else
{
enabled = false;
HelpDirective.enabled = false;
}
}
/// <summary>
/// Prevent the user from executing the command
/// </summary>
public void Disable()
{
HelpDirective.enabled = false;
enabled = false;
}
/// <summary>
/// Allow the user to execute the command
/// </summary>
public void Enable()
{
HelpDirective.enabled = true;
enabled = true;
}
}
public class ClearCommand : Command
{
/// <summary>
/// What should be typed in order to execute the command
/// </summary>
private string alias;
/// <summary>
/// Whether this command should be able to be executed by the user
/// </summary>
private bool enabled = true;
public ClearCommand() : base("clear", "Clears the window.", new ClearDirective())
{
alias = "clear";
}
/// <param name="alias">What should be typed to execute the command</param>
public ClearCommand(string alias) : base(alias, "Clears the window.", new ClearDirective())
{
this.alias = alias;
}
/// <summary>
/// Change what should be typed to execute the command
/// </summary>
/// <param name="alias">What should be typed to execute the command</param>
/// <exception cref="InvalidAliasException">You are trying to set two different commands to have the same alias</exception>
public void UpdateAlias(string alias)
{
foreach(Command c in Engine.GameReference.Commands)
{
if(alias == c.Get())
{
if(c.GetType() != typeof(ClearCommand)) {
throw new InvalidAliasException("Two different commands cannot have the same alias.");
}
}
}
if (alias != null)
{
this.alias = alias;
Data = alias;
ClearDirective.enabled = true;
enabled = true;
}
else
{
enabled = false;
ClearDirective.enabled = false;
}
}
/// <summary>
/// Prevent the user from executing the command
/// </summary>
public void Disable()
{
ClearDirective.enabled = false;
enabled = false;
}
/// <summary>
/// Allow the user to execute the command
/// </summary>
public void Enable()
{
ClearDirective.enabled = true;
enabled = true;
}
}
}