Skip to content

Commit

Permalink
Only count votes of online players at end of poll
Browse files Browse the repository at this point in the history
Signed-off-by: applenick <[email protected]>
  • Loading branch information
applenick committed Aug 4, 2023
1 parent a492ab1 commit d3f3d69
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/main/java/dev/pgm/community/polls/Poll.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +22,11 @@ public interface Poll {

boolean vote(Player player, boolean option);

Map<UUID, Boolean> getVotes();
long getTotalVotes();

long getYesVotesCount();

long getNoVotesCount();

EndAction getEndAction();
}
2 changes: 1 addition & 1 deletion src/main/java/dev/pgm/community/polls/PollComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 27 additions & 1 deletion src/main/java/dev/pgm/community/polls/types/NormalPoll.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -75,11 +77,35 @@ public boolean vote(Player player, boolean option) {
return false;
}

@Override
public Map<UUID, Boolean> getVotes() {
return votes;
}

public Map<UUID, Boolean> 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());
}
Expand Down

0 comments on commit d3f3d69

Please sign in to comment.