/// <summary> /// Writes a header line with the items provided in the List /// </summary> /// <param name="Headers"></param> public void WriteHeaderLine(List <string> Headers) { if (Transpose) { if (Formatted != null) { ResponseBody.Append(Formatted.ToString()); } Formatted = new FormattedOutput(Headers); Formatted.Transpose = true; } else { ResponseBody.AppendFormat("\nH\t{0}", String.Join("\t", Headers)); } }
/// <summary> /// Writes a Header line with the specified parameters /// </summary> /// <param name="Params"></param> public void WriteHeaderLine(params object[] Params) { if (Transpose) { if (Formatted != null) { ResponseBody.Append(Formatted.ToString()); } Formatted = new FormattedOutput(Params); Formatted.Transpose = true; } else { ResponseBody.AppendFormat("\nH\t{0}", String.Join("\t", Params)); } }
/// <summary> /// Adds HeaderData to the current output /// </summary> /// <param name="Data"></param> public void WriteHeaderDataPair(Dictionary <string, object> Data) { if (Transpose) { if (Formatted != null) { ResponseBody.Append(Formatted.ToString()); } List <string> Params = new List <string>(Data.Count); foreach (KeyValuePair <string, object> Item in Data) { Params.Add(Item.Key); } Formatted = new FormattedOutput(Params); Formatted.Transpose = true; Params = new List <string>(Data.Count); foreach (KeyValuePair <string, object> Item in Data) { Params.Add(Item.Value.ToString()); } Formatted.AddRow(Params); } else { // Add Keys ResponseBody.Append("\nH"); foreach (KeyValuePair <string, object> Item in Data) { ResponseBody.Append("\t" + Item.Key); } // Add Data ResponseBody.Append("\nD"); foreach (KeyValuePair <string, object> Item in Data) { ResponseBody.Append("\t" + Item.Value); } } }
/// <summary> /// Adds HeaderData to the current output /// </summary> /// <param name="Data"></param> public void AddData(FormattedOutput Data) { Data.Transpose = Transpose; ResponseBody.Append(Data.ToString().Trim()); }
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(); } }
/// <summary> /// Writes a header line with the items provided in the List /// </summary> /// <param name="Headers"></param> public void WriteHeaderLine(List<string> Headers) { if (Transpose) { if (Formatted != null) ResponseBody.Append(Formatted.ToString()); Formatted = new FormattedOutput(Headers); Formatted.Transpose = true; } else ResponseBody.AppendFormat("\nH\t{0}", String.Join("\t", Headers)); }
/// <summary> /// Writes a Header line with the specified parameters /// </summary> /// <param name="Params"></param> public void WriteHeaderLine(params object[] Params) { if (Transpose) { if (Formatted != null) ResponseBody.Append(Formatted.ToString()); Formatted = new FormattedOutput(Params); Formatted.Transpose = true; } else ResponseBody.AppendFormat("\nH\t{0}", String.Join("\t", Params)); }
/// <summary> /// Adds HeaderData to the current output /// </summary> /// <param name="Data"></param> public void WriteHeaderDataPair(Dictionary<string, object> Data) { if (Transpose) { if (Formatted != null) ResponseBody.Append(Formatted.ToString()); List<string> Params = new List<string>(Data.Count); foreach (KeyValuePair<string, object> Item in Data) Params.Add(Item.Key); Formatted = new FormattedOutput(Params); Formatted.Transpose = true; Params = new List<string>(Data.Count); foreach (KeyValuePair<string, object> Item in Data) Params.Add(Item.Value.ToString()); Formatted.AddRow(Params); } else { // Add Keys ResponseBody.Append("\nH"); foreach (KeyValuePair<string, object> Item in Data) ResponseBody.Append("\t" + Item.Key); // Add Data ResponseBody.Append("\nD"); foreach (KeyValuePair<string, object> Item in Data) ResponseBody.Append("\t" + Item.Value); } }
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(); } }