Search in sources :

Example 6 with CardUtil

use of mage.util.CardUtil 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 7 with CardUtil

use of mage.util.CardUtil in project mage by magefree.

the class HintOfInsanityEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getFirstTarget());
    if (player == null) {
        return false;
    }
    Map<String, Integer> nameCounts = new HashMap<>();
    player.getHand().getCards(game).stream().map(MageObject::getName).forEach(s -> nameCounts.compute(s, CardUtil::setOrIncrementValue));
    Cards cards = new CardsImpl(player.getHand().getCards(game).stream().filter(Objects::nonNull).filter(card -> !card.isLand(game)).filter(card -> nameCounts.getOrDefault(card.getName(), 0) > 1).collect(Collectors.toSet()));
    player.discard(cards, false, source, game);
    return true;
}
Also used : Cards(mage.cards.Cards) Outcome(mage.constants.Outcome) OneShotEffect(mage.abilities.effects.OneShotEffect) CardUtil(mage.util.CardUtil) HashMap(java.util.HashMap) TargetPlayer(mage.target.TargetPlayer) UUID(java.util.UUID) CardsImpl(mage.cards.CardsImpl) Collectors(java.util.stream.Collectors) Player(mage.players.Player) CardSetInfo(mage.cards.CardSetInfo) Objects(java.util.Objects) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) Map(java.util.Map) CardType(mage.constants.CardType) MageObject(mage.MageObject) Ability(mage.abilities.Ability) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) HashMap(java.util.HashMap) Objects(java.util.Objects) Cards(mage.cards.Cards) CardsImpl(mage.cards.CardsImpl)

Example 8 with CardUtil

use of mage.util.CardUtil in project mage by magefree.

the class SorcererClassWatcher method watch.

@Override
public void watch(GameEvent event, Game game) {
    if (event.getType() != GameEvent.EventType.SPELL_CAST) {
        return;
    }
    Spell spell = game.getSpell(event.getTargetId());
    if (spell == null || !spell.isInstantOrSorcery(game)) {
        return;
    }
    spellMap.compute(spell.getControllerId(), CardUtil::setOrIncrementValue);
}
Also used : Spell(mage.game.stack.Spell) CardUtil(mage.util.CardUtil)

Example 9 with CardUtil

use of mage.util.CardUtil in project mage by magefree.

the class WhispersteelDaggerWatcher method addPlayable.

void addPlayable(Ability source, UUID ownerId, Game game) {
    MageObjectReference mor = new MageObjectReference(source.getSourceId(), source.getSourceObjectZoneChangeCounter(), game);
    morMap.computeIfAbsent(mor, m -> new HashMap<>()).computeIfAbsent(ownerId, m -> new HashMap<>()).compute(source.getControllerId(), CardUtil::setOrIncrementValue);
}
Also used : SimpleStaticAbility(mage.abilities.common.SimpleStaticAbility) ManaPoolItem(mage.players.ManaPoolItem) AsThoughManaEffect(mage.abilities.effects.AsThoughManaEffect) BoostEquippedEffect(mage.abilities.effects.common.continuous.BoostEquippedEffect) EquipAbility(mage.abilities.keyword.EquipAbility) MageObjectReference(mage.MageObjectReference) CardUtil(mage.util.CardUtil) HashMap(java.util.HashMap) DealsDamageToAPlayerAttachedTriggeredAbility(mage.abilities.common.DealsDamageToAPlayerAttachedTriggeredAbility) UUID(java.util.UUID) CardSetInfo(mage.cards.CardSetInfo) Game(mage.game.Game) Watcher(mage.watchers.Watcher) AsThoughEffectImpl(mage.abilities.effects.AsThoughEffectImpl) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) Map(java.util.Map) Card(mage.cards.Card) mage.constants(mage.constants) Ability(mage.abilities.Ability) HashMap(java.util.HashMap) MageObjectReference(mage.MageObjectReference) CardUtil(mage.util.CardUtil)

Example 10 with CardUtil

use of mage.util.CardUtil in project mage by magefree.

the class ChainerNightmareAdeptTriggeredAbility method addPlayable.

void addPlayable(Ability source, Game game) {
    MageObjectReference mor = new MageObjectReference(source.getSourceId(), source.getSourceObjectZoneChangeCounter(), game);
    morMap.computeIfAbsent(mor, m -> new HashMap<>()).compute(source.getControllerId(), CardUtil::setOrIncrementValue);
}
Also used : LimitedTimesPerTurnActivatedAbility(mage.abilities.common.LimitedTimesPerTurnActivatedAbility) HasteAbility(mage.abilities.keyword.HasteAbility) MageObjectReference(mage.MageObjectReference) CardUtil(mage.util.CardUtil) HashMap(java.util.HashMap) UUID(java.util.UUID) MageInt(mage.MageInt) EntersBattlefieldAllTriggeredAbility(mage.abilities.common.EntersBattlefieldAllTriggeredAbility) ContinuousEffect(mage.abilities.effects.ContinuousEffect) DiscardCardCost(mage.abilities.costs.common.DiscardCardCost) CardSetInfo(mage.cards.CardSetInfo) Game(mage.game.Game) Watcher(mage.watchers.Watcher) AsThoughEffectImpl(mage.abilities.effects.AsThoughEffectImpl) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) Map(java.util.Map) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) Card(mage.cards.Card) mage.constants(mage.constants) CastFromHandWatcher(mage.watchers.common.CastFromHandWatcher) Ability(mage.abilities.Ability) GainAbilityTargetEffect(mage.abilities.effects.common.continuous.GainAbilityTargetEffect) TokenPredicate(mage.filter.predicate.permanent.TokenPredicate) HashMap(java.util.HashMap) MageObjectReference(mage.MageObjectReference) CardUtil(mage.util.CardUtil)

Aggregations

CardUtil (mage.util.CardUtil)10 Ability (mage.abilities.Ability)6 Game (mage.game.Game)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 UUID (java.util.UUID)5 MageObjectReference (mage.MageObjectReference)4 AsThoughEffectImpl (mage.abilities.effects.AsThoughEffectImpl)4 CardImpl (mage.cards.CardImpl)4 CardSetInfo (mage.cards.CardSetInfo)4 mage.constants (mage.constants)4 GameEvent (mage.game.events.GameEvent)4 Permanent (mage.game.permanent.Permanent)4 Player (mage.players.Player)4 Watcher (mage.watchers.Watcher)4 OneShotEffect (mage.abilities.effects.OneShotEffect)3 Card (mage.cards.Card)3 Collectors (java.util.stream.Collectors)2 SimpleActivatedAbility (mage.abilities.common.SimpleActivatedAbility)2 TapSourceCost (mage.abilities.costs.common.TapSourceCost)2