From daa003db1e6d0f114a593287049c91f59c3b84f9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 28 Mar 2016 08:38:09 +1100 Subject: [PATCH] Add custom infection messages to zombie survival, also fix /rp rainbow 2+ not working when the block is not a wool to start with. --- Commands/World/CmdMap.cs | 2 +- Database/PlayerDB.cs | 16 ++++++++-- Economy/Economy.cs | 2 +- Economy/Item.cs | 25 ++++++++-------- Economy/RankItem.cs | 3 +- Economy/ZombieItems.cs | 40 +++++++++++++++++++++++++ Games/ZombieSurvival/ZombieGame.Core.cs | 19 ++++++++---- Levels/Physics/ExtraInfoPhysics.cs | 2 +- Player/Player.Handlers.cs | 1 + Player/Player.cs | 9 +++--- util/CP437Writer.cs | 7 +++++ 11 files changed, 98 insertions(+), 28 deletions(-) diff --git a/Commands/World/CmdMap.cs b/Commands/World/CmdMap.cs index b9482bc92..9bbdef106 100644 --- a/Commands/World/CmdMap.cs +++ b/Commands/World/CmdMap.cs @@ -133,7 +133,7 @@ namespace MCGalaxy.Commands SetBool(p, lvl, ref lvl.leafDecay, "Leaf deacy: "); break; case "flow": case "randomflow": - SetBool(p, lvl, ref lvl.randomFlow, "Ranbow flow: "); break; + SetBool(p, lvl, ref lvl.randomFlow, "Random flow: "); break; case "tree": case "growtrees": SetBool(p, lvl, ref lvl.growTrees, "Tree growing: "); break; diff --git a/Database/PlayerDB.cs b/Database/PlayerDB.cs index a6106d027..119cb88b7 100644 --- a/Database/PlayerDB.cs +++ b/Database/PlayerDB.cs @@ -55,11 +55,23 @@ namespace MCGalaxy { } public static void SetLoginMessage(string name, string value) { - CP437Writer.WriteAllText("text/login/" + name.ToLower() + ".txt", value); + CP437Writer.WriteAllText("text/login/" + name.ToLower() + ".txt", value); } public static void SetLogoutMessage(string name, string value) { - CP437Writer.WriteAllText("text/logout/" + name.ToLower() + ".txt", value); + CP437Writer.WriteAllText("text/logout/" + name.ToLower() + ".txt", value); + } + + public static List GetInfectMessages(Player p) { + if (p.name == null || !Directory.Exists("text/infect")) return null; + string path = "text/infect/" + p.name.ToLower() + ".txt"; + return File.Exists(path) ? CP437Reader.ReadAllLines(path) : null; + } + + public static void AppendInfectMessage(string name, string value) { + if (!Directory.Exists("text/infect")) + Directory.CreateDirectory("text/infect"); + CP437Writer.AppendLine("text/infect/" + name.ToLower() + ".txt", value); } } } \ No newline at end of file diff --git a/Economy/Economy.cs b/Economy/Economy.cs index 73766b256..617c66696 100644 --- a/Economy/Economy.cs +++ b/Economy/Economy.cs @@ -150,7 +150,7 @@ namespace MCGalaxy { public static Item[] Items = { new ColorItem(), new TitleColorItem(), new TitleItem(), new RankItem(), new LevelItem(), new LoginMessageItem(), - new LogoutMessageItem(), new BlocksItem(), new QueueLevelItem() }; + new LogoutMessageItem(), new BlocksItem(), new QueueLevelItem(), new InfectMessageItem() }; public static Item GetItem(string name) { foreach (Item item in Items) { diff --git a/Economy/Item.cs b/Economy/Item.cs index f5b42c073..fcd5314d3 100644 --- a/Economy/Item.cs +++ b/Economy/Item.cs @@ -82,23 +82,23 @@ namespace MCGalaxy.Eco { writer.WriteLine(Name + ":price:" + Price); } - protected internal override void OnBuyCommand(Command cmd, Player p, + protected internal override void OnBuyCommand(Command cmd, Player p, string message, string[] args) { - if (NoArgsResetsItem && args.Length == 1) { - OnBuyCommand(p, message, args); return; - } - // Must always provide an argument. - if (args.Length < 2) { cmd.Help(p); return; } + if (NoArgsResetsItem && args.Length == 1) { + OnBuyCommand(p, message, args); return; + } + // Must always provide an argument. + if (args.Length < 2) { cmd.Help(p); return; } if (p.money < Price) { Player.SendMessage(p, "%cYou don't have enough %3" + Server.moneys + "%c to buy a " + Name + "."); return; } - OnBuyCommand(p, message, args); - } - - protected abstract void OnBuyCommand(Player p, string message, string[] args); + OnBuyCommand(p, message, args); + } + + protected abstract void OnBuyCommand(Player p, string message, string[] args); protected internal override void OnSetupCommand(Player p, string[] args) { - switch (args[1].ToLower()) { + switch (args[1].ToLower()) { case "enable": Player.SendMessage(p, "%a" + Name + "s are now enabled for the economy system."); Enabled = true; break; @@ -118,7 +118,8 @@ namespace MCGalaxy.Eco { } protected internal override void OnStoreCommand(Player p) { - Player.SendMessage(p, Name + "s cost %f" + Price + " %3" + Server.moneys + " %Seach"); + string plural = Name[Name.Length - 1] == 's' ? Name : Name + "s"; + Player.SendMessage(p, plural + " cost %f" + Price + " %3" + Server.moneys + " %Seach"); } } } diff --git a/Economy/RankItem.cs b/Economy/RankItem.cs index 31db9238b..fb6aaf390 100644 --- a/Economy/RankItem.cs +++ b/Economy/RankItem.cs @@ -123,10 +123,9 @@ namespace MCGalaxy.Eco { Group maxrank = Group.Find(MaxRank); Player.SendMessage(p, "%fThe maximum buyable rank is: " + maxrank.color + maxrank.name); Player.SendMessage(p, "%cRanks purchased will be bought in order."); - Player.SendMessage(p, "%fRanks cost:"); foreach (Rank rnk in RanksList) { - Player.SendMessage(p, rnk.group.color + rnk.group.name + ": %f" + rnk.price + " %3" + Server.moneys); + Player.SendMessage(p, rnk.group.color + rnk.group.name + " costs&f" + rnk.price + " &3" + Server.moneys); if (rnk.group.name.CaselessEq(maxrank.name)) break; } } diff --git a/Economy/ZombieItems.cs b/Economy/ZombieItems.cs index 231e055b9..ad85f5ff0 100644 --- a/Economy/ZombieItems.cs +++ b/Economy/ZombieItems.cs @@ -16,6 +16,7 @@ permissions and limitations under the Licenses. */ using System; +using System.Collections.Generic; namespace MCGalaxy.Eco { @@ -64,4 +65,43 @@ namespace MCGalaxy.Eco { MakePurchase(p, Price, "%3QueueLevel: " + message); } } + + public sealed class InfectMessageItem : SimpleItem { + + public InfectMessageItem() { + Aliases = new[] { "infectmessage", "infectmsg" }; + Price = 150; + } + + public override string Name { get { return "InfectMessage"; } } + + static char[] trimChars = { ' ' }; + protected override void OnBuyCommand(Player p, string message, string[] args) { + string text = message.Split(trimChars, 2)[1]; // keep spaces this way + bool hasAToken = false; + for (int i = 0; i < text.Length; i++) { + if (!CheckEscape(text, i, ref hasAToken)) { + Player.SendMessage(p, "You can only use {0} and {1} for tokens in infect messages."); return; + } + } + if (!hasAToken) { + Player.SendMessage(p, "You need to include a \"{0}\" (placeholder for zombie player) " + + "and/or a \"{1}\" (placeholder for human player) in the infect message."); return; + } + + PlayerDB.AppendInfectMessage(p.name, text); + if (p.infectMessages == null) p.infectMessages = new List(); + p.infectMessages.Add(text); + Player.SendMessage(p, "%aAdded infect message: %f" + text); + MakePurchase(p, Price, "%3InfectMessage: " + message); + } + + static bool CheckEscape(string text, int i, ref bool hasAToken) { + // Only {0} and {1} are allowed in infect messages for the Format() call + if (text[i] != '{') return true; + hasAToken = true; + if ((i + 2) >= text.Length) return false; + return (text[i + 1] == '0' || text[i + 1] == '1') && text[i + 2] == '}'; + } + } } diff --git a/Games/ZombieSurvival/ZombieGame.Core.cs b/Games/ZombieSurvival/ZombieGame.Core.cs index 407aae605..b3a56c82c 100644 --- a/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/Games/ZombieSurvival/ZombieGame.Core.cs @@ -186,11 +186,7 @@ namespace MCGalaxy.Games { lastPlayerToInfect = pKiller.name; pKiller.playersInfected++; - CurLevel.ChatLevel(String.Format( - messages[random.Next(messages.Length)], - Colors.red + pKiller.DisplayName + Colors.yellow, - Colors.red + pAlive.DisplayName + Colors.yellow)); - + ShowInfectMessage(random, pAlive, pKiller); CheckHumanPledge(pAlive); CheckBounty(pAlive, pKiller); UpdatePlayerColor(pAlive, InfectCol); @@ -209,6 +205,19 @@ namespace MCGalaxy.Games { pAlive.money = Math.Max(pAlive.money - 2, 0); pAlive.OnMoneyChanged(); } + + void ShowInfectMessage(Random random, Player pAlive, Player pKiller) { + string text = null; + List infectMsgs = pKiller.infectMessages; + if (infectMsgs != null && random.Next(0, 10) < 5) + text = infectMsgs[random.Next(infectMsgs.Count)]; + else + text = messages[random.Next(messages.Length)]; + + CurLevel.ChatLevel(String.Format(text, + Colors.red + pKiller.DisplayName + Colors.yellow, + Colors.red + pAlive.DisplayName + Colors.yellow)); + } void CheckBounty(Player pAlive, Player pKiller) { BountyData bounty; diff --git a/Levels/Physics/ExtraInfoPhysics.cs b/Levels/Physics/ExtraInfoPhysics.cs index 88395fd88..da896726c 100644 --- a/Levels/Physics/ExtraInfoPhysics.cs +++ b/Levels/Physics/ExtraInfoPhysics.cs @@ -179,7 +179,7 @@ namespace MCGalaxy.BlockPhysics { if (rainbownum > 2) { byte block = lvl.blocks[C.b]; if (block < Block.red || block > Block.darkpink) { - lvl.AddUpdate(C.b, Block.red, true); + lvl.AddUpdate(C.b, Block.red, true, C.data); } else { byte next = block == Block.darkpink ? Block.red : (byte)(block + 1); lvl.AddUpdate(C.b, next); diff --git a/Player/Player.Handlers.cs b/Player/Player.Handlers.cs index e9858e2aa..d96096d74 100644 --- a/Player/Player.Handlers.cs +++ b/Player/Player.Handlers.cs @@ -532,6 +532,7 @@ namespace MCGalaxy { } Server.s.Log(name + " [" + ip + "] has joined the server."); + infectMessages = PlayerDB.GetInfectMessages(this); Server.zombie.PlayerJoinedServer(this); try { ushort x = (ushort)((0.5 + level.spawnx) * 32); diff --git a/Player/Player.cs b/Player/Player.cs index 08e47c18e..cb888d1b5 100644 --- a/Player/Player.cs +++ b/Player/Player.cs @@ -188,17 +188,18 @@ namespace MCGalaxy { //Zombie public bool referee = false; - public int blockCount = 50; + internal int blockCount = 50; public bool voted = false; - public int blocksStacked = 0; - public int lastYblock = 0, lastXblock = 0, lastZblock = 0; + internal int blocksStacked = 0; + internal int lastYblock = 0, lastXblock = 0, lastZblock = 0; public bool infected = false; public bool aka = false; public bool flipHead = false; - public int playersInfected = 0; + internal int playersInfected = 0; internal string lastSpawnColor = ""; internal bool ratedMap = false; internal bool pledgeSurvive = false; + internal List infectMessages = null; //Tnt Wars public bool PlayingTntWars = false; diff --git a/util/CP437Writer.cs b/util/CP437Writer.cs index 5010bdaa7..88173f70a 100644 --- a/util/CP437Writer.cs +++ b/util/CP437Writer.cs @@ -63,6 +63,13 @@ namespace MCGalaxy { writer.Write(text); } + public static void AppendLine(string file, string text) { + using (CP437Writer writer = new CP437Writer(file, true)) { + writer.Write(text); + writer.WriteLine(); + } + } + public static string ConvertToUnicode(string text) { if (text == null) return null; if (text.Length == 0) return "";