From ccbbb62bcc04a7aeffd8a1f02805f0723fdeec31 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 14 Mar 2021 10:38:49 +1100 Subject: [PATCH] Move some methods from PlayerInfo to PlayerDB and add a few more comments --- MCGalaxy/Commands/Information/CmdBanInfo.cs | 3 +- MCGalaxy/Commands/Information/CmdMapInfo.cs | 4 +- MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs | 2 +- MCGalaxy/Commands/Moderation/ModActionCmd.cs | 4 +- MCGalaxy/CorePlugin/ModActionHandler.cs | 3 +- MCGalaxy/Database/PlayerDB.cs | 24 +++++++++++ MCGalaxy/Player/PlayerInfo.cs | 43 ++++++-------------- 7 files changed, 45 insertions(+), 38 deletions(-) diff --git a/MCGalaxy/Commands/Information/CmdBanInfo.cs b/MCGalaxy/Commands/Information/CmdBanInfo.cs index 80cbb7b13..959069765 100644 --- a/MCGalaxy/Commands/Information/CmdBanInfo.cs +++ b/MCGalaxy/Commands/Information/CmdBanInfo.cs @@ -16,6 +16,7 @@ permissions and limitations under the Licenses. */ using System; +using MCGalaxy.DB; namespace MCGalaxy.Commands.Info { public sealed class CmdBanInfo : Command2 { @@ -41,7 +42,7 @@ namespace MCGalaxy.Commands.Info { bool permaBanned = Group.BannedRank.Players.Contains(plName); bool isBanned = permaBanned || tempExpiry >= DateTime.UtcNow; string msg = nick; - string ip = PlayerInfo.FindIP(plName); + string ip = PlayerDB.FindIP(plName); bool ipBanned = ip != null && Server.bannedIP.Contains(ip); if (!ipBanned && isBanned) msg += " &Sis &CBANNED"; diff --git a/MCGalaxy/Commands/Information/CmdMapInfo.cs b/MCGalaxy/Commands/Information/CmdMapInfo.cs index cd68c60ed..be4795a8b 100644 --- a/MCGalaxy/Commands/Information/CmdMapInfo.cs +++ b/MCGalaxy/Commands/Information/CmdMapInfo.cs @@ -133,12 +133,12 @@ namespace MCGalaxy.Commands.Info { while (map.Length > 0 && Char.IsNumber(map[map.Length - 1])) { // If the server does not have account with +, we have to account for the // that say Player123's second level is Player1232, and the realm owner is Player123 - name = plus ? null : PlayerInfo.FindName(map); + name = plus ? null : PlayerDB.FindName(map); if (name != null) break; map = map.Substring(0, map.Length - 1); } - if (name == null) name = PlayerInfo.FindName(map); + if (name == null) name = PlayerDB.FindName(map); if (name != null && !LevelInfo.IsRealmOwner(name, origMap)) return null; return name; } diff --git a/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs b/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs index 67eaa1b33..d037537fd 100644 --- a/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs +++ b/MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs @@ -51,7 +51,7 @@ namespace MCGalaxy.Commands.Maintenance { p.Message("\"{0}\" must be offline to use &T/InfoSwap", name); return null; } - string match = PlayerInfo.FindName(name); + string match = PlayerDB.FindName(name); if (match == null) { p.Message("\"{0}\" was not found in the database.", name); return null; } diff --git a/MCGalaxy/Commands/Moderation/ModActionCmd.cs b/MCGalaxy/Commands/Moderation/ModActionCmd.cs index c99ce9dde..4a6438a2e 100644 --- a/MCGalaxy/Commands/Moderation/ModActionCmd.cs +++ b/MCGalaxy/Commands/Moderation/ModActionCmd.cs @@ -195,7 +195,7 @@ namespace MCGalaxy.Commands.Moderation { // TryParse returns "0.0.0.123" for "123", we do not want that behaviour if (IPAddress.TryParse(message, out ip) && message.Split('.').Length == 4) { string account = Server.Config.ClassicubeAccountPlus ? message + "+" : message; - if (PlayerInfo.FindName(account) == null) return message; + if (PlayerDB.FindName(account) == null) return message; // Some classicube.net accounts can be parsed as valid IPs, so warn in this case. p.Message("Note: \"{0}\" is both an IP and an account name. " @@ -209,7 +209,7 @@ namespace MCGalaxy.Commands.Moderation { p.Message("Searching PlayerDB.."); string dbIP; - name = PlayerInfo.FindOfflineIPMatches(p, message, out dbIP); + name = PlayerDB.FindOfflineIPMatches(p, message, out dbIP); return dbIP; } } diff --git a/MCGalaxy/CorePlugin/ModActionHandler.cs b/MCGalaxy/CorePlugin/ModActionHandler.cs index 4e3d9f078..05c227ca1 100644 --- a/MCGalaxy/CorePlugin/ModActionHandler.cs +++ b/MCGalaxy/CorePlugin/ModActionHandler.cs @@ -19,6 +19,7 @@ using System; using MCGalaxy.Commands; using MCGalaxy.Commands.Moderation; +using MCGalaxy.DB; using MCGalaxy.Events; using MCGalaxy.Tasks; @@ -139,7 +140,7 @@ namespace MCGalaxy.Core { Ban.UnbanPlayer(e.Actor, e.Target, e.Reason); ModActionCmd.ChangeRank(e.Target, Group.BannedRank, Group.DefaultRank, who, false); - string ip = PlayerInfo.FindIP(e.Target); + string ip = PlayerDB.FindIP(e.Target); if (ip != null && Server.bannedIP.Contains(ip)) { e.Actor.Message("NOTE: Their IP is still banned."); } diff --git a/MCGalaxy/Database/PlayerDB.cs b/MCGalaxy/Database/PlayerDB.cs index c4aba0b69..6561ef78a 100644 --- a/MCGalaxy/Database/PlayerDB.cs +++ b/MCGalaxy/Database/PlayerDB.cs @@ -92,6 +92,30 @@ namespace MCGalaxy.DB { } + public static PlayerData FindData(string name) { + string suffix = Database.Backend.CaselessWhereSuffix; + object raw = Database.ReadRows("Players", "*", null, PlayerData.Read, + "WHERE Name=@0" + suffix, name); + return (PlayerData)raw; + } + + public static string FindName(string name) { + string suffix = Database.Backend.CaselessWhereSuffix; + return Database.ReadString("Players", "Name", "WHERE Name=@0" + suffix, name); + } + + public static string FindIP(string name) { + string suffix = Database.Backend.CaselessWhereSuffix; + return Database.ReadString("Players", "IP", "WHERE Name=@0" + suffix, name); + } + + public static string FindOfflineIPMatches(Player p, string name, out string ip) { + string[] match = PlayerDB.MatchValues(p, name, "Name,IP"); + ip = match == null ? null : match[1]; + return match == null ? null : match[0]; + } + + public static void Update(string name, string column, string value) { Database.UpdateRows("Players", column + "=@1", "WHERE Name=@0", name, value); } diff --git a/MCGalaxy/Player/PlayerInfo.cs b/MCGalaxy/Player/PlayerInfo.cs index 5038b8b4f..809d990a6 100644 --- a/MCGalaxy/Player/PlayerInfo.cs +++ b/MCGalaxy/Player/PlayerInfo.cs @@ -40,6 +40,7 @@ namespace MCGalaxy { return col.Length > 0 ? col : p.group.Color; } + /// Returns the number of non-hidden players that are currently online public static int NonHiddenCount() { Player[] players = Online.Items; int count = 0; @@ -56,23 +57,27 @@ namespace MCGalaxy { } return uniqueIPs.Count; } + // TODO: remove _useless parameter. but this breaks backwards compatibility with plugins - public static Player FindMatches(Player pl, string name, bool onlyCanSee = true) { - int matches; return FindMatches(pl, name, out matches, onlyCanSee); + /// Matches given name against the names of all online players that the given player can see + /// A Player instance if exactly one match was found + public static Player FindMatches(Player pl, string name, bool _useless = false) { + int matches; return FindMatches(pl, name, out matches); } - public static Player FindMatches(Player pl, string name, - out int matches, bool onlyCanSee = true) { + /// Matches given name against the names of all online players that the given player can see + /// Outputs the number of matching players + /// A Player instance if exactly one match was found + public static Player FindMatches(Player pl, string name, out int matches, bool _useless = false) { matches = 0; if (!Formatter.ValidName(pl, name, "player")) return null; // Try to exactly match name first (because names have + at end) Player exact = FindExact(name); - if (exact != null) { matches = 1; return exact; } + if (exact != null && pl.CanSee(exact)) { matches = 1; return exact; } return Matcher.Find(pl, name, out matches, Online.Items, - p => pl.CanSee(p) || !onlyCanSee, - p => p.name, "online players"); + p => pl.CanSee(p), p => p.name, "online players"); } public static string FindMatchesPreferOnline(Player p, string name) { @@ -97,31 +102,7 @@ namespace MCGalaxy { } return null; } - - - public static PlayerData FindData(string name) { - string suffix = Database.Backend.CaselessWhereSuffix; - object raw = Database.ReadRows("Players", "*", null, PlayerData.Read, - "WHERE Name=@0" + suffix, name); - return (PlayerData)raw; - } - public static string FindName(string name) { - string suffix = Database.Backend.CaselessWhereSuffix; - return Database.ReadString("Players", "Name", "WHERE Name=@0" + suffix, name); - } - - public static string FindIP(string name) { - string suffix = Database.Backend.CaselessWhereSuffix; - return Database.ReadString("Players", "IP", "WHERE Name=@0" + suffix, name); - } - - public static string FindOfflineIPMatches(Player p, string name, out string ip) { - string[] match = PlayerDB.MatchValues(p, name, "Name,IP"); - ip = match == null ? null : match[1]; - return match == null ? null : match[0]; - } - static object ReadAccounts(IDataRecord record, object arg) { List names = (List)arg;