Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kennycud committed Dec 22, 2023
2 parents 423a1f7 + e118f4a commit aaba6bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/main/java/org/qortal/api/model/PollVotes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<OptionCount> voteCounts;

@Schema(description = "List of vote weights for each option")
public List<OptionWeight> voteWeights;

// For JAX-RS
protected PollVotes() {
}

public PollVotes(List<VoteOnPollData> votes, Integer totalVotes, List<OptionCount> voteCounts) {
public PollVotes(List<VoteOnPollData> votes, Integer totalVotes, Integer totalWeight, List<OptionCount> voteCounts, List<OptionWeight> voteWeights) {
this.votes = votes;
this.totalVotes = totalVotes;
this.totalWeight = totalWeight;
this.voteCounts = voteCounts;
this.voteWeights = voteWeights;
}

@Schema(description = "Vote info")
Expand All @@ -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;
}
}
}
23 changes: 21 additions & 2 deletions src/main/java/org/qortal/api/resource/PollsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Integer> 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++;
}
}
Expand All @@ -143,11 +158,15 @@ public PollVotes getPollVotes(@PathParam("pollName") String pollName, @QueryPara
List<PollVotes.OptionCount> voteCounts = voteCountMap.entrySet().stream()
.map(entry -> new PollVotes.OptionCount(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
// Convert map to list of WeightInfo
List<PollVotes.OptionWeight> 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;
Expand Down

0 comments on commit aaba6bf

Please sign in to comment.