Modularise chat tokens, also add description for what each chat token does

This commit is contained in:
UnknownShadow200 2017-07-25 09:14:59 +10:00
parent 659ad6577e
commit af4ade0e88
3 changed files with 77 additions and 45 deletions

View File

@ -19,6 +19,16 @@ using System.Text;
using MCGalaxy.Util; using MCGalaxy.Util;
namespace MCGalaxy { namespace MCGalaxy {
public sealed class ChatToken {
public readonly string Trigger;
public readonly string Description;
public readonly StringFormatter<Player> Formatter;
public ChatToken(string trigger, string desc, StringFormatter<Player> formatter) {
Trigger = trigger; Description = desc; Formatter = formatter;
}
}
public static class ChatTokens { public static class ChatTokens {
public static string Apply(string text, Player p) { public static string Apply(string text, Player p) {
@ -32,59 +42,71 @@ namespace MCGalaxy {
// only apply standard $tokens when necessary // only apply standard $tokens when necessary
for (int i = 0; i < sb.Length; i++) { for (int i = 0; i < sb.Length; i++) {
if (sb[i] != '$') continue; if (sb[i] != '$') continue;
ApplyStandard(sb, p); break;
foreach (var token in standardTokens) {
if (ServerConfig.DisabledChatTokens.Contains(token.Key)) continue;
string value = token.Value(p);
if (value == null) continue;
sb.Replace(token.Key, value);
}
break;
} }
foreach (var token in CustomTokens) ApplyCustom(sb);
sb.Replace(token.Key, token.Value);
} }
public static string ApplyCustom(string text) { public static string ApplyCustom(string text) {
if (CustomTokens.Count == 0) return text; if (Custom.Count == 0) return text;
StringBuilder sb = new StringBuilder(text); StringBuilder sb = new StringBuilder(text);
foreach (var token in CustomTokens) ApplyCustom(sb);
sb.Replace(token.Key, token.Value);
return sb.ToString(); return sb.ToString();
} }
static void ApplyStandard(StringBuilder sb, Player p) {
foreach (ChatToken token in Standard) {
if (ServerConfig.DisabledChatTokens.Contains(token.Trigger)) continue;
string value = token.Formatter(p);
if (value != null) sb.Replace(token.Trigger, value);
}
}
internal static Dictionary<string, StringFormatter<Player>> standardTokens static void ApplyCustom(StringBuilder sb) {
= new Dictionary<string, StringFormatter<Player>> { foreach (ChatToken token in Custom) {
{ "$name", p => p.DisplayName == null ? null : sb.Replace(token.Trigger, token.Description);
(ServerConfig.DollarBeforeNamesToken ? "$" : "") + Colors.StripColors(p.DisplayName) }, }
{ "$truename", p => p.truename == null ? null : }
(ServerConfig.DollarBeforeNamesToken ? "$" : "") + p.truename },
{ "$date", p => DateTime.Now.ToString("yyyy-MM-dd") },
{ "$time", p => DateTime.Now.ToString("HH:mm:ss") }, public static List<ChatToken> Standard = new List<ChatToken>() {
{ "$ip", p => p.ip }, new ChatToken("$name", "Nickname of the player",
{ "$serverip", p => Player.IsLocalIpAddress(p.ip) ? p.ip : Server.IP }, p => (ServerConfig.DollarBeforeNamesToken ? "$" : "") + Colors.StripColors(p.DisplayName)),
{ "$color", p => p.color }, new ChatToken("$truename", "Account name of the player",
{ "$rank", p => p.group == null ? null : p.group.Name }, p => (ServerConfig.DollarBeforeNamesToken ? "$" : "") + p.truename),
{ "$level", p => p.level == null ? null : p.level.name }, new ChatToken("$date", "Current date (year-month-day)",
p => DateTime.Now.ToString("yyyy-MM-dd")),
new ChatToken("$time", "Current time of day (hour:minute:second)",
p => DateTime.Now.ToString("HH:mm:ss")),
new ChatToken("$ip", "IP of the player", p => p.ip),
new ChatToken("$serverip", "IP player connected to the server via",
p => Player.IsLocalIpAddress(p.ip) ? p.ip : Server.IP),
new ChatToken("$color", "Color code of the player's nick", p => p.color),
new ChatToken("$rank", "Name of player's rank/group", p => p.group.Name),
new ChatToken("$level", "Name of level/map player is on",
p => p.level == null ? null : p.level.name),
{ "$deaths", p => p.TimesDied.ToString() }, new ChatToken("$deaths", "Times the player died",
{ "$money", p => p.money.ToString() }, p => p.TimesDied.ToString()),
{ "$blocks", p => p.TotalModified.ToString() }, new ChatToken("$money", "Amount of server currency player has",
{ "$first", p => p.FirstLogin.ToString() }, p => p.money.ToString()),
{ "$kicked", p => p.TimesBeenKicked.ToString() }, new ChatToken("$blocks", "Number of blocks modified by the player",
{ "$server", p => ServerConfig.Name }, p => p.TotalModified.ToString()),
{ "$motd", p => ServerConfig.MOTD }, new ChatToken("$first", "Date player first logged in",
{ "$banned", p => Group.BannedRank.Players.Count.ToString() }, p => p.FirstLogin.ToString()),
{ "$irc", p => ServerConfig.IRCServer + " > " + ServerConfig.IRCChannels }, new ChatToken("$kicked", "Times the player was kicked",
p => p.TimesBeenKicked.ToString()),
{ "$infected", p => p.Game.TotalInfected.ToString() }, new ChatToken("$server", "Server's name", p => ServerConfig.Name),
{ "$survived", p => p.Game.TotalRoundsSurvived.ToString() }, new ChatToken("$motd", "Server's motd", p => ServerConfig.MOTD),
new ChatToken("$banned", "Number of banned players",
p => Group.BannedRank.Players.Count.ToString()),
new ChatToken("$irc", "IRC server and channels",
p => ServerConfig.IRCServer + " > " + ServerConfig.IRCChannels),
}; };
public static Dictionary<string, string> CustomTokens = new Dictionary<string, string>(); public static List<ChatToken> Custom = new List<ChatToken>();
internal static void LoadCustom() { internal static void LoadCustom() {
CustomTokens.Clear(); Custom.Clear();
TextFile tokensFile = TextFile.Files["Custom $s"]; TextFile tokensFile = TextFile.Files["Custom $s"];
tokensFile.EnsureExists(); tokensFile.EnsureExists();
@ -99,7 +121,7 @@ namespace MCGalaxy {
string key = parts[0].Trim(), value = parts[1].Trim(); string key = parts[0].Trim(), value = parts[1].Trim();
if (key.Length == 0) continue; if (key.Length == 0) continue;
CustomTokens.Add(key, value); Custom.Add(new ChatToken(key, value, null));
} }
} }
} }

View File

@ -46,10 +46,10 @@ namespace MCGalaxy.Commands.Building {
data.Block = GetBlock(p, block); data.Block = GetBlock(p, block);
if (data.Block == ExtBlock.Invalid) return; if (data.Block == ExtBlock.Invalid) return;
if (!CommandParser.IsBlockAllowed(p, "place a portal of", data.Block)) return; if (!CommandParser.IsBlockAllowed(p, "place a portal of", data.Block)) return;
data.Entries = new List<PortalPos>();
Player.Message(p, "Place an &aEntry block %Sfor the portal"); Player.Message(p, "Place an &aEntry block %Sfor the portal");
p.ClearBlockchange(); p.ClearBlockchange();
data.Entries = new List<PortalPos>();
p.blockchangeObject = data; p.blockchangeObject = data;
p.Blockchange += EntryChange; p.Blockchange += EntryChange;
} }

View File

@ -64,7 +64,7 @@ namespace MCGalaxy.Games {
CurLevel = level; CurLevel = level;
} }
CurLevel.SaveChanges = false; CurLevel.SaveChanges = false;
Chat.MessageGlobal("A game of zombie survival is starting on: {0}", CurLevelName); Chat.MessageGlobal("A game of zombie survival is starting on: {0}", CurLevelName);
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) { foreach (Player p in players) {
@ -312,6 +312,7 @@ namespace MCGalaxy.Games {
TopStat statMostInfected, statMaxInfected, statMostSurvived, statMaxSurvived; TopStat statMostInfected, statMaxInfected, statMostSurvived, statMaxSurvived;
OfflineStatPrinter offlineZSStats; OfflineStatPrinter offlineZSStats;
OnlineStatPrinter onlineZSStats; OnlineStatPrinter onlineZSStats;
ChatToken infectedToken, survivedToken;
void HookStats() { void HookStats() {
if (TopStat.Stats.Contains(statMostInfected)) return; // don't duplicate if (TopStat.Stats.Contains(statMostInfected)) return; // don't duplicate
@ -324,11 +325,18 @@ namespace MCGalaxy.Games {
statMaxSurvived = new TopStat("ConsecutiveSurvived", "ZombieStats", "MaxRounds", statMaxSurvived = new TopStat("ConsecutiveSurvived", "ZombieStats", "MaxRounds",
() => "Most consecutive rounds survived", TopStat.FormatInteger); () => "Most consecutive rounds survived", TopStat.FormatInteger);
infectedToken = new ChatToken("$infected", "Total number of players infected",
p => p.Game.TotalInfected.ToString());
survivedToken = new ChatToken("$survived", "Total number of rounds survived",
p => p.Game.TotalRoundsSurvived.ToString());
offlineZSStats = PrintOfflineZSStats; offlineZSStats = PrintOfflineZSStats;
onlineZSStats = PrintOnlineZSStats; onlineZSStats = PrintOnlineZSStats;
OfflineStat.Stats.Add(offlineZSStats); OfflineStat.Stats.Add(offlineZSStats);
OnlineStat.Stats.Add(onlineZSStats); OnlineStat.Stats.Add(onlineZSStats);
ChatTokens.Standard.Add(infectedToken);
ChatTokens.Standard.Add(survivedToken);
TopStat.Stats.Add(statMostInfected); TopStat.Stats.Add(statMostInfected);
TopStat.Stats.Add(statMostSurvived); TopStat.Stats.Add(statMostSurvived);
TopStat.Stats.Add(statMaxInfected); TopStat.Stats.Add(statMaxInfected);
@ -338,6 +346,8 @@ namespace MCGalaxy.Games {
void UnhookStats() { void UnhookStats() {
OfflineStat.Stats.Remove(offlineZSStats); OfflineStat.Stats.Remove(offlineZSStats);
OnlineStat.Stats.Remove(onlineZSStats); OnlineStat.Stats.Remove(onlineZSStats);
ChatTokens.Standard.Remove(infectedToken);
ChatTokens.Standard.Remove(survivedToken);
TopStat.Stats.Remove(statMostInfected); TopStat.Stats.Remove(statMostInfected);
TopStat.Stats.Remove(statMostSurvived); TopStat.Stats.Remove(statMostSurvived);