Reduce code duplication between /undo and /undoarea, cleanup /help for /undo and /undoarea.

This commit is contained in:
UnknownShadow200 2016-08-29 18:42:45 +10:00
parent 7bb22c4c69
commit 461fb25729
3 changed files with 98 additions and 123 deletions

View File

@ -85,8 +85,8 @@ namespace MCGalaxy.Commands {
public override void Help(Player p) {
Player.Message(p, "%T/highlight [player] <timespan>");
Player.Message(p, "%HHighlights blocks modified by [player] in the past <timespan>");
Player.Message(p, "%H If <timespan> is not given, highlights for last 30 minutes");
Player.Message(p, "%H e.g. to highlight for 90 minutes, <timespan> would be %S1h30m");
Player.Message(p, "%HIf <timespan> is not given, highlights for last 30 minutes");
Player.Message(p, "&c/highlight cannot be disabled, use /reload to un-highlight");
}
}

View File

@ -17,25 +17,26 @@
*/
using System;
using MCGalaxy.Commands.Building;
using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands {
public sealed class CmdUndoArea : Command {
public sealed class CmdUndoArea : CmdUndo {
public override string name { get { return "undoarea"; } }
public override string shortcut { get { return "ua"; } }
public override string type { get { return CommandTypes.Building; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override CommandPerm[] ExtraPerms { get { return null; } }
public override CommandAlias[] Aliases { get { return null; } }
public override void Use(Player p, string message) {
UndoArgs args = default(UndoArgs);
UndoAreaArgs args = default(UndoAreaArgs);
if (Player.IsSuper(p)) { MessageInGameOnly(p); return; }
if (message == "") { Player.Message(p, "You need to provide a player name."); return; }
string[] parts = message.Split(' ');
args.message = parts[0];
args.delta = CmdUndo.GetDelta(p, null, parts.Length > 1 ? parts[1] : "30");
args.delta = GetDelta(p, null, parts.Length > 1 ? parts[1] : "30m");
if (args.delta == TimeSpan.MinValue) return;
Player.Message(p, "Place two blocks to determine the edges.");
@ -43,57 +44,28 @@ namespace MCGalaxy.Commands {
}
bool DoUndo(Player p, Vec3S32[] marks, object state, byte type, byte extType) {
UndoArgs args = (UndoArgs)state;
UndoAreaArgs args = (UndoAreaArgs)state;
Player who = PlayerInfo.Find(args.message);
if (who != null) UndoOnlinePlayer(p, who, args, marks);
else UndoOfflinePlayer(p, args.message, args, marks);
if (who != null) UndoOnlinePlayer(p, args.delta, who, marks);
else UndoOfflinePlayer(p, args.delta, args.message, marks);
return false;
}
void UndoOnlinePlayer(Player p, Player who, UndoArgs args, Vec3S32[] marks) {
if (p != who && who.Rank >= p.Rank) {
MessageTooHighRank(p, "undo", false); return;
}
UndoOnlineDrawOp op = new UndoOnlineDrawOp();
op.Start = DateTime.UtcNow.Subtract(args.delta);
op.who = who;
DrawOp.DoDrawOp(op, null, p, marks);
Player.SendChatFrom(who, who.ColoredName +
"%S's actions for the past &b" + args.delta.Shorten() + " %Swere undone.", false);
Server.s.Log(who.name + "'s actions for the past " + args.delta.Shorten() + " were undone.");
protected override bool CheckUndoPerms(Player p, Group grp) {
if (grp.Permission >= p.Rank) { MessageTooHighRank(p, "undo", false); return false; }
return true;
}
void UndoOfflinePlayer(Player p, string whoName, UndoArgs args, Vec3S32[] marks) {
Group group = Group.findPlayerGroup(whoName);
if (group.Permission >= p.Rank) {
MessageTooHighRank(p, "undo", false); return;
}
UndoOfflineDrawOp op = new UndoOfflineDrawOp();
op.Start = DateTime.UtcNow.Subtract(args.delta);
op.whoName = whoName;
DrawOp.DoDrawOp(op, null, p, marks);
if (op.found) {
Chat.MessageAll("{0}{1}%S's actions for the past &b{2} %Swere undone.",
group.color, whoName, args.delta.Shorten());
Server.s.Log(whoName + "'s actions for the past " + args.delta.Shorten() + " were undone.");
p.level.Save(true);
} else {
Player.Message(p, "Could not find player specified.");
}
}
struct UndoArgs { public string message; public TimeSpan delta; }
struct UndoAreaArgs { public string message; public TimeSpan delta; }
public override void Help(Player p) {
Player.Message(p, "%T/undoarea [player] [seconds]");
Player.Message(p, "%HUndoes the blockchanges made by [player] in the previous [seconds] in a specific area.");
if (p == null || (p.group.maxUndo <= 500000 || p.group.maxUndo == 0))
Player.Message(p, "%T/undoarea [player] all %H- Undoes 68 years for [player]");
Player.Message(p, "%T/undoarea [player] <timespan>");
Player.Message(p, "%HUndoes the blockchanges made by [player] in the previous <timespan> in a specific area.");
Player.Message(p, "%H If <timespan> is not given, undoes 30 minutes.");
Player.Message(p, "%H e.g. to undo the past 90 minutes, <timespan> would be %S1h30m");
if (p == null || p.group.maxUndo == -1 || p.group.maxUndo == int.MaxValue)
Player.Message(p, "%T/undoarea [player] all &c- Undoes 68 years for [player]");
}
}
}

View File

@ -20,7 +20,7 @@ using MCGalaxy.Drawing.Ops;
using MCGalaxy.Undo;
namespace MCGalaxy.Commands.Building {
public sealed class CmdUndo : Command {
public class CmdUndo : Command {
public override string name { get { return "undo"; } }
public override string shortcut { get { return "u"; } }
public override string type { get { return CommandTypes.Building; } }
@ -48,7 +48,7 @@ namespace MCGalaxy.Commands.Building {
string[] parts = message.Split(' ');
bool undoPhysics = parts[0].CaselessEq("physics");
Player who = undoPhysics ? null : PlayerInfo.Find(parts[0]);
TimeSpan delta = GetDelta(p, who, parts.Length > 1 ? parts[1] : "30");
TimeSpan delta = GetDelta(p, who, parts.Length > 1 ? parts[1] : "30m");
if (delta == TimeSpan.MinValue) return;
if (parts.Length > 1 && parts[1].CaselessEq("update")) {
@ -57,34 +57,13 @@ namespace MCGalaxy.Commands.Building {
return;
}
Vec3S32[] marks = new Vec3S32[] { Vec3U16.MaxVal, Vec3U16.MaxVal };
if (who != null)
UndoOnlinePlayer(p, delta, who);
UndoOnlinePlayer(p, delta, who, marks);
else if (undoPhysics)
UndoLevelPhysics(p, delta);
else
UndoOfflinePlayer(p, delta, parts[0]);
}
const int undoMax = -1; // allows everything to be undone.
internal static TimeSpan GetDelta(Player p, Player who, string param) {
TimeSpan delta;
bool canAll = p == null || p == who || p.group.maxUndo == undoMax;
if (param.CaselessEq("all")) {
return TimeSpan.FromSeconds(canAll ? int.MaxValue : p.group.maxUndo);
} else if (!param.TryParseShort(p, 's', "undo the past", out delta)) {
Player.Message(p, "Undoing for 30 seconds.");
return TimeSpan.MinValue;
}
if (delta.TotalSeconds == 0)
delta = TimeSpan.FromMinutes(90);
if (!canAll && delta.TotalSeconds > p.group.maxUndo) {
Player.Message(p, "{0}%Ss may only undo up to {1} seconds.",
p.group.ColoredName, p.group.maxUndo);
return TimeSpan.FromSeconds(p.group.maxUndo);
}
return delta;
UndoOfflinePlayer(p, delta, parts[0], marks);
}
void UndoSelf(Player p) {
@ -113,48 +92,6 @@ namespace MCGalaxy.Commands.Building {
Player.Message(p, "Try using %T/undo <seconds> %Sinstead.");
}
void UndoOnlinePlayer(Player p, TimeSpan delta, Player who) {
if (p != null && p != who && !CheckUndoPerms(p, who.group)) return;
UndoOnlineDrawOp op;
if (p == who) op = new UndoSelfDrawOp();
else op = new UndoOnlineDrawOp();
op.Start = DateTime.UtcNow.Subtract(delta);
op.who = who;
DrawOp.DoDrawOp(op, null, p, new Vec3S32[] { Vec3U16.MaxVal, Vec3U16.MaxVal } );
if (p == who) {
Player.Message(p, "Undid your actions for the past &b" + delta.Shorten() + "%S.");
} else {
Player.SendChatFrom(who, who.ColoredName + "%S's actions for the past &b" + delta.Shorten() + " %Swere undone.", false);
}
Server.s.Log(who.name + "'s actions for the past " + delta.Shorten() + " were undone.");
}
void UndoOfflinePlayer(Player p, TimeSpan delta, string whoName) {
if (p != null && !CheckUndoPerms(p, Group.findPlayerGroup(whoName))) return;
UndoOfflineDrawOp op = new UndoOfflineDrawOp();
op.Start = DateTime.UtcNow.Subtract(delta);
op.whoName = whoName;
DrawOp.DoDrawOp(op, null, p, new Vec3S32[] { Vec3U16.MaxVal, Vec3U16.MaxVal } );
if (op.found) {
Chat.MessageAll("{0}%S's actions for the past &b{1} %S were undone.",
PlayerInfo.GetColoredName(p, whoName), delta.Shorten());
Server.s.Log(whoName + "'s actions for the past " + delta.Shorten() + " were undone.");
if (p != null) p.level.Save(true);
} else {
Player.Message(p, "Could not find player specified.");
}
}
bool CheckUndoPerms(Player p, Group grp) {
if (!CheckExtraPerm(p)) { MessageNeedExtra(p, "undo other players."); return false; }
if (grp.Permission > p.Rank) { MessageTooHighRank(p, "undo", true); return false; }
return true;
}
void UndoLevelPhysics(Player p, TimeSpan delta) {
if (!CheckExtraPerm(p, 2)) { MessageNeedExtra(p, "undo physics.", 2); return; }
if (p != null && !p.group.CanExecute("physics")) {
@ -170,14 +107,80 @@ namespace MCGalaxy.Commands.Building {
p.level.Save(true);
}
const int undoMax = -1; // allows everything to be undone.
protected TimeSpan GetDelta(Player p, Player who, string param) {
TimeSpan delta;
bool canAll = p == null || p == who || p.group.maxUndo == undoMax;
if (param.CaselessEq("all")) {
return TimeSpan.FromSeconds(canAll ? int.MaxValue : p.group.maxUndo);
} else if (!param.TryParseShort(p, 's', "undo the past", out delta)) {
Player.Message(p, "Undoing for 30 seconds.");
return TimeSpan.MinValue;
}
if (delta.TotalSeconds == 0)
delta = TimeSpan.FromMinutes(90);
if (!canAll && delta.TotalSeconds > p.group.maxUndo) {
Player.Message(p, "{0}%Ss may only undo up to {1} seconds.",
p.group.ColoredName, p.group.maxUndo);
return TimeSpan.FromSeconds(p.group.maxUndo);
}
return delta;
}
protected void UndoOnlinePlayer(Player p, TimeSpan delta, Player who, Vec3S32[] marks) {
if (p != null && p != who && !CheckUndoPerms(p, who.group)) return;
UndoOnlineDrawOp op;
if (p == who) op = new UndoSelfDrawOp();
else op = new UndoOnlineDrawOp();
op.Start = DateTime.UtcNow.Subtract(delta);
op.who = who;
DrawOp.DoDrawOp(op, null, p, marks);
if (p == who) {
Player.Message(p, "Undid your actions for the past &b" + delta.Shorten(true) + "%S.");
} else {
Player.SendChatFrom(who, who.ColoredName + "%S's actions for the past &b" + delta.Shorten(true) + " %Swere undone.", false);
}
Server.s.Log(who.name + "'s actions for the past " + delta.Shorten(true) + " were undone.");
}
protected void UndoOfflinePlayer(Player p, TimeSpan delta, string whoName, Vec3S32[] marks) {
if (p != null && !CheckUndoPerms(p, Group.findPlayerGroup(whoName))) return;
UndoOfflineDrawOp op = new UndoOfflineDrawOp();
op.Start = DateTime.UtcNow.Subtract(delta);
op.whoName = whoName;
DrawOp.DoDrawOp(op, null, p, marks);
if (op.found) {
Chat.MessageAll("{0}%S's actions for the past &b{1} %S were undone.",
PlayerInfo.GetColoredName(p, whoName), delta.Shorten(true));
Server.s.Log(whoName + "'s actions for the past " + delta.Shorten(true) + " were undone.");
if (p != null) p.level.Save(true);
} else {
Player.Message(p, "Could not find player specified.");
}
}
protected virtual bool CheckUndoPerms(Player p, Group grp) {
if (!CheckExtraPerm(p)) { MessageNeedExtra(p, "undo other players."); return false; }
if (grp.Permission >= p.Rank) { MessageTooHighRank(p, "undo", false); return false; }
return true;
}
public override void Help(Player p) {
Player.Message(p, "/undo - Undoes your last draw operation.");
Player.Message(p, "/undo [player] [seconds] - Undoes the blockchanges made by [player] in the previous [seconds].");
if (p == null || (p.group.maxUndo <= 500000 || p.group.maxUndo == 0))
Player.Message(p, "/undo [player] all - &cUndoes 68 years for [player]");
if (p == null || (p.group.maxUndo <= 1800 || p.group.maxUndo == 0))
Player.Message(p, "/undo [player] - &cUndoes 30 minutes for [player]");
Player.Message(p, "/undo physics [seconds] - Undoes physics on your current map");
Player.Message(p, "%T/undo [player] <timespan>");
Player.Message(p, "%HUndoes the blockchanges made by [player] in the past <timespan>. " +
"If <timespan> is not given, undoes 30 minutes.");
Player.Message(p, "%H e.g. to undo the past 90 minutes, <timespan> would be %S1h30m");
if (p == null || p.group.maxUndo == -1 || p.group.maxUndo == int.MaxValue)
Player.Message(p, "%T/undo [player] all %c- Undoes 68 years for [player]");
Player.Message(p, "%T/undo %H- Undoes your last draw operation.");
Player.Message(p, "%T/undo physics [seconds] %H- Undoes physics on current map");
}
}
}