WikiCreachia/Program.cs

207 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.IO;
using System.Text.RegularExpressions;
namespace wikicreachia {
class Program {
static void Main(string[] args) {
(string major, int minor) status = ("base", 0);
var creachers = new Dictionary<string, Creacher>();
var userCreacher = new Dictionary<string, object>();
string prompt = "";
string input;
string inputLower;
bool readKey;
Regex inputTrimmer = new Regex(@" *(.+) *");
if (File.Exists("wikicreachia.json")) {
using (var sr = new StreamReader("wikicreachia.json")) {
creachers = JsonSerializer.Deserialize<Dictionary<string, Creacher>>(sr.ReadToEnd());
}
}
while (true) {
readKey = false;
switch (status.major) {
case "base":
prompt = "Welcome to Wikicreachia! Type 'help' for help, or 'q' to quit.";
break;
case "add":
if (status.minor <= 3) {
string[] prompts = {
"Input the name of your creacher, or 'c' to cancel.",
"Input the species of your creacher.",
"Input the desired number of footsies (number).",
"Input the biome your creacher inhabits most often (freeform text).",
};
prompt = prompts[status.minor];
}
else if (status.minor == 4) {
prompt = "Press the number corresponding to your creacher's primary method of locomotion:";
foreach (Locomotion option in Enum.GetValues(typeof(Locomotion))) {
prompt += $"\n{(int)option + 1}. {option}";
}
readKey = true;
}
else if (status.minor == 5) {
prompt = "Press the number corresponding to the elevation level your creacher is usually found at:\n1. Below sea level\n2. At sea level\n3. Above sea level";
readKey = true;
}
break;
default:
prompt = $"Unknown status {status.major}!";
break;
}
Console.Write($"\n{prompt}\n> ");
if (readKey) {
input = Console.ReadKey().KeyChar.ToString();
Console.WriteLine(); // insert a newline after the character
}
else {
input = Console.ReadLine();
if (input.Length > 0) {
input = inputTrimmer.Match(input).Groups[1].Value; // trim leading/traling whitespace
}
}
inputLower = input.ToLower();
// general cases - commands that can be used at any time, such as "quit"
if (String.IsNullOrWhiteSpace(input)) {
Console.WriteLine("I can't hear you!");
continue;
}
if (inputLower == "q" || inputLower == "quit") {
Console.WriteLine("Thanks for using WikiCreachia!");
using (var sw = new StreamWriter("wikicreachia.json")) {
sw.Write(JsonSerializer.Serialize(creachers));
}
return;
}
else if (new List<string> { "h", "help", "?" }.Contains(inputLower)) {
Console.WriteLine("WikiCreachia is a program for creating and viewing info about creachers.");
Console.WriteLine("To add a new creacher, type 'add'.");
Console.WriteLine("To view info about a creacher, type its name.");
Console.WriteLine("To see the list of creachers, type 'list'.");
Console.WriteLine();
continue;
}
// specific cases - commands that depend on status.major
if (status.major == "base") {
if (input == "add") {
status.major = "add";
continue;
}
else if (input == "list") {
if (creachers.Count == 0) {
Console.WriteLine("We don't have even a single creacher! 0uo\nWhy not crete one?");
}
else {
Console.WriteLine("Here comes the creacher parade!");
foreach (Creacher entry in creachers.Values) {
Console.WriteLine(entry);
}
}
}
else if (creachers.ContainsKey(input)) {
Console.WriteLine(creachers[input]);
}
else {
Console.WriteLine($"I don't have any info on \"{input}\".");
}
}
else if (status.major == "add") {
if (input == "c") {
Console.WriteLine("Creacher creation cancelled.");
status.major = "base";
continue;
}
if (status.minor == 0) {
if (creachers.ContainsKey(input)) {
Console.WriteLine("Such a creacher already exists! 0uo");
}
else {
// create new dict to ensure all values are empty
userCreacher = new Dictionary<string, object>();
userCreacher["name"] = input;
status.minor++;
}
}
else if (status.minor == 1) {
userCreacher["species"] = input;
status.minor++;
}
else if (status.minor == 2) {
if (int.TryParse(input, out int footsies) && footsies >= 0) {
userCreacher["footsies"] = footsies;
status.minor++;
}
else {
Console.WriteLine("Please enter a valid number.");
}
}
else if (status.minor == 3) {
userCreacher["biome"] = input;
status.minor++;
}
else if (status.minor == 4) {
if (int.TryParse(input, out int choice) && Enum.IsDefined(typeof(Locomotion), choice - 1)) {
userCreacher["locomotion"] = (Locomotion)choice - 1;
status.minor++;
}
else {
Console.WriteLine("Invalid choice - please try again.");
}
}
else if (status.minor == 5) {
if (int.TryParse(input, out int choice) && Enum.IsDefined(typeof(RelativeElevation), choice - 1)) {
userCreacher["elevation"] = (RelativeElevation)choice - 1;
// try to create the new creacher
creachers.Add(
(string)userCreacher["name"], new Creacher(
(string)userCreacher["name"],
(string)userCreacher["species"],
(int)userCreacher["footsies"],
(string)userCreacher["biome"],
(Locomotion)userCreacher["locomotion"],
(RelativeElevation)userCreacher["elevation"]
)
);
Console.WriteLine("Added {0}!", userCreacher["name"]);
using (var sw = new StreamWriter("wikicreachia.json")) {
sw.Write(JsonSerializer.Serialize(creachers));
}
status.major = "base";
status.minor = 0;
}
else {
Console.WriteLine("Invalid choice - please try again.");
}
}
}
}
}
}
}