Use a timespan instead of an awful string for storing 'total time played', cleanup /whois and /whowas.

This commit is contained in:
UnknownShadow200 2016-02-24 21:47:19 +11:00
parent a761599480
commit 5aef35730c
7 changed files with 123 additions and 150 deletions

View File

@ -14,7 +14,7 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands
{ {
@ -25,26 +25,28 @@ namespace MCGalaxy.Commands
public override string type { get { return CommandTypes.Information; } } public override string type { get { return CommandTypes.Information; } }
public override bool museumUsable { get { return true; } } public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Banned; } } public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
public CmdWhois() { }
public override void Use(Player p, string message) public override void Use(Player p, string message) {
{ Player who = message == "" ? p : PlayerInfo.Find(message);
Player who = null; if (message == "") message = p.name;
if (message == "") { who = p; message = p.name; } else { who = PlayerInfo.Find(message); } if (who == null || who.hidden) {
if (who != null && !who.hidden) Player.SendMessage(p, "\"" + message + "\" is offline! Using /whowas instead.");
{ Command.all.Find("whowas").Use(p, message); return;
}
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.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, who.color + who.prefix + who.name + Server.DefaultColor + " has :");
Player.SendMessage(p, "> > the rank of " + who.group.color + who.group.name); Player.SendMessage(p, "> > the rank of " + who.group.color + who.group.name);
try
{ Group nobody = Group.findPerm(LevelPermission.Nobody);
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); 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);
catch { }
Player.SendMessage(p, "> > &cdied &a" + who.overallDeath + Server.DefaultColor + " times"); 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."); 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"); 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."); 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, "> > 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, "> > 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, "> > logged in &a" + who.totalLogins + Server.DefaultColor + " times, &c" + who.totalKicked + Server.DefaultColor + " of which ended in a kick.");
@ -54,32 +56,21 @@ namespace MCGalaxy.Commands
Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]); Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]);
} }
if (who.isDev) { if (who.isDev) Player.SendMessage(p, "> > Player is a &9Developer");
Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9Developer"); else if (who.isMod) Player.SendMessage(p, "> > Player is a &9MCGalaxy Moderator");
} else if (who.isMod) { else if (who.isGCMod) Player.SendMessage(p, "> > Player is a &9Global Chat Moderator");
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))) if (p == null || (int)p.group.Permission <= CommandOtherPerms.GetPerm(this)) {
{
string givenIP; string givenIP;
if (Server.bannedIP.Contains(who.ip)) givenIP = "&8" + who.ip + ", which is banned"; if (Server.bannedIP.Contains(who.ip)) givenIP = "&8" + who.ip + ", which is banned";
else givenIP = who.ip; else givenIP = who.ip;
Player.SendMessage(p, "> > the IP of " + givenIP); Player.SendMessage(p, "> > the IP of " + givenIP);
if (Server.useWhitelist) if (Server.useWhitelist&& Server.whiteList.Contains(who.name))
{
if (Server.whiteList.Contains(who.name))
{
Player.SendMessage(p, "> > Player is &fWhitelisted"); Player.SendMessage(p, "> > Player is &fWhitelisted");
} }
} }
}
} public override void Help(Player p) {
else { Player.SendMessage(p, "\"" + message + "\" is offline! Using /whowas instead."); Command.all.Find("whowas").Use(p, message); }
}
public override void Help(Player p)
{
Player.SendMessage(p, "/whois [player] - Displays information about someone."); Player.SendMessage(p, "/whois [player] - Displays information about someone.");
} }
} }

View File

