From 3a1378337c1d09079db64deec2fc319741eee041 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 14 Apr 2016 17:00:55 +1000 Subject: [PATCH] Cleanup Awards.cs. --- Commands/Economy/CmdAward.cs | 6 +- Commands/Economy/CmdAwardMod.cs | 4 +- Commands/Economy/CmdAwards.cs | 20 +-- Commands/Information/CmdWhois.cs | 2 +- Commands/Information/CmdWhowas.cs | 2 +- Economy/Awards.cs | 263 +++++++++++++++--------------- Player/Player.Timers.cs | 2 +- 7 files changed, 146 insertions(+), 153 deletions(-) diff --git a/Commands/Economy/CmdAward.cs b/Commands/Economy/CmdAward.cs index ca178b1f7..7cb2b017c 100644 --- a/Commands/Economy/CmdAward.cs +++ b/Commands/Economy/CmdAward.cs @@ -46,7 +46,7 @@ namespace MCGalaxy.Commands Player who = PlayerInfo.Find(message); if (who != null) foundPlayer = who.name; string awardName = message.Substring(message.IndexOf(' ') + 1); - if (!Awards.awardExists(awardName)) + if (!Awards.ExistsAward(awardName)) { Player.SendMessage(p, "The award you entered doesn't exist"); Player.SendMessage(p, "Use /awards for a list of awards"); @@ -55,7 +55,7 @@ namespace MCGalaxy.Commands if (give) { - if (Awards.giveAward(foundPlayer, awardName)) + if (Awards.GiveAward(foundPlayer, awardName)) { Player.GlobalMessage(Server.FindColor(foundPlayer) + foundPlayer + " %Swas awarded: &b" + Awards.camelCase(awardName)); } @@ -66,7 +66,7 @@ namespace MCGalaxy.Commands } else { - if (Awards.takeAward(foundPlayer, awardName)) + if (Awards.TakeAward(foundPlayer, awardName)) { Player.GlobalMessage(Server.FindColor(foundPlayer) + foundPlayer + " %Shad their &b" + Awards.camelCase(awardName) + " %Saward removed"); } diff --git a/Commands/Economy/CmdAwardMod.cs b/Commands/Economy/CmdAwardMod.cs index e15b1c1cb..06627d896 100644 --- a/Commands/Economy/CmdAwardMod.cs +++ b/Commands/Economy/CmdAwardMod.cs @@ -47,14 +47,14 @@ namespace MCGalaxy.Commands string awardName = message.Split(':')[0].Trim(); string description = message.Split(':')[1].Trim(); - if (!Awards.addAward(awardName, description)) + if (!Awards.AddAward(awardName, description)) Player.SendMessage(p, "This award already exists!"); else Player.GlobalMessage("Award added: &6" + awardName + " : " + description); } else { - if (!Awards.removeAward(message)) + if (!Awards.RemoveAward(message)) Player.SendMessage(p, "This award doesn't exist!"); else Player.GlobalMessage("Award removed: &6" + message); diff --git a/Commands/Economy/CmdAwards.cs b/Commands/Economy/CmdAwards.cs index d41fc7189..a1e54d2b5 100644 --- a/Commands/Economy/CmdAwards.cs +++ b/Commands/Economy/CmdAwards.cs @@ -84,18 +84,18 @@ namespace MCGalaxy.Commands return; } - List awardList = new List(); + List awardList = new List(); if (foundPlayer == "") { - awardList = Awards.allAwards; + awardList = Awards.Awards; } else { - foreach (string s in Awards.getPlayersAwards(foundPlayer)) + foreach (string s in Awards.GetPlayerAwards(foundPlayer)) { - Awards.awardData aD = new Awards.awardData(); - aD.awardName = s; - aD.description = Awards.getDescription(s); + Awards.Award aD = new Awards.Award(); + aD.Name = s; + aD.Description = Awards.GetDescription(s); awardList.Add(aD); } } @@ -127,8 +127,8 @@ namespace MCGalaxy.Commands if (totalCount == 0) { - foreach (Awards.awardData aD in awardList) - Player.SendMessage(p, "&6" + aD.awardName + ": &7" + aD.description); + foreach (Awards.Award aD in awardList) + Player.SendMessage(p, "&6" + aD.Name + ": &7" + aD.Description); if (awardList.Count > 8) Player.SendMessage(p, "&5Use &b/awards " + message + " 1/2/3/... &5for a more ordered list"); } @@ -136,8 +136,8 @@ namespace MCGalaxy.Commands { for (int i = start; i < max; i++) { - Awards.awardData aD = awardList[i]; - Player.SendMessage(p, "&6" + aD.awardName + ": &7" + aD.description); + Awards.Award aD = awardList[i]; + Player.SendMessage(p, "&6" + aD.Name + ": &7" + aD.Description); } } } diff --git a/Commands/Information/CmdWhois.cs b/Commands/Information/CmdWhois.cs index d3a3b4146..993cd600b 100644 --- a/Commands/Information/CmdWhois.cs +++ b/Commands/Information/CmdWhois.cs @@ -51,7 +51,7 @@ namespace MCGalaxy.Commands Player.SendMessage(p, "> > been logged in for &a" + storedTime); Player.SendMessage(p, "> > first logged into the server on &a" + who.firstLogin.ToString("yyyy-MM-dd") + " at " + who.firstLogin.ToString("HH:mm:ss")); Player.SendMessage(p, "> > logged in &a" + who.totalLogins + " %Stimes, &c" + who.totalKicked + " %Sof which ended in a kick."); - Player.SendMessage(p, "> > " + Awards.awardAmount(who.name) + " awards"); + Player.SendMessage(p, "> > " + Awards.AwardAmount(who.name) + " awards"); string[] data = Ban.GetBanData(who.name); if (data != null) Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]); diff --git a/Commands/Information/CmdWhowas.cs b/Commands/Information/CmdWhowas.cs index ee1eca13e..7039e85b2 100644 --- a/Commands/Information/CmdWhowas.cs +++ b/Commands/Information/CmdWhowas.cs @@ -60,7 +60,7 @@ namespace MCGalaxy.Commands Player.SendMessage(p, "> > " + TotalTime(target.totalTime)); Player.SendMessage(p, "> > first logged into the server on &a" + target.firstLogin); Player.SendMessage(p, "> > logged in &a" + target.logins + " %Stimes, &c" + target.kicks + " %Sof which ended in a kick."); - Player.SendMessage(p, "> > " + Awards.awardAmount(message) + " awards"); + Player.SendMessage(p, "> > " + Awards.AwardAmount(message) + " awards"); string[] data = Ban.GetBanData(message); if (data != null) Player.SendMessage(p, "> > was banned by " + data[0] + " for " + data[1] + " on " + data[2]); diff --git a/Economy/Awards.cs b/Economy/Awards.cs index 616e074f0..deb654919 100644 --- a/Economy/Awards.cs +++ b/Economy/Awards.cs @@ -14,34 +14,34 @@ BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. -*/ + */ using System; using System.Collections.Generic; using System.IO; -namespace MCGalaxy -{ - public sealed class Awards - { - public struct playerAwards { public string playerName; public List awards; } - public class awardData - { - public string awardName, description; - public void setAward(string name) { awardName = camelCase(name); } - } +namespace MCGalaxy { + + /// Manages the awards the server has, and which players have which awards. + public static class Awards { + + public struct PlayerAward { public string Name; public List Awards; } + + public class Award { public string Name, Description; } + + /// List of all awards the server has. + public static List Awards = new List(); - public static List playersAwards = new List(); - public static List allAwards = new List(); + /// List of all players who have awards. + public static List PlayerAwards = new List(); - public static void Load() - { - if (!File.Exists("text/awardsList.txt")) - { - using (StreamWriter SW = File.CreateText("text/awardsList.txt")) - { + #region I/O + + public static void Load() { + if (!File.Exists("text/awardsList.txt")) { + using (StreamWriter SW = File.CreateText("text/awardsList.txt")) { SW.WriteLine("#This is a full list of awards. The server will load these and they can be awarded as you please"); SW.WriteLine("#Format is:"); - SW.WriteLine("# awardName : Description of award goes after the colon"); + SW.WriteLine("# AwardName : Description of award goes after the colon"); SW.WriteLine(); SW.WriteLine("Gotta start somewhere : Built your first house"); SW.WriteLine("Climbing the ladder : Earned a rank advancement"); @@ -49,155 +49,148 @@ namespace MCGalaxy } } - allAwards = new List(); + Awards = new List(); PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':'); - playersAwards = new List(); + PlayerAwards = new List(); PropertiesFile.Read("text/playerAwards.txt", PlayerAwardsLineProcessor, ':'); Save(); } static void AwardsListLineProcessor(string key, string value) { if (value == "") return; - awardData aD = new awardData(); - aD.setAward(key); - aD.description = value; - allAwards.Add(aD); + Award award = new Award(); + award.Name = key; + award.Description = value; + Awards.Add(award); } static void PlayerAwardsLineProcessor(string key, string value) { if (value == "") return; - playerAwards pA; - pA.playerName = key.ToLower(); - pA.awards = new List(); + PlayerAward pl; + pl.Name = key.ToLower(); + pl.Awards = new List(); if (value.IndexOf(',') != -1) - foreach (string a in value.Split(',')) - pA.awards.Add(camelCase(a)); + foreach (string award in value.Split(',')) + pl.Awards.Add(award); else if (value != "") - pA.awards.Add(camelCase(value)); - playersAwards.Add(pA); + pl.Awards.Add(value); + PlayerAwards.Add(pl); } - public static void Save() - { - using (StreamWriter SW = File.CreateText("text/awardsList.txt")) - { - SW.WriteLine("#This is a full list of awards. The server will load these and they can be awarded as you please"); - SW.WriteLine("#Format is:"); - SW.WriteLine("# awardName : Description of award goes after the colon"); + public static void Save() { + using (StreamWriter SW = File.CreateText("text/awardsList.txt")) { + SW.WriteLine("# This is a full list of awards. The server will load these and they can be awarded as you please"); + SW.WriteLine("# Format is:"); + SW.WriteLine("# AwardName : Description of award goes after the colon"); SW.WriteLine(); - foreach (awardData aD in allAwards) - SW.WriteLine(camelCase(aD.awardName) + " : " + aD.description); + foreach (Award award in Awards) + SW.WriteLine(award.Name + " : " + award.Description); } - using (StreamWriter SW = File.CreateText("text/playerAwards.txt")) - { - foreach (playerAwards pA in playersAwards) - SW.WriteLine(pA.playerName.ToLower() + " : " + string.Join(",", pA.awards.ToArray())); + + using (StreamWriter SW = File.CreateText("text/playerAwards.txt")) { + foreach (PlayerAward pA in PlayerAwards) + SW.WriteLine(pA.Name.ToLower() + " : " + string.Join(",", pA.Awards.ToArray())); } } - - public static bool giveAward(string playerName, string awardName) - { - foreach (playerAwards pA in playersAwards) - { - if (pA.playerName == playerName.ToLower()) - { - if (pA.awards.Contains(camelCase(awardName))) - return false; - pA.awards.Add(camelCase(awardName)); - return true; + #endregion + + + #region Player awards + + /// Adds the given award to that player's list of awards. + public static bool GiveAward(string playerName, string name) { + foreach (PlayerAward pl in PlayerAwards) { + if (!pl.Name.CaselessEq(playerName)) continue; + + foreach (Award award in pl.Awards) { + if (award.Name.CaselessEq(name)) return false; } + pl.Awards.Add(name); + return true; } - playerAwards newPlayer; - newPlayer.playerName = playerName.ToLower(); - newPlayer.awards = new List(); - newPlayer.awards.Add(camelCase(awardName)); - playersAwards.Add(newPlayer); + PlayerAward pl; + pl.Name = playerName; + pl.Awards = new List(); + pl.Awards.Add(name); + PlayerAwards.Add(pl); return true; } - public static bool takeAward(string playerName, string awardName) - { - foreach (playerAwards pA in playersAwards) - { - if (pA.playerName == playerName.ToLower()) - { - if (!pA.awards.Contains(camelCase(awardName))) - return false; - pA.awards.Remove(camelCase(awardName)); + + /// Removes the given award from that player's list of awards. + public static bool TakeAward(string playerName, string name) { + foreach (PlayerAward pl in PlayerAwards) { + if (!pl.Name.CaselessEq(playerName)) continue; + + for (int i = 0; i < pl.Awards.Count; i++) { + if (!pl.Awards[i].CaselessEq(name)) continue; + pl.Awards.RemoveAt(i); return true; } + return false; } - return false; } - public static List getPlayersAwards(string playerName) - { - foreach (playerAwards pA in playersAwards) - if (pA.playerName == playerName.ToLower()) - return pA.awards; - + + /// Returns the percentage of all the awards that the given player has. + public static string AwardAmount(string playerName) { + foreach (PlayerAward pl in PlayerAwards) { + if (!pl.Name.CaselessEq(playerName)) continue; + double percentage = Math.Round(((double)pA.Awards.Count / Awards.Count) * 100, 2); + return "&f" + pA.Awards.Count + "/" + Awards.Count + " (" + percentage + "%)" + Server.DefaultColor; + } + return "&f0/" + Awards.Count + " (0%)" + Server.DefaultColor; + } + + /// Finds the list of awards that the given player has. + public static List GetPlayerAwards(string name) { + foreach (PlayerAward pl in PlayerAwards) + if (pl.Name.CaselessEq(name)) return pl.Awards; return new List(); } - public static string getDescription(string awardName) - { - foreach (awardData aD in allAwards) - if (camelCase(aD.awardName) == camelCase(awardName)) - return aD.description; + #endregion + + + #region Awards management + + /// Adds a new award with the given name. + public static bool AddAward(string name, string desc) { + if (ExistsAward(name)) return false; + + Award award = new Award(); + award.Name = name; + award.Description = desc; + Awards.Add(award); + return true; + } + + /// Removes the award with the given name. + public static bool RemoveAward(string name) { + foreach (Award award in Awards) { + if (!award.Name.CaselessEq(name)) continue; + Awards.Remove(award); + return true; + } + return false; + } + + /// Whether an award with that name exists. + public static bool ExistsAward(string name) { + foreach (Award award in Awards) + if (award.Name.CaselessEq(name)) return true; + return false; + } + + /// Gets the description of the award matching the given name, + /// or an empty string if no matching award was found. + public static string GetDescription(string name) { + foreach (Award award in Awards) + if (award.Name.CaselessEq(name)) + return award.Description; return ""; } - public static string awardAmount(string playerName) - { - foreach (playerAwards pA in playersAwards) - if (pA.playerName == playerName.ToLower()) - return "&f" + pA.awards.Count + "/" + allAwards.Count + " (" + Math.Round((double)((double)pA.awards.Count / allAwards.Count) * 100, 2) + "%)" + Server.DefaultColor; - - return "&f0/" + allAwards.Count + " (0%)" + Server.DefaultColor; - } - public static bool addAward(string awardName, string awardDescription) - { - if (awardExists(awardName)) return false; - - awardData aD = new awardData(); - aD.awardName = camelCase(awardName); - aD.description = awardDescription; - allAwards.Add(aD); - return true; - } - public static bool removeAward(string awardName) - { - foreach (awardData aD in allAwards) - { - if (camelCase(aD.awardName) == camelCase(awardName)) - { - allAwards.Remove(aD); - return true; - } - } - return false; - } - public static bool awardExists(string awardName) - { - foreach (awardData aD in allAwards) - if (camelCase(aD.awardName) == camelCase(awardName)) - return true; - - return false; - } - - - public static string camelCase(string givenName) - { - string returnString = ""; - if (givenName != "") - foreach (string s in givenName.Split(' ')) - if (s.Length > 1) - returnString += s[0].ToString().ToUpper() + s.Substring(1).ToLower() + " "; - else - returnString += s.ToUpper() + " "; - - return returnString.Trim(); - } + #endregion } } diff --git a/Player/Player.Timers.cs b/Player/Player.Timers.cs index f5bdd7a51..763c67eb2 100644 --- a/Player/Player.Timers.cs +++ b/Player/Player.Timers.cs @@ -90,7 +90,7 @@ namespace MCGalaxy { try { if ( !Group.Find("Nobody").commands.Contains("award") && !Group.Find("Nobody").commands.Contains("awards") && !Group.Find("Nobody").commands.Contains("awardmod") ) - SendMessage("You have " + Awards.awardAmount(name) + " awards."); + SendMessage("You have " + Awards.AwardAmount(name) + " awards."); } catch { }