use of mage.game.events.VoteEvent in project mage by magefree.
the class VoteHandler method doVotes.
public void doVotes(Ability source, Game game, VoteHandlerAI<T> aiVoteHint) {
this.aiVoteHint = aiVoteHint;
this.playerMap.clear();
int stepCurrent = 0;
int stepTotal = game.getState().getPlayersInRange(source.getControllerId(), game).size();
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
stepCurrent++;
VoteEvent event = new VoteEvent(playerId, source);
game.replaceEvent(event);
Player player = game.getPlayer(event.getTargetId());
Player decidingPlayer = game.getPlayer(event.getPlayerId());
if (player == null || decidingPlayer == null) {
continue;
}
int voteCount = event.getExtraVotes() + event.getOptionalExtraVotes() + 1;
for (int i = 0; i < voteCount; i++) {
// Outcome.Benefit - AI must use extra vote all the time
if (i > event.getExtraVotes() && !player.chooseUse(Outcome.Benefit, "Use an extra vote?", source, game)) {
continue;
}
String stepName = (i > 0 ? "extra step" : "step");
String voteInfo = String.format("Vote, %s %d of %d", stepName, stepCurrent, stepTotal);
T vote;
if (decidingPlayer.isComputer() && this.aiVoteHint != null) {
// ai choose
vote = this.aiVoteHint.makeChoice(this, player, decidingPlayer, source, game);
} else {
// human choose
vote = playerChoose(voteInfo, player, decidingPlayer, source, game);
}
if (vote == null) {
continue;
}
String message = voteInfo + ": " + player.getName() + " voted for " + voteName(vote);
if (!Objects.equals(player, decidingPlayer)) {
message += " (chosen by " + decidingPlayer.getName() + ')';
}
game.informPlayers(message);
this.playerMap.computeIfAbsent(playerId, x -> new ArrayList<>()).add(vote);
}
}
// show final results to players
Map<T, Integer> totalVotes = new LinkedHashMap<>();
// fill by possible choices
this.getPossibleVotes(source, game).forEach(vote -> {
totalVotes.putIfAbsent(vote, 0);
});
// fill by real choices
playerMap.entrySet().stream().flatMap(votesList -> votesList.getValue().stream()).forEach(vote -> {
totalVotes.compute(vote, CardUtil::setOrIncrementValue);
});
Set<T> winners = this.getMostVoted();
String totalVotesStr = totalVotes.entrySet().stream().map(entry -> (winners.contains(entry.getKey()) ? " -win- " : " -lose- ") + voteName(entry.getKey()) + ": " + entry.getValue()).sorted().collect(Collectors.joining("<br>"));
game.informPlayers("Vote results:<br>" + totalVotesStr);
game.fireEvent(new VotedEvent(source, this));
}
Aggregations