From 5aef35730cf27c0654e593f6f3a817397a7dd59f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 24 Feb 2016 21:47:19 +1100 Subject: [PATCH] Use a timespan instead of an awful string for storing 'total time played', cleanup /whois and /whowas. --- Commands/Information/CmdWhois.cs | 127 ++++++++++++------------- Commands/Information/CmdWhowas.cs | 98 +++++++++---------- Commands/Moderation/CmdPlayerEditDB.cs | 2 +- Player/Player.Timers.cs | 21 +--- Player/Player.cs | 4 +- Player/PlayerInfo.cs | 11 ++- util/Extensions.cs | 10 ++ 7 files changed, 123 insertions(+), 150 deletions(-) diff --git a/Commands/Information/CmdWhois.cs b/Commands/Information/CmdWhois.cs index e40fb7700..acd329597 100644 --- a/Commands/Information/CmdWhois.cs +++ b/Commands/Information/CmdWhois.cs @@ -1,20 +1,20 @@ /* - Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) - - Dual-licensed under the Educational Community License, Version 2.0 and - the GNU General Public License, Version 3 (the "Licenses"); you may - not use this file except in compliance with the Licenses. You may - obtain a copy of the Licenses at - - http://www.opensource.org/licenses/ecl2.php - http://www.gnu.org/licenses/gpl-3.0.html - - Unless required by applicable law or agreed to in writing, - software distributed under the Licenses are distributed on an "AS IS" - BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - or implied. See the Licenses for the specific language governing - permissions and limitations under the Licenses. -*/ + Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ using System; namespace MCGalaxy.Commands { @@ -25,61 +25,52 @@ namespace MCGalaxy.Commands public override string type { get { return CommandTypes.Information; } } public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Banned; } } - public CmdWhois() { } - public override void Use(Player p, string message) - { - Player who = null; - if (message == "") { who = p; message = p.name; } else { who = PlayerInfo.Find(message); } - if (who != null && !who.hidden) - { - Player.SendMessage(p, who.color + who.name + Server.DefaultColor + " (" + who.DisplayName + ") " + Server.DefaultColor + " is on &b" + who.level.name); - Player.SendMessage(p, who.color + who.prefix + who.name + Server.DefaultColor + " has :"); - Player.SendMessage(p, "> > the rank of " + who.group.color + who.group.name); - try - { - if (!Group.Find("Nobody").commands.Contains("pay") && !Group.Find("Nobody").commands.Contains("give") && !Group.Find("Nobody").commands.Contains("take")) Player.SendMessage(p, "> > &a" + who.money + Server.DefaultColor + " " + Server.moneys); - } - catch { } - Player.SendMessage(p, "> > &cdied &a" + who.overallDeath + Server.DefaultColor + " times"); - Player.SendMessage(p, "> > &bmodified &a" + who.overallBlocks + " &eblocks &eand &a" + who.loginBlocks + " &ewere changed &9since logging in&e."); - string storedTime = Convert.ToDateTime(DateTime.Now.Subtract(who.timeLogged).ToString()).ToString("HH:mm:ss"); - Player.SendMessage(p, "> > time spent on server: " + who.time.Split(' ')[0] + " Days, " + who.time.Split(' ')[1] + " Hours, " + who.time.Split(' ')[2] + " Minutes, " + who.time.Split(' ')[3] + " Seconds."); - Player.SendMessage(p, "> > been logged in for &a" + storedTime); - Player.SendMessage(p, "> > first logged into the server on &a" + who.firstLogin.ToString("yyyy-MM-dd") + " at " + who.firstLogin.ToString("HH:mm:ss")); - Player.SendMessage(p, "> > logged in &a" + who.totalLogins + Server.DefaultColor + " times, &c" + who.totalKicked + Server.DefaultColor + " of which ended in a kick."); - Player.SendMessage(p, "> > " + Awards.awardAmount(who.name) + " awards"); - if (Ban.IsBanned(who.name)) { - string[] data = Ban.GetBanData(who.name); - Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]); - } - - if (who.isDev) { - Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9Developer"); - } else if (who.isMod) { - Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9MCGalaxy Moderator"); - } else if (who.isGCMod) - Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9Global Chat Moderator"); - - if (!(p != null && (int)p.group.Permission <= CommandOtherPerms.GetPerm(this))) - { - string givenIP; - if (Server.bannedIP.Contains(who.ip)) givenIP = "&8" + who.ip + ", which is banned"; - else givenIP = who.ip; - Player.SendMessage(p, "> > the IP of " + givenIP); - if (Server.useWhitelist) - { - if (Server.whiteList.Contains(who.name)) - { - Player.SendMessage(p, "> > Player is &fWhitelisted"); - } - } - } + public override void Use(Player p, string message) { + Player who = message == "" ? p : PlayerInfo.Find(message); + if (message == "") message = p.name; + if (who == null || who.hidden) { + Player.SendMessage(p, "\"" + message + "\" is offline! Using /whowas instead."); + Command.all.Find("whowas").Use(p, message); return; } - else { Player.SendMessage(p, "\"" + message + "\" is offline! Using /whowas instead."); Command.all.Find("whowas").Use(p, message); } + + Player.SendMessage(p, who.color + who.name + Server.DefaultColor + " (" + who.DisplayName + ") " + Server.DefaultColor + " is on &b" + who.level.name); + Player.SendMessage(p, who.color + who.prefix + who.name + Server.DefaultColor + " has :"); + Player.SendMessage(p, "> > the rank of " + who.group.color + who.group.name); + + Group nobody = Group.findPerm(LevelPermission.Nobody); + if (nobody == null || (!nobody.commands.Contains("pay") && !nobody.commands.Contains("give") && !nobody.commands.Contains("take"))) + Player.SendMessage(p, "> > &a" + who.money + Server.DefaultColor + " " + Server.moneys); + + Player.SendMessage(p, "> > &cdied &a" + who.overallDeath + Server.DefaultColor + " times"); + Player.SendMessage(p, "> > &bmodified &a" + who.overallBlocks + " &eblocks &eand &a" + who.loginBlocks + " &ewere changed &9since logging in&e."); + string storedTime = Convert.ToDateTime(DateTime.Now.Subtract(who.timeLogged).ToString()).ToString("HH:mm:ss"); + TimeSpan time = who.time; + Player.SendMessage(p, "> > time spent on server: " + time.Days + " Days, " + time.Hours + " Hours, " + time.Minutes + " Minutes, " + time.Seconds + " Seconds."); + Player.SendMessage(p, "> > been logged in for &a" + storedTime); + Player.SendMessage(p, "> > first logged into the server on &a" + who.firstLogin.ToString("yyyy-MM-dd") + " at " + who.firstLogin.ToString("HH:mm:ss")); + Player.SendMessage(p, "> > logged in &a" + who.totalLogins + Server.DefaultColor + " times, &c" + who.totalKicked + Server.DefaultColor + " of which ended in a kick."); + Player.SendMessage(p, "> > " + Awards.awardAmount(who.name) + " awards"); + if (Ban.IsBanned(who.name)) { + string[] data = Ban.GetBanData(who.name); + Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]); + } + + if (who.isDev) Player.SendMessage(p, "> > Player is a &9Developer"); + else if (who.isMod) Player.SendMessage(p, "> > Player is a &9MCGalaxy Moderator"); + else if (who.isGCMod) Player.SendMessage(p, "> > Player is a &9Global Chat Moderator"); + + if (p == null || (int)p.group.Permission <= CommandOtherPerms.GetPerm(this)) { + string givenIP; + if (Server.bannedIP.Contains(who.ip)) givenIP = "&8" + who.ip + ", which is banned"; + else givenIP = who.ip; + Player.SendMessage(p, "> > the IP of " + givenIP); + if (Server.useWhitelist&& Server.whiteList.Contains(who.name)) + Player.SendMessage(p, "> > Player is &fWhitelisted"); + } } - public override void Help(Player p) - { + + public override void Help(Player p) { Player.SendMessage(p, "/whois [player] - Displays information about someone."); } } diff --git a/Commands/Information/CmdWhowas.cs b/Commands/Information/CmdWhowas.cs index d824308d1..7c77d7098 100644 --- a/Commands/Information/CmdWhowas.cs +++ b/Commands/Information/CmdWhowas.cs @@ -27,14 +27,11 @@ namespace MCGalaxy.Commands public override string type { get { return CommandTypes.Information; } } public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Banned; } } - public CmdWhowas() { } - public override void Use(Player p, string message) - { + public override void Use(Player p, string message) { if (message == "") { Help(p); return; } Player pl = PlayerInfo.Find(message); - if (pl != null && !pl.hidden) - { + if (pl != null && !pl.hidden) { Player.SendMessage(p, pl.color + pl.name + Server.DefaultColor + " is online, using /whois instead."); Command.all.Find("whois").Use(p, message); return; @@ -44,67 +41,58 @@ namespace MCGalaxy.Commands string syntax = Server.useMySQL ? "SELECT * FROM Players WHERE Name=@Name COLLATE utf8_general_ci" : "SELECT * FROM Players WHERE Name=@Name COLLATE NOCASE"; Database.AddParams("@Name", message); - DataTable playerDb = Database.fillData(syntax); + DataTable table = Database.fillData(syntax); - string FoundRank = Group.findPlayer(message.ToLower()); - if (playerDb.Rows.Count == 0) { Player.SendMessage(p, Group.Find(FoundRank).color + message + Server.DefaultColor + " has the rank of " + Group.Find(FoundRank).color + FoundRank); return; } - string title = playerDb.Rows[0]["Title"].ToString(); - string color = Colors.Parse(playerDb.Rows[0]["color"].ToString().Trim()); - if (color == "" || color == null || String.IsNullOrEmpty(color)) color = Group.Find(FoundRank).color; - string tcolor = Colors.Parse(playerDb.Rows[0]["title_color"].ToString().Trim()); - if (title == "" || title == null || String.IsNullOrEmpty(title)) - Player.SendMessage(p, color + message + Server.DefaultColor + " has :"); + string plGroup = Group.findPlayer(message.ToLower()); + if (table.Rows.Count == 0) { Player.SendMessage(p, Group.Find(plGroup).color + message + " %Shas the rank of " + Group.Find(plGroup).color + plGroup); return; } + string title = table.Rows[0]["Title"].ToString(); + string color = Colors.Parse(table.Rows[0]["color"].ToString().Trim()); + if (String.IsNullOrEmpty(color)) color = Group.Find(plGroup).color; + string tcolor = Colors.Parse(table.Rows[0]["title_color"].ToString().Trim()); + if (String.IsNullOrEmpty(title)) + Player.SendMessage(p, color + message + " %Shas :"); else - Player.SendMessage(p, color + "[" + tcolor + playerDb.Rows[0]["Title"] + color + "] " + message + Server.DefaultColor + " has :"); - Player.SendMessage(p, "> > the rank of " + Group.Find(FoundRank).color + FoundRank); - try - { - if (!Group.Find("Nobody").commands.Contains("pay") && !Group.Find("Nobody").commands.Contains("give") && !Group.Find("Nobody").commands.Contains("take")) Player.SendMessage(p, "> > &a" + playerDb.Rows[0]["Money"] + Server.DefaultColor + " " + Server.moneys); - } - catch { } - Player.SendMessage(p, "> > &cdied &a" + playerDb.Rows[0]["TotalDeaths"] + Server.DefaultColor + " times"); - Player.SendMessage(p, "> > &bmodified &a" + playerDb.Rows[0]["totalBlocks"] + " &eblocks."); - Player.SendMessage(p, "> > was last seen on &a" + playerDb.Rows[0]["LastLogin"]); - Player.SendMessage(p, "> > " + TotalTime(playerDb.Rows[0]["TimeSpent"].ToString())); - Player.SendMessage(p, "> > first logged into the server on &a" + playerDb.Rows[0]["FirstLogin"]); - Player.SendMessage(p, "> > logged in &a" + playerDb.Rows[0]["totalLogin"] + Server.DefaultColor + " times, &c" + playerDb.Rows[0]["totalKicked"] + Server.DefaultColor + " of which ended in a kick."); + Player.SendMessage(p, color + "[" + tcolor + title + color + "] " + message + " %Shas :"); + Player.SendMessage(p, "> > the rank of " + Group.Find(plGroup).color + plGroup); + + Group nobody = Group.findPerm(LevelPermission.Nobody); + if (nobody == null || (!nobody.commands.Contains("pay") && !nobody.commands.Contains("give") && !nobody.commands.Contains("take"))) + Player.SendMessage(p, "> > &a" + table.Rows[0]["Money"] + Server.DefaultColor + " " + Server.moneys); + + Player.SendMessage(p, "> > &cdied &a" + table.Rows[0]["TotalDeaths"] + Server.DefaultColor + " times"); + Player.SendMessage(p, "> > &bmodified &a" + table.Rows[0]["totalBlocks"] + " &eblocks."); + Player.SendMessage(p, "> > was last seen on &a" + table.Rows[0]["LastLogin"]); + Player.SendMessage(p, "> > " + TotalTime(table.Rows[0]["TimeSpent"].ToString())); + Player.SendMessage(p, "> > first logged into the server on &a" + table.Rows[0]["FirstLogin"]); + Player.SendMessage(p, "> > logged in &a" + table.Rows[0]["totalLogin"] + Server.DefaultColor + " times, &c" + table.Rows[0]["totalKicked"] + Server.DefaultColor + " of which ended in a kick."); Player.SendMessage(p, "> > " + Awards.awardAmount(message) + " awards"); - if (Ban.IsBanned(message)) - { + if (Ban.IsBanned(message)) { string[] data = Ban.GetBanData(message); Player.SendMessage(p, "> > was banned by " + data[0] + " for " + data[1] + " on " + data[2]); } - if (Server.Devs.ContainsInsensitive(message)) { - Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9Developer"); - } else if (Server.Mods.ContainsInsensitive(message)) { - Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9MCGalaxy Moderator"); - } else if (Server.GCmods.ContainsInsensitive(message)) { - Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9Global Chat Moderator"); - } + if (Server.Devs.ContainsInsensitive(message)) Player.SendMessage(p, "> > Player is a &9Developer"); + else if (Server.Mods.ContainsInsensitive(message)) Player.SendMessage(p, "> > Player is a &9MCGalaxy Moderator"); + else if (Server.GCmods.ContainsInsensitive(message)) Player.SendMessage(p, "> > Player is a &9Global Chat Moderator"); - if (!(p != null && (int)p.group.Permission <= CommandOtherPerms.GetPerm(this))) - { - if (Server.bannedIP.Contains(playerDb.Rows[0]["IP"].ToString())) - playerDb.Rows[0]["IP"] = "&8" + playerDb.Rows[0]["IP"] + ", which is banned"; - Player.SendMessage(p, "> > the IP of " + playerDb.Rows[0]["IP"]); - if (Server.useWhitelist) - { - if (Server.whiteList.Contains(message.ToLower())) - { - Player.SendMessage(p, "> > Player is &fWhitelisted"); - } - } + if (p == null || (int)p.group.Permission <= CommandOtherPerms.GetPerm(this)) { + if (Server.bannedIP.Contains(table.Rows[0]["IP"].ToString())) + table.Rows[0]["IP"] = "&8" + table.Rows[0]["IP"] + ", which is banned"; + Player.SendMessage(p, "> > the IP of " + table.Rows[0]["IP"]); + if (Server.useWhitelist&& Server.whiteList.Contains(message)) + Player.SendMessage(p, "> > Player is &fWhitelisted"); } - playerDb.Dispose(); + table.Dispose(); } - public override void Help(Player p) - { + + string TotalTime(string time) { + TimeSpan value = time.ParseDBTime(); + return "time spent on server: " + value.Days + " Days, " + value.Hours + " Hours, " + value.Minutes + " Minutes, " + value.Seconds + " Seconds."; + } + + public override void Help(Player p) { Player.SendMessage(p, "/whowas - Displays information about someone who left."); } - public string TotalTime(string time) - { - return "time spent on server: " + time.Split(' ')[0] + " Days, " + time.Split(' ')[1] + " Hours, " + time.Split(' ')[2] + " Minutes, " + time.Split(' ')[3] + " Seconds."; - } + } } diff --git a/Commands/Moderation/CmdPlayerEditDB.cs b/Commands/Moderation/CmdPlayerEditDB.cs index d9e7f4e68..039d9b5cf 100644 --- a/Commands/Moderation/CmdPlayerEditDB.cs +++ b/Commands/Moderation/CmdPlayerEditDB.cs @@ -84,7 +84,7 @@ namespace MCGalaxy.Commands { case "totalkicked": SetInteger(p, args, "totalKicked", 1000000000, who, v => who.totalKicked = v); break; case "timespent": - SetTimespan(p, args, "TimeSpent", who, v => who.time = v); break; + SetTimespan(p, args, "TimeSpent", who, v => who.time = v.ParseDBTime()); break; case "color": SetColor(p, args, "color", who, v => who.color = (v == "" ? who.group.color : Colors.Parse(v))); break; case "titlecolor": diff --git a/Player/Player.Timers.cs b/Player/Player.Timers.cs index 1e6bc9560..ed6352c8f 100644 --- a/Player/Player.Timers.cs +++ b/Player/Player.Timers.cs @@ -45,26 +45,9 @@ namespace MCGalaxy { resetSpamCount.Start(); } + static readonly TimeSpan delta = TimeSpan.FromSeconds(1); void TimeSpentElapsed(object sender, ElapsedEventArgs e) { - try { - int Days = Convert.ToInt32(time.Split(' ')[0]); - int Hours = Convert.ToInt32(time.Split(' ')[1]); - int Minutes = Convert.ToInt32(time.Split(' ')[2]); - int Seconds = Convert.ToInt32(time.Split(' ')[3]); - Seconds++; - - if (Seconds >= 60) { - Minutes++; Seconds = 0; - } - if (Minutes >= 60) { - Hours++; Minutes = 0; - } - if (Hours >= 24) { - Days++; Hours = 0; - } - time = "" + Days + " " + Hours + " " + Minutes + " " + Seconds; - } - catch { time = "0 0 0 1"; } + time = time.Add(delta); } void LoginTimerElapsed(object sender, ElapsedEventArgs e) { diff --git a/Player/Player.cs b/Player/Player.cs index e77d04495..d19c3991e 100644 --- a/Player/Player.cs +++ b/Player/Player.cs @@ -76,7 +76,7 @@ namespace MCGalaxy { byte[] buffer = new byte[0]; byte[] tempbuffer = new byte[0xFF]; public bool disconnected = false; - public string time; + public TimeSpan time; public string name; public string DisplayName; public string SkinName; @@ -376,7 +376,7 @@ namespace MCGalaxy { ", Money=" + money + ", totalBlocks=" + overallBlocks + ", totalKicked=" + totalKicked + - ", TimeSpent='" + time + + ", TimeSpent='" + time.ToDBTime() + "' WHERE Name='" + name + "'"; if ( MySQLSave != null ) MySQLSave(this, commandString); diff --git a/Player/PlayerInfo.cs b/Player/PlayerInfo.cs index 996205f64..940dd2de8 100644 --- a/Player/PlayerInfo.cs +++ b/Player/PlayerInfo.cs @@ -76,7 +76,7 @@ namespace MCGalaxy { internal static void CreateInfo(Player p) { p.prefix = ""; - p.time = "0 0 0 1"; + p.time = new TimeSpan(0, 0, 0, 1); p.title = ""; p.titlecolor = ""; p.color = p.group.color; @@ -89,9 +89,10 @@ namespace MCGalaxy { p.overallBlocks = 0; p.timeLogged = DateTime.Now; - const string query = "INSERT INTO Players (Name, IP, FirstLogin, LastLogin, totalLogin, Title, totalDeaths, Money, totalBlocks, totalKicked, TimeSpent) VALUES " + - "('{0}', '{1}', '{2:yyyy-MM-dd HH:mm:ss}', '{3:yyyy-MM-dd HH:mm:ss}', {4}, '{5}', {6}, {7}, {8}, {9}, '{10}')"; - Database.executeQuery(String.Format(query, p.name, p.ip, p.firstLogin, DateTime.Now, p.totalLogins, p.prefix, p.overallDeath, p.money, p.loginBlocks, p.totalKicked, p.time)); + const string query = "INSERT INTO Players (Name, IP, FirstLogin, LastLogin, totalLogin, Title, totalDeaths, Money, totalBlocks, totalKicked, TimeSpent) " + + "VALUES ('{0}', '{1}', '{2:yyyy-MM-dd HH:mm:ss}', '{3:yyyy-MM-dd HH:mm:ss}', {4}, '{5}', {6}, {7}, {8}, {9}, '{10}')"; + Database.executeQuery(String.Format(query, p.name, p.ip, p.firstLogin, DateTime.Now, p.totalLogins, + p.prefix, p.overallDeath, p.money, p.loginBlocks, p.totalKicked, p.time.ToDBTime())); string ecoQuery = "INSERT INTO Economy (player, money, total, purchase, payment, salary, fine) VALUES ('" + p.name + "', " + p.money + ", 0, '%cNone', '%cNone', '%cNone', '%cNone')"; Database.executeQuery(ecoQuery); } @@ -99,7 +100,7 @@ namespace MCGalaxy { internal static void LoadInfo(DataTable playerDb, Player p) { DataRow row = playerDb.Rows[0]; p.totalLogins = int.Parse(row["totalLogin"].ToString()) + 1; - p.time = row["TimeSpent"].ToString(); + p.time = row["TimeSpent"].ToString().ParseDBTime(); p.userID = int.Parse(row["ID"].ToString()); p.firstLogin = DateTime.Parse(row["firstLogin"].ToString()); p.timeLogged = DateTime.Now; diff --git a/util/Extensions.cs b/util/Extensions.cs index 560b4f645..5cc1a2779 100644 --- a/util/Extensions.cs +++ b/util/Extensions.cs @@ -150,6 +150,16 @@ namespace MCGalaxy { foreach (string item in list) if (item.Equals(value, StringComparison.OrdinalIgnoreCase)) return true; return false; + } + + public static string ToDBTime(this TimeSpan value) { + return value.Days + " " + value.Hours + " " + value.Minutes + " " + value.Seconds; + } + + public static TimeSpan ParseDBTime(this string value) { + string[] parts = value.Split(' '); + return new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]), + int.Parse(parts[2]), int.Parse(parts[3])); } } }