mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 12:05:51 -04:00
Reduce code duplication in PlayerInfo class.
This commit is contained in:
parent
0e6ee0c8de
commit
445e263816
@ -75,7 +75,9 @@ namespace MCGalaxy.Commands.Moderation {
|
||||
if (who != null) return who.ip;
|
||||
|
||||
Player.Message(p, "Searching PlayerDB..");
|
||||
return PlayerInfo.FindOfflineIPMatches(p, message);
|
||||
string databaseIP;
|
||||
PlayerInfo.FindOfflineIPMatches(p, message, out databaseIP);
|
||||
return databaseIP;
|
||||
}
|
||||
|
||||
static bool CheckIP(Player p, string ip) {
|
||||
|
@ -35,20 +35,20 @@ namespace MCGalaxy.Commands {
|
||||
public override void Use(Player p, string message) {
|
||||
string ip = "";
|
||||
Player who = PlayerInfo.Find(message);
|
||||
string targetName = message;
|
||||
|
||||
if (who == null) {
|
||||
Player.Message(p, "&eNo online player \"{0}\", searching database..", message);
|
||||
string targetIP = PlayerInfo.FindOfflineIPMatches(p, message);
|
||||
if (targetIP == null) return;
|
||||
ip = targetIP;
|
||||
targetName = PlayerInfo.FindOfflineIPMatches(p, message, out ip);
|
||||
if (targetName == null) return;
|
||||
} else {
|
||||
ip = who.ip;
|
||||
targetName = who.name; ip = who.ip;
|
||||
}
|
||||
|
||||
if (Player.IPInPrivateRange(ip)) {
|
||||
Player.Message(p, Colors.red + "Player has an internal IP, cannot trace"); return;
|
||||
}
|
||||
string name = who != null ? who.name : message;
|
||||
Player.Message(p, "&aThe IP of &b" + name + " &ahas been traced to: &b" + GetIPLocation(ip));
|
||||
Player.Message(p, "The IP of &a" + targetName + " %Shas been traced to: &b" + GetIPLocation(ip));
|
||||
}
|
||||
|
||||
static string GetIPLocation(string IP) {
|
||||
|
@ -109,11 +109,11 @@ namespace MCGalaxy {
|
||||
}
|
||||
|
||||
|
||||
static long ParseLong(string value) {
|
||||
internal static long ParseLong(string value) {
|
||||
return (value == "" || value.CaselessEq("null")) ? 0 : long.Parse(value);
|
||||
}
|
||||
|
||||
static int ParseInt(string value) {
|
||||
internal static int ParseInt(string value) {
|
||||
return (value == "" || value.CaselessEq("null")) ? 0 : int.Parse(value);
|
||||
}
|
||||
|
||||
|
@ -123,30 +123,25 @@ namespace MCGalaxy {
|
||||
|
||||
|
||||
public static PlayerData FindOfflineMatches(Player p, string name) {
|
||||
using (DataTable results = QueryMulti(name, "*")) {
|
||||
int matches = 0;
|
||||
DataRow row = Utils.FindMatches<DataRow>(p, name, out matches, results.Rows,
|
||||
r => true, r => r["Name"].ToString(), "players", 20);
|
||||
DataRow row = QueryMulti(p, name, "*");
|
||||
return row == null ? null : PlayerData.Fill(row);
|
||||
}
|
||||
}
|
||||
|
||||
public static string FindOfflineNameMatches(Player p, string name) {
|
||||
using (DataTable results = QueryMulti(name, "Name")) {
|
||||
int matches = 0;
|
||||
DataRow row = Utils.FindMatches<DataRow>(p, name, out matches, results.Rows,
|
||||
r => true, r => r["Name"].ToString(), "players", 20);
|
||||
DataRow row = QueryMulti(p, name, "Name");
|
||||
return row == null ? null : row["Name"].ToString();
|
||||
}
|
||||
|
||||
public static string FindOfflineIPMatches(Player p, string name, out string ip) {
|
||||
DataRow row = QueryMulti(p, name, "Name, IP");
|
||||
ip = row == null ? null : row["IP"].ToString();
|
||||
return row == null ? null : row["Name"].ToString();
|
||||
}
|
||||
|
||||
public static string FindOfflineIPMatches(Player p, string name) {
|
||||
using (DataTable results = QueryMulti(name, "Name, IP")) {
|
||||
int matches = 0;
|
||||
DataRow row = Utils.FindMatches<DataRow>(p, name, out matches, results.Rows,
|
||||
r => true, r => r["Name"].ToString(), "players", 20);
|
||||
return row == null ? null : row["IP"].ToString();
|
||||
}
|
||||
public static string FindOfflineMoneyMatches(Player p, string name, out int money) {
|
||||
DataRow row = QueryMulti(p, name, "Name, Money");
|
||||
money = row == null ? 0 : PlayerData.ParseInt(row["Money"].ToString());
|
||||
return row == null ? null : row["Name"].ToString();
|
||||
}
|
||||
|
||||
/// <summary> Retrieves from the database the names of all players whose
|
||||
@ -172,11 +167,16 @@ namespace MCGalaxy {
|
||||
return Database.Fill(syntax, name);
|
||||
}
|
||||
|
||||
static DataTable QueryMulti(string name, string selector) {
|
||||
static DataRow QueryMulti(Player p, string name, string selector) {
|
||||
string syntax = Server.useMySQL ?
|
||||
"SELECT " + selector + " FROM Players WHERE Name LIKE @0 LIMIT 21" :
|
||||
"SELECT " + selector + " FROM Players WHERE Name LIKE @0 LIMIT 21 COLLATE NOCASE";
|
||||
return Database.Fill(syntax, "%" + name + "%");
|
||||
|
||||
using (DataTable results = Database.Fill(syntax, "%" + name + "%")) {
|
||||
int matches = 0;
|
||||
return Utils.FindMatches<DataRow>(p, name, out matches, results.Rows,
|
||||
r => true, r => r["Name"].ToString(), "players", 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user