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
|
package net.uomc.mineshaft.resources;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONObject;
import com.mouldycheerio.dbot.CustomBot;
import com.mouldycheerio.dbot.commands.CommandController;
import com.mouldycheerio.dbot.commands.CommandDetails;
import com.mouldycheerio.dbot.util.PeelingUtils;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.uomc.mineshaft.resources.commands.CreateResourceCommand;
import net.uomc.mineshaft.resources.commands.GiveResourcesCommand;
import net.uomc.mineshaft.resources.commands.LeaderBoardCommand;
import net.uomc.mineshaft.resources.commands.ListResourcesCommand;
import net.uomc.mineshaft.resources.commands.MyResourcesCommand;
import net.uomc.mineshaft.resources.commands.RemoveResourceCommand;
import net.uomc.mineshaft.resources.commands.SetPrimaryResourceCommand;
import net.uomc.mineshaft.resources.commands.ValuesCommand;
import net.uomc.mineshaft.resources.market.MarketCommand;
import net.uomc.mineshaft.resources.market.MarketManager;
public class ResourceManager {
private static final String PRIMARY_RESOURCE_KEY = "primary_resource";
private static final String RESOURCES_KEY = "resources";
private List<Resource> resources = new ArrayList<Resource>();
private File configFile;
private CustomBot bot;
private String databasepath;
private String primaryResource = "money";
private MarketManager marketManager;
public ResourceManager(CustomBot customBot) {
this.bot = customBot;
configFile = new File(customBot.getDatadir(), "resources_config.json");
File database = new File(customBot.getDatadir(), "resources.db");
databasepath = database.getPath();
loadResources();
marketManager = new MarketManager(this);
initCommands();
}
private void initCommands() {
CommandController cc = bot.getCommandController();
// setup commands
cc.addCommand(new ListResourcesCommand(this));
cc.addCommand(new CreateResourceCommand(this));
cc.addCommand(new RemoveResourceCommand(this));
cc.addCommand(new SetPrimaryResourceCommand(this));
MyResourcesCommand myResourcesCommand = new MyResourcesCommand(this);
cc.addCommand(CommandDetails.from("balance,bal,$,money"), (e, b, args) -> {
Member member = PeelingUtils.getSingleMentionFromArgs(e);
bot.sendMessage(
e, (member.equals(e.getMember()) ? "You have" : member.getEffectiveName() + " has")
+ " **" + getPrimaryResource().prettyValue(
getPrimaryResource().get(member)
) + "**"
);
});
// public commands
cc.addCommand(myResourcesCommand);
cc.addCommand(new GiveResourcesCommand(this));
//cc.addCommand(new MarketCommand(marketManager));
cc.addCommand(new ValuesCommand(this));
cc.addCommand(new LeaderBoardCommand(this));
}
private void loadResources() {
JSONObject loadJSON = PeelingUtils.loadJSON(configFile);
if (loadJSON.has(RESOURCES_KEY)) {
JSONArray jsonArray = loadJSON.getJSONArray(RESOURCES_KEY);
for (Object o : jsonArray) {
if (o instanceof JSONObject) {
Resource resource = new Resource((JSONObject) o, this);
resources.add(resource);
}
}
}
if (loadJSON.has(PRIMARY_RESOURCE_KEY)) {
setPrimaryResource(loadJSON.getString(PRIMARY_RESOURCE_KEY));
}
}
private void saveResources() {
JSONObject save = new JSONObject();
save.put(PRIMARY_RESOURCE_KEY, primaryResource);
resources.forEach(r -> save.append(RESOURCES_KEY, r));
PeelingUtils.saveJSONPretty(configFile, save);
}
public void addResource(Resource resource) {
resources.add(resource);
saveResources();
}
public Resource getResource(String input) {
for (Resource resource : resources) {
if (resource.getName().equalsIgnoreCase(input) || resource.getSymbol().equalsIgnoreCase(input)) {
return resource;
}
}
return null;
}
public List<Resource> listResources() {
return Collections.unmodifiableList(resources);
}
public boolean removeResource(String name) {
Resource resource = getResource(name);
if (resource != null) {
resources.remove(resource);
saveResources();
return true;
} else {
return false;
}
}
public CustomBot getBot() {
return bot;
}
public void setBot(CustomBot bot) {
this.bot = bot;
}
public String getDatabasepath() {
return databasepath;
}
public void setDatabasepath(String databasepath) {
this.databasepath = databasepath;
}
public Resource getPrimaryResource() {
Resource resource = getResource(primaryResource);
return resource == null ? resources.get(0) : resource;
}
public long getTotalResources() {
return resources.stream().mapToLong(r -> r.total()).sum();
}
public String getPrimaryResourceName() {
return primaryResource;
}
public void setPrimaryResource(String primaryResource) {
this.primaryResource = primaryResource;
saveResources();
}
public List<Resource> getResources() {
return Collections.unmodifiableList(resources);
}
public long getResource(Member member, String resourceName) {
Resource resource = getResource(resourceName);
if (resource == null)
return 0;
return resource.get(member);
}
public void setResource(Member member, String resourceName, long amount) {
Resource resource = getResource(resourceName);
if (resource == null)
return;
resource.set(member, amount);
}
public void addResource(Member member, String resource, long amount){
long a = getResource(member, resource);
setResource(member, resource, a + amount);
}
public void addResources(Member member, Map<String, Long> resources) {
resources.forEach((k, v) -> {
addResource(member, k, v);
});
}
public String createResourceList(Map<String, Long> resources) {
StringBuilder string = new StringBuilder();
resources.entrySet().stream()
.sorted((e1, e2) ->
(int)(e2.getValue() - e1.getValue()))
.forEach((e) -> {
Resource resource = getResource(e.getKey());
if (resource != null)
string.append(resource.prettyValue(e.getValue()) + "\n");
});
return string.toString();
}
public MarketManager getMarketManager() {
return marketManager;
}
}
|