Move some methods from PlayerInfo to PlayerDB and add a few more comments

This commit is contained in:
UnknownShadow200 2021-03-14 10:38:49 +11:00
parent ad64bb5ffd
commit ccbbb62bcc
7 changed files with 45 additions and 38 deletions

View File

@ -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";

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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.");
}

View File

@ -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);
}

View File

@ -40,6 +40,7 @@ namespace MCGalaxy {
return col.Length > 0 ? col : p.group.Color;
}
/// <summary> Returns the number of non-hidden players that are currently online </summary>
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);
/// <summary> Matches given name against the names of all online players that the given player can see </summary>
/// <returns> A Player instance if exactly one match was found </returns>
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) {
/// <summary> Matches given name against the names of all online players that the given player can see </summary>
/// <param name="matches"> Outputs the number of matching players </param>
/// <returns> A Player instance if exactly one match was found </returns>
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<string> names = (List<string>)arg;