示例#1
0
        public void Start()
        {
            System.Console.Title = AppName;

            while (true)
            {
                System.Console.Clear();
                WriteHeader();
                WriteLine(WelcomeMessage);

                WriteLine();

                if (!_repo.HasData())
                {
                    ShowConnectionError();
                    return;
                }

                WriteLine(HelpMessage);
                WriteLine();

                string input = AskForInput();

                switch (input)
                {
                case "view":
                    input = StartView();
                    break;

                case "add":
                    input = StartAddView();
                    break;

                case "wereami":
                case "wai":
                case "w":
                    WriteLine(_repo.WhereAmI());
                    AskForInput("Press any button to continue");
                    break;

                case "clean":
                case "c":
                    WriteLine("Removing all dead Tamagotchis, this might take some time.", ConsoleColor.Red);
                    WriteLine();

                    var allCount  = _repo.TamagotchiCount();
                    var perPage   = _repo.TamagotchiPerPage();
                    int pagecount = allCount / perPage;

                    for (int i = 0; i < allCount; i += perPage)
                    {
                        var all = _repo.GetAll(i);
                        if (all.Count() <= 0)
                        {
                            break;
                        }

                        if (all.Any(t => t.HasDied))
                        {
                            foreach (var item in all.Where(t => t.HasDied))
                            {
                                Write(".", ConsoleColor.Red);
                                _repo.Remove(item.Name);
                            }

                            allCount  = _repo.TamagotchiCount();
                            perPage   = _repo.TamagotchiPerPage();
                            pagecount = allCount / perPage;

                            i = 0;
                        }
                    }
                    break;

                case "purge":
                case "p":
                    WriteLine("Removing all Tamagotchis, this might take some time.", ConsoleColor.Red);
                    WriteLine();

                    while (_repo.TamagotchiCount() != 0)
                    {
                        var all = _repo.GetAll(0);

                        foreach (var item in all)
                        {
                            Write(".", ConsoleColor.Red);
                            _repo.Remove(item.Name);
                        }
                    }
                    break;

                case "bulk":
                case "b":
                    WriteLine("Bulk adding Tamagotchis", ConsoleColor.Red);
                    int amount = 0;
                    int.TryParse(AskForInput("amount > "), out amount);
                    if (amount <= 0)
                    {
                        break;
                    }

                    var prefix = AskForInput("prefix > ");
                    if (string.IsNullOrWhiteSpace(prefix))
                    {
                        break;
                    }

                    for (int i = 0; i < amount; i++)
                    {
                        Write(".", ConsoleColor.Red);
                        _repo.Add($"{prefix} {i}");
                    }

                    break;

                default:
                    WriteLine("Command not recognized.", ConsoleColor.Red);
                    AskForInput("Press any button to continue");
                    break;
                }

                if (input == "quit")
                {
                    return;
                }
            }
        }