forked from Peteys93/MCForge-MCLawl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Awards.cs
198 lines (173 loc) · 7.3 KB
/
Awards.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//Sample license text.
using System;
using System.Collections.Generic;
using System.IO;
namespace MCForge
{
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); }
}
public static List<Awards.playerAwards> playersAwards = new List<Awards.playerAwards>();
public static List<Awards.awardData> allAwards = new List<Awards.awardData>();
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();
SW.WriteLine("Gotta start somewhere : Built your first house");
SW.WriteLine("Climbing the ladder : Earned a rank advancement");
SW.WriteLine("Do you live here? : Joined the server a huge bunch of times");
}
}
allAwards = new List<awardData>();
foreach (string s in File.ReadAllLines("text/awardsList.txt"))
{
if (s == "" || s[0] == '#') continue;
if (s.IndexOf(" : ") == -1) continue;
awardData aD = new awardData();
aD.setAward(s.Split(new string[] { " : " }, StringSplitOptions.None)[0]);
aD.description = s.Split(new string[] { " : " }, StringSplitOptions.None)[1];
allAwards.Add(aD);
}
playersAwards = new List<playerAwards>();
if (File.Exists("text/playerAwards.txt"))
{
foreach (String s in File.ReadAllLines("text/playerAwards.txt"))
{
if (s.IndexOf(" : ") == -1) continue;
playerAwards pA;
pA.playerName = s.Split(new string[] { " : " }, StringSplitOptions.None)[0].ToLower();
string myAwards = s.Split(new string[] { " : " }, StringSplitOptions.None)[1];
pA.awards = new List<string>();
if (myAwards.IndexOf(',') != -1)
foreach (string a in myAwards.Split(','))
pA.awards.Add(camelCase(a));
else if (myAwards.Trim() != "")
pA.awards.Add(camelCase(myAwards));
playersAwards.Add(pA);
}
}
Save();
}
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()));
}
}
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;
}
}
playerAwards newPlayer;
newPlayer.playerName = playerName.ToLower();
newPlayer.awards = new List<string>();
newPlayer.awards.Add(camelCase(awardName));
playersAwards.Add(newPlayer);
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));
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 new List<string>();
}
public static string getDescription(string awardName)
{
foreach (awardData aD in allAwards)
if (camelCase(aD.awardName) == camelCase(awardName))
return aD.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();
}
}
}