/// <summary>
        /// Loads the players stats from the database, and fills out the forms
        /// labels with the current information
        /// </summary>
        private void LoadPlayer()
        {
            StatsDatabase Driver;

            // Establish DB connection
            try
            {
                Driver = new StatsDatabase();
            }
            catch (DbConnectException Ex)
            {
                ExceptionForm.ShowDbConnectError(Ex);
                HttpServer.Stop();
                Load += (s, e) => Close(); // Close form
                return;
            }

            // Fetch Player from database
            SelectQueryBuilder Builder = new SelectQueryBuilder(Driver);
            Builder.SelectFromTable("player");
            Builder.SelectColumns(
                "name", "score", "cmdscore", "skillscore", "teamscore", "joined",
                "country", "rank", "wins", "losses", "permban", "clantag", "isbot");
            Builder.AddWhere("id", Comparison.Equals, Pid);
            List<Dictionary<string, object>> Rows = Driver.ExecuteReader(Builder.BuildCommand());
            Player = Rows[0];

            // Set window title
            this.Text = String.Concat(Player["name"].ToString().Trim(), " (", Pid, ")");

            // Set country flag
            try
            {
                string Country = String.IsNullOrEmpty(Player["country"].ToString()) ? "XX" : Player["country"].ToString();
                CountryPicture.Image = Image.FromStream(Program.GetResource("BF2Statistics.Resources." + Country.ToUpper() + ".png"));
            }
            catch { }

            // Joined Label
            int Joind = Int32.Parse(Player["joined"].ToString());
            DateTime D = DateTime.UtcNow.FromUnixTimestamp(Joind);
            LabelJoined.Text = String.Concat(D.ToString("yyyy-MM-dd HH:mm"), " GMT");
            Tipsy.SetToolTip(LabelJoined, String.Concat(D.ToLocalTime().ToString("yyyy-MM-dd HH:mm"), " Local Time."));

            // Fill out the rest of the labels
            LabelNick.Text = Player["name"].ToString().Trim();
            ClanTagBox.Text = Player["clantag"].ToString();
            RankSelect.SelectedIndex = Int32.Parse(Player["rank"].ToString());
            PermBanSelect.SelectedIndex = Int32.Parse(Player["permban"].ToString());
            LabelGlobalScore.Text = Player["score"].ToString();
            LabelWinLoss.Text = String.Concat(Player["wins"], " / ", Player["losses"]);
            LabelTeamScore.Text = Player["teamscore"].ToString();
            LabelCombatScore.Text = Player["skillscore"].ToString();
            LabelCommandScore.Text = Player["cmdscore"].ToString();

            // Get Leaderboard Position
            Rows = Driver.Query("SELECT COUNT(id) as count FROM player WHERE score > @P0", Int32.Parse(Player["score"].ToString()));
            int Position = Int32.Parse(Rows[0]["count"].ToString()) + 1;
            LabelPosition.Text = Position.ToString();
            SaveBtn.Enabled = false;

            // Lock unlocks button if player is Bot
            if (Int32.Parse(Player["isbot"].ToString()) > 0)
                ResetUnlocksBtn.Enabled = false;

            // Close Connection
            Driver.Dispose();
        }
