Esempio n. 1
0
 string HtmlResponse(string rawUrl, string lastPart, Holder holder)
 {
     if (lastPart == "now.html")
     {
         return(NowPage());
     }
     else if (holder == null)
     {
         return(string.Format(CultureInfo.InvariantCulture, "<html><body>Couldn't find a league key in \"<br>{0}\". Try <a href=\"now.html\">Now Playing</a> instead.</body></html>", rawUrl));
     }
     else if (lastPart == "index.html")
     {
         return(ReportPages.OverviewPage(holder, false, ReportPages.GameHyper, OutputFormat.Svg));
     }
     else if (lastPart.StartsWith("game", StringComparison.OrdinalIgnoreCase))
     {
         DateTime dt   = DateTime.ParseExact(lastPart.Substring(4, 12), "yyyyMMddHHmm", CultureInfo.InvariantCulture);
         Game     game = holder.League.AllGames.Find(x => x.Time.Subtract(dt).TotalSeconds < 60);
         if (game == null)
         {
             return(string.Format(CultureInfo.InvariantCulture, "<html><body>Invalid game: <br>{0}</body></html>", rawUrl));
         }
         else
         {
             return(ReportPages.GamePage(holder.League, game));
         }
     }
     else if (lastPart.StartsWith("team", StringComparison.OrdinalIgnoreCase))
     {
         if (int.TryParse(lastPart.Substring(4, 2), out int teamId))
         {
             LeagueTeam leagueTeam = holder.League.Teams.Find(x => x.TeamId == teamId);
             if (leagueTeam == null)
             {
                 return(string.Format(CultureInfo.InvariantCulture, "<html><body>Invalid team number: <br>{0}</body></html>", rawUrl));
             }
             else
             {
                 return(ReportPages.TeamPage(holder.League, false, leagueTeam, ReportPages.GameHyper, OutputFormat.Svg));
             }
         }
         else
         {
             return(string.Format(CultureInfo.InvariantCulture, "<html><body>Invalid team: <br>{0}</body></html>", rawUrl));
         }
     }
     else if (lastPart.StartsWith("fixture", StringComparison.OrdinalIgnoreCase))
     {
         return(ReportPages.FixturePage(holder.Fixture, holder.League));
     }
     else if (lastPart == "scoreboard.html")
     {
         return(ScoreboardPage(holder.League));
     }
     else
     {
         return(string.Format(CultureInfo.InvariantCulture, "<html><body>Invalid path: <br>{0}</body></html>", rawUrl));
     }
 }
Esempio n. 2
0
        string SendResponse(HttpListenerRequest request)
        {
            try
            {
                string[] urlParts = request.RawUrl.Split('/');
                string   lastPart = urlParts.Length > 0? urlParts[urlParts.Length - 1] : null;
                Holder   holder   = null;

                if (Leagues.Count == 1)
                {
                    holder = Leagues.First();
                }
                else if (urlParts.Length > 1)
                {
                    holder = Leagues.Find(x => x.Key == urlParts[1]);
                }

                if (request.RawUrl == "/")
                {
                    if (Leagues.Count == 1)
                    {
                        return(ReportPages.OverviewPage(holder, false, ReportPages.GameHyper, OutputFormat.Svg));
                    }
                    else
                    {
                        return(ReportPages.RootPage(Leagues));
                    }
                }
                else if (lastPart.EndsWith(".html"))
                {
                    return(HtmlResponse(request.RawUrl, lastPart, holder));
                }
                else
                {
                    return(RestResponse(request));
                }
            }
            catch (Exception ex)
            {
                return("<html><body>\n" + ex.Message + "\n<br/><br/>\n" + ex.StackTrace + "</body></html>");

                throw;
            }
        }
Esempio n. 3
0
        /// <summary>Generate reports for the selected leagues, and write them to disk.</summary>
        public static void ExportReports(string path, bool includeSecret, List <Holder> selected, Progress progress = null)
        {
            if (path != null)
            {
                int    denominator = selected.Count * 4 + 1;
                double numerator   = 0.0;
                if (progress == null)
                {
                    progress = DummyProgress;
                }

                if (selected.Any())
                {
                    using (StreamWriter sw = File.CreateText(Path.Combine(path, "index." + selected[0].ReportTemplates.OutputFormat.ToExtension())))
                        sw.Write(ReportPages.RootPage(selected));
                }
                progress(++numerator / denominator, "Root page exported.");

                foreach (Holder holder in selected)
                {
                    Directory.CreateDirectory(Path.Combine(path, holder.Key));

                    using (StreamWriter sw = File.CreateText(Path.Combine(path, holder.Key, "index." + holder.ReportTemplates.OutputFormat.ToExtension())))
                        sw.Write(ReportPages.OverviewPage(holder, includeSecret, ReportPages.GameHyper, holder.ReportTemplates.OutputFormat));
                    progress(++numerator / denominator, "Overview page exported.");

                    ExportPlayers(holder, path);
                    progress(++numerator / denominator, "Players pages exported.");

                    foreach (LeagueTeam leagueTeam in holder.League.Teams)
                    {
                        using (StreamWriter sw = File.CreateText(Path.Combine(path, holder.Key, "team" + leagueTeam.TeamId.ToString("D2", CultureInfo.InvariantCulture) + "." + holder.ReportTemplates.OutputFormat.ToExtension())))
                            sw.Write(ReportPages.TeamPage(holder.League, includeSecret, leagueTeam, ReportPages.GameHyper, holder.ReportTemplates.OutputFormat));
                    }
                    progress(++numerator / denominator, "Team pages exported.");

                    ExportGames(holder, path);
                    progress(++numerator / denominator, "Games pages exported.");
                }
            }
        }
Esempio n. 4
0
        /// <summary>Generate reports for the selected leagues, and write them to disk.</summary>
        public static void ExportReports(string path, bool includeSecret, List <Holder> selected, ShowProgress progress = null)
        {
            if (path != null)
            {
                Progress myProgress = new Progress()
                {
                    Denominator = selected.Count * 3 + 1, ShowProgress = progress
                };

                if (selected.Any())
                {
                    using (StreamWriter sw = File.CreateText(Path.Combine(path, "index." + selected[0].ReportTemplates.OutputFormat.ToExtension())))
                        sw.Write(ReportPages.RootPage(selected));
                }
                myProgress.Increment("Root page exported.");

                foreach (Holder holder in selected)
                {
                    Directory.CreateDirectory(Path.Combine(path, holder.Key));

                    using (StreamWriter sw = File.CreateText(Path.Combine(path, holder.Key, "index." + holder.ReportTemplates.OutputFormat.ToExtension())))
                        sw.Write(ReportPages.OverviewPage(holder, includeSecret, holder.ReportTemplates.OutputFormat));
                    myProgress.Increment(holder.League.Title + " Overview page exported.");

                    foreach (LeagueTeam leagueTeam in holder.League.Teams)
                    {
                        using (StreamWriter sw = File.CreateText(Path.Combine(path, holder.Key, "team" + leagueTeam.TeamId.ToString("D2", CultureInfo.InvariantCulture) + "." + holder.ReportTemplates.OutputFormat.ToExtension())))
                            sw.Write(ReportPages.TeamPage(holder.League, includeSecret, leagueTeam, holder.ReportTemplates.OutputFormat));
                        myProgress.Advance(1 / holder.League.Teams.Count, "Team " + leagueTeam.Name + " page exported.");
                    }
                    myProgress.Advance(0, "Team pages exported.");

                    ExportGames(holder, path, myProgress);
                    myProgress.Advance(0, "Games pages exported.");
                }
            }
        }