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 resources = new ArrayList(); 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 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 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 resources) { resources.forEach((k, v) -> { addResource(member, k, v); }); } public String createResourceList(Map 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; } }