-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.java
381 lines (364 loc) · 14.5 KB
/
Command.java
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.util.HashMap;
import java.util.ArrayList;
/**
* A class that will processes all commands which are available
* in TGNU.
*
* @author TGNU Team
* @version 1.0
*/
public class Command
{
private static HashMap<String, CommandString> validCommands;
/**
* Initialize the HashMap for the validcommands and
* put the commands in the HashMap.
*/
public static void initCommandList()
{
validCommands = new HashMap<String, CommandString>();
validCommands.put("help", CommandString.HELP);
validCommands.put("cp", CommandString.COPY);
validCommands.put("exit", CommandString.QUIT);
validCommands.put("cd", CommandString.CD);
validCommands.put("cat", CommandString.CAT);
validCommands.put("ls", CommandString.LIST);
validCommands.put("rm", CommandString.RM);
validCommands.put("df", CommandString.DF);
}
/**
* Check whether a given String is a valid command word.
*
* @return true if it is, false if it isn't.
* @param String aString which could be a valid command word
*/
public static boolean isCommand(String aString)
{
return validCommands.containsKey(aString);
}
/**
* Find the CommandWord associated with a command word.
*
* @param commandWord The word to look up.
* @return The CommandWord correspondng to commandWord,
* or UNKNOWN if it is not a valid command word.
*/
public static CommandString getCommand(String commandWord)
{
CommandString command = validCommands.get(commandWord);
if(command != null) {
return command;
}
else {
return CommandString.UNKNOWN;
}
}
/**
* Processes the commands and the words which he receives
* from the terminal and will look if the first word
* is a command, if so he will excute the method of that command,
* if not he will say that it is a unknown command.
*
* checks if the player wants to exit the game,
* if not wantToQuit will be false and checks if the word
* is another command. If the player types exit, wantToQuit
* will be true and the game will stop.
*
* @return wantToQuit will return true if the players wants to exit.
* @param ArrayList<String> words, which are the words which the player typed.
*/
public static boolean processCommand(ArrayList<String> words)
{
boolean wantToQuit = false;
CommandString commands = getCommand(words.get(0));
if(commands == CommandString.UNKNOWN) {
Terminal.print("Unknown Command.");
}
else if(commands == CommandString.HELP) {
if(words.size() == 2) {
showCommandHelp(words.get(1));
}
else
{
showAllCommands();
}
}
else if (commands == CommandString.COPY) {
if(words.size() == 3) {
cp(words.get(1),words.get(2));
}
else {
Terminal.print("Error: Insufficient parameters.");
Terminal.print("For more information about the usage of this command type \"help cp\"");
}
}
else if (commands == CommandString.CD) {
if(words.size() == 2){
cd(words.get(1));
}else{
Terminal.print("For more information about the usage of this command type \"help cd\"");
}
}
else if (commands == CommandString.CAT) {
if(words.size() == 2){
cat(words.get(1));
}
else{
Terminal.print("For more information about the usage of this command type \"help cat\"");
}
}
else if (commands == CommandString.LIST) {
if(words.size() == 1){
ls();
}
else{
Terminal.print("For more information about the usage of this command type \"help ls\"");
}
}
else if (commands == CommandString.RM) {
if(words.size() == 2){
rm(words.get(1));
}
else{
Terminal.print("For more information about the usage of this command type \"help rm\"");
}
}
else if (commands == CommandString.DF) {
if(words.size() == 1){
df();
}
else{
Terminal.print("For more information about the usage of this command type \"help df\"");
}
}
else if (commands == CommandString.QUIT) {
wantToQuit = true;
}
return wantToQuit;
}
/**
* Print all valid commands to the Terminal
*/
public static void showAllCommands()
{
for(String existingCommand : validCommands.keySet()) {
Terminal.printInline(existingCommand + " ");
}
Terminal.print("");
Terminal.print("for more information about a command type help [command]");
}
/**
* Method to print the description and the use of the specific command word.
*
* @param String command, command is the word which should be a command word to get the correct message for a command.
*/
public static void showCommandHelp(String command)
{
//check trough all available help messages to find the one the user wants more information abou
if(command.equals("help")) Terminal.print("There is no help about help because this would destroy the earth, just use help without a parameter");
else if(command.equals("cp")) Terminal.print("You can copy files with this command, by using cp file /home/[username]");
else if(command.equals("cd")) Terminal.print("You can browse true the filesystem with this command, by using cd directory, i.e. cd mainframe.\nParameters .., you can go back with this parameter, by using cd ..");
else if(command.equals("cat")) Terminal.print("The cat command is for viewing the content of the files, by using cat file i.e. cat obama.txt.");
else if(command.equals("ls")) Terminal.print("The ls command is the command that will show you a list of the directory's and files in your current directory,\n i.e. you are in the mainframe/boot map, if you typ the ls command it will display grub. ");
else if(command.equals("rm")) Terminal.print("The rm command is made for removing files, by using rm file . This command can only be applied in you home directory (/home/[username])");
else if(command.equals("df")) Terminal.print("This command will give you the information about your harddrive storage, by using df");
else if(command.equals("exit")) Terminal.print("This command exits the game/shell.");
else Terminal.print("Sorry, there is no help topic found for the that command.");
}
/**
* Method which processes the command "cd".
* Checks wether if the user gives any parameters to cd or
* if there is a password on a directory.
*
* cd command also checks if there is a directory which
* the user types after the cd command.
*
* @param String options, the second word of the ArrayList<String> words which should be a directory.
*/
public static void cd(String options)
{
if(options.equals("..") == true)
{
if(Filesystem.getCurrentDirectory().getParent() != null)
{
Filesystem.setCurrentDirectory(Filesystem.getCurrentDirectory().getParent());
}
else
{
Terminal.print("You are already at root");
}
}
else if(options.equals("/"))
{
Filesystem.setCurrentDirectory(Filesystem.getRoot());
}
else
{
Directory found = Filesystem.findDirectoryByName(options);
if(found != null)
{
if(options.equals("roadhouse") || options.equals("Dont_look") || options.equals("important"))
{
GameController.gameOver();
}
else if(found.getPassword() == null)
{
Filesystem.setCurrentDirectory(found);
}
else{
Terminal.printInline("Password: ");
String input = Terminal.getRawUserInput();
String password = found.getPassword();
if(input.equals(password)){
Filesystem.setCurrentDirectory(found);
}
else
{
Terminal.print("Sorry, try again");
}
}
}
else
{
Terminal.print("Sorry no such file or directory");
}
}
}
/**
* Method that processes the ls command.
*
* The command whill get all the directory´s and files
* of the currentDirectory. He will display for the directory´s
* that is it actually a directory ([dir]) and for files he will print
* that it are files with a file sizes ([file] && "kb")
*/
public static void ls()
{
for(Directory dir : Filesystem.getCurrentChilds())
{
Terminal.print("[dir] " + dir.getName());
}
for(File file : Filesystem.getCurrentFiles())
{
Terminal.print("[file] " + file.getName() + " " + file.getSize() + " kb");
}
}
/**
* command that will excute the copy command.
*
* It checks if he copies too the right folder,
* if so he will copy the file, if not he will print the
* second error message that the required persmissions are not ok.
*
* If the file already exists in the home folder it will say that the file
* is already in the home folder. If the file cannot be found it wil say that
* he can't find the file.
*
* @param String param1 is the file which is going to be copied
* @param String param2 is the directory where the file is going to be copied too.
*/
public static void cp(String param1, String param2)
{
File searchResult = Filesystem.findFileByName(param1);
if(searchResult != null) {
if(param2.equals("/home/"+GameController.getUsername()) || param2.equals("/home/" + GameController.getUsername() + "/")) {
if(Filesystem.fileExists(searchResult) != true)
{
if(Filesystem.getUsedDiskspace() + searchResult.getSize() <= Filesystem.getDiskSize())
{
Filesystem.copyFile(searchResult);
Filesystem.updateDiskspace();
GameController.checkVictory();
}
else
{
Terminal.print("Error: insufficient diskspace");
}
}
else
{
Terminal.print("Error: File already exists in the directory /home/" + GameController.getUsername());
}
}
else {
Terminal.print("Error: You don't have the required permissions to copy the file " + param1 + " to the folder " + param2);
}
}
else {
Terminal.print("Error: The source file you specified can not be found.");
}
}
/**
* the cat command will show the content of the file.
*
* If there is no content than it will say that
* he can't view the content of it.
* Or the file which the user types doesn't exists.
*
* @param String options, the second word of the ArrayList<String> words which should be a file.
*/
public static void cat(String options)
{
File searchResult = Filesystem.findFileByName(options);
if(searchResult != null) {
if(searchResult.getContent() != null){
Terminal.print(searchResult.getContent());
}
else{
Terminal.print("Sorry: cat can not view this content-type");
}
}
else{
Terminal.print("Error: The source file you specified can not be found.");
}
}
/**
* Processes the rm command.
* Checks if the user that is trying to use the rm command
* is in it's home directory (/home/<username>). If so
* it will display the files which the player has copied to his folder and can
* if he wants to, remove the files which are unnecessary.
*
* Gives error if the user tries to delete a file which doesn't exsists in
* his home folder.
*
* If the user isn't in his home folder and tries to delete a file or directory,
* it will give an error that he is not authorized to delete files or directory's.
*
* @param String options, the second word of the ArrayList<String> words which should be a file.
*/
public static void rm(String options)
{
File file = Filesystem.findFileByName(options);
boolean message = false;
if(file != null)
{
if(Filesystem.getCurrentDirectory().getName().equals(GameController.getUsername()))
{
Filesystem.removeFile(file);
Filesystem.updateDiskspace();
Terminal.print("File deleted");
}
else
{
Terminal.print("Error: You are not authorized to make changes in this directory");
message = true;
}
}
else if(message == false)
{
Terminal.print("Error: The source file you specified can not be found.");
}
}
/**
* df command will print the available disksize that the player can use.
* It also displays the maximum of which the user can use for the storage of his/her files
*/
public static void df()
{
Terminal.print("Filesystem used available");
Terminal.print("/ 300 gb 500 gb");
Terminal.print("/home " + Filesystem.getUsedDiskspace() + " kb " + Filesystem.getDiskSize() + " kb");
Terminal.print("/mnt/mainframe 300 gb 10000 gb");
}
}