diff --git a/Mage.Tests/src/test/java/org/mage/test/load/LoadCallbackClient.java b/Mage.Tests/src/test/java/org/mage/test/load/LoadCallbackClient.java index 41f31bd388..0f07cbb193 100644 --- a/Mage.Tests/src/test/java/org/mage/test/load/LoadCallbackClient.java +++ b/Mage.Tests/src/test/java/org/mage/test/load/LoadCallbackClient.java @@ -4,9 +4,14 @@ import mage.interfaces.callback.CallbackClient; import mage.interfaces.callback.ClientCallback; import mage.remote.Session; import mage.utils.CompressUtil; +import mage.view.GameClientMessage; +import mage.view.GameView; +import mage.view.SimpleCardView; import mage.view.TableClientMessage; import org.apache.log4j.Logger; +import java.util.UUID; + /** * @author noxx */ @@ -15,15 +20,58 @@ public class LoadCallbackClient implements CallbackClient { private static final transient Logger log = Logger.getLogger(LoadCallbackClient.class); private Session session; + private UUID gameId; + private UUID playerId; + private boolean gameOver; + + private volatile int controlCount; + + private GameView gameView; @Override public void processCallback(ClientCallback callback) { //TODO + controlCount = 0; log.info(callback.getMethod()); callback.setData(CompressUtil.decompress(callback.getData())); if (callback.getMethod().equals("startGame")) { TableClientMessage message = (TableClientMessage) callback.getData(); + gameId = message.getGameId(); + playerId = message.getPlayerId(); session.joinGame(message.getGameId()); + startControlThread(); + } else if (callback.getMethod().equals("gameInform")) { + GameClientMessage message = (GameClientMessage) callback.getData(); + log.info("Inform: " + message.getMessage()); + gameView = message.getGameView(); + } else if (callback.getMethod().equals("gameInit")) { + + } else if (callback.getMethod().equals("gameTarget")) { + GameClientMessage message = (GameClientMessage) callback.getData(); + log.info("Target: " + message.getMessage()); + if (message.getMessage().equals("Select a starting player")) { + session.sendPlayerUUID(gameId, playerId); + } else if (message.getMessage().equals("Select a card to discard")) { + log.info("hand size: " + gameView.getHand().size()); + SimpleCardView card = gameView.getHand().values().iterator().next(); + session.sendPlayerUUID(gameId, card.getId()); + } + } else if (callback.getMethod().equals("gameAsk")) { + GameClientMessage message = (GameClientMessage) callback.getData(); + log.info("Ask: " + message.getMessage()); + if (message.getMessage().equals("Do you want to take a mulligan?")) { + session.sendPlayerBoolean(gameId, false); + } + } else if (callback.getMethod().equals("gameSelect")) { + GameClientMessage message = (GameClientMessage) callback.getData(); + log.info("Select: " + message.getMessage()); + if (LoadPhaseManager.getInstance().isSkip(message.getGameView(), message.getMessage(), playerId)) { + log.info("Skipped: " + message.getMessage()); + session.sendPlayerBoolean(gameId, false); + } + } else if (callback.getMethod().equals("gameOver")) { + log.info("Game over"); + gameOver = true; } } @@ -31,4 +79,30 @@ public class LoadCallbackClient implements CallbackClient { public void setSession(Session session) { this.session = session; } + + public boolean isGameOver() { + return gameOver; + } + + private void startControlThread() { + new Thread(new Runnable() { + @Override + public void run() { + while (true) { + controlCount++; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (controlCount > 5) { + log.warn("Game seems freezed. Sending boolean message to server."); + session.sendPlayerBoolean(gameId, false); + controlCount = 0; + } + } + + } + }).start(); + } } diff --git a/Mage.Tests/src/test/java/org/mage/test/load/LoadPhaseManager.java b/Mage.Tests/src/test/java/org/mage/test/load/LoadPhaseManager.java new file mode 100644 index 0000000000..8b9470864f --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/load/LoadPhaseManager.java @@ -0,0 +1,82 @@ +package org.mage.test.load; + +import mage.view.GameView; +import mage.view.PlayerView; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class LoadPhaseManager { + + private static final LoadPhaseManager fInstance = new LoadPhaseManager(); + + public static String DEFAULT_PLAYER_NAME = "player"; + + public static String PHASE_ON = "on"; + public static String PHASE_OFF = "off"; + public static String UPKEEP_YOU = "upkeepYou"; + public static String DRAW_YOU = "drawYou"; + public static String MAIN_YOU = "mainYou"; + public static String BEFORE_COMBAT_YOU = "beforeCombatYou"; + public static String END_OF_COMBAT_YOU = "endOfCombatYou"; + public static String MAIN_2_YOU = "main2You"; + public static String END_OF_TURN_YOU = "endOfTurnYou"; + + public static String UPKEEP_OTHERS = "upkeepOthers"; + public static String DRAW_OTHERS = "drawOthers"; + public static String MAIN_OTHERS = "mainOthers"; + public static String BEFORE_COMBAT_OTHERS = "beforeCombatOthers"; + public static String END_OF_COMBAT_OTHERS = "endOfCombatOthers"; + public static String MAIN_2_OTHERS = "main2Others"; + public static String END_OF_TURN_OTHERS = "endOfTurnOthers"; + + private static Map mapYou = new HashMap() {{ + put("Upkeep - play instants and activated abilities.", UPKEEP_YOU); + put("Draw - play instants and activated abilities.", DRAW_YOU); + put("Precombat Main - play spells and abilities.", MAIN_YOU); + put("Begin Combat - play instants and activated abilities.", BEFORE_COMBAT_YOU); + put("End Combat - play instants and activated abilities.", END_OF_COMBAT_YOU); + put("Postcombat Main - play spells and abilities.", MAIN_2_YOU); + put("End Turn - play instants and activated abilities.", END_OF_TURN_YOU); + }}; + + private static Map mapOthers = new HashMap() {{ + put("Upkeep - play instants and activated abilities.", UPKEEP_OTHERS); + put("Draw - play instants and activated abilities.", DRAW_OTHERS); + put("Precombat Main - play instants and activated abilities.", MAIN_OTHERS); + put("Begin Combat - play instants and activated abilities.", BEFORE_COMBAT_OTHERS); + put("End Combat - play instants and activated abilities.", END_OF_COMBAT_OTHERS); + put("Postcombat Main - play instants and activated abilities.", MAIN_2_OTHERS); + put("End Turn - play instants and activated abilities.", END_OF_TURN_OTHERS); + }}; + + public static LoadPhaseManager getInstance() { + return fInstance; + } + + public boolean isSkip(GameView gameView, String message, UUID playerId) { + UUID activePlayer = null; + Map map = mapOthers; + for (PlayerView playerView : gameView.getPlayers()) { + if (playerView.isActive()) { + activePlayer = playerView.getPlayerId(); + if (activePlayer.equals(playerId)) { + map = mapYou; + } + } + } + if (activePlayer == null) { + throw new IllegalStateException("No active player found."); + } + for (Map.Entry entry : map.entrySet()) { + if (message.equals(entry.getKey())) { + /*if (message.equals("Precombat Main - play spells and abilities.")) { + return false; + }*/ + return true; + } + } + return false; + } +} diff --git a/Mage.Tests/src/test/java/org/mage/test/load/LoadTest.java b/Mage.Tests/src/test/java/org/mage/test/load/LoadTest.java index 8e72dea2c3..21fd8ebe3e 100644 --- a/Mage.Tests/src/test/java/org/mage/test/load/LoadTest.java +++ b/Mage.Tests/src/test/java/org/mage/test/load/LoadTest.java @@ -129,15 +129,14 @@ public class LoadTest { } /** - * Test playing the whole game. - * Player use cheat to add lands, creatures and other cards. - * Then play only lands, one of them plays 1 damage targeting player. + * Tests simple game till the end (game over). + * Players do nothing but skip phases and discard cards at the end. * - * This results in 40 turns of the game. + * This results in a game that lasts until there is no cards in library. */ @Test @Ignore - public void testPlayGame() throws Exception { + public void testSimpleGame() throws Exception { DeckCardLists deckList = createDeck(); for (int i = 0; i < EXECUTION_COUNT_PLAY_GAME; i++) { @@ -182,10 +181,25 @@ public class LoadTest { /*** Start game ***/ session.startGame(roomId, table.getTableId()); - Thread.sleep(100); + while (!mageClient.isGameOver()) { + Thread.sleep(1000); + } } } + /** + * Tests playing the whole game. + * Player use cheat to add lands, creatures and other cards. + * Then play only lands, one of them plays 1 damage targeting player. + * + * This results in 40 turns of the game. + */ + @Test + @Ignore + public void testPlayGame() throws Exception { + //TODO: to be implemented + } + /** * Creates connection to the server. * Server should run independently. diff --git a/Mage.Tests/src/test/java/org/mage/test/load/SimpleMageClient.java b/Mage.Tests/src/test/java/org/mage/test/load/SimpleMageClient.java index b46a2ed987..a21da72c79 100644 --- a/Mage.Tests/src/test/java/org/mage/test/load/SimpleMageClient.java +++ b/Mage.Tests/src/test/java/org/mage/test/load/SimpleMageClient.java @@ -66,4 +66,8 @@ public class SimpleMageClient implements MageClient { public void setSession(Session session) { ((LoadCallbackClient)callbackClient).setSession(session); } + + public boolean isGameOver() { + return ((LoadCallbackClient)callbackClient).isGameOver(); + } }