From 72aa7b7a770840c83062847ad7e13ba21c5d5e37 Mon Sep 17 00:00:00 2001 From: QuickMythril Date: Tue, 19 Dec 2023 20:38:41 -0500 Subject: [PATCH] Add vote weights to API call --- .../java/org/qortal/api/model/PollVotes.java | 30 ++++++++++++++++++- .../qortal/api/resource/PollsResource.java | 23 ++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/qortal/api/model/PollVotes.java b/src/main/java/org/qortal/api/model/PollVotes.java index 2768694f7..49191be22 100644 --- a/src/main/java/org/qortal/api/model/PollVotes.java +++ b/src/main/java/org/qortal/api/model/PollVotes.java @@ -20,17 +20,25 @@ public class PollVotes { @Schema(description = "Total number of votes") public Integer totalVotes; + @Schema(description = "Total weight of votes") + public Integer totalWeight; + @Schema(description = "List of vote counts for each option") public List voteCounts; + @Schema(description = "List of vote weights for each option") + public List voteWeights; + // For JAX-RS protected PollVotes() { } - public PollVotes(List votes, Integer totalVotes, List voteCounts) { + public PollVotes(List votes, Integer totalVotes, Integer totalWeight, List voteCounts, List voteWeights) { this.votes = votes; this.totalVotes = totalVotes; + this.totalWeight = totalWeight; this.voteCounts = voteCounts; + this.voteWeights = voteWeights; } @Schema(description = "Vote info") @@ -52,4 +60,24 @@ public OptionCount(String optionName, Integer voteCount) { this.voteCount = voteCount; } } + + @Schema(description = "Vote weights") + // All properties to be converted to JSON via JAX-RS + @XmlAccessorType(XmlAccessType.FIELD) + public static class OptionWeight { + @Schema(description = "Option name") + public String optionName; + + @Schema(description = "Vote weight") + public Integer voteWeight; + + // For JAX-RS + protected OptionWeight() { + } + + public OptionWeight(String optionName, Integer voteWeight) { + this.optionName = optionName; + this.voteWeight = voteWeight; + } + } } diff --git a/src/main/java/org/qortal/api/resource/PollsResource.java b/src/main/java/org/qortal/api/resource/PollsResource.java index aaa4f79c2..2b9020ab4 100644 --- a/src/main/java/org/qortal/api/resource/PollsResource.java +++ b/src/main/java/org/qortal/api/resource/PollsResource.java @@ -13,6 +13,8 @@ import org.qortal.api.ApiException; import org.qortal.api.ApiExceptionFactory; import org.qortal.api.model.PollVotes; +import org.qortal.crypto.Crypto; +import org.qortal.data.account.AccountData; import org.qortal.data.transaction.CreatePollTransactionData; import org.qortal.data.transaction.VoteOnPollTransactionData; import org.qortal.data.voting.PollData; @@ -129,12 +131,25 @@ public PollVotes getPollVotes(@PathParam("pollName") String pollName, @QueryPara for (PollOptionData optionData : pollData.getPollOptions()) { voteCountMap.put(optionData.getOptionName(), 0); } + // Initialize map for counting vote weights + Map voteWeightMap = new HashMap<>(); + for (PollOptionData optionData : pollData.getPollOptions()) { + voteWeightMap.put(optionData.getOptionName(), 0); + } int totalVotes = 0; + int totalWeight = 0; for (VoteOnPollData vote : votes) { + String voter = Crypto.toAddress(vote.getVoterPublicKey()); + AccountData voterData = repository.getAccountRepository().getAccount(voter); + int voteWeight = voterData.getBlocksMinted() - voterData.getBlocksMintedPenalty(); + if (voteWeight < 0) voteWeight = 0; + totalWeight += voteWeight; + String selectedOption = pollData.getPollOptions().get(vote.getOptionIndex()).getOptionName(); if (voteCountMap.containsKey(selectedOption)) { voteCountMap.put(selectedOption, voteCountMap.get(selectedOption) + 1); + voteWeightMap.put(selectedOption, voteWeightMap.get(selectedOption) + voteWeight); totalVotes++; } } @@ -143,11 +158,15 @@ public PollVotes getPollVotes(@PathParam("pollName") String pollName, @QueryPara List voteCounts = voteCountMap.entrySet().stream() .map(entry -> new PollVotes.OptionCount(entry.getKey(), entry.getValue())) .collect(Collectors.toList()); + // Convert map to list of WeightInfo + List voteWeights = voteWeightMap.entrySet().stream() + .map(entry -> new PollVotes.OptionWeight(entry.getKey(), entry.getValue())) + .collect(Collectors.toList()); if (onlyCounts != null && onlyCounts) { - return new PollVotes(null, totalVotes, voteCounts); + return new PollVotes(null, totalVotes, totalWeight, voteCounts, voteWeights); } else { - return new PollVotes(votes, totalVotes, voteCounts); + return new PollVotes(votes, totalVotes, totalWeight, voteCounts, voteWeights); } } catch (ApiException e) { throw e;