Cleanup Awards.cs.

This commit is contained in:
UnknownShadow200 2016-04-14 17:00:55 +10:00
parent c08b5d1a40
commit 3a1378337c
7 changed files with 146 additions and 153 deletions

View File

@ -46,7 +46,7 @@ namespace MCGalaxy.Commands
Player who = PlayerInfo.Find(message); Player who = PlayerInfo.Find(message);
if (who != null) foundPlayer = who.name; if (who != null) foundPlayer = who.name;
string awardName = message.Substring(message.IndexOf(' ') + 1); 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, "The award you entered doesn't exist");
Player.SendMessage(p, "Use /awards for a list of awards"); Player.SendMessage(p, "Use /awards for a list of awards");
@ -55,7 +55,7 @@ namespace MCGalaxy.Commands
if (give) if (give)
{ {
if (Awards.giveAward(foundPlayer, awardName)) if (Awards.GiveAward(foundPlayer, awardName))
{ {
Player.GlobalMessage(Server.FindColor(foundPlayer) + foundPlayer + " %Swas awarded: &b" + Awards.camelCase(awardName)); Player.GlobalMessage(Server.FindColor(foundPlayer) + foundPlayer + " %Swas awarded: &b" + Awards.camelCase(awardName));
} }
@ -66,7 +66,7 @@ namespace MCGalaxy.Commands
} }
else 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"); Player.GlobalMessage(Server.FindColor(foundPlayer) + foundPlayer + " %Shad their &b" + Awards.camelCase(awardName) + " %Saward removed");
} }

View File

@ -47,14 +47,14 @@ namespace MCGalaxy.Commands
string awardName = message.Split(':')[0].Trim(); string awardName = message.Split(':')[0].Trim();
string description = message.Split(':')[1].Trim(); string description = message.Split(':')[1].Trim();
if (!Awards.addAward(awardName, description)) if (!Awards.AddAward(awardName, description))
Player.SendMessage(p, "This award already exists!"); Player.SendMessage(p, "This award already exists!");
else else
Player.GlobalMessage("Award added: &6" + awardName + " : " + description); Player.GlobalMessage("Award added: &6" + awardName + " : " + description);
} }
else else
{ {
if (!Awards.removeAward(message)) if (!Awards.RemoveAward(message))
Player.SendMessage(p, "This award doesn't exist!"); Player.SendMessage(p, "This award doesn't exist!");
else else
Player.GlobalMessage("Award removed: &6" + message); Player.GlobalMessage("Award removed: &6" + message);

View File

@ -84,18 +84,18 @@ namespace MCGalaxy.Commands
return; return;
} }
List<Awards.awardData> awardList = new List<Awards.awardData>(); List<Awards.Award> awardList = new List<Awards.Award>();
if (foundPlayer == "") if (foundPlayer == "")
{ {
awardList = Awards.allAwards; awardList = Awards.Awards;
} }
else else
{ {
foreach (string s in Awards.getPlayersAwards(foundPlayer)) foreach (string s in Awards.GetPlayerAwards(foundPlayer))
{ {
Awards.awardData aD = new Awards.awardData(); Awards.Award aD = new Awards.Award();
aD.awardName = s; aD.Name = s;
aD.description = Awards.getDescription(s); aD.Description = Awards.GetDescription(s);
awardList.Add(aD); awardList.Add(aD);
} }
} }
@ -127,8 +127,8 @@ namespace MCGalaxy.Commands
if (totalCount == 0) if (totalCount == 0)
{ {
foreach (Awards.awardData aD in awardList) foreach (Awards.Award aD in awardList)
Player.SendMessage(p, "&6" + aD.awardName + ": &7" + aD.description); 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"); 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++) for (int i = start; i < max; i++)
{ {
Awards.awardData aD = awardList[i]; Awards.Award aD = awardList[i];
Player.SendMessage(p, "&6" + aD.awardName + ": &7" + aD.description); Player.SendMessage(p, "&6" + aD.Name + ": &7" + aD.Description);
} }
} }
} }

View File

@ -51,7 +51,7 @@ namespace MCGalaxy.Commands
Player.SendMessage(p, "> > been logged in for &a" + storedTime); 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, "> > 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, "> > 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); string[] data = Ban.GetBanData(who.name);
if (data != null) if (data != null)
Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]); Player.SendMessage(p, "> > is banned for " + data[1] + " by " + data[0]);

