From f3b4bd99908981d865f2ab56754baf28bc96214b Mon Sep 17 00:00:00 2001 From: ProjectInfinity Date: Wed, 25 Apr 2018 01:32:14 +0200 Subject: [PATCH] Add /vote top --- src/main/java/io/pocketvote/PocketVote.java | 5 +- .../java/io/pocketvote/cmd/VoteCommand.java | 37 ++++++++++++ .../java/io/pocketvote/task/ApiRequest.java | 21 +++---- .../io/pocketvote/task/SchedulerTask.java | 4 +- .../java/io/pocketvote/task/TopVoterTask.java | 59 +++++++++++++++++++ .../io/pocketvote/task/VoteCheckTask.java | 14 +---- src/main/resources/plugin.yml | 5 +- 7 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 src/main/java/io/pocketvote/cmd/VoteCommand.java create mode 100644 src/main/java/io/pocketvote/task/TopVoterTask.java diff --git a/src/main/java/io/pocketvote/PocketVote.java b/src/main/java/io/pocketvote/PocketVote.java index ca8b996..2a0de29 100644 --- a/src/main/java/io/pocketvote/PocketVote.java +++ b/src/main/java/io/pocketvote/PocketVote.java @@ -4,6 +4,7 @@ import cn.nukkit.scheduler.TaskHandler; import cn.nukkit.utils.ConfigSection; import cn.nukkit.utils.TextFormat; +import io.pocketvote.cmd.VoteCommand; import io.pocketvote.listener.VoteListener; import io.pocketvote.cmd.PocketVoteCommand; import io.pocketvote.task.ExpireVotesTask; @@ -55,7 +56,6 @@ public void onEnable() { /* * List of things I need to do. * TODO: Add GURU support. - * TODO: Add TopVoter * TODO: Add Vote link. * TODO: Add diagnose. * TODO: Add heartbeat. @@ -77,6 +77,7 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new VoteListener(plugin), plugin); getServer().getCommandMap().register("pocketvote", new PocketVoteCommand(plugin)); + getServer().getCommandMap().register("vote", new VoteCommand(plugin)); /* Register tasks */ tasks = new ArrayList<>(); @@ -168,7 +169,7 @@ public VoteManager getVoteManager() { return vm; } - public void updateConfig() { + private void updateConfig() { if(plugin.getConfig().getInt("version", 1) == 1) { getLogger().info(TextFormat.YELLOW + "Migrating config to version 2."); getConfig().set("multi-server.enabled", false); diff --git a/src/main/java/io/pocketvote/cmd/VoteCommand.java b/src/main/java/io/pocketvote/cmd/VoteCommand.java new file mode 100644 index 0000000..96d434d --- /dev/null +++ b/src/main/java/io/pocketvote/cmd/VoteCommand.java @@ -0,0 +1,37 @@ +package io.pocketvote.cmd; + +import cn.nukkit.command.Command; +import cn.nukkit.command.CommandSender; +import cn.nukkit.command.PluginIdentifiableCommand; +import cn.nukkit.plugin.Plugin; +import cn.nukkit.utils.TextFormat; +import io.pocketvote.PocketVote; +import io.pocketvote.task.TopVoterTask; + +public class VoteCommand extends Command implements PluginIdentifiableCommand { + + private PocketVote plugin; + + public VoteCommand(PocketVote plugin) { + super("vote", "PocketVote vote command", "/vote [top]", new String[]{"v"}); + this.plugin = plugin; + } + + @Override + public boolean execute(CommandSender sender, String s, String[] args) { + if(!sender.hasPermission("pocketvote.vote")) { + sender.sendMessage(TextFormat.RED + "You do not have permission to use /vote."); + return true; + } + if(args.length > 0 && args[0].equalsIgnoreCase("TOP")) { + plugin.getServer().getScheduler().scheduleAsyncTask(plugin, new TopVoterTask(plugin, sender.getName())); + return true; + } + return false; // TODO: Add vote link here when "top" is not specified. + } + + @Override + public Plugin getPlugin() { + return plugin; + } +} diff --git a/src/main/java/io/pocketvote/task/ApiRequest.java b/src/main/java/io/pocketvote/task/ApiRequest.java index 10ca4ac..9b188c6 100644 --- a/src/main/java/io/pocketvote/task/ApiRequest.java +++ b/src/main/java/io/pocketvote/task/ApiRequest.java @@ -21,25 +21,22 @@ public class ApiRequest extends AsyncTask { private String url; private String method; + private String action; private HashMap postFields; private String identity; private String secret; private String version; - public ApiRequest(String url, String method, HashMap postFields) { + public ApiRequest(String url, String method, String action, HashMap postFields) { this.url = url; this.method = method; + this.action = action; this.postFields = postFields; this.identity = PocketVote.getPlugin().identity; this.secret = PocketVote.getPlugin().secret; this.version = PocketVote.getPlugin().getDescription().getVersion(); } - /*@Override - public void onRun() { - // Overridden by the extending class. - }*/ - @Override public void onRun() { HttpURLConnection con; @@ -93,10 +90,14 @@ public void onRun() { return; } - // Only a successful API request can reach this. - if(result.hasPayload()) { - Jws claims = Jwts.parser().setSigningKey(secret.getBytes("UTF-8")).parseClaimsJws(json.get("payload").asText()); - result.setClaims(claims.getBody(), true); + switch(action) { + case "VOTE": + // Only a successful API request can reach this. + if(result.hasPayload()) { + Jws claims = Jwts.parser().setSigningKey(secret.getBytes("UTF-8")).parseClaimsJws(json.get("payload").asText()); + result.setClaims(claims.getBody(), true); + } + break; } setResult(result); diff --git a/src/main/java/io/pocketvote/task/SchedulerTask.java b/src/main/java/io/pocketvote/task/SchedulerTask.java index 808fc49..0792d72 100644 --- a/src/main/java/io/pocketvote/task/SchedulerTask.java +++ b/src/main/java/io/pocketvote/task/SchedulerTask.java @@ -6,11 +6,9 @@ public class SchedulerTask extends Task { private PocketVote plugin; - private String version; public SchedulerTask(PocketVote plugin) { this.plugin = plugin; - this.version = plugin.getDescription().getVersion(); } @Override @@ -19,7 +17,7 @@ public void onRun(int i) { if(!plugin.multiserver || plugin.multiserverRole.equalsIgnoreCase("master")) { if(plugin.secret != null && !plugin.secret.isEmpty() && plugin.identity != null && !plugin.identity.isEmpty()) { - plugin.getServer().getScheduler().scheduleAsyncTask(plugin, new VoteCheckTask(plugin, plugin.identity, plugin.secret, version)); + plugin.getServer().getScheduler().scheduleAsyncTask(plugin, new VoteCheckTask(plugin)); } else { plugin.getLogger().critical("Please finish configuring PocketVote, then restart your server."); } diff --git a/src/main/java/io/pocketvote/task/TopVoterTask.java b/src/main/java/io/pocketvote/task/TopVoterTask.java new file mode 100644 index 0000000..39cf405 --- /dev/null +++ b/src/main/java/io/pocketvote/task/TopVoterTask.java @@ -0,0 +1,59 @@ +package io.pocketvote.task; + +import cn.nukkit.Server; +import cn.nukkit.command.CommandSender; +import cn.nukkit.command.ConsoleCommandSender; +import cn.nukkit.utils.TextFormat; +import com.fasterxml.jackson.databind.JsonNode; +import io.pocketvote.PocketVote; +import io.pocketvote.data.TaskResult; + +public class TopVoterTask extends ApiRequest { + + private final String player; + + public TopVoterTask(PocketVote plugin, String player) { + super(plugin.isDev() ? "http://127.0.0.1:9000/v2/top/10" : "https://api.pocketvote.io/v2/top/10", "GET", "TOP", null); + this.player = player; + + plugin.getLogger().debug("Getting top voters."); + } + + @Override + public void onCompletion(Server server) { + CommandSender player = this.player.equalsIgnoreCase("CONSOLE") ? new ConsoleCommandSender() : server.getPlayer(this.player); + if(player == null) return; + + if(!(super.getResult() instanceof TaskResult) || !hasResult() || !(getResult() instanceof TaskResult)) { + player.sendMessage(TextFormat.RED + "Failed to retrieve top voters. Try again later."); + server.getLogger().error("[PocketVote] Result of " + getClass().getCanonicalName() + " was not an instance of TaskResult."); + return; + } + + TaskResult result = (TaskResult) getResult(); + + if(result.hasError()) { + player.sendMessage(TextFormat.RED + "An error occurred while contacting the PocketVote servers, please try again later."); + server.getLogger().error("[PocketVote] TopVoterTask: " + result.getMessage()); + return; + } + + if(!result.hasPayload() || !result.getRawPayload().isArray()) { + server.getLogger().debug("[PocketVote] TopVoterTask: No payload or payload was not an array."); + return; + } + + player.sendMessage(TextFormat.AQUA + "### Current top 10 voters ###"); + int rank = 1; + boolean color = true; + + for(final JsonNode vote : result.getRawPayload()) { + player.sendMessage("" + (color ? TextFormat.WHITE : TextFormat.GRAY) + rank + ". " + vote.get("player").asText() + " (" + vote.get("votes").asInt() + ")"); + rank++; + color = !color; + } + + if(result.getRawPayload().size() == 0) player.sendMessage(TextFormat.GRAY + "No voters found, start voting!"); + } + +} \ No newline at end of file diff --git a/src/main/java/io/pocketvote/task/VoteCheckTask.java b/src/main/java/io/pocketvote/task/VoteCheckTask.java index a21368c..fb85ca5 100644 --- a/src/main/java/io/pocketvote/task/VoteCheckTask.java +++ b/src/main/java/io/pocketvote/task/VoteCheckTask.java @@ -25,18 +25,10 @@ public class VoteCheckTask extends ApiRequest { private PocketVote plugin; - private String identity; - private String secret; - private String version; - private boolean dev; - public VoteCheckTask(PocketVote plugin, String identity, String secret, String version) { - super(plugin.isDev() ? "http://127.0.0.1:9000/v2/check" : "https://api.pocketvote.io/v2/check", "GET", null); + public VoteCheckTask(PocketVote plugin) { + super(plugin.isDev() ? "http://127.0.0.1:9000/v2/check" : "https://api.pocketvote.io/v2/check", "GET", "VOTE", null); this.plugin = plugin; - this.identity = identity; - this.secret = secret; - this.version = version; - this.dev = plugin.isDev(); plugin.getLogger().debug("Checking for outstanding votes."); } @@ -44,7 +36,7 @@ public VoteCheckTask(PocketVote plugin, String identity, String secret, String v @Override public void onCompletion(Server server) { if(!(super.getResult() instanceof TaskResult)) { - server.getLogger().error("Result of " + getClass().getCanonicalName() + " was not an instance of TaskResult."); + server.getLogger().error("[PocketVote] Result of " + getClass().getCanonicalName() + " was not an instance of TaskResult."); return; } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0098fc3..07e69f6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -7,4 +7,7 @@ description: Simple, elegant and efficient voting suite. permissions: pocketvote.admin: default: op - description: "Allows user to administer pocketvote" \ No newline at end of file + description: "Allows user to administer pocketvote" + pocketvote.vote: + default: true + description: "Allows a player to use the /vote command" \ No newline at end of file