Skip to content

Commit

Permalink
compare last vote with received vote date (vote processing might have…
Browse files Browse the repository at this point in the history
… been queued)
  • Loading branch information
kikelkik committed Feb 7, 2023
1 parent 2f7eadd commit 0dab784
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -160,7 +162,8 @@ public List<PlayerVotes> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,34 +216,34 @@ 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);
}
}

@Override
public List<PlayerVotes> 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<PlayerVotes> 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;
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public List<PlayerVotes> getAllPlayersWithNoVotesToday(List<UUID> onlinePlayers)
List<PlayerVotes> 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());
}
Expand All @@ -302,12 +302,13 @@ public List<PlayerVotes> getAllPlayersWithNoVotesToday(List<UUID> 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);
}
Expand All @@ -317,7 +318,7 @@ public List<PlayerVotes> getAllPlayersWithNoVotesToday(List<UUID> onlinePlayers)
List<UUID> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 0dab784

Please sign in to comment.