Reduce code duplication in IRC bot.

This commit is contained in:
UnknownShadow200 2016-08-30 11:05:28 +10:00
parent e8a6e83a0c
commit 33694517b5
4 changed files with 48 additions and 61 deletions

View File

@ -31,17 +31,13 @@ namespace MCGalaxy.Commands
public CmdPlayers() { }
public override void Use(Player p, string message) {
DisplayPlayers(p, message, text => Player.Message(p, text),
true, Server.showEmptyRanks);
DisplayPlayers(p, message, true, Server.showEmptyRanks);
}
public static void DisplayPlayers(Player p, string message, Action<string> output,
bool showHidden, bool showEmptyRanks) {
public static void DisplayPlayers(Player p, string message, bool showHidden, bool showEmptyRanks) {
if (message != "") {
Group grp = Group.Find(message);
if( grp == null ) {
output("There is no rank with that name"); return;
}
Group grp = Group.FindMatches(p, message);
if(grp == null) return;
string title = ":" + grp.color + GetPlural(grp.trueName) + ":";
Section rankSec = MakeSection(grp, title);
@ -57,9 +53,9 @@ namespace MCGalaxy.Commands
}
if (rankSec.Empty) {
output( "There are no players of that rank online.");
Player.Message(p, "There are no players of that rank online.");
} else {
rankSec.Print(output, false);
rankSec.Print(p, false);
}
return;
}
@ -83,12 +79,12 @@ namespace MCGalaxy.Commands
}
if (totalPlayers == 1)
output("There is &a1 %Splayer online.");
Player.Message(p, "There is &a1 %Splayer online.");
else
output("There are &a" + totalPlayers + " %Splayers online.");
Player.Message(p, "There are &a" + totalPlayers + " %Splayers online.");
for (int i = playerList.Count - 1; i >= 0; i--)
playerList[i].Print(output, showEmptyRanks);
playerList[i].Print(p, showEmptyRanks);
}
static void AddStates(Player pl, ref string name) {
@ -106,13 +102,13 @@ namespace MCGalaxy.Commands
public bool Empty { get { return builder.Length == 0; } }
public void Print(Action<string> output, bool showEmpty) {
public void Print(Player p, bool showEmpty) {
if (builder.Length == 0 && !showEmpty) return;
if (builder.Length > 0)
builder.Remove(builder.Length - 1, 1);
string message = title + builder.ToString();
output(message);
Player.Message(p, message);
}
public void Append(Player pl, string name) {

View File

@ -102,10 +102,10 @@ namespace MCGalaxy.Eco {
case "max":
case "maximum":
case "maximumrank":
Group grp = Group.Find(args[2]);
if (grp == null) { Player.Message(p, "%cThat wasn't a rank!"); return; }
Group grp = Group.FindMatches(p, args[2]);
if (grp == null) return;
if (p != null && p.Rank < grp.Permission) { Player.Message(p, "%cCannot set maxrank to a rank higher than yours."); return; }
MaxRank = args[2].ToLower();
MaxRank = grp.name.ToLower();
Player.Message(p, "%aSuccessfully set max rank to: " + grp.ColoredName);
UpdatePrices();
break;

View File

@ -246,14 +246,7 @@ namespace MCGalaxy {
string cmdName = parts[0].ToLower();
string cmdArgs = parts.Length > 1 ? parts[1] : "";
if (cmdName == ".who" || cmdName == ".players") {
try {
CmdPlayers.DisplayPlayers(null, "", text => Pm(user.Nick, text), false, false);
} catch (Exception e) {
Server.ErrorLog(e);
}
return;
}
if (HandleWhoCommand(user.Nick, cmdName, false)) return;
Command.Search(ref cmdName, ref cmdArgs);
string error;
@ -261,21 +254,8 @@ namespace MCGalaxy {
if (!CheckIRCCommand(user, cmdName, chan, out error)) {
if (error != null) Pm(user.Nick, error);
return;
}
Command cmd = Command.all.Find(cmdName);
if (cmd == null) { Pm(user.Nick, "Unknown command!"); return; }
Server.s.Log("IRC Command: /" + message + " (by " + user.Nick + ")");
string args = parts.Length > 1 ? parts[1] : "";
Player p = MakeIRCPlayer(user.Nick);
try {
if (!p.group.CanExecute(cmd)) { cmd.MessageCannotUse(p); return; }
cmd.Use(p, args);
} catch (Exception ex) {
Pm(user.Nick, "CMD Error: " + ex);
}
}
HandleIRCCommand(user.Nick, user.Nick, cmdName, cmdArgs);
}
void Listener_OnPublic(UserInfo user, string channel, string message) {
@ -288,18 +268,8 @@ namespace MCGalaxy {
string[] parts = message.SplitSpaces(3);
string ircCmd = parts[0].ToLower();
bool whoCmd = ircCmd == ".who" || ircCmd == ".players" || ircCmd == "!players";
DateTime last = opchat ? lastOpWho : lastWho;
if (whoCmd && (DateTime.UtcNow - last).TotalSeconds > 1) {
try {
CmdPlayers.DisplayPlayers(null, "", text => Say(text, opchat, true), false, false);
} catch (Exception e) {
Server.ErrorLog(e);
}
if (opchat) lastOpWho = DateTime.UtcNow;
else lastWho = DateTime.UtcNow;
}
string nick = opchat ? "#@private@#" : "#@public@#";
if (HandleWhoCommand(nick, ircCmd, opchat)) return;
if (ircCmd == ".x" && !HandlePublicCommand(user, channel, message, parts, opchat)) return;
@ -313,7 +283,7 @@ namespace MCGalaxy {
Server.profanityFilter ? ProfanityFilter.Parse(message) : message));
}
}
bool HandlePublicCommand(UserInfo user, string channel, string message,
string[] parts, bool opchat) {
string cmdName = parts.Length > 1 ? parts[1].ToLower() : "";
@ -326,19 +296,40 @@ namespace MCGalaxy {
return false;
}
Command cmd = Command.all.Find(cmdName);
if (cmdName == "" || cmd == null) {
Say("Unknown command!", opchat); return false;
}
Server.s.Log("IRC Command: /" + message.Substring(3) + " (by " + user.Nick + ")");
string nick = opchat ? "#@private@#" : "#@public@#";
return HandleIRCCommand(nick, user.Nick, cmdName, cmdArgs);
}
bool HandleWhoCommand(string nick, string ircCmd, bool opchat) {
bool whoCmd = ircCmd == ".who" || ircCmd == ".players" || ircCmd == "!players";
DateTime last = opchat ? lastOpWho : lastWho;
if (!whoCmd || (DateTime.UtcNow - last).TotalSeconds <= 1) return false;
try {
Player p = MakeIRCPlayer(nick);
CmdPlayers.DisplayPlayers(p, "", false, false);
} catch (Exception e) {
Server.ErrorLog(e);
}
if (opchat) lastOpWho = DateTime.UtcNow;
else lastWho = DateTime.UtcNow;
return true;
}
bool HandleIRCCommand(string nick, string logNick, string cmdName, string cmdArgs) {
Command cmd = Command.all.Find(cmdName);
Player p = MakeIRCPlayer(nick);
if (cmd == null) { Player.Message(p, "Unknown command!"); return false; }
string logCmd = cmdArgs == "" ? cmdName : cmdName + " " + cmdArgs;
Server.s.Log("IRC Command: /" + logCmd + " (by " + logNick + ")");
try {
if (!p.group.CanExecute(cmd)) { cmd.MessageCannotUse(p); return false; }
cmd.Use(p, cmdArgs);
} catch (Exception ex) {
Say("CMD Error: " + ex, opchat);
Player.Message(p, "CMD Error: " + ex);
}
return true;
}

View File

@ -37,7 +37,7 @@ using System.Runtime.InteropServices;
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("24d9085c-78ba-4f53-b69c-f2b52153683f")]
[assembly: AssemblyVersion("1.8.8.0")]
[assembly: AssemblyVersion("1.8.7.5")]
// Version information for an assembly consists of the following four values:
//