Search in sources :

Example 1 with VotedEvent

use of mage.game.events.VotedEvent 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));
}
Also used : Game(mage.game.Game) java.util(java.util) VotedEvent(mage.game.events.VotedEvent) Outcome(mage.constants.Outcome) CardUtil(mage.util.CardUtil) Collectors(java.util.stream.Collectors) VoteEvent(mage.game.events.VoteEvent) Player(mage.players.Player) Ability(mage.abilities.Ability) Player(mage.players.Player) CardUtil(mage.util.CardUtil) VotedEvent(mage.game.events.VotedEvent) VoteEvent(mage.game.events.VoteEvent)

Example 2 with VotedEvent

use of mage.game.events.VotedEvent in project mage by magefree.

the class GrudgeKeeperEffect method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    VotedEvent votedEvent = (VotedEvent) event;
    this.getEffects().clear();
    this.addEffect(new GrudgeKeeperEffect(votedEvent.getDidntVote(getControllerId())));
    return true;
}
Also used : VotedEvent(mage.game.events.VotedEvent)

Aggregations

VotedEvent (mage.game.events.VotedEvent)2 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Ability (mage.abilities.Ability)1 Outcome (mage.constants.Outcome)1 Game (mage.game.Game)1 VoteEvent (mage.game.events.VoteEvent)1 Player (mage.players.Player)1 CardUtil (mage.util.CardUtil)1