From 445e263816fc6611eee5cb17a4e6b3bc96195cc2 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 29 Aug 2016 09:31:45 +1000 Subject: [PATCH] Reduce code duplication in PlayerInfo class. --- Commands/Moderation/CmdBanip.cs | 4 ++- Commands/Moderation/CmdLocation.cs | 12 ++++---- Player/PlayerData.cs | 4 +-- Player/PlayerInfo.cs | 44 +++++++++++++++--------------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/Commands/Moderation/CmdBanip.cs b/Commands/Moderation/CmdBanip.cs index 03b8321d2..623f98f46 100644 --- a/Commands/Moderation/CmdBanip.cs +++ b/Commands/Moderation/CmdBanip.cs @@ -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) { diff --git a/Commands/Moderation/CmdLocation.cs b/Commands/Moderation/CmdLocation.cs index 2fe9e0ef8..f965bec12 100644 --- a/Commands/Moderation/CmdLocation.cs +++ b/Commands/Moderation/CmdLocation.cs @@ -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) { diff --git a/Player/PlayerData.cs b/Player/PlayerData.cs index d7a2212e7..272b8b794 100644 --- a/Player/PlayerData.cs +++ b/Player/PlayerData.cs @@ -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); } diff --git a/Player/PlayerInfo.cs b/Player/PlayerInfo.cs index c85f34fce..218e806f9 100644 --- a/Player/PlayerInfo.cs +++ b/Player/PlayerInfo.cs @@ -20,7 +20,7 @@ using MCGalaxy.SQL; namespace MCGalaxy { public static class PlayerInfo { - + /// Array of all currently online players. /// Note this field is highly volatile, you should cache references to the items array. public static VolatileArray Online = new VolatileArray(true); @@ -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(p, name, out matches, results.Rows, - r => true, r => r["Name"].ToString(), "players", 20); - return row == null ? null : PlayerData.Fill(row); - } + 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(p, name, out matches, results.Rows, - r => true, r => r["Name"].ToString(), "players", 20); - return row == null ? null : row["Name"].ToString(); - } + DataRow row = QueryMulti(p, name, "Name"); + 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(p, name, out matches, results.Rows, - r => true, r => r["Name"].ToString(), "players", 20); - return row == null ? null : row["IP"].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 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(); } /// 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(p, name, out matches, results.Rows, + r => true, r => r["Name"].ToString(), "players", 20); + } } } }