Skip to content

Commit

Permalink
Add vote expiration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectInfinity committed Apr 21, 2018
1 parent f4b5597 commit 8777eaf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<directory>src/main/resources</directory>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/io/pocketvote/PocketVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import cn.nukkit.plugin.PluginBase;
import cn.nukkit.scheduler.TaskHandler;
import cn.nukkit.utils.ConfigSection;
import cn.nukkit.utils.TextFormat;
import io.pocketvote.listener.VoteListener;
import io.pocketvote.cmd.PocketVoteCommand;
import io.pocketvote.task.ExpireVotesTask;
import io.pocketvote.task.SchedulerTask;
import io.pocketvote.util.VoteManager;

Expand Down Expand Up @@ -50,13 +52,13 @@ public class PocketVote extends PluginBase {

@Override
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.
* TODO: Add expire.
*/
plugin = this;
saveDefaultConfig();
Expand All @@ -77,14 +79,15 @@ public void onEnable() {
getServer().getCommandMap().register("pocketvote", new PocketVoteCommand(plugin));

/* Register tasks */
this.tasks = new ArrayList<>();
tasks = new ArrayList<>();
tasks.add(getServer().getScheduler().scheduleRepeatingTask(plugin, new ExpireVotesTask(plugin), 6000).getTaskId());

schedulerTask = getServer().getScheduler().scheduleRepeatingTask(plugin, new SchedulerTask(plugin), 1200); // 1200 = 60 seconds.
}

@Override
public void onDisable() {
for(int task : tasks) getServer().getScheduler().cancelTask(task); // TODO: Figure out if I can remove the tasks array.
for(int task : tasks) getServer().getScheduler().cancelTask(task);
getServer().getScheduler().cancelTask(schedulerTask.getTaskId());
plugin = null;
}
Expand Down Expand Up @@ -177,12 +180,24 @@ public void updateConfig() {
getConfig().set("multi-server.mysql.database", "pocketvote");
getConfig().set("version", 2);
saveConfig();
reloadConfig();
}
if(plugin.getConfig().getInt("version", 2) == 2) {
getLogger().info(TextFormat.YELLOW + "Migrating config to version 3");
getLogger().info(TextFormat.YELLOW + "Migrating config to version 3.");
getConfig().set("development", false);
getConfig().set("version", 3);
saveConfig();
reloadConfig();
}
if(plugin.getConfig().getInt("version", 3) == 3) {
getLogger().info(TextFormat.YELLOW + "Migrating config to version 4.");
for(Object v : (ArrayList) getConfig().getList("votes", new ArrayList())) {
((ConfigSection) v).set("expires", Instant.now().getEpochSecond() + (86400 * 7));
}
getConfig().set("vote-expiration", 7);
getConfig().set("version", 4);
saveConfig();
reloadConfig();
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/pocketvote/task/ExpireVotesTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.pocketvote.task;

import cn.nukkit.scheduler.Task;
import io.pocketvote.PocketVote;

public class ExpireVotesTask extends Task {

private PocketVote plugin;

public ExpireVotesTask(PocketVote plugin) {
this.plugin = plugin;
}

@Override
public void onRun(int i) {
plugin.getLogger().debug("Cleaning up expired votes.");
int expired = plugin.getVoteManager().expireVotes();
if(expired > 0) plugin.getLogger().debug("Expired " + expired + " votes.");
}

}
17 changes: 17 additions & 0 deletions src/main/java/io/pocketvote/util/VoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -123,4 +124,20 @@ public void removeVRCTask(String player) {
vrcTasks.remove(player);
}

public int expireVotes() {
int expired = 0;
Iterator<HashMap<String, String>> votes = getVotes();

if(!votes.hasNext()) return expired;
while(votes.hasNext()) {
HashMap<String, String> vote = votes.next();
if(!vote.containsKey("expires") || Instant.now().getEpochSecond() > Long.valueOf(String.valueOf(vote.get("expires")))) {
votes.remove();
expired++;
}
}
commit();
return expired;
}

}
3 changes: 2 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version: 3
version: 4
development: false
identity: ''
secret: ''
lock: false
vote-expiration: 7
multi-server:
enabled: false
role: 'master'
Expand Down

0 comments on commit 8777eaf

Please sign in to comment.