diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index 206d361141..76b5cdd886 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -28,6 +28,13 @@ package mage.server.game; +import java.io.*; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.zip.GZIPOutputStream; import mage.Constants.Zone; import mage.MageException; import mage.abilities.Ability; @@ -55,13 +62,6 @@ import mage.view.GameView; import mage.view.PermanentView; import org.apache.log4j.Logger; -import java.io.*; -import java.util.*; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; -import java.util.zip.GZIPOutputStream; /** * @@ -130,7 +130,7 @@ public class GameController implements GameCallback { new Listener () { @Override public void event(PlayerQueryEvent event) { -// logger.info(event.getPlayerId() + "--" + event.getQueryType() + "--" + event.getMessage()); + logger.trace(new StringBuilder(event.getPlayerId().toString()).append("--").append(event.getQueryType()).append("--").append(event.getMessage()).toString()); try { switch (event.getQueryType()) { case ASK: @@ -198,8 +198,8 @@ public class GameController implements GameCallback { User user = UserManager.getInstance().getUser(userId); gameSession.setUserData(user.getUserData()); user.addGame(playerId, gameSession); - logger.info("player " + playerId + " has joined game " + game.getId()); - ChatManager.getInstance().broadcast(chatId, "", game.getPlayer(playerId).getName() + " has joined the game", MessageColor.BLACK); + logger.info(new StringBuilder("Player ").append(playerId).append(" has joined game ").append(game.getId()).toString()); + ChatManager.getInstance().broadcast(chatId, "", new StringBuilder(game.getPlayer(playerId).getName()).append(" has joined the game").toString(), MessageColor.BLACK); checkStart(); } @@ -328,15 +328,16 @@ public class GameController implements GameCallback { public void sendPlayerUUID(UUID userId, final UUID data) { sendMessage(userId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).sendPlayerUUID(data); } }); - } public void sendPlayerString(UUID userId, final String data) { sendMessage(userId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).sendPlayerString(data); } @@ -345,6 +346,7 @@ public class GameController implements GameCallback { public void sendPlayerBoolean(UUID userId, final Boolean data) { sendMessage(userId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).sendPlayerBoolean(data); } @@ -354,6 +356,7 @@ public class GameController implements GameCallback { public void sendPlayerInteger(UUID userId, final Integer data) { sendMessage(userId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).sendPlayerInteger(data); } @@ -371,15 +374,8 @@ public class GameController implements GameCallback { } private synchronized void ask(UUID playerId, final String question) throws MageException { - /*if (question.equals("Do you want to take a mulligan?")) { - System.out.println("reverted"); - for (UUID uuid : game.getOpponents(playerId)) { - gameSessions.get(uuid).ask(question); - informOthers(uuid); - } - return; - }*/ perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).ask(question); } @@ -389,6 +385,7 @@ public class GameController implements GameCallback { private synchronized void chooseAbility(UUID playerId, final List choices) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).chooseAbility(new AbilityPickerView(choices)); } @@ -397,6 +394,7 @@ public class GameController implements GameCallback { private synchronized void choosePile(UUID playerId, final String message, final List pile1, final List pile2) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).choosePile(message, new CardsView(pile1), new CardsView(pile2)); } @@ -405,6 +403,7 @@ public class GameController implements GameCallback { private synchronized void chooseMode(UUID playerId, final Map modes) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).chooseAbility(new AbilityPickerView(modes)); } @@ -413,6 +412,7 @@ public class GameController implements GameCallback { private synchronized void choose(UUID playerId, final String message, final Set choices) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).choose(message, choices); } @@ -421,6 +421,7 @@ public class GameController implements GameCallback { private synchronized void target(UUID playerId, final String question, final Cards cards, final List perms, final Set targets, final boolean required, final Map options) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { if (cards != null) { gameSessions.get(playerId).target(question, new CardsView(cards.getCards(game)), targets, required, options); @@ -430,15 +431,17 @@ public class GameController implements GameCallback { permsView.put(perm.getId(), new PermanentView(perm, game.getCard(perm.getId()))); } gameSessions.get(playerId).target(question, permsView, targets, required, options); - } else + } else { gameSessions.get(playerId).target(question, new CardsView(), targets, required, options); } + } }); } private synchronized void target(UUID playerId, final String question, final Collection abilities, final boolean required, final Map options) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).target(question, new CardsView(abilities, game), null, required, options); } @@ -447,6 +450,7 @@ public class GameController implements GameCallback { private synchronized void select(final UUID playerId, final String message) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).select(message); } @@ -455,6 +459,7 @@ public class GameController implements GameCallback { private synchronized void playMana(UUID playerId, final String message) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).playMana(message); } @@ -463,6 +468,7 @@ public class GameController implements GameCallback { private synchronized void playXMana(UUID playerId, final String message) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).playXMana(message); } @@ -471,6 +477,7 @@ public class GameController implements GameCallback { private synchronized void amount(UUID playerId, final String message, final int min, final int max) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).getAmount(message, min, max); } @@ -485,6 +492,7 @@ public class GameController implements GameCallback { private synchronized void lookAtCards(UUID playerId, final String name, final Cards cards) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).revealCards(name, new CardsView(cards.getCards(game))); } @@ -492,23 +500,24 @@ public class GameController implements GameCallback { } private void informOthers(UUID playerId) throws MageException { - String message = ""; - if (game.getStep() != null) - message += game.getStep().getType().toString() + " - "; - message += "Waiting for " + game.getPlayer(playerId).getName(); + StringBuilder message = new StringBuilder(); + if (game.getStep() != null) { + message.append(game.getStep().getType().toString()).append(" - "); + } + message.append("Waiting for ").append(game.getPlayer(playerId).getName()); for (final Entry entry: gameSessions.entrySet()) { if (!entry.getKey().equals(playerId)) { - entry.getValue().inform(message); + entry.getValue().inform(message.toString()); } } for (final GameWatcher watcher: watchers.values()) { - watcher.inform(message); + watcher.inform(message.toString()); } } private void informOthers(List players) throws MageException { // first player is always original controller - final String message = game.getStep().toString() + " - Waiting for " + game.getPlayer(players.get(0)).getName(); + final String message = new StringBuilder(game.getStep().toString()).append(" - Waiting for ").append(game.getPlayer(players.get(0)).getName()).toString(); for (final Entry entry: gameSessions.entrySet()) { boolean skip = false; for (UUID uuid : players) { @@ -517,7 +526,9 @@ public class GameController implements GameCallback { break; } } - if (!skip) entry.getValue().inform(message); + if (!skip) { + entry.getValue().inform(message); + } } for (final GameWatcher watcher: watchers.values()) { watcher.inform(message); @@ -526,6 +537,7 @@ public class GameController implements GameCallback { private synchronized void informPersonal(UUID playerId, final String message) throws MageException { perform(playerId, new Command() { + @Override public void execute(UUID playerId) { gameSessions.get(playerId).informPersonal(message); } @@ -590,16 +602,22 @@ public class GameController implements GameCallback { private void perform(UUID playerId, Command command, boolean informOthers) throws MageException { if (game.getPlayer(playerId).isGameUnderControl()) { - if (gameSessions.containsKey(playerId)) + if (gameSessions.containsKey(playerId)) { command.execute(playerId); - if (informOthers) informOthers(playerId); + } + if (informOthers) { + informOthers(playerId); + } } else { List players = Splitter.split(game, playerId); for (UUID uuid : players) { - if (gameSessions.containsKey(uuid)) + if (gameSessions.containsKey(uuid)) { command.execute(uuid); + } + } + if (informOthers) { + informOthers(players); } - if (informOthers) informOthers(players); } } @@ -609,8 +627,9 @@ public class GameController implements GameCallback { // if it's your priority (or game not started yet in which case it will be null) // then execute only your action if (game.getPriorityPlayerId() == null || game.getPriorityPlayerId().equals(playerId)) { - if (gameSessions.containsKey(playerId)) + if (gameSessions.containsKey(playerId)) { command.execute(playerId); + } } // otherwise execute the action under other player's control else { //System.out.println("asThough: " + playerId + " " + game.getPriorityPlayerId()); @@ -630,11 +649,11 @@ public class GameController implements GameCallback { Player priorityPlayer = game.getPlayer(game.getPriorityPlayerId()); logger.warn(" priority player: " + priorityPlayer.getName() + ", id: " + priorityPlayer.getId()); logger.warn(" command: " + command.toString()); + logger.warn(" command-class: " + command.getClass().getName()); } } } else { // ignore - no control over the turn - return; } } diff --git a/Mage/src/mage/abilities/effects/common/turn/ControlTargetPlayerNextTurnEffect.java b/Mage/src/mage/abilities/effects/common/turn/ControlTargetPlayerNextTurnEffect.java index 7c246b25a8..e2a8969363 100644 --- a/Mage/src/mage/abilities/effects/common/turn/ControlTargetPlayerNextTurnEffect.java +++ b/Mage/src/mage/abilities/effects/common/turn/ControlTargetPlayerNextTurnEffect.java @@ -27,13 +27,14 @@ */ package mage.abilities.effects.common.turn; -import mage.Constants; +import java.util.UUID; +import mage.Constants.Outcome; import mage.abilities.Ability; import mage.abilities.effects.OneShotEffect; import mage.game.Game; import mage.game.turn.TurnMod; -import java.util.UUID; + /** * @author nantuko @@ -41,7 +42,7 @@ import java.util.UUID; public class ControlTargetPlayerNextTurnEffect extends OneShotEffect { public ControlTargetPlayerNextTurnEffect() { - super(Constants.Outcome.Benefit); + super(Outcome.Benefit); staticText = "You control target player during that player's next turn. (You see all cards that player could see and make all decisions for the player.)"; } diff --git a/Mage/src/mage/game/turn/Turn.java b/Mage/src/mage/game/turn/Turn.java index 4a94fd54a0..67c498d883 100644 --- a/Mage/src/mage/game/turn/Turn.java +++ b/Mage/src/mage/game/turn/Turn.java @@ -28,17 +28,17 @@ package mage.game.turn; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; import mage.Constants.PhaseStep; import mage.Constants.TurnPhase; import mage.game.Game; import mage.game.events.GameEvent; import mage.players.Player; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.UUID; /** * @@ -59,8 +59,9 @@ public class Turn implements Serializable { } public Turn(final Turn turn) { - if (turn.currentPhase != null) + if (turn.currentPhase != null) { this.currentPhase = turn.currentPhase.copy(); + } this.activePlayerId = turn.activePlayerId; for (Phase phase: turn.phases) { this.phases.add(phase.copy()); @@ -69,8 +70,9 @@ public class Turn implements Serializable { } public TurnPhase getPhaseType() { - if (currentPhase != null) + if (currentPhase != null) { return currentPhase.getType(); + } return null; } @@ -92,23 +94,27 @@ public class Turn implements Serializable { } public Step getStep() { - if (currentPhase != null) + if (currentPhase != null) { return currentPhase.getStep(); + } return null; } public PhaseStep getStepType() { - if (currentPhase != null && currentPhase.getStep() != null) + if (currentPhase != null && currentPhase.getStep() != null) { return currentPhase.getStep().getType(); + } return null; } public void play(Game game, UUID activePlayerId) { - if (game.isPaused() || game.isGameOver()) + if (game.isPaused() || game.isGameOver()) { return; + } - if (game.getState().getTurnMods().skipTurn(activePlayerId)) + if (game.getState().getTurnMods().skipTurn(activePlayerId)) { return; + } checkTurnIsControlledByOtherPlayer(game, activePlayerId); @@ -116,8 +122,9 @@ public class Turn implements Serializable { resetCounts(); game.getPlayer(activePlayerId).beginTurn(game); for (Phase phase: phases) { - if (game.isPaused() || game.isGameOver()) + if (game.isPaused() || game.isGameOver()) { return; + } currentPhase = phase; game.fireEvent(new GameEvent(GameEvent.EventType.PHASE_CHANGED, activePlayerId, null, activePlayerId)); if (!game.getState().getTurnMods().skipPhase(activePlayerId, currentPhase.getType())) { @@ -128,7 +135,8 @@ public class Turn implements Serializable { //game.saveState(); //20091005 - 500.8 - while (playExtraPhases(game, phase.getType())); + while (playExtraPhases(game, phase.getType())) { + } } } // magenoxx: this causes bugs when we need to add several phases connected with each other (WorldAtWarTest) @@ -159,8 +167,9 @@ public class Turn implements Serializable { } while (it.hasNext()) { phase = it.next(); - if (game.isPaused() || game.isGameOver()) + if (game.isPaused() || game.isGameOver()) { return; + } currentPhase = phase; if (!game.getState().getTurnMods().skipPhase(activePlayerId, currentPhase.getType())) { if (phase.play(game, activePlayerId)) { @@ -193,11 +202,13 @@ public class Turn implements Serializable { private boolean playExtraPhases(Game game, TurnPhase afterPhase) { TurnMod extraPhaseTurnMod = game.getState().getTurnMods().extraPhase(activePlayerId, afterPhase); - if (extraPhaseTurnMod == null) + if (extraPhaseTurnMod == null) { return false; + } TurnPhase extraPhase = extraPhaseTurnMod.getExtraPhase(); - if (extraPhase == null) + if (extraPhase == null) { return false; + } Phase phase; switch(extraPhase) { case BEGINNING: diff --git a/Mage/src/mage/game/turn/TurnMod.java b/Mage/src/mage/game/turn/TurnMod.java index 9d0d533e85..bd6cff31c2 100644 --- a/Mage/src/mage/game/turn/TurnMod.java +++ b/Mage/src/mage/game/turn/TurnMod.java @@ -28,11 +28,11 @@ package mage.game.turn; +import java.io.Serializable; +import java.util.UUID; import mage.Constants.PhaseStep; import mage.Constants.TurnPhase; -import java.io.Serializable; -import java.util.UUID; /** * stores extra turns, phases or steps @@ -53,15 +53,29 @@ public class TurnMod implements Serializable { private TurnPhase afterPhase; private PhaseStep afterStep; + /** + * Used to define if a player skips the next turn or gets an extra turn. + * + * @param playerId + * @param skip - true = skips next turn, false = player gets extra turn + */ public TurnMod(UUID playerId, boolean skip) { this.id = UUID.randomUUID(); this.playerId = playerId; - if (skip) + if (skip) { this.skipTurn = true; - else + } + else { this.extraTurn = true; + } } + /** + * Used to define that a player controlls the next turn of another player. + * + * @param playerId - id of the player whose next turn is controlled by newControllerId + * @param newControllerId - id of the player that controlls playerId's next turn + */ public TurnMod(UUID playerId, UUID newControllerId) { this.id = UUID.randomUUID(); this.playerId = playerId; @@ -69,6 +83,7 @@ public class TurnMod implements Serializable { } /** + * Used to define if and when a player gets an extra phase. * * @param playerId * @param phase @@ -77,17 +92,20 @@ public class TurnMod implements Serializable { public TurnMod(UUID playerId, TurnPhase phase, TurnPhase afterPhase, boolean skip) { this.id = UUID.randomUUID(); this.playerId = playerId; - if (skip) + if (skip) { this.skipPhase = phase; - else + } + else { this.extraPhase = phase; + } this.afterPhase = afterPhase; } /** + * Used to define if and when a player gets an extra step. * * @param playerId - * @param step + * @param step - extra step the player gets * @param afterStep - set to null if extraStep is after the next step */ public TurnMod(UUID playerId, Step step, PhaseStep afterStep) { @@ -97,6 +115,12 @@ public class TurnMod implements Serializable { this.afterStep = afterStep; } + /** + * Used to define that a player skips the next time the specified step + * + * @param playerId + * @param step - step to skip the next time + */ public TurnMod(UUID playerId, PhaseStep step) { this.id = UUID.randomUUID(); this.playerId = playerId; @@ -111,8 +135,9 @@ public class TurnMod implements Serializable { this.skipTurn = mod.skipTurn; this.extraPhase = mod.extraPhase; this.skipPhase = mod.skipPhase; - if (mod.extraStep != null) + if (mod.extraStep != null) { this.extraStep = mod.extraStep.copy(); + } this.skipStep = mod.skipStep; this.afterPhase = mod.afterPhase; this.afterStep = mod.afterStep;