Beispiel #1
0
        static void Main(string[] args)
        {
            string eventId;

            Console.WriteLine("Enter Event id: ");
            eventId = Console.ReadLine();

            if (eventId == "")
            {
                eventId = tcs10k2015;
            }

            Dictionary <int, int> bibRanges = new Dictionary <int, int>(1);

            Console.WriteLine("Enter bib range as: NNN NNN. Press enter to end entering ranges.");

            string line = "";

            while (true)
            {
                line = Console.ReadLine();

                if (line == "")
                {
                    break;
                }
                string[] split = line.Split(' ');
                if (split.Length != 2)
                {
                    Console.WriteLine("Invalid range. Enter NNN NNN. Example: 1000 2000"); continue;
                }
                int start, end;
                if (!Int32.TryParse(split[0], out start) || !Int32.TryParse(split[1], out end))
                {
                    Console.WriteLine("Invalid range. Enter NNN NNN. Example: 1000 2000"); continue;
                }
                if (start > end)
                {
                    Console.WriteLine("Invalid range. Enter NNN NNN. Example: 1000 2000"); continue;
                }
                bibRanges.Add(start, end);
            }

            if (bibRanges.Count == 0)
            {
                Console.WriteLine("No range provided. Exiting");
                Environment.Exit(1);
            }

            SortedDictionary <int, Runner> bibSortedRunners = new SortedDictionary <int, Runner>();

            //Load runners if have already got them in a previous attempt.
            Cache.LoadSortedRunners(bibSortedRunners);

            Console.WriteLine("Starting to work..");
            int completed         = 0;
            int completed_success = 0;
            int completed_failed  = 0;

            //int completed_invalid = 0;

            try
            {
                foreach (KeyValuePair <int, int> pair in bibRanges)
                {
                    for (int bibNo = pair.Key; bibNo <= pair.Value; bibNo++)
                    {
                        if (bibSortedRunners.ContainsKey(bibNo))
                        {
                            //We already have it.
                            continue;
                        }

                        completed++;
                        Runner runner = new Runner(bibNo.ToString(), eventId);
                        try
                        {
                            //Console.WriteLine("Getting runner # {0}", bibNo);
                            runner.ParseAndLoad();
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine("Could not get runner with bib # {0}. Reason = {1}", bibNo, exp.Message);
                            completed_failed++;
                            continue;
                        }

                        completed_success++;

                        //Add based on bibNo.
                        bibSortedRunners.Add(bibNo, runner);

                        //Console.WriteLine("Completed {0} entries", completed);

                        if (completed % SYNC_INTERVAL == 0)
                        {
                            Console.WriteLine("....Serializing {0} items ....", bibSortedRunners.Count);
                            Cache.WriteSortedRunners(bibSortedRunners);
                        }
                    }
                }
                //Done. Write finally again.
                Console.WriteLine("Serializing {0} items", bibSortedRunners.Count);
                Cache.WriteSortedRunners(bibSortedRunners);
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
                Console.WriteLine("Serializing {0} items", bibSortedRunners.Count);
                Cache.WriteSortedRunners(bibSortedRunners);
            }
            //Got all runners.
            CreateHtmlPage(bibSortedRunners);
            CreateCsvFile(bibSortedRunners);
        }
Beispiel #2
0
        private static void CreateHtmlPage(SortedDictionary <int, Runner> zombiedSortedRunners)
        {
            SortedDictionary <int, Runner> rankSortedRunners = new SortedDictionary <int, Runner>();

            //sorted runners conatains lots of invalid runners. Runners who dont exist. Remove them
            foreach (var pair in zombiedSortedRunners)
            {
                if (pair.Value.IsValid)
                {
                    //Console.WriteLine("Rank = {0}, RankInt = {1}", pair.Value.Rank, pair.Value.RankInt);
                    rankSortedRunners.Add(pair.Value.RankInt, pair.Value);
                }
                else
                {
                    // Console.WriteLine("Invalid:  Rank = {0}, RankInt = {1} Bib = {2}", pair.Value.Rank, pair.Value.RankInt, pair.Value.BibNo);
                }
            }

            //How many pages?
            int pages = rankSortedRunners.Count / ITEMS_PER_PAGE;

            if (rankSortedRunners.Count % ITEMS_PER_PAGE != 0)
            {
                pages++;
            }

            Console.WriteLine("Total runners = {0}. Number of pages = {1}", rankSortedRunners.Count, pages);

            string text = System.IO.File.ReadAllText(@"..\..\sample_table.html");

            string[] split = text.Split('$');

            for (int page = 1; page <= pages; page++)
            {
                int startIndex = (page - 1) * ITEMS_PER_PAGE;
                int endIndex   = startIndex + ITEMS_PER_PAGE - 1;

                //if last page, then we dont have all elements
                if (page == pages)
                {
                    endIndex = startIndex + (rankSortedRunners.Count % ITEMS_PER_PAGE) - 1;
                }

                Console.WriteLine("Generating page# {0}. Start index = {1} End index = {2}",
                                  page, startIndex, endIndex);

                using (StreamWriter writer = new StreamWriter("run_" + page + ".html", false))
                {
                    //Write the first part of the page
                    writer.Write(split[0]);

                    //Write the pages links.
                    writer.Write(GenerateLinks(page, pages));

                    //Write the second split
                    writer.Write(split[1]);

                    //Write the dynamic part. Rows.
                    //foreach (var rpair in sortedRunners)
                    for (int i = startIndex; i <= endIndex; i++)
                    {
                        var    rpair = rankSortedRunners.ElementAt(i);
                        Runner r     = rpair.Value;
                        string row   =
                            String.Format(@"<tr>
<td>{0}</td>
<td>{1}</td>
<td>{2}</td>
<td>{3}</td>
<td>{4}</td>
<td>{5}</td>
<td>{6}</td>
<td>{7}</td>
<td>{8}</td>
", r.Rank, r.NameHref, r.BibNo, r.Gender, r.GenderRank, r.Category, r.CategoryRank, r.NetTime, r.GrossTime);
                        writer.Write(row);
                    }

                    //Write the last part of the page
                    writer.Write(split[2]);

                    //Write the pages links.
                    writer.Write(GenerateLinks(page, pages));

                    writer.Write(split[3]);
                }
            }
        }