Search in sources :

Example 1 with Vote

use of org.xel.Vote in project elastic-core-maven by OrdinaryDude.

the class GetPollVotes method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    boolean includeWeights = "true".equalsIgnoreCase(req.getParameter("includeWeights"));
    Poll poll = ParameterParser.getPoll(req);
    int countHeight;
    JSONData.VoteWeighter weighter = null;
    if (includeWeights && (countHeight = Math.min(poll.getFinishHeight(), Nxt.getBlockchain().getHeight())) >= Nxt.getBlockchainProcessor().getMinRollbackHeight()) {
        VoteWeighting voteWeighting = poll.getVoteWeighting();
        VoteWeighting.VotingModel votingModel = voteWeighting.getVotingModel();
        weighter = voterId -> votingModel.calcWeight(voteWeighting, voterId, countHeight);
    }
    JSONArray votesJson = new JSONArray();
    try (DbIterator<Vote> votes = Vote.getVotes(poll.getId(), firstIndex, lastIndex)) {
        for (Vote vote : votes) {
            votesJson.add(JSONData.vote(vote, weighter));
        }
    }
    JSONObject response = new JSONObject();
    response.put("votes", votesJson);
    return response;
}
Also used : Vote(org.xel.Vote) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Poll(org.xel.Poll) VoteWeighting(org.xel.VoteWeighting)

Example 2 with Vote

use of org.xel.Vote in project elastic-core-maven by OrdinaryDude.

the class GetPollVote method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    Poll poll = ParameterParser.getPoll(req);
    long accountId = ParameterParser.getAccountId(req, true);
    boolean includeWeights = "true".equalsIgnoreCase(req.getParameter("includeWeights"));
    Vote vote = Vote.getVote(poll.getId(), accountId);
    if (vote != null) {
        int countHeight;
        JSONData.VoteWeighter weighter = null;
        if (includeWeights && (countHeight = Math.min(poll.getFinishHeight(), Nxt.getBlockchain().getHeight())) >= Nxt.getBlockchainProcessor().getMinRollbackHeight()) {
            VoteWeighting voteWeighting = poll.getVoteWeighting();
            VoteWeighting.VotingModel votingModel = voteWeighting.getVotingModel();
            weighter = voterId -> votingModel.calcWeight(voteWeighting, voterId, countHeight);
        }
        return JSONData.vote(vote, weighter);
    }
    return JSON.emptyJSON;
}
Also used : Vote(org.xel.Vote) Poll(org.xel.Poll) VoteWeighting(org.xel.VoteWeighting)

Aggregations

Poll (org.xel.Poll)2 Vote (org.xel.Vote)2 VoteWeighting (org.xel.VoteWeighting)2 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1