Now /pe timespent uses new timespan parsing code.

This commit is contained in:
UnknownShadow200 2016-06-13 15:40:04 +10:00
parent ff53457f00
commit 0d80d571e4
2 changed files with 32 additions and 21 deletions

View File

@ -137,24 +137,13 @@ namespace MCGalaxy.Commands {
static void SetTimespan(Player p, string[] args, string column, Player who, Action<string> setter) {
if (args.Length < 3) {
Player.Message(p, "Timespan must be in the format: ddd:hh:mm:ss");
Player.Message(p, "Do not include spaces or other special characters other than what you see above.");
return;
}
args[2] = args[2].Replace(" ", "");
if (!Regex.IsMatch(args[2], @"\d{3}:\d{2}:\d{2}:\d{2}")) {
Player.Message(p, "Timespan must be in the format: ddd:hh:mm:ss");
Player.Message(p, "If your number needs to be a single digit place a 0 in front.");
Player.Message(p, "Timespan must be in the format: <number><quantifier>..");
Player.Message(p, TimeUtils.Help, "set time spent to");
return;
}
TimeSpan timeFrame;
if (!TimeSpan.TryParse(args[2], out timeFrame)) {
Player.Message(p, "Timespan must be in the format: ddd:hh:mm:ss");
Player.Message(p, "Hour range(0-23), Minute and Second range(0-59)");
return;
}
if (!args[2].TryParseShort(p, "set time spent to", out timeFrame)) return;
string time = timeFrame.ToDBTime();
if (who != null) {
@ -162,7 +151,7 @@ namespace MCGalaxy.Commands {
} else {
UpdateDB(p, args[0], args[1], time, column);
}
MessageDataChanged(p, args[0], args[1], args[2]);
MessageDataChanged(p, args[0], args[1], timeFrame.Shorten(true));
}
static void SetInteger(Player p, string[] args, string column, int max, Player who, Action<int> setter) {
@ -191,7 +180,7 @@ namespace MCGalaxy.Commands {
}
static void UpdateDB(Player p, string name, string type, string value, string column) {
ParameterisedQuery query = ParameterisedQuery.Create();
ParameterisedQuery query = ParameterisedQuery.Create();
query.AddParam("@Name", name);
if (value != "") {
query.AddParam("@ArgValue", value);
@ -203,7 +192,7 @@ namespace MCGalaxy.Commands {
static void MessageDataChanged(Player p, string name, string type, string value) {
string msg = value == "" ? String.Format("The {1} data for &b{0} %Shas been reset.", name, type)
: String.Format("The {1} data for &b{0} %Shas been updated to &a{2} %S.", name, type, value);
: String.Format("The {1} data for &b{0} %Shas been updated to &a{2}%S.", name, type, value);
Player.Message(p, msg);
}

View File

@ -28,7 +28,7 @@ namespace MCGalaxy {
if (value.Days >= 1) time = value.Days + "d " + value.Hours + "h " + value.Minutes + "m";
else if (value.Hours >= 1) time = value.Hours + "h " + value.Minutes + "m";
else time = value.Minutes + "m";
if (seconds) time += " " + value.Seconds + "s";
if (seconds && value.Seconds != 0) time += " " + value.Seconds + "s";
return time;
}
@ -38,9 +38,13 @@ namespace MCGalaxy {
foreach (char c in value) {
long amount = 0;
if (c == ' ') continue;
if (c >= '0' && c <= '9') {
num = checked(num * 10); num += (c - '0');
} else if (c == 's' || c == 'S') {
num = checked(num * 10); num += (c - '0');
continue;
}
if (c == 's' || c == 'S') {
amount = num * TimeSpan.TicksPerSecond;
} else if (c == 'm' || c == 'M') {
amount = num * TimeSpan.TicksPerMinute;
@ -49,13 +53,31 @@ namespace MCGalaxy {
} else if (c == 'd' || c == 'D') {
amount = num * TimeSpan.TicksPerDay;
} else {
throw new FormatException(c.ToString());
throw new FormatException(c.ToString());
}
total = checked(total + amount);
num = 0;
}
return TimeSpan.FromTicks(total);
}
public static bool TryParseShort(this string value, Player p,
string action, out TimeSpan span) {
span = TimeSpan.Zero;
try {
span = ParseShort(value);
return true;
} catch (OverflowException) {
Player.Message(p, "Timespan given is too big.");
} catch (FormatException ex) {
Player.Message(p, "{0} is not a valid quantifier.", ex.Message);
Player.Message(p, Help, action);
}
return false;
}
public const string Help = "For example, to {0} 25 and a half hours, use \"1d1h30m\".";
public static string ToDBTime(this TimeSpan value) {
return value.Days + " " + value.Hours + " " + value.Minutes + " " + value.Seconds;
}