-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandWords.cs
More file actions
98 lines (86 loc) · 2.83 KB
/
CommandWords.cs
File metadata and controls
98 lines (86 loc) · 2.83 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Zuul
{
/*
Refactor this to use JSON based commands.
*/
public class CommandWord
{
public string Command {get; set;}
public List<string> Options {get; set;}
public string Description {get; set;}
public string Usage {get; set;}
}
public class CommandWords
{
public List<CommandWord> ValidCommands = new List<CommandWord>();
public CommandWords()
{
_init();
}
public string GetRootCommand(string cmd)
{
return ValidCommands.Where(vc => vc.Options.Contains(cmd)).SingleOrDefault().Command;
}
public bool IsCommandValid(string cmd)
{
return ValidCommands.Where(vc => vc.Options.Contains(cmd)).SingleOrDefault() != null;
}
public void ShowAll()
{
foreach (CommandWord cmdWord in ValidCommands)
{
foreach (string option in cmdWord.Options)
{
Console.Write($"\"{option}\", ");
}
}
// write the usage examples for each command
// example: go east(e/E) | west(w/W) | north (n/N) | south (s/S)
Console.WriteLine();
}
public void Show(string cmd)
{
var command = ValidCommands.Where(vc => vc.Options.Contains(cmd)).SingleOrDefault();
if (command == null)
{
Console.WriteLine("No command was found.");
}
Console.WriteLine($">> {command.Command}");
Console.WriteLine($"> {command.Description}");
Console.WriteLine($"---------------------------");
Console.WriteLine($"> {command.Usage}");
}
private void _init()
{
try
{
var json = File.ReadAllText("assets/commands.json");
dynamic results = JsonConvert.DeserializeObject(json);
foreach (var cmd in results.commands)
{
var options = new List<string>();
foreach (var o in cmd.options)
{
options.Add(o.ToString());
}
ValidCommands.Add(new CommandWord {
Command = cmd.command.ToString(),
Options = options,
Description = cmd.description.ToString(),
Usage = cmd.usage.ToString()
});
}
}
catch (IOException e)
{
// pokeball exception for now.
Console.WriteLine(e.StackTrace);
}
}
}
}