@ -27,14 +27,11 @@ namespace MCGalaxy.Commands
public override string type { get { return CommandTypes.Information; } } public override string type { get { return CommandTypes.Information; } }
public override bool museumUsable { get { return true; } } public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Banned; } } 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; } if (message == "") { Help(p); return; }
Player pl = PlayerInfo.Find(message); 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."); Player.SendMessage(p, pl.color + pl.name + Server.DefaultColor + " is online, using /whois instead.");
Command.all.Find("whois").Use(p, message); Command.all.Find("whois").Use(p, message);
return; return;
@ -44,67 +41,58 @@ namespace MCGalaxy.Commands
string syntax = Server.useMySQL ? "SELECT * FROM Players WHERE Name=@Name COLLATE utf8_general_ci" : string syntax = Server.useMySQL ? "SELECT * FROM Players WHERE Name=@Name COLLATE utf8_general_ci" :
"SELECT * FROM Players WHERE Name=@Name COLLATE NOCASE"; "SELECT * FROM Players WHERE Name=@Name COLLATE NOCASE";
Database.AddParams("@Name", message); Database.AddParams("@Name", message);
DataTable playerDb = Database.fillData(syntax); DataTable table = Database.fillData(syntax);
string FoundRank = Group.findPlayer(message.ToLower()); string plGroup = 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; } 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 = playerDb.Rows[0]["Title"].ToString(); string title = table.Rows[0]["Title"].ToString();
string color = Colors.Parse(playerDb.Rows[0]["color"].ToString().Trim()); string color = Colors.Parse(table.Rows[0]["color"].ToString().Trim());
if (color == "" || color == null || String.IsNullOrEmpty(color)) color = Group.Find(FoundRank).color; if (String.IsNullOrEmpty(color)) color = Group.Find(plGroup).color;
string tcolor = Colors.Parse(playerDb.Rows[0]["title_color"].ToString().Trim()); string tcolor = Colors.Parse(table.Rows[0]["title_color"].ToString().Trim());
if (title == "" || title == null || String.IsNullOrEmpty(title)) if (String.IsNullOrEmpty(title))
Player.SendMessage(p, color + message + Server.DefaultColor + " has :"); Player.SendMessage(p, color + message + " %Shas :");
else else
Player.SendMessage(p, color + "[" + tcolor + playerDb.Rows[0]["Title"] + color + "] " + message + Server.DefaultColor + " has :"); Player.SendMessage(p, color + "[" + tcolor + title + color + "] " + message + " %Shas :");
Player.SendMessage(p, "> > the rank of " + Group.Find(FoundRank).color + FoundRank); Player.SendMessage(p, "> > the rank of " + Group.Find(plGroup).color + plGroup);
try
{ Group nobody = Group.findPerm(LevelPermission.Nobody);
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); 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);
catch { }
Player.SendMessage(p, "> > &cdied &a" + playerDb.Rows[0]["TotalDeaths"] + Server.DefaultColor + " times"); Player.SendMessage(p, "> > &cdied &a" + table.Rows[0]["TotalDeaths"] + Server.DefaultColor + " times");
Player.SendMessage(p, "> > &bmodified &a" + playerDb.Rows[0]["totalBlocks"] + " &eblocks."); Player.SendMessage(p, "> > &bmodified &a" + table.Rows[0]["totalBlocks"] + " &eblocks.");
Player.SendMessage(p, "> > was last seen on &a" + playerDb.Rows[0]["LastLogin"]); Player.SendMessage(p, "> > was last seen on &a" + table.Rows[0]["LastLogin"]);
Player.SendMessage(p, "> > " + TotalTime(playerDb.Rows[0]["TimeSpent"].ToString())); Player.SendMessage(p, "> > " + TotalTime(table.Rows[0]["TimeSpent"].ToString()));
Player.SendMessage(p, "> > first logged into the server on &a" + playerDb.Rows[0]["FirstLogin"]); Player.SendMessage(p, "> > first logged into the server on &a" + table.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, "> > 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"); Player.SendMessage(p, "> > " + Awards.awardAmount(message) + " awards");
if (Ban.IsBanned(message)) if (Ban.IsBanned(message)) {
{
string[] data = Ban.GetBanData(message); string[] data = Ban.GetBanData(message);
Player.SendMessage(p, "> > was banned by " + data[0] + " for " + data[1] + " on " + data[2]); Player.SendMessage(p, "> > was banned by " + data[0] + " for " + data[1] + " on " + data[2]);
} }
if (Server.Devs.ContainsInsensitive(message)) { if (Server.Devs.ContainsInsensitive(message)) Player.SendMessage(p, "> > Player is a &9Developer");
Player.SendMessage(p, Server.DefaultColor + "> > Player is a &9Developer"); else if (Server.Mods.ContainsInsensitive(message)) Player.SendMessage(p, "> > Player is a &9MCGalaxy Moderator");
} else if (Server.Mods.ContainsInsensitive(message)) { else if (Server.GCmods.ContainsInsensitive(message)) Player.SendMessage(p, "> > Player is a &9Global Chat Moderator");
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 (!(p != null && (int)p.group.Permission <= CommandOtherPerms.GetPerm(this))) if (p == null || (int)p.group.Permission <= CommandOtherPerms.GetPerm(this)) {
{ if (Server.bannedIP.Contains(table.Rows[0]["IP"].ToString()))
if (Server.bannedIP.Contains(playerDb.Rows[0]["IP"].ToString())) table.Rows[0]["IP"] = "&8" + table.Rows[0]["IP"] + ", which is banned";
playerDb.Rows[0]["IP"] = "&8" + playerDb.Rows[0]["IP"] + ", which is banned"; Player.SendMessage(p, "> > the IP of " + table.Rows[0]["IP"]);
Player.SendMessage(p, "> > the IP of " + playerDb.Rows[0]["IP"]); if (Server.useWhitelist&& Server.whiteList.Contains(message))
if (Server.useWhitelist)
{
if (Server.whiteList.Contains(message.ToLower()))
{
Player.SendMessage(p, "> > Player is &fWhitelisted"); Player.SendMessage(p, "> > Player is &fWhitelisted");
} }
table.Dispose();
} }
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.";
} }
playerDb.Dispose();
} public override void Help(Player p) {
public override void Help(Player p)
{
Player.SendMessage(p, "/whowas <name> - Displays information about someone who left."); Player.SendMessage(p, "/whowas <name> - 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.";
}
} }
} }