Esempio n. 2
0
        public override void HandleRequest()
        {
            int Type = 0;
            Dictionary<string, string> QueryString = Request.QueryString;

            // make sure we have a valid player ID
            if (!QueryString.ContainsKey("type") || !Int32.TryParse(QueryString["type"], out Type))
            {
                Response.WriteResponseStart(false);
                Response.WriteHeaderLine("asof", "err");
                Response.WriteDataLine(DateTime.UtcNow.ToUnixTimestamp(), "Invalid Syntax!");
                Response.Send();
                return;
            }

            // NOTE: The HttpServer will handle the DbConnectException
            using (Database = new StatsDatabase())
            {
                // Filler Variables
                int I = 0;
                float F;
                string S;
                List<DbParameter> Params = new List<DbParameter>();

                // Prepare Query
                SelectQueryBuilder Query = new SelectQueryBuilder(Database);
                Query.SelectColumns("id", "name");
                Query.SelectFromTable("player");
                Query.SetWhereOperator(LogicOperator.And);
                Query.AddWhere("ip", Comparison.NotEqualTo, "0.0.0.0");
                Query.AddOrderBy("id", Sorting.Ascending);
                WhereClause Where = null;

                switch (Type)
                {
                    // Blacklist
                    case 0:
                        int BanLimit = (QueryString.ContainsKey("banned") && Int32.TryParse(QueryString["banned"], out I)) ? I : 100;
                        Where = new WhereClause("banned", Comparison.GreaterOrEquals, BanLimit);
                        Where.AddClause(LogicOperator.Or, "permban", Comparison.Equals, 1);
                        break;
                    // Whitelist
                    case 1:
                        if (QueryString.ContainsKey("clantag"))
                        {
                            Where = new WhereClause("clantag", Comparison.Equals, QueryString["clantag"]);
                            Where.AddClause(LogicOperator.And, "permban", Comparison.Equals, 0);
                        }
                        break;
                    // Greylist
                    case 2:
                        // List of possible query's
                        string[] Queries = new string[] { "score", "rank", "time", "kdratio", "country", "banned" };
                        foreach (string Param in Queries)
                        {
                            if (QueryString.ContainsKey(Param))
                            {
                                switch (Param)
                                {
                                    case "score":
                                    case "time":
                                    case "rank":
                                        if (Int32.TryParse(QueryString[Param], out I))
                                        {
                                            if (Where == null)
                                                Where = new WhereClause(Param, Comparison.GreaterOrEquals, I);
                                            else
                                                Where.AddClause(LogicOperator.And, Param, Comparison.GreaterOrEquals, I);
                                        }
                                        break;
                                    case "kdratio":
                                        if (float.TryParse(QueryString["kdratio"], out F))
                                        {
                                            if (Where == null)
                                                Where = new WhereClause("(kills / deaths)", Comparison.GreaterOrEquals, F);
                                            else
                                                Where.AddClause(LogicOperator.And, "(kills / deaths)", Comparison.GreaterOrEquals, F);
                                        }
                                        break;
                                    case "country":
                                        S = QueryString["country"].Replace(",", "','");
                                        if (Where == null)
                                            Where = new WhereClause(Param, Comparison.In, S.Split(','));
                                        else
                                            Where.AddClause(LogicOperator.And, Param, Comparison.In, S.Split(','));
                                        break;
                                    case "banned":
                                        if (Int32.TryParse(QueryString["banned"], out I))
                                        {
                                            if (Where == null)
                                                Where = new WhereClause("banned", Comparison.LessThan, I);
                                            else
                                                Where.AddClause(LogicOperator.And, "banned", Comparison.LessThan, I);

                                            Where.AddClause(LogicOperator.And, "permban", Comparison.Equals, 0);
                                        }
                                        break;
                                }
                            }
                        }
                        break;
                }

                // Pepare 2 output headers
                int size = 0;
                FormattedOutput Output1 = new FormattedOutput("size", "asof");
                FormattedOutput Output2 = new FormattedOutput("pid", "nick");

                // Query the database, add each player to Output 2
                if (Where != null) Query.AddWhere(Where);
                List<Dictionary<string, object>> Players = Database.ExecuteReader(Query.BuildCommand());
                foreach (Dictionary<string, object> P in Players)
                {
                    size++;
                    Output2.AddRow(P["id"].ToString(), P["name"].ToString());
                }

                // Send Response
                Output1.AddRow(size, DateTime.UtcNow.ToUnixTimestamp());
                Response.AddData(Output1);
                Response.AddData(Output2);
                Response.Send();
            }
        }