Better Chat class

This commit is contained in:
UnknownShadow200 2017-03-03 19:24:24 +11:00
parent c52b91feff
commit d1e9862c6b
8 changed files with 56 additions and 48 deletions

View File

@ -18,50 +18,54 @@ using System.Text;
namespace MCGalaxy {
public static class Chat {
public static void GlobalChatLevel(Player from, string message, bool showname) {
public static void GlobalChatLevel(Player source, string message, bool showname) {
if (showname)
message = "<Level>" + from.FullName + ": &f" + message;
message = "<Level>" + source.FullName + ": &f" + message;
Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) {
if (p.level == from.level && p.Chatroom == null)
SendMessage(p, from, message);
if (!NotIgnoring(source, p)) continue;
if (p.level == source.level && p.Chatroom == null)
Player.Message(p, message);
}
}
public static void GlobalChatRoom(Player from, string message, bool showname) {
string rawMessage = message;
if (showname)
message = "<GlobalChatRoom> " + from.FullName + ": &f" + message;
/// <summary> Sends a message to all players who are in any chatroom,
/// and are not ignoring source player. </summary>
/// <remarks> Optionally prefixes message by &lt;GlobalChatRoom&gt; [source name]: </remarks>
public static void MessageAllChatRooms(Player source, string message, bool showPrefix) {
Server.s.Log("<GlobalChatRoom>" + source.name + ": " + message);
if (showPrefix)
message = "<GlobalChatRoom> " + source.FullName + ": &f" + message;
Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) {
if (!NotIgnoring(source, p)) continue;
if (p.Chatroom != null)
SendMessage(p, from, message);
Player.Message(p, message);
}
Server.s.Log("<GlobalChatRoom>" + from.name + ": " + rawMessage);
}
public static void ChatRoom(Player from, string message, bool showname, string chatroom) {
string rawMessage = message;
string messageforspy = "<ChatRoomSPY: " + chatroom + "> " + from.FullName + ": &f" + message;
if (showname)
message = "<ChatRoom: " + chatroom + "> " + from.FullName + ": &f" + message;
/// <summary> Sends a message to all players who are either in or spying on the given chatroom,
/// and are not ignoring source player. </summary>
/// <remarks> Optionally prefixes message by &lt;ChatRoom: [chatRoom]&gt; [source name]: </remarks>
public static void MessageChatRoom(Player source, string message, bool showPrefix, string chatRoom) {
Server.s.Log("<ChatRoom " + chatRoom + ">" + source.name + ": " + message);
string spyMessage = "<ChatRoomSPY: " + chatRoom + "> " + source.FullName + ": &f" + message;
if (showPrefix)
message = "<ChatRoom: " + chatRoom + "> " + source.FullName + ": &f" + message;
Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) {
if (p.Chatroom == chatroom)
SendMessage(p, from, message);
if (p.spyChatRooms.Contains(chatroom) && p.Chatroom != chatroom)
SendMessage(p, from, messageforspy);
if (!NotIgnoring(source, p)) continue;
if (p.Chatroom == chatRoom)
Player.Message(p, message);
if (p.spyChatRooms.Contains(chatRoom) && p.Chatroom != chatRoom)
Player.Message(p, spyMessage);
}
Server.s.Log("<ChatRoom " + chatroom + ">" + from.name + ": " + rawMessage);
}
static void SendMessage(Player p, Player from, string message) {
if (from != null && p.listignored.Contains(from.name)) return;
if (!p.ignoreAll || (from != null && from == p))
Player.Message(p, Server.DefaultColor + message);
}
@ -101,6 +105,7 @@ namespace MCGalaxy {
}
}
public static string Format(string message, Player p, bool colors = true,
bool tokens = true, bool emotes = true) {
if (colors) message = Colors.EscapeColors(message);
@ -139,6 +144,13 @@ namespace MCGalaxy {
}
}
/// <summary> Returns true if the target player can see chat messags by source. </summary>
public static bool NotIgnoring(Player source, Player target) {
if (target.ignoreAll) return source == target; // don't ignore messages from self
return source == null || !target.listignored.Contains(source.name);
}
#region Format helpers

View File

@ -76,10 +76,8 @@ namespace MCGalaxy {
string format = "To " + group + " &f-{0}&f- {1}";
Chat.MessageWhere(format,
pl => pl.Rank >= perm && !pl.listignored.Contains(name),
pl => (p == pl || pl.Rank >= perm) && Chat.NotIgnoring(p, pl),
displayName, message);
if (p != null && p.Rank < perm)
Player.Message(p, format, displayName, message);
Server.s.Log("(" + group + "): " + name + ": " + message);
Server.IRC.Say(displayName + "%S: " + message, true);

View File

@ -89,7 +89,7 @@ namespace MCGalaxy {
string line;
while ((line = r.ReadLine()) != null) {
if (line.StartsWith("//")) continue;
string[] split = line.Split(new[] char{ ':' }, 2);
string[] split = line.Split(new char[] { ':' }, 2);
if (split.Length == 2 && !String.IsNullOrEmpty(split[0]))
CustomTokens.Add(split[0], split[1]);
}

View File

@ -90,7 +90,7 @@ namespace MCGalaxy.Commands {
}
Player.Message(p, "You joined the chat room '{0}'", room);
Chat.ChatRoom(p, p.ColoredName + " %Shas joined your chat room", false, room);
Chat.MessageChatRoom(p, p.ColoredName + " %Shas joined your chat room", false, room);
p.Chatroom = room;
} else {
Player.Message(p, "There is no chat room with that name");
@ -99,7 +99,7 @@ namespace MCGalaxy.Commands {
void HandleLeave(Player p) {
Player.Message(p, "You left the chat room '{0}'", p.Chatroom);
Chat.ChatRoom(p, p.ColoredName + " %Shas left the chat room", false, p.Chatroom);
Chat.MessageChatRoom(p, p.ColoredName + " %Shas left the chat room", false, p.Chatroom);
Chat.MessageAll("{0} %Shas left their chat room {1}", p.ColoredName, p.Chatroom);
p.Chatroom = null;
}
@ -210,7 +210,7 @@ namespace MCGalaxy.Commands {
}
Player.Message(pl, "You've been forced to join the chat room '{0}'", room);
Chat.ChatRoom(pl, pl.ColoredName + " %Shas force joined your chat room", false, room);
Chat.MessageChatRoom(pl, pl.ColoredName + " %Shas force joined your chat room", false, room);
pl.Chatroom = room;
Player.Message(p, pl.ColoredName + " %Swas forced to join the chatroom '{0}' by you", room);
}
@ -231,7 +231,7 @@ namespace MCGalaxy.Commands {
Player.Message(pl, "You were kicked from the chat room '" + pl.Chatroom + "'");
Player.Message(p, pl.ColoredName + " %Swas kicked from the chat room '" + pl.Chatroom + "'");
Chat.ChatRoom(pl, pl.ColoredName + " %Swas kicked from your chat room", false, pl.Chatroom);
Chat.MessageChatRoom(pl, pl.ColoredName + " %Swas kicked from your chat room", false, pl.Chatroom);
pl.Chatroom = null;
}
@ -239,12 +239,12 @@ namespace MCGalaxy.Commands {
int length = parts.Length > 1 ? parts[0].Length + 1 : parts[0].Length;
message = message.Substring( length );
if (CheckExtraPerm(p, 7)) {
Chat.GlobalChatRoom(p, message, true);
Chat.MessageAllChatRooms(p, message, true);
return;
}
if (p.lastchatroomglobal.AddSeconds(30) < DateTime.UtcNow) {
Chat.GlobalChatRoom(p, message, true);
Chat.MessageAllChatRooms(p, message, true);
p.lastchatroomglobal = DateTime.UtcNow;
} else {
Player.Message(p, "Sorry, you must wait 30 seconds in between each global chatroom message!!");

View File

@ -95,7 +95,7 @@ namespace MCGalaxy.Commands.Moderation {
static void Delete(Player p, string name) {
bool assigned = false;
StringBuilder all = new StringBuilder();
Player who = PlayerInfo.Find(name);
Player who = PlayerInfo.FindExact(name);
foreach (string line in File.ReadAllLines(Paths.TempRanksFile)) {
if (!line.CaselessStarts(name)) { all.AppendLine(line); continue; }

View File

@ -27,11 +27,8 @@ namespace MCGalaxy.Commands.Moderation {
public override void Use(Player p, string message) {
if (message == "") { Help(p); return; }
string[] args = message.SplitSpaces(2);
Player who = PlayerInfo.Find(args[0]);
string name = who == null ? args[0] : who.name;
string reason = args.Length > 1 ? args[1] : "(none given)";
Unban(p, name, reason);
Unban(p, args[0], reason);
}
void Unban(Player p, string name, string reason) {

View File

@ -565,7 +565,7 @@ namespace MCGalaxy {
}
text = HandleJoker(text);
if (Chatroom != null) { Chat.ChatRoom(this, text, true, Chatroom); return; }
if (Chatroom != null) { Chat.MessageChatRoom(this, text, true, Chatroom); return; }
if (!level.worldChat) {
Server.s.Log("<" + name + ">[level] " + text);

View File

@ -38,6 +38,7 @@ namespace MCGalaxy {
}
[Obsolete("Prefer FindMatches() or FindExact()")]
public static Player Find(string name) {
Player[] players = PlayerInfo.Online.Items;
Player match = null; int matches = 0;