View File

@ -60,7 +60,7 @@ namespace MCGalaxy.Commands
Player.SendMessage(p, "> > " + TotalTime(target.totalTime)); Player.SendMessage(p, "> > " + TotalTime(target.totalTime));
Player.SendMessage(p, "> > first logged into the server on &a" + target.firstLogin); 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, "> > 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); string[] data = Ban.GetBanData(message);
if (data != null) if (data != null)
Player.SendMessage(p, "> > was banned by " + data[0] + " for " + data[1] + " on " + data[2]); Player.SendMessage(p, "> > was banned by " + data[0] + " for " + data[1] + " on " + data[2]);

View File

@ -14,34 +14,34 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
namespace MCGalaxy namespace MCGalaxy {
{
public sealed class Awards /// <summary> Manages the awards the server has, and which players have which awards. </summary>
{ public static class Awards {
public struct playerAwards { public string playerName; public List<string> awards; }
public class awardData public struct PlayerAward { public string Name; public List<string> Awards; }
{
public string awardName, description; public class Award { public string Name, Description; }
public void setAward(string name) { awardName = camelCase(name); }
} /// <summary> List of all awards the server has. </summary>
public static List<Award> Awards = new List<Award>();
public static List<Awards.playerAwards> playersAwards = new List<Awards.playerAwards>(); /// <summary> List of all players who have awards. </summary>
public static List<Awards.awardData> allAwards = new List<Awards.awardData>(); public static List<PlayerAward> PlayerAwards = new List<PlayerAward>();
public static void Load() #region I/O
{
if (!File.Exists("text/awardsList.txt")) public static void Load() {
{ if (!File.Exists("text/awardsList.txt")) {
using (StreamWriter SW = File.CreateText("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("#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("#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();
SW.WriteLine("Gotta start somewhere : Built your first house"); SW.WriteLine("Gotta start somewhere : Built your first house");
SW.WriteLine("Climbing the ladder : Earned a rank advancement"); SW.WriteLine("Climbing the ladder : Earned a rank advancement");
@ -49,155 +49,148 @@ namespace MCGalaxy
} }
} }
allAwards = new List<awardData>(); Awards = new List<Award>();
PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':'); PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':');
playersAwards = new List<playerAwards>(); PlayerAwards = new List<PlayerAward>();
PropertiesFile.Read("text/playerAwards.txt", PlayerAwardsLineProcessor, ':'); PropertiesFile.Read("text/playerAwards.txt", PlayerAwardsLineProcessor, ':');
Save(); Save();
} }
static void AwardsListLineProcessor(string key, string value) { static void AwardsListLineProcessor(string key, string value) {
if (value == "") return; if (value == "") return;
awardData aD = new awardData(); Award award = new Award();
aD.setAward(key); award.Name = key;
aD.description = value; award.Description = value;
allAwards.Add(aD); Awards.Add(award);
} }
static void PlayerAwardsLineProcessor(string key, string value) { static void PlayerAwardsLineProcessor(string key, string value) {
if (value == "") return; if (value == "") return;
playerAwards pA; PlayerAward pl;
pA.playerName = key.ToLower(); pl.Name = key.ToLower();
pA.awards = new List<string>(); pl.Awards = new List<string>();
if (value.IndexOf(',') != -1) if (value.IndexOf(',') != -1)
foreach (string a in value.Split(',')) foreach (string award in value.Split(','))
pA.awards.Add(camelCase(a)); pl.Awards.Add(award);
else if (value != "") else if (value != "")
pA.awards.Add(camelCase(value)); pl.Awards.Add(value);
playersAwards.Add(pA); PlayerAwards.Add(pl);
} }
public static void Save() public static void Save() {
{ using (StreamWriter SW = File.CreateText("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("#This is a full list of awards. The server will load these and they can be awarded as you please"); SW.WriteLine("# AwardName : Description of award goes after the colon");
SW.WriteLine("#Format is:");
SW.WriteLine("# awardName : Description of award goes after the colon");
SW.WriteLine(); SW.WriteLine();
foreach (awardData aD in allAwards) foreach (Award award in Awards)
SW.WriteLine(camelCase(aD.awardName) + " : " + aD.description); SW.WriteLine(award.Name + " : " + award.Description);
} }
using (StreamWriter SW = File.CreateText("text/playerAwards.txt"))
{ using (StreamWriter SW = File.CreateText("text/playerAwards.txt")) {
foreach (playerAwards pA in playersAwards) foreach (PlayerAward pA in PlayerAwards)
SW.WriteLine(pA.playerName.ToLower() + " : " + string.Join(",", pA.awards.ToArray())); SW.WriteLine(pA.Name.ToLower() + " : " + string.Join(",", pA.Awards.ToArray()));
} }
} }
#endregion
public static bool giveAward(string playerName, string awardName)
{
foreach (playerAwards pA in playersAwards) #region Player awards
{
if (pA.playerName == playerName.ToLower()) /// <summary> Adds the given award to that player's list of awards. </summary>
{ public static bool GiveAward(string playerName, string name) {
if (pA.awards.Contains(camelCase(awardName))) foreach (PlayerAward pl in PlayerAwards) {
return false; if (!pl.Name.CaselessEq(playerName)) continue;
pA.awards.Add(camelCase(awardName));
return true; foreach (Award award in pl.Awards) {
if (award.Name.CaselessEq(name)) return false;
} }
pl.Awards.Add(name);
return true;
} }
playerAwards newPlayer; PlayerAward pl;
newPlayer.playerName = playerName.ToLower(); pl.Name = playerName;
newPlayer.awards = new List<string>(); pl.Awards = new List<string>();
newPlayer.awards.Add(camelCase(awardName)); pl.Awards.Add(name);
playersAwards.Add(newPlayer); PlayerAwards.Add(pl);
return true; return true;
} }
public static bool takeAward(string playerName, string awardName)
{ /// <summary> Removes the given award from that player's list of awards. </summary>
foreach (playerAwards pA in playersAwards) public static bool TakeAward(string playerName, string name) {
{ foreach (PlayerAward pl in PlayerAwards) {
if (pA.playerName == playerName.ToLower()) if (!pl.Name.CaselessEq(playerName)) continue;
{
if (!pA.awards.Contains(camelCase(awardName))) for (int i = 0; i < pl.Awards.Count; i++) {
return false; if (!pl.Awards[i].CaselessEq(name)) continue;
pA.awards.Remove(camelCase(awardName)); pl.Awards.RemoveAt(i);
return true; return true;
} }
return false;
} }
return false; return false;
} }
public static List<string> getPlayersAwards(string playerName)
{ /// <summary> Returns the percentage of all the awards that the given player has. </summary>
foreach (playerAwards pA in playersAwards) public static string AwardAmount(string playerName) {
if (pA.playerName == playerName.ToLower()) foreach (PlayerAward pl in PlayerAwards) {
return pA.awards; 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;
}
/// <summary> Finds the list of awards that the given player has. </summary>
public static List<string> GetPlayerAwards(string name) {
foreach (PlayerAward pl in PlayerAwards)
if (pl.Name.CaselessEq(name)) return pl.Awards;
return new List<string>(); return new List<string>();
} }
public static string getDescription(string awardName) #endregion
{
foreach (awardData aD in allAwards)
if (camelCase(aD.awardName) == camelCase(awardName)) #region Awards management
return aD.description;
/// <summary> Adds a new award with the given name. </summary>
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;
}
/// <summary> Removes the award with the given name. </summary>
public static bool RemoveAward(string name) {
foreach (Award award in Awards) {
if (!award.Name.CaselessEq(name)) continue;
Awards.Remove(award);
return true;
}
return false;
}
/// <summary> Whether an award with that name exists. </summary>
public static bool ExistsAward(string name) {
foreach (Award award in Awards)
if (award.Name.CaselessEq(name)) return true;
return false;
}
/// <summary> Gets the description of the award matching the given name,
/// or an empty string if no matching award was found. </summary>
public static string GetDescription(string name) {
foreach (Award award in Awards)
if (award.Name.CaselessEq(name))
return award.Description;
return ""; return "";
} }
public static string awardAmount(string playerName) #endregion
{
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();
}
} }
} }

View File

@ -90,7 +90,7 @@ namespace MCGalaxy {
try { try {
if ( !Group.Find("Nobody").commands.Contains("award") && !Group.Find("Nobody").commands.Contains("awards") && !Group.Find("Nobody").commands.Contains("awardmod") ) 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 { } catch {
} }