diff --git a/src/main/java/io/minimum/minecraft/superbvote/storage/JsonVoteStorage.java b/src/main/java/io/minimum/minecraft/superbvote/storage/JsonVoteStorage.java index cc28dcf..05d3735 100644 --- a/src/main/java/io/minimum/minecraft/superbvote/storage/JsonVoteStorage.java +++ b/src/main/java/io/minimum/minecraft/superbvote/storage/JsonVoteStorage.java @@ -143,8 +143,10 @@ public PlayerVotes getVotes(UUID player) { rwl.readLock().lock(); try { PlayerRecord pr = voteCounts.get(player); - return new PlayerVotes(player, pr != null ? pr.lastKnownUsername : null, pr == null ? 0 : pr.votes, - PlayerVotes.Type.CURRENT); + if (pr == null) { + return new PlayerVotes(player, null, 0, null, PlayerVotes.Type.CURRENT); + } + return new PlayerVotes(player, pr.lastKnownUsername, pr.votes, Date.from(Instant.ofEpochMilli(pr.lastVoted)), PlayerVotes.Type.CURRENT); } finally { rwl.readLock().unlock(); } @@ -160,7 +162,8 @@ public List getTopVoters(int amount, int page) { .sorted(Collections.reverseOrder(Comparator.comparing(Map.Entry::getValue))) .skip(skip) .limit(amount) - .map(e -> new PlayerVotes(e.getKey(), e.getValue().lastKnownUsername, e.getValue().votes, PlayerVotes.Type.CURRENT)) + .map(e -> new PlayerVotes(e.getKey(), e.getValue().lastKnownUsername, e.getValue().votes, + Date.from(Instant.ofEpochMilli(e.getValue().lastVoted)), PlayerVotes.Type.CURRENT)) .collect(Collectors.toList()); } finally { rwl.readLock().unlock(); diff --git a/src/main/java/io/minimum/minecraft/superbvote/storage/MysqlVoteStorage.java b/src/main/java/io/minimum/minecraft/superbvote/storage/MysqlVoteStorage.java index ad5c569..d5dcab1 100644 --- a/src/main/java/io/minimum/minecraft/superbvote/storage/MysqlVoteStorage.java +++ b/src/main/java/io/minimum/minecraft/superbvote/storage/MysqlVoteStorage.java @@ -216,19 +216,19 @@ public void clearVotes() { public PlayerVotes getVotes(UUID player) { Preconditions.checkNotNull(player, "player"); try (Connection connection = dbPool.getConnection()) { - try (PreparedStatement statement = connection.prepareStatement("SELECT last_name, votes FROM " + tableName + " WHERE uuid = ?")) { + try (PreparedStatement statement = connection.prepareStatement("SELECT last_name, votes, last_vote FROM " + tableName + " WHERE uuid = ?")) { statement.setString(1, player.toString()); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { - return new PlayerVotes(player, resultSet.getString(1), resultSet.getInt(2), PlayerVotes.Type.CURRENT); + return new PlayerVotes(player, resultSet.getString(1), resultSet.getInt(2), resultSet.getDate(3), PlayerVotes.Type.CURRENT); } else { - return new PlayerVotes(player, null, 0, PlayerVotes.Type.CURRENT); + return new PlayerVotes(player, null, 0, null, PlayerVotes.Type.CURRENT); } } } } catch (SQLException e) { SuperbVote.getPlugin().getLogger().log(Level.SEVERE, "Unable to get votes for " + player.toString(), e); - return new PlayerVotes(player, null, 0, PlayerVotes.Type.CURRENT); + return new PlayerVotes(player, null, 0, null, PlayerVotes.Type.CURRENT); } } @@ -236,14 +236,14 @@ public PlayerVotes getVotes(UUID player) { public List getTopVoters(int amount, int page) { int offset = page * amount; try (Connection connection = dbPool.getConnection()) { - try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes FROM " + tableName + " WHERE votes > 0 ORDER BY votes DESC " + + try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes, last_vote FROM " + tableName + " WHERE votes > 0 ORDER BY votes DESC " + "LIMIT " + amount + " OFFSET " + offset)) { try (ResultSet resultSet = statement.executeQuery()) { List records = new ArrayList<>(); while (resultSet.next()) { UUID uuid = UUID.fromString(resultSet.getString(1)); String name = resultSet.getString(2); - records.add(new PlayerVotes(uuid, name, resultSet.getInt(3), PlayerVotes.Type.CURRENT)); + records.add(new PlayerVotes(uuid, name, resultSet.getInt(3), resultSet.getDate(4), PlayerVotes.Type.CURRENT)); } return records; } @@ -293,7 +293,7 @@ public List getAllPlayersWithNoVotesToday(List onlinePlayers) List votes = new ArrayList<>(); try (Connection connection = dbPool.getConnection()) { String valueStatement = Joiner.on(", ").join(Collections.nCopies(onlinePlayers.size(), "?")); - try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes, (DATE(last_vote) = CURRENT_DATE()) AS has_voted_today FROM " + tableName + " WHERE uuid IN (" + valueStatement + ")")) { + try (PreparedStatement statement = connection.prepareStatement("SELECT uuid, last_name, votes, last_vote, (DATE(last_vote) = CURRENT_DATE()) AS has_voted_today FROM " + tableName + " WHERE uuid IN (" + valueStatement + ")")) { for (int i = 0; i < onlinePlayers.size(); i++) { statement.setString(i + 1, onlinePlayers.get(i).toString()); } @@ -302,12 +302,13 @@ public List getAllPlayersWithNoVotesToday(List onlinePlayers) while (resultSet.next()) { UUID uuid = UUID.fromString(resultSet.getString(1)); found.add(uuid); - if (resultSet.getBoolean(4)) { + if (resultSet.getBoolean(5)) { continue; // already voted today } PlayerVotes pv = new PlayerVotes(UUID.fromString(resultSet.getString(1)), resultSet.getString(2), resultSet.getInt(3), + resultSet.getDate(4), PlayerVotes.Type.CURRENT); votes.add(pv); } @@ -317,7 +318,7 @@ public List getAllPlayersWithNoVotesToday(List onlinePlayers) List missing = new ArrayList<>(onlinePlayers); missing.removeAll(found); for (UUID uuid : missing) { - votes.add(new PlayerVotes(uuid, null, 0, PlayerVotes.Type.CURRENT)); + votes.add(new PlayerVotes(uuid, null, 0, null, PlayerVotes.Type.CURRENT)); } } return votes; diff --git a/src/main/java/io/minimum/minecraft/superbvote/util/PlayerVotes.java b/src/main/java/io/minimum/minecraft/superbvote/util/PlayerVotes.java index de23b97..4d0d604 100644 --- a/src/main/java/io/minimum/minecraft/superbvote/util/PlayerVotes.java +++ b/src/main/java/io/minimum/minecraft/superbvote/util/PlayerVotes.java @@ -1,5 +1,7 @@ package io.minimum.minecraft.superbvote.util; +import java.util.Date; +import javax.annotation.Nullable; import lombok.Value; import org.bukkit.Bukkit; @@ -10,6 +12,8 @@ public class PlayerVotes { private final UUID uuid; private final String associatedUsername; private final int votes; + @Nullable + private final Date lastVote; private final Type type; public String getAssociatedUsername() { diff --git a/src/main/java/io/minimum/minecraft/superbvote/votes/SuperbVoteListener.java b/src/main/java/io/minimum/minecraft/superbvote/votes/SuperbVoteListener.java index e4303a8..f415b74 100644 --- a/src/main/java/io/minimum/minecraft/superbvote/votes/SuperbVoteListener.java +++ b/src/main/java/io/minimum/minecraft/superbvote/votes/SuperbVoteListener.java @@ -11,6 +11,7 @@ import io.minimum.minecraft.superbvote.util.BrokenNag; import io.minimum.minecraft.superbvote.util.PlayerVotes; import io.minimum.minecraft.superbvote.votes.rewards.VoteReward; +import org.apache.commons.lang3.time.DateUtils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -43,7 +44,7 @@ public void onVote(final VotifierEvent event) { VoteStorage voteStorage = SuperbVote.getPlugin().getVoteStorage(); VoteStreak voteStreak = voteStorage.getVoteStreakIfSupported(op.getUniqueId(), false); PlayerVotes pvCurrent = voteStorage.getVotes(op.getUniqueId()); - PlayerVotes pv = new PlayerVotes(op.getUniqueId(), op.getName(), pvCurrent.getVotes() + 1, PlayerVotes.Type.FUTURE); + PlayerVotes pv = new PlayerVotes(op.getUniqueId(), op.getName(), pvCurrent.getVotes() + 1, pvCurrent.getLastVote(), PlayerVotes.Type.FUTURE); Vote vote = new Vote(op.getName(), op.getUniqueId(), event.getVote().getServiceName(), event.getVote().getAddress().equals(SuperbVoteCommand.FAKE_HOST_NAME_FOR_VOTE), worldName, new Date()); @@ -89,12 +90,13 @@ private void processVote(PlayerVotes pv, VoteStreak voteStreak, Vote vote, boole throw new RuntimeException("No vote rewards found for '" + vote + "'"); } - boolean hasAlreadyVoted = SuperbVote.getPlugin() - .getVoteStorage() - .hasVotedToday(vote.getUuid()); // TODO: use getVotes(vote.getReceived()) and add lastVote to PlayerVotes - if (hasAlreadyVoted && SuperbVote.getPlugin().getConfig().getBoolean("votes.one-vote-per-day")) { - Date todoReplace = new Date(); // TODO: replace with lastVote date - SuperbVote.getPlugin().getLogger().log(Level.INFO, "Discarding vote: " + vote.getName() + " already voted the same day at " + todoReplace); + if (pv.getLastVote() != null + && DateUtils.isSameDay(pv.getLastVote(), vote.getReceived()) + && SuperbVote.getPlugin().getConfig().getBoolean("votes.one-vote-per-day")) { + + SuperbVote.getPlugin() + .getLogger() + .log(Level.INFO, "Discarding vote: " + vote.getName() + " already received a vote the same day at " + vote.getReceived()); return; } if (queue) { @@ -150,7 +152,7 @@ public void onPlayerJoin(PlayerJoinEvent event) { if (!votes.isEmpty()) { for (Vote vote : votes) { processVote(pv, voteStreak, vote, false, false, true); - pv = new PlayerVotes(pv.getUuid(), event.getPlayer().getName(),pv.getVotes() + 1, PlayerVotes.Type.CURRENT); + pv = new PlayerVotes(pv.getUuid(), event.getPlayer().getName(),pv.getVotes() + 1, pv.getLastVote(), PlayerVotes.Type.CURRENT); } afterVoteProcessing(); }