From d3f3d69761b5d8534874a3e1f84112a445ed73b4 Mon Sep 17 00:00:00 2001 From: applenick Date: Thu, 3 Aug 2023 17:23:46 -0700 Subject: [PATCH] Only count votes of online players at end of poll Signed-off-by: applenick --- .../java/dev/pgm/community/polls/Poll.java | 7 +++-- .../pgm/community/polls/PollComponents.java | 2 +- .../community/polls/feature/PollFeature.java | 4 +-- .../pgm/community/polls/types/NormalPoll.java | 28 ++++++++++++++++++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/pgm/community/polls/Poll.java b/src/main/java/dev/pgm/community/polls/Poll.java index 7b5a4979..19a4fabc 100644 --- a/src/main/java/dev/pgm/community/polls/Poll.java +++ b/src/main/java/dev/pgm/community/polls/Poll.java @@ -2,7 +2,6 @@ import dev.pgm.community.polls.ending.EndAction; import java.time.Instant; -import java.util.Map; import java.util.UUID; import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; @@ -23,7 +22,11 @@ public interface Poll { boolean vote(Player player, boolean option); - Map getVotes(); + long getTotalVotes(); + + long getYesVotesCount(); + + long getNoVotesCount(); EndAction getEndAction(); } diff --git a/src/main/java/dev/pgm/community/polls/PollComponents.java b/src/main/java/dev/pgm/community/polls/PollComponents.java index d074a86f..2002c6c0 100644 --- a/src/main/java/dev/pgm/community/polls/PollComponents.java +++ b/src/main/java/dev/pgm/community/polls/PollComponents.java @@ -250,7 +250,7 @@ default void sendPollDetails(Poll poll, CommandAudience audience) { formatCategoryDetail( "Time Since Start", TemporalComponent.duration(timeSinceStart, NamedTextColor.AQUA))); } - audience.sendMessage(formatCategoryDetail("Total Votes", text(poll.getVotes().size()))); + audience.sendMessage(formatCategoryDetail("Total Votes", text(poll.getTotalVotes()))); audience.sendMessage( formatButton("End", NamedTextColor.RED, "/poll end", "Click to end the poll", false)); audience.sendMessage(getFooter()); diff --git a/src/main/java/dev/pgm/community/polls/feature/PollFeature.java b/src/main/java/dev/pgm/community/polls/feature/PollFeature.java index 6d977c86..4bc4970b 100644 --- a/src/main/java/dev/pgm/community/polls/feature/PollFeature.java +++ b/src/main/java/dev/pgm/community/polls/feature/PollFeature.java @@ -143,8 +143,8 @@ public void end(CommandAudience audience) { poll.setEndTime(Instant.now()); } - long yesVotes = poll.getVotes().values().stream().filter(Boolean::booleanValue).count(); - long noVotes = poll.getVotes().size() - yesVotes; + long yesVotes = poll.getYesVotesCount(); + long noVotes = poll.getNoVotesCount(); boolean majorityOption = yesVotes > noVotes; diff --git a/src/main/java/dev/pgm/community/polls/types/NormalPoll.java b/src/main/java/dev/pgm/community/polls/types/NormalPoll.java index 2fc74e45..1577e298 100644 --- a/src/main/java/dev/pgm/community/polls/types/NormalPoll.java +++ b/src/main/java/dev/pgm/community/polls/types/NormalPoll.java @@ -8,7 +8,9 @@ import java.time.Instant; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; public class NormalPoll implements Poll { @@ -75,11 +77,35 @@ public boolean vote(Player player, boolean option) { return false; } - @Override public Map getVotes() { return votes; } + public Map getOnlineVotes() { + return getVotes().entrySet().stream() + .filter( + entry -> { + Player player = Bukkit.getPlayer(entry.getKey()); + return player != null && player.isOnline(); + }) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + @Override + public long getYesVotesCount() { + return getOnlineVotes().values().stream().filter(Boolean::booleanValue).count(); + } + + @Override + public long getNoVotesCount() { + return getOnlineVotes().values().stream().filter(vote -> !vote).count(); + } + + @Override + public long getTotalVotes() { + return getOnlineVotes().size(); + } + public boolean hasVoted(Player player) { return votes.containsKey(player.getUniqueId()); }