コード例 #1
0
        void DoMain(string[] args)
        {
            bool changesMade = false;

            ProgramOptions.Parse(args);

            OutputBannerAndCopyright(ProgramOptions.StdOut);

            try {
                Database = new Database(ProgramOptions);
                Database.Open();
                Database.BeginTransaction();
                Database.ClearAndLoad();

                Database.ReportEvents(ProgramOptions.StdOut);
                Database.ValidateReadyForEqualization();

                int  equalizationMatchesNeeded = Database.ReportTeamsAndPlanMatches(ProgramOptions.StdOut, ProgramOptions.Verbose, false);
                bool equalizationNeeded        = equalizationMatchesNeeded > 0;
                if (equalizationNeeded)
                {
                    if (Database.LoadedEqualizationMatches.Count > 0)
                    {
                        throw new EqualizationMatchesPresentException();
                    }
                }

                bool scoringNeeded = false;
                if (ProgramOptions.ScoreEqualizationMatches)
                {
                    if (equalizationNeeded || Database.LoadedEqualizationMatches.Exists(match => !match.IsScored))
                    {
                        scoringNeeded = true;
                    }
                }

                if (equalizationNeeded || scoringNeeded)
                {
                    ProgramOptions.StdOut.WriteLine();
                    ConsoleKey response = ConsoleKey.Y;

                    if (!ProgramOptions.Quiet)
                    {
                        do
                        {
                            string msg;
                            if (equalizationNeeded && scoringNeeded)
                            {
                                msg = $"Update this event with {equalizationMatchesNeeded} new equalization matches and score them?";
                            }
                            else if (equalizationNeeded)
                            {
                                msg = $"Update this event with {equalizationMatchesNeeded} new equalization matches?";
                            }
                            else
                            {
                                int scoringMatchesNeeded = Database.LoadedEqualizationMatches.FindAll(match => !match.IsScored).Count;
                                msg = $"Score the {scoringMatchesNeeded} equalization matches in this event?";
                            }

                            ProgramOptions.StdOut.Write($"{msg} [y|n]");
                            response = Console.ReadKey(false).Key;   // true is intercept key (don't show), false is show
                            if (response != ConsoleKey.Enter)
                            {
                                Console.WriteLine();
                            }
                        }while (response != ConsoleKey.Y && response != ConsoleKey.N);
                    }

                    if (response == ConsoleKey.Y)
                    {
                        if (Database.BackupFile())
                        {
                            if (equalizationNeeded)
                            {
                                Database.SaveNewEqualizationMatches(ProgramOptions.StdOut, ProgramOptions.Verbose);
                                changesMade = true;
                            }

                            List <EqualizationMatch> matchesToScore = Database.UnscoredEqualizationMatches;

                            if (scoringNeeded)
                            {
                                Trace.Assert(matchesToScore.Count > 0);
                                foreach (var match in matchesToScore)
                                {
                                    match.ScoreMatch();
                                    changesMade = true;
                                }
                            }

                            if (equalizationNeeded || scoringNeeded)
                            {
                                Trace.Assert(changesMade);
                                ProgramOptions.StdOut.WriteLine();
                                if (equalizationNeeded)
                                {
                                    ProgramOptions.StdOut.WriteLine($"Database updated with {Database.NewEqualizationMatches.Count} new equalization matches.");
                                }
                                if (scoringNeeded)
                                {
                                    ProgramOptions.StdOut.WriteLine($"Scored {matchesToScore.Count} equalization event matches in database.");
                                }
                                ProgramOptions.StdOut.WriteLine();
                            }
                        }
                    }
                }

                if (changesMade)
                {
                    Database.CommitTransaction();

                    // Report new state
                    Database.BeginTransaction(); // mostly (?) for isolation; harmless in any case
                    Database.ClearAndLoad();

                    ProgramOptions.StdOut.WriteLine("Post-Update Report:");
                    Database.ReportTeamsAndPlanMatches(ProgramOptions.StdOut, false, true);
                }
                else
                {
                    ProgramOptions.StdOut.WriteLine($"No changes made to database");
                }
            }
            catch (DatabaseNotReadyException e)
            {
                ProgramOptions.StdErr.WriteLine($"This event database not ready: {e.Message}");
            }
            catch (Exception e)
            {
                MiscUtil.DumpStackTrance(ProgramOptions.ProgramDescription(), ProgramOptions.StdErr, e);
            }
            finally
            {
                Database.AbortTransaction();
                Database.Close();
            }
        }