Skip to content

Commit

Permalink
Correct last vote handling in poll card
Browse files Browse the repository at this point in the history
The handling for last vote did not really work in the case
where the walletholder votes multiple times. This commit implements
a sensible and correct version.
  • Loading branch information
jamescowens committed Nov 25, 2023
1 parent dd7186e commit c5d1d58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/gridcoin/voting/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,11 @@ void PollResult::TallyVote(VoteDetail detail)

if (detail.m_ismine != ISMINE_NO) {
m_self_voted = true;
m_self_vote_detail = detail;

m_self_vote_detail.m_amount += detail.m_amount;
m_self_vote_detail.m_mining_id = detail.m_mining_id;
m_self_vote_detail.m_magnitude = detail.m_magnitude;
m_self_vote_detail.m_ismine = detail.m_ismine;
}

for (const auto& response_pair : detail.m_responses) {
Expand All @@ -1258,6 +1262,22 @@ void PollResult::TallyVote(VoteDetail detail)
m_responses[response_offset].m_weight += response_weight;
m_responses[response_offset].m_votes += 1.0 / detail.m_responses.size();
m_total_weight += response_weight;

if (detail.m_ismine != ISMINE_NO) {
bool choice_found = false;

for (auto& choice : m_self_vote_detail.m_responses) {
if (choice.first == response_offset) {
choice.second += response_weight;
choice_found = true;
break;
}
}

if (!choice_found) {
m_self_vote_detail.m_responses.push_back(std::make_pair(response_offset, response_weight));
}
}
}

m_votes.emplace_back(std::move(detail));
Expand Down
4 changes: 2 additions & 2 deletions src/qt/forms/voting/pollcard.ui
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@
<item row="2" column="0">
<widget class="QLabel" name="myLastVoteAnswerTextLabel">
<property name="text">
<string>Your Last Vote:</string>
<string>Your Vote(s):</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QLabel" name="myVoteWeightTextLabel">
<property name="text">
<string>Your Vote Weight:</string>
<string>Your Vote Weight(s):</string>
</property>
</widget>
</item>
Expand Down
9 changes: 8 additions & 1 deletion src/qt/voting/pollcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ PollCard::PollCard(const PollItem& poll_item, QWidget* parent)
ui->myPercentAVWLabel->setText("N/A");
} else {
QString choices_str;
QString weights_str;

int64_t my_total_weight = 0;

Expand All @@ -49,11 +50,17 @@ PollCard::PollCard(const PollItem& poll_item, QWidget* parent)
choices_str = QString(poll_item.m_choices[choice.first].m_label);
}

if (!weights_str.isEmpty()) {
weights_str += ", " + QString::number(choice.second / COIN);
} else {
weights_str = QString::number(choice.second / COIN);
}

my_total_weight += choice.second / COIN;
}

ui->myLastVoteAnswerLabel->setText(choices_str);
ui->myVoteWeightLabel->setText(QString::number(my_total_weight));
ui->myVoteWeightLabel->setText(weights_str);
if (poll_item.m_active_weight) ui->myPercentAVWLabel->setText(QString::number((double) my_total_weight
/ (double) poll_item.m_active_weight
* (double) 100.0, 'f', 4) + '\%');
Expand Down

0 comments on commit c5d1d58

Please sign in to comment.