Inheritance: BF2PageModel
        private void ShowRankingType(MvcRoute Route)
        {
            // Create our model
            RankingsTypeModel Model = new RankingsTypeModel(Client);
            Model.UrlName = Route.Action;
            string CacheName = $"rankings_{Route.Action}_1";

            // Parse our country and page filters based on URL
            // Url formats:
            // - scoreType/country/pageNumber
            // - scoreType/pageNumber
            if (Route.Params.Length == 1)
            {
                if (Int32.TryParse(Route.Params[0], out Model.CurrentPage))
                {
                    // Just a page number provided
                    CacheName = $"rankings_{Route.Action}_{Model.CurrentPage}";
                }
                else if (Route.Params[0].Length == 2)
                {
                    // Just a country code provided, default to page 1
                    Model.Country = Route.Params[0];
                    CacheName = $"rankings_{Route.Action}_{Model.Country}_1";
                }
            }
            else if (Route.Params.Length == 2 && Int32.TryParse(Route.Params[1], out Model.CurrentPage))
            {
                if (Route.Params[0].Length == 2) // Check valid country code
                {
                    Model.Country = Route.Params[0];
                    CacheName = $"rankings_{Route.Action}_{Model.Country}_{Model.CurrentPage}";
                }
                else
                    CacheName = $"rankings_{Route.Action}_{Model.CurrentPage}";
            }

            // Check the cache file
            if (!base.CacheFileExpired(CacheName, 30))
            {
                base.SendCachedResponse(CacheName);
                return;
            }

            // NOTE: The HttpServer will handle the DbConnectException
            using (StatsDatabase Database = new StatsDatabase())
            {
                // Get our DISTINCT country list from our player pool
                SelectQueryBuilder builder = new SelectQueryBuilder(Database);
                builder.SelectColumn("country");
                builder.Distinct = true;
                builder.SelectFromTable("player");
                builder.AddWhere("country", Comparison.NotEqualTo, "xx");
                foreach (var Row in builder.ExecuteQuery())
                    Model.CountryList.Add(Row["country"].ToString());

                // Start building our player query
                builder = new SelectQueryBuilder(Database);
                builder.SelectCount();
                builder.SelectFromTable("player");
                WhereClause Where = builder.AddWhere("score", Comparison.GreaterOrEquals, 1);

                // Add country filter
                if (Model.Country.Length == 2)
                    Where.AddClause(LogicOperator.And, "country", Comparison.Equals, Model.Country);

                // Hpd additional Where
                if (Route.Action.Equals("hpd", StringComparison.InvariantCultureIgnoreCase))
                    Where.AddClause(LogicOperator.And, "time", Comparison.GreaterOrEquals, 3600);

                // Get our total records
                Model.TotalRecords = builder.ExecuteScalar<int>();
                Model.TotalPages = 1 + (Model.TotalRecords / PlayersPerPage);
                Model.ScoreHeader = GetHeaderName(Route.Action);

                // Now, Build Query that will select the players, not just the count
                bool isDecimal = false;
                FinishQuery(Route.Action, builder, out isDecimal);

                // Get our players, limiting to 50 and starting by page
                builder.Limit(PlayersPerPage, (Model.CurrentPage * PlayersPerPage) - PlayersPerPage);
                var Rows = builder.ExecuteQuery();

                // Initialize records based on records returned from Database
                Model.Records = new List<RankingsTypeModel.PlayerRow>(Rows.Count);
                foreach (Dictionary<string, object> Player in Rows)
                {
                    Model.Records.Add(new RankingsTypeModel.PlayerRow()
                    {
                        Pid = Int32.Parse(Player["pid"].ToString()),
                        Name = Player["name"].ToString(),
                        Rank = Int32.Parse(Player["rank"].ToString()),
                        Country = Player["country"].ToString(),
                        Time = Int32.Parse(Player["time"].ToString()),
                        ScorePerMin = Double.Parse(Player["spm"].ToString()),
                        KillDeathRatio = Double.Parse(Player["kdr"].ToString()),
                        WinLossRatio = Double.Parse(Player["wlr"].ToString()),
                        ScoreValue = (isDecimal)
                            ? String.Format(CultureInfo.InvariantCulture, "{0:n4}", Player["value"])
                            : String.Format(CultureInfo.InvariantCulture, "{0:n0}", Player["value"])
                    });
                }
            }

            // Send response
            base.SendTemplateResponse("rankings_type", typeof(RankingsTypeModel), Model, CacheName);
        }
