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));
}
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;
}
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);
}
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);
}
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);
}
Aggregations