/
Economy.cs
174 lines (145 loc) · 6.02 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
/*
Copyright 2011 MCForge
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;
using System.Collections.Generic;
using System.IO;
using MCGalaxy.Events.EconomyEvents;
namespace MCGalaxy.Eco
{
public static partial class Economy
{
public static bool Enabled;
static Dictionary<string, List<string>> itemCfg = new Dictionary<string, List<string>>();
public static bool CheckIsEnabled(Player p, Command cmd) {
if (Economy.Enabled) return true;
p.Message("Cannot use &T/{0} &Scurrently as economy is disabled", cmd.name);
return false;
}
static List<string> GetConfig(string item) {
List<string> cfg;
if (itemCfg.TryGetValue(item, out cfg)) return cfg;
cfg = new List<string>();
itemCfg[item] = cfg;
return cfg;
}
public static void Load() {
if (!File.Exists(Paths.EconomyPropsFile)) {
Logger.Log(LogType.SystemActivity, "Economy properties don't exist, creating");
Save();
}
using (StreamReader r = new StreamReader(Paths.EconomyPropsFile)) {
string line;
while ((line = r.ReadLine()) != null)
{
line = line.Trim();
try {
ParseLine(line);
} catch (Exception ex) {
Logger.LogError(ex);
}
}
}
}
static void ParseLine(string line) {
string name, value;
line.Separate(':', out name, out value);
if (value.Length == 0) return;
if (name.CaselessEq("enabled")) {
Enabled = value.CaselessEq("true"); return;
}
Item item = GetItem(name);
name = item != null ? item.Name : name;
GetConfig(name).Add(value);
if (item != null) item.LoadConfig(value);
}
static readonly object saveLock = new object();
public static void Save() {
try {
lock (saveLock) SaveCore();
} catch (Exception e) {
Logger.LogError("Error saving " + Paths.EconomyPropsFile, e);
}
}
static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.EconomyPropsFile, false)) {
w.WriteLine("enabled:" + Enabled);
foreach (Item item in Items)
{
List<string> cfg = GetConfig(item.Name);
cfg.Clear();
item.SaveConfig(cfg);
}
foreach (var kvp in itemCfg)
{
w.WriteLine();
foreach (string prop in kvp.Value)
{
w.WriteLine(kvp.Key + ":" + prop);
}
}
}
}
public static List<Item> Items = new List<Item>() {
new ColorItem(), new TitleColorItem(), new TitleItem(),
new RankItem(), new LevelItem(), new LoginMessageItem(),
new LogoutMessageItem(), new NickItem(), new SnackItem()
};
public static void RegisterItem(Item item) {
List<string> cfg = GetConfig(item.Name);
foreach (string line in cfg)
{
item.LoadConfig(line);
}
Items.Add(item);
}
/// <summary> Finds the item whose name or one of its aliases caselessly matches the input. </summary>
public static Item GetItem(string name) {
foreach (Item item in Items)
{
if (name.CaselessEq(item.Name)) return item;
foreach (string alias in item.Aliases)
{
if (name.CaselessEq(alias)) return item;
}
}
return null;
}
public static List<Item> GetEnabledItems() {
List<Item> enabled = new List<Item>();
foreach (Item item in Economy.Items)
{
if (item.Enabled) enabled.Add(item);
}
return enabled;
}
/// <summary> Gets comma separated list of enabled items. </summary>
public static string EnabledItemNames() {
string items = Items.Join(x => x.Enabled ? x.ShopName : null);
return items.Length == 0 ? "(no enabled items)" : items;
}
public static RankItem Ranks { get { return (RankItem)Items[3]; } }
public static LevelItem Levels { get { return (LevelItem)Items[4]; } }
public static void MakePurchase(Player p, int cost, string item) {
p.SetMoney(p.money - cost);
EcoTransaction transaction = new EcoTransaction();
transaction.TargetName = p.name;
transaction.TargetFormatted = p.ColoredName;
transaction.Amount = cost;
transaction.Type = EcoTransactionType.Purchase;
transaction.ItemDescription = item;
OnEcoTransactionEvent.Call(transaction);
}
}
}