This repository was archived by the owner on Mar 2, 2021. It is now read-only.
forked from Peteys93/MCForge-MCLawl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEconomy.cs
348 lines (319 loc) · 16.3 KB
/
Economy.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Copyright 2011 MCForge (modified by Sinjai for use with SinCraft)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
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.Collections.Generic;
using System.Data;
using System.IO;
using SinCraft.SQL;
namespace SinCraft {
public static class Economy {
public const string createTable =
@"CREATE TABLE if not exists Economy (
player VARCHAR(20),
money INT UNSIGNED,
total INT UNSIGNED NOT NULL DEFAULT 0,
purchase VARCHAR(255) NOT NULL DEFAULT '%cNone',
payment VARCHAR(255) NOT NULL DEFAULT '%cNone',
salary VARCHAR(255) NOT NULL DEFAULT '%cNone',
fine VARCHAR(255) NOT NULL DEFAULT '%cNone',
PRIMARY KEY(player)
);";
public struct EcoStats {
public string playerName, purchase, payment, salary, fine;
public int money, totalSpent;
public EcoStats(string name, int mon, int tot, string pur, string pay, string sal, string fin) {
playerName = name;
money = mon;
totalSpent = tot;
purchase = pur;
payment = pay;
salary = sal;
fine = fin;
}
}
public static class Settings {
public static bool Enabled = false;
//Maps
public static bool Levels = false;
public static List<Level> LevelsList = new List<Level>();
public class Level {
public int price;
public string name;
public string x;
public string y;
public string z;
public string type;
}
//Titles
public static bool Titles = false;
public static int TitlePrice = 100;
//Colors
public static bool Colors = false;
public static int ColorPrice = 100;
//TitleColors
public static bool TColors = false;
public static int TColorPrice = 100;
//Ranks
public static bool Ranks = false;
public static string MaxRank = Group.findPerm(LevelPermission.AdvBuilder).name;
public static List<Rank> RanksList = new List<Rank>();
public class Rank {
public Group group;
public int price = 1000;
}
}
public static void LoadDatabase() {
retry:
Database.executeQuery(createTable); //create database
DataTable eco = Database.fillData("SELECT * FROM Economy");
try {
DataTable players = Database.fillData("SELECT * FROM Players");
if (players.Rows.Count == eco.Rows.Count) { } //move along, nothing to do here
else if (eco.Rows.Count == 0) { //if first time, copy content from player to economy
Database.executeQuery("INSERT INTO Economy (player, money) SELECT Players.Name, Players.Money FROM Players");
} else {
//this will only be needed when the server shuts down while it was copying content (or some other error)
Database.executeQuery("DROP TABLE Economy");
goto retry;
}
players.Dispose(); eco.Dispose();
} catch { }
}
public static void Load(bool loadDatabase = false) {
/*if (loadDatabase) {
retry:
if (Server.useMySQL) MySQL.executeQuery(createTable); else SQLite.executeQuery(createTable); //create database on server loading
string queryP = "SELECT * FROM Players"; string queryE = "SELECT * FROM Economy";
DataTable eco = Server.useMySQL ? MySQL.fillData(queryE) : SQLite.fillData(queryE);
try {
DataTable players = Server.useMySQL ? MySQL.fillData(queryP) : SQLite.fillData(queryP);
if (players.Rows.Count == eco.Rows.Count) { } //move along, nothing to do here
else if (eco.Rows.Count == 0) { //if first time, copy content from player to economy
string query = "INSERT INTO Economy (player, money) SELECT Players.Name, Players.Money FROM Players";
if (Server.useMySQL) MySQL.executeQuery(query); else SQLite.executeQuery(query);
} else {
//this will only be needed when the server shuts down while it was copying content (or some other error)
if (Server.useMySQL) MySQL.executeQuery("DROP TABLE Economy"); else SQLite.executeQuery("DROP TABLE Economy");
goto retry;
}
players.Dispose(); eco.Dispose();
} catch { }
return;
}*/
if (!File.Exists("properties/economy.properties")) { Server.s.Log("Economy properties don't exist, creating"); File.Create("properties/economy.properties").Close(); Save(); }
using (StreamReader r = File.OpenText("properties/economy.properties")) {
string line;
while (!r.EndOfStream) {
line = r.ReadLine().ToLower().Trim();
string[] linear = line.ToLower().Trim().Split(':');
try {
switch (linear[0]) {
case "enabled":
if (linear[1] == "true") { Settings.Enabled = true; } else if (linear[1] == "false") { Settings.Enabled = false; }
break;
case "title":
if (linear[1] == "price") { Settings.TitlePrice = int.Parse(linear[2]); }
if (linear[1] == "enabled") {
if (linear[2] == "true") { Settings.Titles = true; } else if (linear[2] == "false") { Settings.Titles = false; }
}
break;
case "color":
if (linear[1] == "price") { Settings.ColorPrice = int.Parse(linear[2]); }
if (linear[1] == "enabled") {
if (linear[2] == "true") { Settings.Colors = true; } else if (linear[2] == "false") { Settings.Colors = false; }
}
break;
case "titlecolor":
if (linear[1] == "price") { Settings.TColorPrice = int.Parse(linear[2]); }
if (linear[1] == "enabled") {
if (linear[2] == "true") { Settings.TColors = true; } else if (linear[2] == "false") { Settings.TColors = false; }
}
break;
case "rank":
if (linear[1] == "price") {
Economy.Settings.Rank rnk = new Economy.Settings.Rank();
rnk = Economy.FindRank(linear[2]);
if (rnk == null) {
rnk = new Economy.Settings.Rank();
rnk.group = Group.Find(linear[2]);
rnk.price = int.Parse(linear[3]);
Economy.Settings.RanksList.Add(rnk);
} else {
Economy.Settings.RanksList.Remove(rnk);
rnk.price = int.Parse(linear[3]);
Economy.Settings.RanksList.Add(rnk);
}
}
if (linear[1] == "maxrank") {
//Group grp = Group.Find(linear[2]);
//if (grp != null) { Settings.MaxRank = grp.Permission; }
string grpname = linear[2];
if (Group.Exists(grpname)) Settings.MaxRank = grpname;
}
if (linear[1] == "enabled") {
if (linear[2] == "true") { Settings.Ranks = true; } else if (linear[2] == "false") { Settings.Ranks = false; }
}
break;
case "level":
if (linear[1] == "enabled") {
if (linear[2] == "true") { Settings.Levels = true; } else if (linear[2] == "false") { Settings.Levels = false; }
}
if (linear[1] == "levels") {
Settings.Level lvl = new Settings.Level();
if (FindLevel(linear[2]) != null) { lvl = FindLevel(linear[2]); Settings.LevelsList.Remove(lvl); }
switch (linear[3]) {
case "name":
lvl.name = linear[4];
break;
case "price":
lvl.price = int.Parse(linear[4]);
break;
case "x":
lvl.x = linear[4];
break;
case "y":
lvl.y = linear[4];
break;
case "z":
lvl.z = linear[4];
break;
case "type":
lvl.type = linear[4];
break;
}
Settings.LevelsList.Add(lvl);
}
break;
}
} catch { }
}
r.Close();
}
Save();
}
public static void Save() {
if (!File.Exists("properties/economy.properties")) { Server.s.Log("Economy properties don't exist, creating"); }
//Thread.Sleep(2000);
File.Delete("properties/economy.properties");
//Thread.Sleep(2000);
using (StreamWriter w = File.CreateText("properties/economy.properties")) {
//enabled
w.WriteLine("enabled:" + Settings.Enabled);
//title
w.WriteLine();
w.WriteLine("title:enabled:" + Settings.Titles);
w.WriteLine("title:price:" + Settings.TitlePrice);
//color
w.WriteLine();
w.WriteLine("color:enabled:" + Settings.Colors);
w.WriteLine("color:price:" + Settings.ColorPrice);
//tcolor
w.WriteLine();
w.WriteLine("titlecolor:enabled:" + Settings.TColors);
w.WriteLine("titlecolor:price:" + Settings.TColorPrice);
//rank
w.WriteLine();
w.WriteLine("rank:enabled:" + Settings.Ranks);
w.WriteLine("rank:maxrank:" + Settings.MaxRank);
foreach (Settings.Rank rnk in Settings.RanksList) {
w.WriteLine("rank:price:" + rnk.group.name + ":" + rnk.price);
if (rnk.group.name == Economy.Settings.MaxRank) break;
}
//maps
w.WriteLine();
w.WriteLine("level:enabled:" + Settings.Levels);
foreach (Settings.Level lvl in Settings.LevelsList) {
w.WriteLine();
w.WriteLine("level:levels:" + lvl.name + ":name:" + lvl.name);
w.WriteLine("level:levels:" + lvl.name + ":price:" + lvl.price);
w.WriteLine("level:levels:" + lvl.name + ":x:" + lvl.x);
w.WriteLine("level:levels:" + lvl.name + ":y:" + lvl.y);
w.WriteLine("level:levels:" + lvl.name + ":z:" + lvl.z);
w.WriteLine("level:levels:" + lvl.name + ":type:" + lvl.type);
}
w.Close();
}
}
public static Settings.Level FindLevel(string name) {
Settings.Level found = null;
foreach (Settings.Level lvl in Settings.LevelsList) {
try {
if (lvl.name.ToLower() == name.ToLower()) {
found = lvl;
}
} catch { }
}
return found;
}
public static Settings.Rank FindRank(string name) {
Settings.Rank found = null;
foreach (Settings.Rank rnk in Settings.RanksList) {
try {
if (rnk.group.name.ToLower() == name.ToLower()) {
found = rnk;
}
} catch { }
}
return found;
}
public static Economy.Settings.Rank NextRank(Player p) {
Group foundGroup = p.group;
Group nextGroup = null; bool nextOne = false;
for (int i = 0; i < Group.GroupList.Count; i++) {
Group grp = Group.GroupList[i];
if (nextOne) {
if (grp.Permission >= LevelPermission.Nobody) break;
nextGroup = grp;
break;
}
if (grp == foundGroup)
nextOne = true;
}
return Economy.FindRank(nextGroup.name);
}
public static EcoStats RetrieveEcoStats(string playername) {
EcoStats es;
es.playerName = playername;
Database.AddParams("@Name", playername);
using (DataTable eco = Database.fillData("SELECT * FROM Economy WHERE player=@Name")) {
if (eco.Rows.Count == 1) {
es.money = int.Parse(eco.Rows[0]["money"].ToString());
es.totalSpent = int.Parse(eco.Rows[0]["total"].ToString());
es.purchase = eco.Rows[0]["purchase"].ToString();
es.payment = eco.Rows[0]["payment"].ToString();
es.salary = eco.Rows[0]["salary"].ToString();
es.fine = eco.Rows[0]["fine"].ToString();
} else {
es.money = 0;
es.totalSpent = 0;
es.purchase = "%cNone";
es.payment = "%cNone";
es.salary = "%cNone";
es.fine = "%cNone";
}
}
return es;
}
public static void UpdateEcoStats(EcoStats es) {
Database.AddParams("@Name", es.playerName);
Database.AddParams("@Money", es.money);
Database.AddParams("@Total", es.totalSpent);
Database.AddParams("@Purchase", es.purchase);
Database.AddParams("@Payment", es.payment);
Database.AddParams("@Salary", es.salary);
Database.AddParams("@Fine", es.fine);
Database.executeQuery(string.Format("{0} Economy (player, money, total, purchase, payment, salary, fine) VALUES (@Name, @Money, @Total, @Purchase, @Payment, @Salary, @Fine)", (Server.useMySQL ? "REPLACE INTO" : "INSERT OR REPLACE INTO")));
}
}
}