Refactor PlayerInfo.FindOrShowMatches into a generic method, which is now also used for level names and bot names.

This commit is contained in:
UnknownShadow200 2016-05-04 18:55:18 +10:00
parent 62dcb0888c
commit eda3b97fcc
21 changed files with 126 additions and 147 deletions

View File

@ -460,16 +460,22 @@ namespace MCGalaxy {
public static PlayerBot Find(string name) {
PlayerBot match = null; int matches = 0;
name = name.ToLower();
PlayerBot[] bots = Bots.Items;
foreach (PlayerBot bot in bots) {
if (bot.name.CaselessEq(name)) return bot;
if (bot.name.ToLower() == name) return bot;
if (bot.name.ToLower().Contains(name)) {
match = bot; matches++;
match = bot; matches++;
}
}
return matches == 1 ? match : null;
}
public static PlayerBot FindOrShowMatches(Player pl, string name) {
int matches = 0;
return Extensions.FindOrShowMatches(pl, name, out matches, Bots.Items, b => true,
b => b.name, "bots");
}
#endregion
public static void GlobalUpdatePosition() {

View File

@ -44,8 +44,8 @@ namespace MCGalaxy.Commands {
string model = null;
if (isBot && args.Length > 2) {
pBot = PlayerBot.Find(args[1]);
if (pBot == null) { Player.SendMessage(p, "There is no bot with that name."); return; }
pBot = PlayerBot.FindOrShowMatches(p, args[1]);
if (pBot == null) return;
model = args[2];
} else if (args.Length > 1) {
isBot = false;

View File

@ -44,8 +44,8 @@ namespace MCGalaxy.Commands {
string skin = null;
if (isBot && args.Length > 2) {
pBot = PlayerBot.Find(args[1]);
if (pBot == null) { Player.SendMessage(p, "There is no bot with that name."); return; }
pBot = PlayerBot.FindOrShowMatches(p, args[1]);
if (pBot == null) return;
skin = args[2];
} else if (args.Length >= 2) {
isBot = false;

View File

@ -106,11 +106,9 @@ namespace MCGalaxy.Commands
if (s[1] == "map")
{
if (s.Length < 3) { SetupHelp(p, "map"); return; }
Level foundLevel = LevelInfo.Find(s[2]);
if (foundLevel == null)
{
Player.SendMessage(p, "The level must be loaded to add/remove it.");
return;
Level foundLevel = LevelInfo.FindOrShowMatches(p, s[2]);
if (foundLevel == null) {
return;
}
else
{

View File

@ -110,24 +110,18 @@ namespace MCGalaxy.Commands
}
else
{
Level lvl = LevelInfo.Find(text[1]);
if (lvl == null)
Level lvl = LevelInfo.FindOrShowMatches(p, text[1]);
if (lvl == null) return;
it = TntWarsGame.Find(lvl);
if (it == null)
{
Player.SendMessage(p, "TNT Wars Error: Couldn't find level '" + text[1] + "'");
Player.SendMessage(p, "TNT Wars Error: There isn't a game on that level!");
return;
}
else
{
it = TntWarsGame.Find(lvl);
if (it == null)
{
Player.SendMessage(p, "TNT Wars Error: There isn't a game on that level!");
return;
}
else
{
text[1] = text[2]; //so the switch later on still works
}
text[1] = text[2]; //so the switch later on still works
}
}
TntWarsGame.player pl = new TntWarsGame.player(p);
@ -673,12 +667,8 @@ namespace MCGalaxy.Commands
}
else
{
it.lvl = LevelInfo.Find(text[2]);
if (it.lvl == null)
{
Player.SendMessage(p, "TNT Wars Error: '" + text[2] + "' is not a level!");
return;
}
it.lvl = LevelInfo.FindOrShowMatches(p, text[2]);
if (it.lvl == null) return;
}
Player.SendMessage(p, "TNT Wars: Level is now '" + it.lvl.name + "'");
it.RedSpawn = null;

View File

@ -37,10 +37,8 @@ namespace MCGalaxy.Commands {
}
lvl = p.level;
} else {
lvl = LevelInfo.Find(message);
if (lvl == null || !File.Exists("levels/" + message + ".lvl")) {
Player.SendMessage(p, "&9The level, &c" + message + " &9does not exist!"); return;
}
lvl = LevelInfo.FindOrShowMatches(p, message);
if (lvl == null) return;
}
if (lvl.guns) {

View File

@ -34,8 +34,8 @@ namespace MCGalaxy.Commands {
PlayerBot.RemoveAllFromLevel(p.level); return;
}
PlayerBot who = PlayerBot.Find(message);
if (who == null) { Player.SendMessage(p, "There is no bot " + who + "!"); return; }
PlayerBot who = PlayerBot.FindOrShowMatches(p, message);
if (who == null) return;
if (!p.level.name.CaselessEq(who.name)) {
Player.SendMessage(p, who.name + " is in a different level."); return;
}

View File

@ -30,8 +30,8 @@ namespace MCGalaxy.Commands
if (message == "") { Help(p); return; }
if (p == null) { MessageInGameOnly(p); return; }
PlayerBot who = PlayerBot.Find(message);
if (who == null) { Player.SendMessage(p, "There is no bot " + message + "!"); return; }
PlayerBot who = PlayerBot.FindOrShowMatches(p, message);
if (who == null) return;
if (p.level != who.level) { Player.SendMessage(p, who.name + " is in a different level."); return; }
who.SetPos(p.pos[0], p.pos[1], p.pos[2], p.rot[0], p.rot[1]);
}

View File

@ -25,19 +25,21 @@ namespace MCGalaxy.Commands
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override void Use(Player p, string message)
{
Level level = LevelInfo.Find(message.Split(' ')[0]);
if (level == null) { Player.SendMessage(p, "There is no level named '" + message.Split(' ')[0] + "'."); return; }
public override void Use(Player p, string message) {
Level level = LevelInfo.FindOrShowMatches(p, message);
if (level == null) return;
Player[] players = PlayerInfo.Online.Items;
foreach (Player pl in players) {
if (p == null || pl.group.Permission < p.group.Permission)
Command.all.Find("move").Use(p, pl.name + " " + level.name);
else
Player.SendMessage(p, "You cannot move " + pl.color + pl.name + " %Sbecause they are of equal or higher rank");
Player.SendMessage(p, "You cannot move " + pl.ColoredName + " %Sbecause they are of equal or higher rank");
}
}
public override void Help(Player p) { Player.SendMessage(p, "/moveall <level> - Moves all players to the level specified."); }
public override void Help(Player p) {
Player.SendMessage(p, "/moveall <level> - Moves all players to the level specified.");
}
}
}

View File

@ -23,11 +23,12 @@ namespace MCGalaxy.Commands
public override bool museumUsable { get { return true; } }
public override string name { get { return "punload"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Moderation; } }
public override string type { get { return CommandTypes.Moderation; } }
public override void Use(Player p, string message)
{
if (Plugin.Find(message) != null)
Plugin.Unload(Plugin.Find(message), false);
Plugin plugin = Plugin.Find(message);
if (plugin != null)
Plugin.Unload(plugin, false);
else
Player.SendMessage(p, "That plugin is not loaded!");
}

View File

@ -31,8 +31,8 @@ namespace MCGalaxy.Commands {
public override void Use(Player p, string message) {
if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; }
string[] args = message.Split(' ');
Level lvl = LevelInfo.Find(args[0]);
if (lvl == null) { Player.SendMessage(p, "Level not found"); return; }
Level lvl = LevelInfo.FindOrShowMatches(p, args[0]);
if (lvl == null) return;
string newName = args[1];
if (!Player.ValidName(newName)) {
Player.SendMessage(p, "\"" + newName + "\" is not a valid level name."); return;

View File

@ -32,10 +32,8 @@ namespace MCGalaxy.Commands {
}
Level lvl = p == null ? null : p.level;
if (message != "") {
lvl = LevelInfo.Find(message.ToLower());
if (lvl == null) {
Player.SendMessage(p, "Could not find the entered level."); return;
}
lvl = LevelInfo.FindOrShowMatches(p, message);
if (lvl == null) return;
}
if ( Server.useMySQL )

View File

@ -35,19 +35,17 @@ namespace MCGalaxy.Commands
if (parts.Length == 1) {
if (!int.TryParse(parts[0], out seconds)) {
seconds = 30;
lvl = LevelInfo.Find(parts[0]);
lvl = LevelInfo.FindOrShowMatches(p, parts[0]);
if (lvl == null) return;
}
} else {
if (!int.TryParse(parts[0], out seconds)) {
Player.SendMessage(p, "You must specify pause time in seconds"); return;
}
lvl = LevelInfo.Find(parts[1]);
lvl = LevelInfo.FindOrShowMatches(p, parts[1]);
if (lvl == null) return;
}
}
if (lvl == null) {
Player.SendMessage(p, "Could not find entered level."); return;
}
bool enabled = lvl.physPause;
lvl.PhysicsEnabled = enabled;

View File

@ -48,10 +48,8 @@ namespace MCGalaxy.Commands
}
if (args.Length == 2) {
level = LevelInfo.Find(args[0].ToLower());
if (level == null) {
Player.SendMessage(p, "Could not find entered level."); return;
}
level = LevelInfo.FindOrShowMatches(p, args[0]);
if (level == null) return;
}
level.setPhysics(state);

View File

@ -46,14 +46,8 @@ namespace MCGalaxy.Commands
}
if (message.Split(' ').Length >= 2)
{
lvl = LevelInfo.Find(text[1]);
if (lvl == null)
{
Player.SendMessage(p, "Level not found!");
return;
}
lvl = LevelInfo.FindOrShowMatches(p, text[1]);
if (lvl == null) return;
}
else
{

View File

@ -33,47 +33,46 @@ namespace MCGalaxy.Commands {
public override void Use(Player p, string message) {
if (message == "") { Help(p); return; }
string[] args = message.Split(' ');
if (args.Length == 1) {
PlayerBot pB = PlayerBot.Find(message);
try { pB.Waypoints.Clear(); } catch { }
pB.kill = false;
pB.hunt = false;
pB.AIName = "";
Player.SendMessage(p, pB.color + pB.name + "%S's AI was turned off.");
Server.s.Log(pB.name + "'s AI was turned off.");
PlayerBot bot = PlayerBot.FindOrShowMatches(p, args[0]);
if (bot == null) return;
if (args.Length == 1) {
try { bot.Waypoints.Clear(); } catch { }
bot.kill = false;
bot.hunt = false;
bot.AIName = "";
Player.SendMessage(p, bot.color + bot.name + "%S's AI was turned off.");
Server.s.Log(bot.name + "'s AI was turned off.");
return;
} else if (args.Length != 2) {
Help(p); return;
}
PlayerBot Pb = PlayerBot.Find(args[0]);
if (Pb == null) { Player.SendMessage(p, "Could not find specified Bot"); return; }
string ai = args[1].ToLower();
if (ai == "hunt") {
Pb.hunt = !Pb.hunt;
try { Pb.Waypoints.Clear(); }
bot.hunt = !bot.hunt;
try { bot.Waypoints.Clear(); }
catch { }
Pb.AIName = "";
if (p != null) Chat.GlobalChatLevel(p, Pb.color + Pb.name + "%S's hunt instinct: " + Pb.hunt, false);
Server.s.Log(Pb.name + "'s hunt instinct: " + Pb.hunt);
BotsFile.UpdateBot(Pb);
bot.AIName = "";
if (p != null) Chat.GlobalChatLevel(p, bot.color + bot.name + "%S's hunt instinct: " + bot.hunt, false);
Server.s.Log(bot.name + "'s hunt instinct: " + bot.hunt);
BotsFile.UpdateBot(bot);
return;
} else if (ai == "kill") {
if (!CheckAdditionalPerm(p)) { MessageNeedPerms(p, "can toggle a bot's killer instinct."); return; }
Pb.kill = !Pb.kill;
if (p != null) Chat.GlobalChatLevel(p, Pb.color + Pb.name + "%S's kill instinct: " + Pb.kill, false);
Server.s.Log(Pb.name + "'s kill instinct: " + Pb.kill);
BotsFile.UpdateBot(Pb);
bot.kill = !bot.kill;
if (p != null) Chat.GlobalChatLevel(p, bot.color + bot.name + "%S's kill instinct: " + bot.kill, false);
Server.s.Log(bot.name + "'s kill instinct: " + bot.kill);
BotsFile.UpdateBot(bot);
return;
}
if (!BotScript.Parse(p, Pb, "bots/" + ai)) return;
Pb.AIName = ai;
if (p != null) Chat.GlobalChatLevel(p, Pb.color + Pb.name + "%S's AI is now set to " + ai, false);
Server.s.Log(Pb.name + "'s AI was set to " + ai);
BotsFile.UpdateBot(Pb);
if (!BotScript.Parse(p, bot, "bots/" + ai)) return;
bot.AIName = ai;
if (p != null) Chat.GlobalChatLevel(p, bot.color + bot.name + "%S's AI is now set to " + ai, false);
Server.s.Log(bot.name + "'s AI was set to " + ai);
BotsFile.UpdateBot(bot);
}
public override void Help(Player p) {

View File

@ -48,9 +48,8 @@ namespace MCGalaxy.Commands
if (param.Length == 2) // /move name map
{
Player who = PlayerInfo.FindOrShowMatches(p, param[0]);
Level where = LevelInfo.Find(param[1]);
if (who == null) return;
if (where == null) { Player.SendMessage(p, "Could not find level specified"); return; }
Level where = LevelInfo.FindOrShowMatches(p, param[1]);
if (who == null || where == null) return;
if (p != null && who.group.Permission > p.group.Permission) {
MessageTooHighRank(p, "move", true); return;
}

View File

@ -40,6 +40,16 @@ namespace MCGalaxy {
}
return matches == 1 ? match : null;
}
public static Level FindOrShowMatches(Player pl, string name) {
int matches = 0;
return FindOrShowMatches(pl, name, out matches);
}
public static Level FindOrShowMatches(Player pl, string name, out int matches) {
return Extensions.FindOrShowMatches(pl, name, out matches, LevelInfo.Loaded.Items,
l => true, l => l.name, "loaded levels");
}
public static Level FindExact(string name) {
Level[] loaded = Loaded.Items;

View File

@ -54,31 +54,9 @@ namespace MCGalaxy {
}
public static Player FindOrShowMatches(Player pl, string name, out int matches, bool onlyCanSee = true) {
Player[] players = PlayerInfo.Online.Items;
Player match = null; matches = 0;
name = name.ToLower();
StringBuilder matchNames = new StringBuilder();
foreach (Player p in players) {
if (onlyCanSee && !Entities.CanSee(pl, p)) continue;
if (p.name.Equals(name, comp)) return p;
if (p.name.IndexOf(name, comp) >= 0) {
match = p; matches++;
if (matches <= 5)
matchNames.Append(p.name).Append(", ");
else if (matches == 6)
matchNames.Append("(and more)").Append(", ");
}
}
if (matches == 0) {
Player.SendMessage(pl, "No online players found matching \"" + name + "\"."); return null;
} else if (matches == 1) {
return match;
} else {
string names = matchNames.ToString(0, matchNames.Length - 2);
Player.SendMessage(pl, "Multiple players found matching \"" + name + "\":");
Player.SendMessage(pl, names); return null;
}
return Extensions.FindOrShowMatches(pl, name, out matches, Online.Items,
p => Entities.CanSee(pl, p) || !onlyCanSee,
p => p.name, "online players");
}
public static Player FindExact(string name) {

View File

@ -87,9 +87,7 @@ namespace MCGalaxy
#endregion
#region Plugin Find
/// <summary>
/// Look to see if a plugin is loaded
/// </summary>
/// <summary> Look to see if a plugin is loaded </summary>
/// <param name="name">The name of the plugin</param>
/// <returns>Returns the plugin (returns null if non is found)</returns>
public static Plugin Find(string name)

View File

@ -120,24 +120,6 @@ namespace MCGalaxy {
return sb.ToString();
}
public static void DeleteLine(string file, string line)
{
var complete = from selectLine in File.ReadAllLines(file) where selectLine != line select selectLine;
File.WriteAllLines(file, complete.ToArray());
}
public static void DeleteLineWord(string file, string word)
{
var complete = from selectLine in File.ReadAllLines(file) where !selectLine.Contains(word) select selectLine;
File.WriteAllLines(file, complete.ToArray());
}
public static void DeleteExactLineWord(string file, string word)
{
var complete = from selectLine in File.ReadAllLines(file) where !selectLine.Equals(word) select selectLine;
File.WriteAllLines(file, complete.ToArray());
}
public static void UncapitalizeAll(string file)
{
string[] complete = File.ReadAllLines(file);
@ -204,5 +186,35 @@ namespace MCGalaxy {
*srcByte = value; srcByte++;
}
}
public static T FindOrShowMatches<T>(Player pl, string name, out int matches, T[] items,
Predicate<T> filter, Func<T, string> nameGetter, string type) {
T match = default(T); matches = 0;
name = name.ToLower();
StringBuilder matchNames = new StringBuilder();
foreach (T item in items) {
if (!filter(item)) continue;
string itemName = nameGetter(item);
if (itemName.Equals(name, comp)) return item;
if (itemName.IndexOf(name, comp) < 0) continue;
match = item; matches++;
if (matches <= 5)
matchNames.Append(itemName).Append(", ");
else if (matches == 6)
matchNames.Append("(and more)").Append(", ");
}
if (matches == 0) {
Player.SendMessage(pl, "No " + type + " match \"" + name + "\"."); return default(T);
} else if (matches == 1) {
return match;
} else {
string names = matchNames.ToString(0, matchNames.Length - 2);
Player.SendMessage(pl, "Multiple " + type + " match \"" + name + "\":");
Player.SendMessage(pl, names); return default(T);
}
}
}
}