From 57ab1949b7d59585fb066270617d443c4c2cb704 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 28 Jun 2018 23:27:10 +1000 Subject: [PATCH] Fix /banedit to actually work --- MCGalaxy/Commands/CPE/CmdTexture.cs | 4 +- MCGalaxy/Commands/Information/CmdRankInfo.cs | 18 ++++----- MCGalaxy/Database/Backends/MySQL.cs | 8 ++-- MCGalaxy/Database/Backends/SQLite.cs | 8 ++-- MCGalaxy/Database/BulkTransaction.cs | 15 +++----- MCGalaxy/Database/ColumnDesc.cs | 2 +- MCGalaxy/Database/ParameterisedQuery.cs | 10 ++--- MCGalaxy/Economy/Economy.DB.cs | 2 +- MCGalaxy/Player/Ban.cs | 40 +++++++++----------- 9 files changed, 50 insertions(+), 57 deletions(-) diff --git a/MCGalaxy/Commands/CPE/CmdTexture.cs b/MCGalaxy/Commands/CPE/CmdTexture.cs index c6214ee9a..1031a8e56 100644 --- a/MCGalaxy/Commands/CPE/CmdTexture.cs +++ b/MCGalaxy/Commands/CPE/CmdTexture.cs @@ -93,10 +93,10 @@ namespace MCGalaxy.Commands.CPE { internal static void FilterURL(ref string url) { // a lot of people try linking to the dropbox page instead of directly to file, so we auto correct them - if (url.StartsWith("http://www.dropbox")) { + if (url.CaselessStarts("http://www.dropbox")) { url = "http://dl.dropbox" + url.Substring("http://www.dropbox".Length); url = url.Replace("?dl=0", ""); - } else if (url.StartsWith("https://www.dropbox")) { + } else if (url.CaselessStarts("https://www.dropbox")) { url = "https://dl.dropbox" + url.Substring("https://www.dropbox".Length); url = url.Replace("?dl=0", ""); } diff --git a/MCGalaxy/Commands/Information/CmdRankInfo.cs b/MCGalaxy/Commands/Information/CmdRankInfo.cs index 1a7e46726..c7e2798d7 100644 --- a/MCGalaxy/Commands/Information/CmdRankInfo.cs +++ b/MCGalaxy/Commands/Information/CmdRankInfo.cs @@ -39,28 +39,28 @@ namespace MCGalaxy.Commands.Info { foreach (string line in rankings) { string[] args = line.SplitSpaces(); - int offset; TimeSpan delta; + string oldRank, newRank; + int offset; if (args.Length <= 6) { delta = DateTime.UtcNow - long.Parse(args[2]).FromUnixTime(); - offset = 3; + newRank = args[3]; oldRank = args[4]; + offset = 5; } else { // Backwards compatibility with old format int min = int.Parse(args[2]), hour = int.Parse(args[3]); int day = int.Parse(args[4]), month = int.Parse(args[5]), year = int.Parse(args[6]); + delta = DateTime.Now - new DateTime(year, month, day, hour, min, 0); - offset = 7; + newRank = args[7]; oldRank = args[8]; + offset = 9; } - - string newRank = Group.GetColoredName(args[offset]); - string oldRank = Group.GetColoredName(args[offset + 1]); - - offset += 2; string reason = args.Length <= offset ? "(no reason given)" : args[offset].Replace("%20", " "); Player.Message(p, "&aFrom {0} &ato {1} &a{2} ago", - oldRank, newRank, delta.Shorten(true, false)); + Group.GetColoredName(oldRank), Group.GetColoredName(newRank), + delta.Shorten(true, false)); Player.Message(p, "&aBy %S{0}&a, reason: %S{1}", PlayerInfo.GetColoredName(p, args[1]), reason); } diff --git a/MCGalaxy/Database/Backends/MySQL.cs b/MCGalaxy/Database/Backends/MySQL.cs index 483e35dd3..0e520de40 100644 --- a/MCGalaxy/Database/Backends/MySQL.cs +++ b/MCGalaxy/Database/Backends/MySQL.cs @@ -177,8 +177,8 @@ namespace MCGalaxy.SQL { transaction = connection.BeginTransaction(); } - public override IDbCommand CreateCommand(string query) { - return new MySqlCommand(query, (MySqlConnection)connection, (MySqlTransaction)transaction); + public override IDbCommand CreateCommand(string sql) { + return new MySqlCommand(sql, (MySqlConnection)connection, (MySqlTransaction)transaction); } public override IDataParameter CreateParam(string paramName, DbType type) { @@ -195,8 +195,8 @@ namespace MCGalaxy.SQL { return new MySqlConnection(connString); } - protected override IDbCommand CreateCommand(string query, IDbConnection conn) { - return new MySqlCommand(query, (MySqlConnection)conn); + protected override IDbCommand CreateCommand(string sql, IDbConnection conn) { + return new MySqlCommand(sql, (MySqlConnection)conn); } protected override IDbDataParameter CreateParameter() { diff --git a/MCGalaxy/Database/Backends/SQLite.cs b/MCGalaxy/Database/Backends/SQLite.cs index c8509b374..768eb377f 100644 --- a/MCGalaxy/Database/Backends/SQLite.cs +++ b/MCGalaxy/Database/Backends/SQLite.cs @@ -161,8 +161,8 @@ namespace MCGalaxy.SQL { transaction = connection.BeginTransaction(); } - public override IDbCommand CreateCommand(string query) { - return new SQLiteCommand(query, (SQLiteConnection)connection, (SQLiteTransaction)transaction); + public override IDbCommand CreateCommand(string sql) { + return new SQLiteCommand(sql, (SQLiteConnection)connection, (SQLiteTransaction)transaction); } public override IDataParameter CreateParam(string paramName, DbType type) { @@ -177,8 +177,8 @@ namespace MCGalaxy.SQL { return new SQLiteConnection(connString); } - protected override IDbCommand CreateCommand(string query, IDbConnection conn) { - return new SQLiteCommand(query, (SQLiteConnection)conn); + protected override IDbCommand CreateCommand(string sql, IDbConnection conn) { + return new SQLiteCommand(sql, (SQLiteConnection)conn); } protected override IDbDataParameter CreateParameter() { diff --git a/MCGalaxy/Database/BulkTransaction.cs b/MCGalaxy/Database/BulkTransaction.cs index c4f86339b..a416c01e7 100644 --- a/MCGalaxy/Database/BulkTransaction.cs +++ b/MCGalaxy/Database/BulkTransaction.cs @@ -22,13 +22,10 @@ namespace MCGalaxy.SQL { public abstract class BulkTransaction : IDisposable { protected IDbConnection connection; - protected IDbTransaction transaction; - + protected IDbTransaction transaction; - public abstract IDbCommand CreateCommand(string query); - - public abstract IDataParameter CreateParam(string paramName, DbType type); - + public abstract IDbCommand CreateCommand(string sql); + public abstract IDataParameter CreateParam(string paramName, DbType type); public void Commit() { try { @@ -58,14 +55,14 @@ namespace MCGalaxy.SQL { connection = null; } - public bool Execute(string query) { + public bool Execute(string sql) { try { - using (IDbCommand cmd = CreateCommand(query)) { + using (IDbCommand cmd = CreateCommand(sql)) { cmd.ExecuteNonQuery(); return true; } } catch (Exception e) { - System.IO.File.AppendAllText("MySQL_error.log", DateTime.Now + " " + query + "\r\n"); + System.IO.File.AppendAllText("MySQL_error.log", DateTime.Now + " " + sql + "\r\n"); Logger.LogError(e); return false; } diff --git a/MCGalaxy/Database/ColumnDesc.cs b/MCGalaxy/Database/ColumnDesc.cs index b278cbab6..d01c9d105 100644 --- a/MCGalaxy/Database/ColumnDesc.cs +++ b/MCGalaxy/Database/ColumnDesc.cs @@ -31,7 +31,7 @@ namespace MCGalaxy.SQL { public ColumnDesc(string col, ColumnType type) : this(col, type, 0, false, false, false) { } - public ColumnDesc(string col, ColumnType type, ushort maxLen = 0) + public ColumnDesc(string col, ColumnType type, ushort maxLen) : this(col, type, maxLen, false, false, false) { } public ColumnDesc(string col, ColumnType type, ushort maxLen = 0, diff --git a/MCGalaxy/Database/ParameterisedQuery.cs b/MCGalaxy/Database/ParameterisedQuery.cs index 55c3877f6..1979e9b96 100644 --- a/MCGalaxy/Database/ParameterisedQuery.cs +++ b/MCGalaxy/Database/ParameterisedQuery.cs @@ -27,18 +27,18 @@ namespace MCGalaxy.SQL { protected abstract bool MultipleSchema { get; } protected abstract IDbConnection CreateConnection(string connString); - protected abstract IDbCommand CreateCommand(string query, IDbConnection conn); + protected abstract IDbCommand CreateCommand(string sql, IDbConnection conn); protected abstract IDbDataParameter CreateParameter(); /// Executes an SQL command that does not return any results. - public void Execute(string query, string connString, bool createDB = false) { + public void Execute(string sql, string connString, bool createDB = false) { using (IDbConnection conn = CreateConnection(connString)) { conn.Open(); if (!createDB && MultipleSchema) conn.ChangeDatabase(ServerConfig.MySQLDatabaseName); - using (IDbCommand cmd = CreateCommand(query, conn)) { + using (IDbCommand cmd = CreateCommand(sql, conn)) { FillParams(cmd); cmd.ExecuteNonQuery(); } @@ -47,13 +47,13 @@ namespace MCGalaxy.SQL { } /// Excecutes an SQL query, invoking a callback on the returned rows one by one. - public object Iterate(string query, string connString, object arg, ReaderCallback callback) { + public object Iterate(string sql, string connString, object arg, ReaderCallback callback) { using (IDbConnection conn = CreateConnection(connString)) { conn.Open(); if (MultipleSchema) conn.ChangeDatabase(ServerConfig.MySQLDatabaseName); - using (IDbCommand cmd = CreateCommand(query, conn)) { + using (IDbCommand cmd = CreateCommand(sql, conn)) { FillParams(cmd); using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { arg = callback(reader, arg); } diff --git a/MCGalaxy/Economy/Economy.DB.cs b/MCGalaxy/Economy/Economy.DB.cs index c84f0622c..baa80438a 100644 --- a/MCGalaxy/Economy/Economy.DB.cs +++ b/MCGalaxy/Economy/Economy.DB.cs @@ -27,7 +27,7 @@ namespace MCGalaxy.Eco { static ColumnDesc[] createEconomy = new ColumnDesc[] { new ColumnDesc("player", ColumnType.VarChar, 20, priKey: true), new ColumnDesc("money", ColumnType.Int32), - new ColumnDesc("total", ColumnType.Integer), + new ColumnDesc("total", ColumnType.Int32), new ColumnDesc("purchase", ColumnType.VarChar, 255), new ColumnDesc("payment", ColumnType.VarChar, 255), new ColumnDesc("salary", ColumnType.VarChar, 255), diff --git a/MCGalaxy/Player/Ban.cs b/MCGalaxy/Player/Ban.cs index 1203ad0da..108a40d39 100644 --- a/MCGalaxy/Player/Ban.cs +++ b/MCGalaxy/Player/Ban.cs @@ -72,14 +72,14 @@ namespace MCGalaxy { } static void AddBanEntry(string pl, string who, string reason, string stealth, string oldrank) { - string datetime = DateTime.UtcNow.ToUnixTime().ToString(); - string data = pl + " " + who + " " + reason + " " + stealth + " " + datetime + " " + oldrank; + string time = DateTime.UtcNow.ToUnixTime().ToString(); + string data = pl + " " + who + " " + reason + " " + stealth + " " + time + " " + oldrank; bans.Append(data); } static void AddUnbanEntry(string pl, string who, string reason) { - string datetime = DateTime.UtcNow.ToUnixTime().ToString(); - string data = pl + " " + who + " " + reason + " " + datetime; + string time = DateTime.UtcNow.ToUnixTime().ToString(); + string data = pl + " " + who + " " + reason + " " + time; unbans.Append(data); } @@ -135,36 +135,32 @@ namespace MCGalaxy { return new DateTime(year, month, day, hour, minute, 0).ToUniversalTime(); } - - /// Deletes the ban information about the user. + public static bool DeleteBan(string name) { return DeleteInfo(name, bans); } - - /// Deletes the unban information about the user. public static bool DeleteUnban(string name) { return DeleteInfo(name, unbans); } static bool DeleteInfo(string name, PlayerMetaList list) { name = name.ToLower(); - bool success = false; + bool found = false; StringBuilder sb = new StringBuilder(); foreach (string line in File.ReadAllLines(list.file)) { string[] parts = line.SplitSpaces(); - if (parts.Length <= 1 || parts[1] != name) + if (parts.Length > 1 && parts[1] == name) { + found = true; + } else { sb.AppendLine(line); - else - success = true; + } } - File.WriteAllText(list.file, sb.ToString()); - return success; + + if (found) File.WriteAllText(list.file, sb.ToString()); + return found; } - /// Change the ban reason for the given user. public static bool ChangeBanReason(string who, string reason) { return ChangeReason(who, reason, bans); } - - /// Change the unban reason for the given user. public static bool ChangeUnbanReason(string who, string reason) { return ChangeReason(who, reason, unbans); } @@ -172,22 +168,22 @@ namespace MCGalaxy { static bool ChangeReason(string who, string reason, PlayerMetaList list) { who = who.ToLower(); reason = reason.Replace(" ", "%20"); - bool success = false; + bool found = false; StringBuilder sb = new StringBuilder(); foreach (string line in File.ReadAllLines(list.file)) { string[] parts = line.SplitSpaces(); if (parts.Length > 2 && parts[1] == who) { - success = true; + found = true; + parts[2] = reason; sb.AppendLine(String.Join(" ", parts)); } else { sb.AppendLine(line); } } - if (success) - File.WriteAllText(list.file, sb.ToString()); - return success; + if (found) File.WriteAllText(list.file, sb.ToString()); + return found; } } } \ No newline at end of file