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); }
/// <summary> /// Fills the DataGridView with a list of players /// </summary> private void BuildList() { // Define initial variables int Limit = Int32.Parse(LimitSelect.SelectedItem.ToString()); string Like = SearchBox.Text.Replace("'", "").Trim(); List<Dictionary<string, object>> Rows; WhereClause Where = null; // Start Record int Start = (ListPage == 1) ? 0 : (ListPage - 1) * Limit; // Build Query SelectQueryBuilder Query = new SelectQueryBuilder(Driver); Query.SelectColumns("id", "name", "clantag", "rank", "score", "country", "permban"); Query.SelectFromTable("player"); Query.AddOrderBy(SortedCol.Name, ((SortDir == ListSortDirection.Ascending) ? Sorting.Ascending : Sorting.Descending)); Query.Limit(Limit, Start); // User entered search if (!String.IsNullOrWhiteSpace(Like)) Where = Query.AddWhere("name", Comparison.Like, "%" + Like + "%"); // Clear out old junk DataTable.Rows.Clear(); // Add players to data grid int RowCount = 0; foreach (Dictionary<string, object> Row in Driver.QueryReader(Query.BuildCommand())) { DataTable.Rows.Add(new object[] { Image.FromStream(Me.GetManifestResourceStream("BF2Statistics.Resources.rank_" + Row["rank"].ToString() + "icon.gif")), Row["id"].ToString(), Row["name"].ToString(), Row["clantag"].ToString(), Row["score"].ToString(), Row["country"].ToString(), Row["permban"].ToString(), }); RowCount++; } // Get Filtered Rows Query = new SelectQueryBuilder(Driver); Query.SelectCount(); Query.SelectFromTable("player"); if (Where != null) Query.AddWhere(Where); Rows = Driver.ExecuteReader(Query.BuildCommand()); int TotalFilteredRows = Int32.Parse(Rows[0]["count"].ToString()); // Get Total Player Count Query = new SelectQueryBuilder(Driver); Query.SelectCount(); Query.SelectFromTable("player"); Rows = Driver.ExecuteReader(Query.BuildCommand()); int TotalRows = Int32.Parse(Rows[0]["count"].ToString()); // Stop Count int Stop = (ListPage == 1) ? RowCount : ((ListPage - 1) * Limit) + RowCount; // First / Previous button if (ListPage == 1) { FirstBtn.Enabled = false; PreviousBtn.Enabled = false; } else { FirstBtn.Enabled = true; PreviousBtn.Enabled = true; } // Next / Last Button LastBtn.Enabled = false; NextBtn.Enabled = false; // Get total number of pages if (TotalFilteredRows / (ListPage * Limit) > 0) { float total = float.Parse(TotalFilteredRows.ToString()) / float.Parse(Limit.ToString()); TotalPages = Int32.Parse(Math.Floor(total).ToString()); if (TotalFilteredRows % Limit != 0) TotalPages++; LastBtn.Enabled = true; NextBtn.Enabled = true; } // Set page number PageNumber.Maximum = TotalPages; PageNumber.Value = ListPage; // Update Row Count Information RowCountDesc.Text = String.Format( "Showing {0} to {1} of {2} player{3}", ++Start, Stop, TotalFilteredRows, ((TotalFilteredRows > 1) ? "s " : " ") ); // Add Total row count if (!String.IsNullOrWhiteSpace(Like)) RowCountDesc.Text += String.Format("(filtered from " + TotalRows + " total player{0})", ((TotalRows > 1) ? "s" : "")); // Update DataTable.Update(); }
/// <summary> /// Fills the DataGridView with a list of accounts /// </summary> private void BuildList() { // Define initial variables int Limit = Int32.Parse(LimitSelect.SelectedItem.ToString()); string Like = SearchBox.Text.Replace("'", "").Trim(); List<Dictionary<string, object>> Rows; WhereClause Where = null; // Start Record int Start = (ListPage == 1) ? 0 : (ListPage - 1) * Limit; // Build Query SelectQueryBuilder Query = new SelectQueryBuilder(Driver); Query.SelectColumns("id", "name", "email", "country", "lastip", "session"); Query.SelectFromTable("accounts"); Query.AddOrderBy(SortedCol.Name, ((SortDir == ListSortDirection.Ascending) ? Sorting.Ascending : Sorting.Descending)); Query.Limit(Limit, Start); // User entered search if (!String.IsNullOrWhiteSpace(Like)) Where = Query.AddWhere("name", Comparison.Like, "%" + Like + "%"); // Online Accounts if (OnlineAccountsCheckBox.Checked) { if (Where == null) Where = Query.AddWhere("session", Comparison.NotEqualTo, 0); else Where.AddClause(LogicOperator.And, "session", Comparison.NotEqualTo, 0); } // Clear out old junk DataTable.Rows.Clear(); // Add players to data grid int RowCount = 0; foreach (Dictionary<string, object> Row in Driver.QueryReader(Query.BuildCommand())) { DataTable.Rows.Add(new string[] { Row["id"].ToString(), Row["name"].ToString(), Row["email"].ToString(), Row["country"].ToString(), ((Row["session"].ToString() == "1") ? "Yes" : "No"), Row["lastip"].ToString(), }); RowCount++; } // Get Filtered Rows Query = new SelectQueryBuilder(Driver); Query.SelectCount(); Query.SelectFromTable("accounts"); if (Where != null) Query.AddWhere(Where); Rows = Driver.ExecuteReader(Query.BuildCommand()); int TotalFilteredRows = Int32.Parse(Rows[0]["count"].ToString()); // Get Total Player Count Query = new SelectQueryBuilder(Driver); Query.SelectCount(); Query.SelectFromTable("accounts"); Rows = Driver.ExecuteReader(Query.BuildCommand()); int TotalRows = Int32.Parse(Rows[0]["count"].ToString()); // Stop Count int Stop = (ListPage == 1) ? RowCount : ((ListPage - 1) * Limit) + RowCount; // First / Previous button if (ListPage == 1) { FirstBtn.Enabled = false; PreviousBtn.Enabled = false; } else { FirstBtn.Enabled = true; PreviousBtn.Enabled = true; } // Next / Last Button LastBtn.Enabled = false; NextBtn.Enabled = false; // Get total number of pages if (TotalFilteredRows / (ListPage * Limit) > 0) { float total = float.Parse(TotalFilteredRows.ToString()) / float.Parse(Limit.ToString()); TotalPages = Int32.Parse(Math.Floor(total).ToString()); if (TotalFilteredRows % Limit != 0) TotalPages++; LastBtn.Enabled = true; NextBtn.Enabled = true; } // Set page number PageNumber.Maximum = TotalPages; PageNumber.Value = ListPage; // Update Row Count Information RowCountDesc.Text = String.Format( "Showing {0} to {1} of {2} account{3}", ++Start, Stop, TotalFilteredRows, ((TotalFilteredRows > 1) ? "s " : " ") ); // Add Total row count if (!String.IsNullOrWhiteSpace(Like)) RowCountDesc.Text += String.Format("(filtered from " + TotalRows + " total account{0})", ((TotalRows > 1) ? "s" : "")); // Update DataTable.Update(); }