Пример #1
0
        static void Main()
        {
            Input Input = Menu.GetInput();

            //Input.Load.Pack = false;
            FormatString = System.IO.File.ReadAllText(Input.FormatDirectory + "/desc.format.txt");

            List <int> loadIds = new List <int>();

            if (!Input.Load.All)
            {
                int        years    = (int)(Input.Months / 12);
                int        months   = Input.Months - years * 12;
                List <int> currDate = new List <int>()
                {
                    DateTime.Now.Year - years, DateTime.Now.Month - months, DateTime.Now.Day
                };

                foreach (string[] row in Input.Packs)
                {
                    if (String.IsNullOrWhiteSpace(row[1]))
                    {
                        continue;
                    }
                    string date = '-' + row[1];

                    List <string> dateListTemp = new List <string>();

                    if (date != null)
                    {
                        for (int i = 0; i < date.Length; ++i)
                        {
                            if (date[i] == '-')
                            {
                                dateListTemp.Add("");
                                dateListTemp[dateListTemp.Count - 1] += date[++i];
                            }
                            else
                            {
                                dateListTemp[dateListTemp.Count - 1] += date[i];
                            }
                        }
                    }

                    List <int> packDate = new List <int>();
                    for (int i = 0, element = 0; i < dateListTemp.Count; ++i)
                    {
                        if (int.TryParse(dateListTemp[i], out element))
                        {
                            packDate.Add(element);
                        }
                    }
                    if (packDate.Count != 3)
                    {
                        continue;
                    }
                    int id = 0;
                    if (packDate[0] > currDate[0] ? true : (packDate[0] == currDate[0] ? (packDate[1] > currDate[1] ? true : packDate[2] >= currDate[2]) : false) && int.TryParse(row[0], out id))
                    {
                        loadIds.Add(id);
                    }
                }
            }

            if ((Input.Load.Wikia || Input.Load.YGODB) && (Input.Load.Name || Input.Load.Desc || Input.Load.Ot))
            {
                /// get all cards from entered Cdb and
                /// create Input objects for yugiohdb and wikia search
                List <CDBName> cdbData  = Load.GetAllCards(Input.CdbPath);
                List <Data>    cardData = new List <Data>();

                foreach (CDBName cdbRow in cdbData)
                {
                    if (cdbRow.ID != null && cdbRow.ID.Count > 0)
                    {
                        List <string> ids = cdbRow.ID.Select(id => id.ToString()).ToList();
                        if (ids.Count < 1 || loadIds.Count > 0 ? !cdbRow.ID.Any(cId => loadIds.Contains(cId)) : false)
                        {
                            continue;
                        }
                        string searchID   = String.Concat(Enumerable.Repeat("0", ids[0].Length <= 8 ? 8 - ids[0].Length : 0)) + ids[0];
                        Data   searchData = new Data(ids, searchID, Input.Language, cdbRow.Name);
                        cardData.Add(searchData);
                    }
                }

                /// save paths in a dictionary for later access
                Dictionary <string, string> dir = new Dictionary <string, string>()
                {
                    { "root", Input.CdbPath + @"\" }, { "save", Input.SavingDirectory + @"\" },
                    { "cards", Input.SavingDirectory + @"\cards_" + Input.Language + ".cdb" },
                    { "pack", Input.SavingDirectory + @"\pack_" + Input.Language + ".cdb" }
                };

                /// create cdbs
                Create.CDB(dir["cards"], Cards.GetCreateSQL());
                if (Input.Load.Pack)
                {
                    Create.CDB(dir["pack"], Pack.GetCreateSQL());
                }

                /// download cards
                double interval = 3.0;
                Console.WriteLine(GetConsoleOut(interval, -1, cardData.Count));

                /// Load already loaded information

                /*List<string[]> partialCdb = new List<string[]>();
                 * SQLiteConnection connection = new SQLiteConnection("Data Source=" + dir["cards"]);
                 * try
                 * {
                 *  connection.Open();
                 *  SQLiteCommand textcommand = new SQLiteCommand("SELECT id FROM texts", connection);
                 *  partialCdb = Commands.ExecuteStringCommand(textcommand, 19);
                 *  connection.Close();
                 * }
                 * catch
                 * {
                 *  connection.Close();
                 * }*/

                File.AppendAllText(dir["save"] + "notloaded.txt", string.Format("{0}-- {1} --{0}", Environment.NewLine,
                                                                                string.Format("{0}-{1}-{2} / {3}:{4}:{5}", DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
                                                                                              DateTime.UtcNow.Hour.ToString("00"), DateTime.UtcNow.Minute.ToString("00"), DateTime.UtcNow.Second.ToString("00"))));

                Task[] tasks = new Task[50];
                for (int index = 0; index < cardData.Count; ++index)
                {
                    if (WriteCDB[0] != null)
                    {
                        WriteCDB[0].Wait();
                    }
                    if (WriteCDB[1] != null)
                    {
                        WriteCDB[1].Wait();
                    }

                    for (int i = 0; i < tasks.Length; ++i)
                    {
                        if (tasks[i] == null || tasks[i].IsCompleted)
                        {
                            tasks[i] = Task.Run(() =>
                            {
                                int localIndex    = index;
                                double searchTime = SearchCard(cardData[localIndex], dir, Input.Load);
                                interval          = (interval + searchTime) / 3.0;
                                Console.SetCursorPosition(0, 2);
                                Console.WriteLine(GetConsoleOut(interval, localIndex, cardData.Count));
                                Console.SetCursorPosition(0, 2);
                            });
                            break;
                        }
                        if (i == tasks.Length - 1)
                        {
                            i = 0;
                            Thread.Sleep(200);
                        }
                    }

                    Thread.Sleep((int)(interval * 1000.0 + (interval < 1.25 ? (1.25 - interval) * 1000.0 : 0.0)));
                }
                tasks.WaitAll();
                Thread.Sleep((int)(interval * 2000.0 + (interval < 1.25 ? (1.25 - interval) * 2000.0 : 0.0)));
            }

            Notification.FlashWindow(Process.GetCurrentProcess().MainWindowHandle);
            Console.WriteLine(" The download has finished.");
            Console.ReadLine();
            return;
        }