示例#1
0
        public static void Access()
        {
            Console.Clear();
            Console.WriteLine(welcomeText);
            String input;

            String[] keys;

            while (true)
            {
                Console.Write("> ");
                input = Console.ReadLine();
                keys  = input.Split(' ');

                switch (keys[0].ToLower())
                {
                case "import":
                    if (keys.Length >= 3)
                    {
                        Type type = LanguageTools.ParseType(keys[1]);
                        if (type == null)
                        {
                            Console.WriteLine("The type \"" + keys[1] + "\" does not exist.\n");
                            break;
                        }
                        String path = savefolder + "/" + type + "/" + LanguageTools.GetWordsStarting(keys, 2) + ".json";
                        if (!File.Exists(path))
                        {
                            Console.WriteLine("No " + type + " file named \"" + LanguageTools.GetWordsStarting(keys, 2) + "\" found.\n");
                            break;
                        }
                        GameObject imported = JsonConvert.DeserializeObject <GameObject[]>(File.ReadAllText(path))[0];

                        if (!ConsoleTools.ConfirmOverwriteIfConflict(imported))
                        {
                            break;
                        }

                        if (imported is IQueriable)
                        {
                            ((IQueriable)imported).Query();
                        }

                        if (ConsoleTools.Confirms("\nThe above " + type + " will be imported. Confirm?", true))
                        {
                            City.Add(imported);
                            Console.WriteLine(imported.name + " has been imported.\n");
                        }
                        else
                        {
                            Console.WriteLine("Operation cancelled\n");
                        }

                        City.Sort();
                        break;
                    }

                    if (keys.Length == 2)
                    {
                        if (keys[1] == "all")
                        {
                            String[] files;

                            files = Directory.GetFiles(savefolder + "/Parahumans");
                            for (int i = 0; i < files.Length; i++)
                            {
                                City.Add(JsonConvert.DeserializeObject <Parahuman[]>(File.ReadAllText(files[i]))[0]);
                            }

                            files = Directory.GetFiles(savefolder + "/Teams");
                            for (int i = 0; i < files.Length; i++)
                            {
                                Team imported = JsonConvert.DeserializeObject <Team[]>(File.ReadAllText(files[i]))[0];
                                for (int j = 0; j < imported.roster.Count; j++)
                                {
                                    if (City.Get <Parahuman>(imported.roster[j]) == null)
                                    {
                                        imported.roster.RemoveAt(j);
                                        j--;
                                    }
                                }
                                City.Add(imported);
                            }

                            Console.WriteLine("Imported all\n");
                            City.Sort();
                            break;
                        }
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "export":
                    if (keys.Length >= 3)
                    {
                        GameObject obj = City.Get(LanguageTools.ParseType(keys[1]), LanguageTools.GetWordsStarting(keys, 2));
                        if (obj == null)
                        {
                            ConsoleTools.PrintNotFound <GameObject>(LanguageTools.GetWordsStarting(keys, 2));
                            break;
                        }
                        String path = savefolder + "/" + obj.GetType() + "/" + obj.name + ".json";
                        if (File.Exists(path))
                        {
                            if (!ConsoleTools.Confirms("A " + obj.GetType() + " file called \"" + obj.name + "\" already exists. Overwrite?", true))
                            {
                                Console.WriteLine("Operation cancelled\n");
                                break;
                            }
                        }
                        File.WriteAllText(path, JsonConvert.SerializeObject(new GameObject[] { obj }));
                        Console.WriteLine(obj.name + " has been exported to " + path + ".\n");

                        break;
                    }

                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "create":
                    if (keys.Length == 2)
                    {
                        Type type = LanguageTools.ParseType(keys[1]);
                        if (type == null || type.IsAssignableFrom(typeof(GameObject)))
                        {
                            ConsoleTools.PrintTypeNotFound(keys[1]);
                            break;
                        }
                        GameObject       newObj = (GameObject)type.GetConstructor(new Type[] { }).Invoke(new object[] { });
                        List <FieldInfo> fields = new List <FieldInfo>(newObj.GetType().GetFields());
                        fields.Sort((x, y) => ((OrderAttribute)x.GetCustomAttribute(typeof(OrderAttribute))).order.CompareTo(
                                        ((OrderAttribute)y.GetCustomAttribute(typeof(OrderAttribute))).order));
                        for (int i = 0; i < fields.Count; i++)
                        {
                            ConsoleTools.RequestFieldAndSet(fields[i], newObj);
                        }
                        if (!ConsoleTools.ConfirmOverwriteIfConflict(newObj))
                        {
                            break;
                        }
                        ConsoleTools.QueryObject(newObj);
                        if (!ConsoleTools.Confirms("The above team will be created. Confirm?", true))
                        {
                            Console.WriteLine("Operation cancelled\n");
                            break;
                        }
                        City.Add(newObj);
                        Console.WriteLine(newObj.name + " has been created. \n");
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "query":
                    if (keys.Length >= 3)
                    {
                        GameObject obj = City.Get(LanguageTools.ParseType(keys[1]), LanguageTools.GetWordsStarting(keys, 2));
                        if (obj == null)
                        {
                            ConsoleTools.PrintNotFound <GameObject>(LanguageTools.GetWordsStarting(keys, 2));
                            break;
                        }
                        List <FieldInfo> fields = new List <FieldInfo>(obj.GetType().GetFields());
                        fields.Sort((x, y) => ((OrderAttribute)x.GetCustomAttribute(typeof(OrderAttribute))).order.CompareTo(
                                        ((OrderAttribute)y.GetCustomAttribute(typeof(OrderAttribute))).order));
                        for (int i = 0; i < fields.Count; i++)
                        {
                            ConsoleTools.QueryField(fields[i], obj);
                        }
                        break;
                    }
                    if (keys.Length == 2)
                    {
                        if (keys[1] == "teams")
                        {
                            foreach (Team team in City.GetAll <Team>())
                            {
                                team.Query();
                            }
                            break;
                        }
                        if (keys[1] == "parahumans")
                        {
                            foreach (Parahuman parahuman in City.GetAll <Parahuman>())
                            {
                                parahuman.Query();
                            }
                            break;
                        }
                        if (keys[1] == "external")
                        {
                            String[] files;

                            files = Directory.GetFiles(savefolder + "/Parahumans");
                            Console.WriteLine("\n Parahumans:");
                            for (int i = 0; i < files.Length; i++)
                            {
                                Console.WriteLine("   " + files[i]);
                            }

                            files = Directory.GetFiles(savefolder + "/Teams");
                            Console.WriteLine("\n Teams:");
                            for (int i = 0; i < files.Length; i++)
                            {
                                Console.WriteLine("   " + files[i]);
                            }
                            Console.WriteLine("");
                            break;
                        }
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "delete":
                    if (keys.Length == 2)
                    {
                        GameObject obj = City.Get(LanguageTools.ParseType(keys[1]), LanguageTools.GetWordsStarting(keys, 2));
                        if (obj == null)
                        {
                            ConsoleTools.PrintNotFound <GameObject>(LanguageTools.GetWordsStarting(keys, 2));
                            break;
                        }
                        City.Delete(obj);
                        City.Sort();
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "edit":
                    if (keys.Length >= 2)
                    {
                        Parahuman parahuman = City.Get <Parahuman>(LanguageTools.GetWordsStarting(keys, 1));
                        if (parahuman == null)
                        {
                            ConsoleTools.PrintNotFound <Parahuman>(LanguageTools.GetWordsStarting(keys, 1));
                            break;
                        }
                        ParahumanConsole.Access(parahuman);
                        Console.WriteLine(welcomeText);
                        City.Sort();
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "move":
                    if (keys.Length >= 2)
                    {
                        Type type = LanguageTools.ParseType(keys[1]);
                        if (type == null)
                        {
                            ConsoleTools.PrintTypeNotFound(keys[1]);
                            break;
                        }
                        GameObject obj = City.Get(type, LanguageTools.GetWordsStarting(keys, 2));
                        if (obj == null)
                        {
                            ConsoleTools.PrintNotFound(LanguageTools.GetWordsStarting(keys, 2));
                            break;
                        }

                        ContainerGameObject container1 = City.GetParentOf(obj);
                        if (container1 != null)
                        {
                            container1.Remove(obj);
                        }

                        GameObject container2 = ConsoleTools.RequestObject("Enter destination: ");
                        if (container2 == null)
                        {
                            break;
                        }
                        if (!(container2 is ContainerGameObject))
                        {
                            Console.WriteLine(container2.name + " cannot contain objects.\n");
                            break;
                        }
                        if (!((ContainerGameObject)container2).Add(obj))
                        {
                            Console.WriteLine(container2.name + " cannot hold objects of type " + obj.GetType() + ".\n");
                            break;
                        }

                        Console.WriteLine(obj.name + " has been moved" + ((container1 == null) ? "" : (" from " + ((GameObject)container1).name)) + " to " + container2.name + ".\n");
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "set":
                    if (keys.Length >= 4)
                    {
                        GameObject obj = City.Get(LanguageTools.ParseType(keys[2]), LanguageTools.GetWordsStarting(keys, 3));
                        if (obj == null)
                        {
                            ConsoleTools.PrintNotFound <GameObject>(LanguageTools.GetWordsStarting(keys, 3));
                            break;
                        }
                        FieldInfo field = obj.GetType().GetField(keys[1]);
                        if (field == null)
                        {
                            ConsoleTools.PrintNotFound <FieldInfo>(keys[1]);
                            break;
                        }
                        if (!ConsoleTools.RequestFieldAndSet(field, obj))
                        {
                            Console.WriteLine("This field type is not supported");
                        }
                        Console.WriteLine("");
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "quit":
                    Console.WriteLine("");
                    return;

                default:
                    ConsoleTools.PrintInvalidCommand();
                    break;
                }
            }
        }
        public static void Access(Parahuman parahuman)
        {
            Console.Clear();
            String welcomeText = "You are accessing the editor for " + parahuman.name + ".\n";

            Console.WriteLine(welcomeText);
            String input;

            String[] keys;

            while (true)
            {
                Console.Write("> ");
                input = Console.ReadLine();
                keys  = input.Split(' ');

                switch (keys[0].ToLower())
                {
                case "printall":
                    parahuman.Query();
                    break;

                case "rename":
                    if (keys.Length >= 2)
                    {
                        if (keys.Length >= 3 && keys[1] == "civilian")
                        {
                            parahuman.civilian_name = LanguageTools.GetWordsStarting(keys, 2);
                            Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " civilian name is now " + parahuman.civilian_name + ".\n");
                            break;
                        }
                        else
                        {
                            parahuman.name = LanguageTools.GetWordsStarting(keys, 1);
                            Console.WriteLine(LanguageTools.Possessive(parahuman.civilian_name) + " cape name is now " + parahuman.name + ".\n");
                            break;
                        }
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "health":
                    if (keys.Length == 2)
                    {
                        if (Enum.TryParse(keys[1], true, out Health health))
                        {
                            parahuman.health = health;
                            Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " state of health is now " + parahuman.health.ToString().ToLower() + ".\n");
                            break;
                        }
                        if (int.TryParse(keys[1], out int healthint))
                        {
                            parahuman.health = (Health)healthint;
                            Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " state of health is now " + parahuman.health.ToString().ToLower() + ".\n");
                            break;
                        }
                        Console.WriteLine("\"" + keys[1] + "\" is not a valid health setting.\nOptions: Deceased, Down, Injured, Healthy OR 0, 1, 2, 3.\n");
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "alignment":
                    if (keys.Length == 1)
                    {
                        parahuman.alignment = ConsoleTools.RequestAlignment("> Enter new alignment: ");
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "pid":
                    if (keys.Length == 1)
                    {
                        parahuman.ID = ConsoleTools.RequestInt("> Enter new PID: ");
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "threshold":

                    if (keys.Length == 2)
                    {
                        if (Enum.TryParse(keys[1], out Threat threshold))
                        {
                            parahuman.reveal_threshold = threshold;
                            Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " escalation threshold has been set to " + threshold + ".\n");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("\"" + keys[1] + "\" is not a valid escalation threshold. It must be an integer from 0-4 (inclusive).\n");
                            break;
                        }
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "set":
                    if (keys.Length % 2 == 1)
                    {
                        Rating parentRating = null;
                        bool   writesub     = false;
                        if (keys[1] == "subrating")
                        {
                            if (!Enum.TryParse(keys[2], true, out Classification parentClssf))
                            {
                                Console.WriteLine("\"" + keys[2] + "\" is not a valid rating wrapper.\nOptions: Master, Breaker, Tinker.\n");
                                break;
                            }
                            if (parentClssf != Classification.Master && parentClssf != Classification.Breaker && parentClssf != Classification.Tinker)
                            {
                                Console.WriteLine("\"" + keys[2] + "\" is not a valid rating wrapper.\nOptions: Master, Breaker, Tinker.\n");
                                break;
                            }
                            parentRating = parahuman.ratings.Find(rat => rat.clssf == parentClssf);
                            if (parentRating == null)
                            {
                                Console.WriteLine(parahuman.name + "does not have a " + keys[2] + "rating.\n");
                                break;
                            }
                            writesub = true;
                        }
                        for (int i = (writesub ? 3 : 1); i < keys.Length; i += 2)
                        {
                            if (!Enum.TryParse(keys[i], true, out Classification clssf))
                            {
                                PrintInvalidWrapper(keys[i]);
                                break;
                            }
                            if (!int.TryParse(keys[i + 1], out int num))
                            {
                                Console.WriteLine("\"" + keys[i + 1] + "\" is not an integer.");
                                break;
                            }
                            if (num < 0 || num > 12)
                            {
                                Console.WriteLine("Requested " + clssf.ToString() + " rating number out of range. Must be within 0-12 (inclusive).");
                                break;
                            }
                            if (writesub)                                       //Write to subrating of parentRating
                            {
                                Rating subrating = parentRating.subratings.Find(rat => rat.clssf == clssf);
                                if (subrating == null)
                                {
                                    subrating = new Rating(clssf, num);
                                    parentRating.subratings.Add(new Rating(clssf, num));
                                    Console.WriteLine(parahuman.name + " has gained a " + clssf.ToString() + " subrating of " + num.ToString() + " under " + parentRating.clssf.ToString());
                                }
                                else
                                {
                                    subrating.num = num;
                                    Console.WriteLine(parahuman.name + "'s " + clssf.ToString() + " subrating (under " + parentRating.clssf.ToString() + ") has been changed to " + num.ToString());
                                }
                            }
                            else                                         //Write to rating
                            {
                                Rating rating = parahuman.ratings.Find(rat => rat.clssf == clssf);
                                if (rating == null)
                                {
                                    rating = new Rating(clssf, num);
                                    parahuman.ratings.Add(new Rating(clssf, num));
                                    Console.WriteLine(parahuman.name + " has gained a " + clssf.ToString() + " rating of " + num.ToString());
                                }
                                else
                                {
                                    rating.num = num;
                                    Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " " + clssf.ToString() + " rating has been changed to " + num.ToString());
                                }
                            }
                        }
                        Console.WriteLine("");
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "trueform":
                    if (keys.Length >= 2)
                    {
                        String    name = LanguageTools.GetWordsStarting(keys, 1);
                        Parahuman para = City.Get <Parahuman>(name);
                        if (para == null)
                        {
                            ConsoleTools.PrintNotFound <Parahuman>(name);
                            break;
                        }
                        Console.WriteLine("");
                        para.Query();
                        Console.WriteLine("");
                        if (ConsoleTools.Confirms(LanguageTools.Possessive(parahuman.name) + " true form will be set to the above. Confirm?", true))
                        {
                            parahuman.true_form = para;
                            Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " true form has been set.\n");
                            City.Delete(para);
                            para.ID = parahuman.ID;
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Operation cancelled");
                            break;
                        }
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "edit":
                    if (keys.Length == 2 && keys[1] == "trueform")
                    {
                        Access(parahuman.true_form);
                        Console.WriteLine(welcomeText);
                    }
                    break;

                case "delete":
                    if (keys.Length == 2)
                    {
                        if (keys[1] == "trueform")
                        {
                            parahuman.true_form = null;
                            Console.WriteLine("The true form of " + parahuman.name + " has been deleted.\n");
                            break;
                        }

                        int index = -1;
                        if (!Enum.TryParse(keys[1], true, out Classification clssf))
                        {
                            PrintInvalidPower(keys[1]);
                            break;
                        }
                        index = parahuman.ratings.FindIndex(rating => rating.clssf == clssf);
                        if (index == -1)
                        {
                            Console.WriteLine(parahuman.name + " does not have a " + clssf.ToString() + " rating.\n");
                            break;
                        }
                        parahuman.ratings.RemoveAt(index);
                        Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " " + clssf.ToString() + " rating has been removed.\n");
                        break;
                    }
                    if (keys.Length == 3)
                    {
                        Rating parentRating;
                        int    index = -1;
                        if (!Enum.TryParse(keys[1], true, out Classification parentClssf))
                        {
                            PrintInvalidWrapper(keys[1]);
                            break;
                        }
                        if (parentClssf != Classification.Master && parentClssf != Classification.Breaker && parentClssf != Classification.Tinker)
                        {
                            PrintInvalidWrapper(keys[1]);
                            break;
                        }
                        parentRating = parahuman.ratings.Find(rat => rat.clssf == parentClssf);
                        if (parentRating == null)
                        {
                            Console.WriteLine(parahuman.name + " does not have a " + keys[1] + " rating.\n");
                            break;
                        }

                        if (!Enum.TryParse(keys[2], true, out Classification subClssf))
                        {
                            PrintInvalidPower(keys[2]);
                            break;
                        }
                        index = parentRating.subratings.FindIndex(rat => rat.clssf == subClssf);
                        if (index == -1)
                        {
                            Console.WriteLine(parahuman.name + " does not have a " + keys[2] + " subrating under " + keys[1] + ".\n");
                            break;
                        }
                        parentRating.subratings.RemoveAt(index);
                        Console.WriteLine(LanguageTools.Possessive(parahuman.name) + " " + subClssf.ToString() + " subrating under " + parentClssf.ToString() + " has been removed.\n");
                        break;
                    }
                    ConsoleTools.PrintInvalidSyntax();
                    break;

                case "exit":
                    Console.Clear();
                    return;

                default:
                    ConsoleTools.PrintInvalidCommand();
                    break;
                }
            }
        }