View File

@ -84,7 +84,7 @@ namespace MCGalaxy.Commands {
case "totalkicked": case "totalkicked":
SetInteger(p, args, "totalKicked", 1000000000, who, v => who.totalKicked = v); break; SetInteger(p, args, "totalKicked", 1000000000, who, v => who.totalKicked = v); break;
case "timespent": 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": case "color":
SetColor(p, args, "color", who, v => who.color = (v == "" ? who.group.color : Colors.Parse(v))); break; SetColor(p, args, "color", who, v => who.color = (v == "" ? who.group.color : Colors.Parse(v))); break;
case "titlecolor": case "titlecolor":

View File

@ -45,26 +45,9 @@ namespace MCGalaxy {
resetSpamCount.Start(); resetSpamCount.Start();
} }
static readonly TimeSpan delta = TimeSpan.FromSeconds(1);
void TimeSpentElapsed(object sender, ElapsedEventArgs e) { void TimeSpentElapsed(object sender, ElapsedEventArgs e) {
try { time = time.Add(delta);
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"; }
} }
void LoginTimerElapsed(object sender, ElapsedEventArgs e) { void LoginTimerElapsed(object sender, ElapsedEventArgs e) {

View File

@ -76,7 +76,7 @@ namespace MCGalaxy {
byte[] buffer = new byte[0]; byte[] buffer = new byte[0];
byte[] tempbuffer = new byte[0xFF]; byte[] tempbuffer = new byte[0xFF];
public bool disconnected = false; public bool disconnected = false;
public string time; public TimeSpan time;
public string name; public string name;
public string DisplayName; public string DisplayName;
public string SkinName; public string SkinName;
@ -376,7 +376,7 @@ namespace MCGalaxy {
", Money=" + money + ", Money=" + money +
", totalBlocks=" + overallBlocks + ", totalBlocks=" + overallBlocks +
", totalKicked=" + totalKicked + ", totalKicked=" + totalKicked +
", TimeSpent='" + time + ", TimeSpent='" + time.ToDBTime() +
"' WHERE Name='" + name + "'"; "' WHERE Name='" + name + "'";
if ( MySQLSave != null ) if ( MySQLSave != null )
MySQLSave(this, commandString); MySQLSave(this, commandString);

View File

@ -76,7 +76,7 @@ namespace MCGalaxy {
internal static void CreateInfo(Player p) { internal static void CreateInfo(Player p) {
p.prefix = ""; p.prefix = "";
p.time = "0 0 0 1"; p.time = new TimeSpan(0, 0, 0, 1);
p.title = ""; p.title = "";
p.titlecolor = ""; p.titlecolor = "";
p.color = p.group.color; p.color = p.group.color;
@ -89,9 +89,10 @@ namespace MCGalaxy {
p.overallBlocks = 0; p.overallBlocks = 0;
p.timeLogged = DateTime.Now; p.timeLogged = DateTime.Now;
const string query = "INSERT INTO Players (Name, IP, FirstLogin, LastLogin, totalLogin, Title, totalDeaths, Money, totalBlocks, totalKicked, TimeSpent) VALUES " + const string query = "INSERT INTO Players (Name, IP, FirstLogin, LastLogin, totalLogin, Title, totalDeaths, Money, totalBlocks, totalKicked, TimeSpent) " +
"('{0}', '{1}', '{2:yyyy-MM-dd HH:mm:ss}', '{3:yyyy-MM-dd HH:mm:ss}', {4}, '{5}', {6}, {7}, {8}, {9}, '{10}')"; "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)); 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')"; 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); Database.executeQuery(ecoQuery);
} }
@ -99,7 +100,7 @@ namespace MCGalaxy {
internal static void LoadInfo(DataTable playerDb, Player p) { internal static void LoadInfo(DataTable playerDb, Player p) {
DataRow row = playerDb.Rows[0]; DataRow row = playerDb.Rows[0];
p.totalLogins = int.Parse(row["totalLogin"].ToString()) + 1; 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.userID = int.Parse(row["ID"].ToString());
p.firstLogin = DateTime.Parse(row["firstLogin"].ToString()); p.firstLogin = DateTime.Parse(row["firstLogin"].ToString());
p.timeLogged = DateTime.Now; p.timeLogged = DateTime.Now;

View File

@ -151,5 +151,15 @@ namespace MCGalaxy {
if (item.Equals(value, StringComparison.OrdinalIgnoreCase)) return true; if (item.Equals(value, StringComparison.OrdinalIgnoreCase)) return true;
return false; 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]));
}
} }
} }