mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-26 06:43:25 -04:00
Cleanup Awards.cs.
This commit is contained in:
parent
c08b5d1a40
commit
3a1378337c
@ -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");
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -84,18 +84,18 @@ namespace MCGalaxy.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
List<Awards.awardData> awardList = new List<Awards.awardData>();
|
||||
List<Awards.Award> awardList = new List<Awards.Award>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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]);
|
||||
|
@ -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]);
|
||||
|
@ -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<string> awards; }
|
||||
public class awardData
|
||||
{
|
||||
public string awardName, description;
|
||||
public void setAward(string name) { awardName = camelCase(name); }
|
||||
}
|
||||
namespace MCGalaxy {
|
||||
|
||||
public static List<Awards.playerAwards> playersAwards = new List<Awards.playerAwards>();
|
||||
public static List<Awards.awardData> allAwards = new List<Awards.awardData>();
|
||||
/// <summary> Manages the awards the server has, and which players have which awards. </summary>
|
||||
public static class Awards {
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
if (!File.Exists("text/awardsList.txt"))
|
||||
{
|
||||
using (StreamWriter SW = File.CreateText("text/awardsList.txt"))
|
||||
{
|
||||
public struct PlayerAward { public string Name; public List<string> Awards; }
|
||||
|
||||
public class Award { public string Name, Description; }
|
||||
|
||||
/// <summary> List of all awards the server has. </summary>
|
||||
public static List<Award> Awards = new List<Award>();
|
||||
|
||||
/// <summary> List of all players who have awards. </summary>
|
||||
public static List<PlayerAward> PlayerAwards = new List<PlayerAward>();
|
||||
|
||||
#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<awardData>();
|
||||
Awards = new List<Award>();
|
||||
PropertiesFile.Read("text/awardsList.txt", AwardsListLineProcessor, ':');
|
||||
playersAwards = new List<playerAwards>();
|
||||
PlayerAwards = new List<PlayerAward>();
|
||||
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<string>();
|
||||
PlayerAward pl;
|
||||
pl.Name = key.ToLower();
|
||||
pl.Awards = new List<string>();
|
||||
|
||||
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);
|
||||
}
|
||||
using (StreamWriter SW = File.CreateText("text/playerAwards.txt"))
|
||||
{
|
||||
foreach (playerAwards pA in playersAwards)
|
||||
SW.WriteLine(pA.playerName.ToLower() + " : " + string.Join(",", pA.awards.ToArray()));
|
||||
}
|
||||
foreach (Award award in Awards)
|
||||
SW.WriteLine(award.Name + " : " + award.Description);
|
||||
}
|
||||
|
||||
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));
|
||||
using (StreamWriter SW = File.CreateText("text/playerAwards.txt")) {
|
||||
foreach (PlayerAward pA in PlayerAwards)
|
||||
SW.WriteLine(pA.Name.ToLower() + " : " + string.Join(",", pA.Awards.ToArray()));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Player awards
|
||||
|
||||
/// <summary> Adds the given award to that player's list of awards. </summary>
|
||||
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<string>();
|
||||
newPlayer.awards.Add(camelCase(awardName));
|
||||
playersAwards.Add(newPlayer);
|
||||
PlayerAward pl;
|
||||
pl.Name = playerName;
|
||||
pl.Awards = new List<string>();
|
||||
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));
|
||||
|
||||
/// <summary> Removes the given award from that player's list of awards. </summary>
|
||||
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;
|
||||
}
|
||||
public static List<string> getPlayersAwards(string playerName)
|
||||
{
|
||||
foreach (playerAwards pA in playersAwards)
|
||||
if (pA.playerName == playerName.ToLower())
|
||||
return pA.awards;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary> Returns the percentage of all the awards that the given player has. </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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>();
|
||||
}
|
||||
public static string getDescription(string awardName)
|
||||
{
|
||||
foreach (awardData aD in allAwards)
|
||||
if (camelCase(aD.awardName) == camelCase(awardName))
|
||||
return aD.description;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Awards management
|
||||
|
||||
/// <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 "";
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user