Beispiel #2
0
        private void ShowRankingType(MvcRoute Route)
        {
            // Create our model
            RankingsTypeModel Model = new RankingsTypeModel(Client);

            Model.UrlName = Route.Action;
            string CacheName = $"rankings_{Route.Action}_1";

            // Parse our country and page filters based on URL
            // Url formats:
            // - scoreType/country/pageNumber
            // - scoreType/pageNumber
            if (Route.Params.Length == 1)
            {
                if (Int32.TryParse(Route.Params[0], out Model.CurrentPage))
                {
                    // Just a page number provided
                    CacheName = $"rankings_{Route.Action}_{Model.CurrentPage}";
                }
                else if (Route.Params[0].Length == 2)
                {
                    // Just a country code provided, default to page 1
                    Model.Country = Route.Params[0];
                    CacheName     = $"rankings_{Route.Action}_{Model.Country}_1";
                }
            }
            else if (Route.Params.Length == 2 && Int32.TryParse(Route.Params[1], out Model.CurrentPage))
            {
                if (Route.Params[0].Length == 2) // Check valid country code
                {
                    Model.Country = Route.Params[0];
                    CacheName     = $"rankings_{Route.Action}_{Model.Country}_{Model.CurrentPage}";
                }
                else
                {
                    CacheName = $"rankings_{Route.Action}_{Model.CurrentPage}";
                }
            }

            // Check the cache file
            if (!base.CacheFileExpired(CacheName, 30))
            {
                base.SendCachedResponse(CacheName);
                return;
            }

            // NOTE: The HttpServer will handle the DbConnectException
            using (StatsDatabase Database = new StatsDatabase())
            {
                // Get our DISTINCT country list from our player pool
                SelectQueryBuilder builder = new SelectQueryBuilder(Database);
                builder.SelectColumn("country");
                builder.Distinct = true;
                builder.SelectFromTable("player");
                builder.AddWhere("country", Comparison.NotEqualTo, "xx");
                foreach (var Row in builder.ExecuteQuery())
                {
                    Model.CountryList.Add(Row["country"].ToString());
                }

                // Start building our player query
                builder = new SelectQueryBuilder(Database);
                builder.SelectCount();
                builder.SelectFromTable("player");
                WhereClause Where = builder.AddWhere("score", Comparison.GreaterOrEquals, 1);

                // Add country filter
                if (Model.Country.Length == 2)
                {
                    Where.AddClause(LogicOperator.And, "country", Comparison.Equals, Model.Country);
                }

                // Hpd additional Where
                if (Route.Action.Equals("hpd", StringComparison.InvariantCultureIgnoreCase))
                {
                    Where.AddClause(LogicOperator.And, "time", Comparison.GreaterOrEquals, 3600);
                }

                // Get our total records
                Model.TotalRecords = builder.ExecuteScalar <int>();
                Model.TotalPages   = 1 + (Model.TotalRecords / PlayersPerPage);
                Model.ScoreHeader  = GetHeaderName(Route.Action);

                // Now, Build Query that will select the players, not just the count
                bool isDecimal = false;
                FinishQuery(Route.Action, builder, out isDecimal);

                // Get our players, limiting to 50 and starting by page
                builder.Limit(PlayersPerPage, (Model.CurrentPage * PlayersPerPage) - PlayersPerPage);
                var Rows = builder.ExecuteQuery();

                // Initialize records based on records returned from Database
                Model.Records = new List <RankingsTypeModel.PlayerRow>(Rows.Count);
                foreach (Dictionary <string, object> Player in Rows)
                {
                    Model.Records.Add(new RankingsTypeModel.PlayerRow()
                    {
                        Pid            = Int32.Parse(Player["pid"].ToString()),
                        Name           = Player["name"].ToString(),
                        Rank           = Int32.Parse(Player["rank"].ToString()),
                        Country        = Player["country"].ToString(),
                        Time           = Int32.Parse(Player["time"].ToString()),
                        ScorePerMin    = Double.Parse(Player["spm"].ToString()),
                        KillDeathRatio = Double.Parse(Player["kdr"].ToString()),
                        WinLossRatio   = Double.Parse(Player["wlr"].ToString()),
                        ScoreValue     = (isDecimal)
                            ? String.Format(CultureInfo.InvariantCulture, "{0:n4}", Player["value"])
                            : String.Format(CultureInfo.InvariantCulture, "{0:n0}", Player["value"])
                    });
                }
            }

            // Send response
            base.SendTemplateResponse("rankings_type", typeof(RankingsTypeModel), Model, CacheName);
        }