Test framework: added support to test client side data in unit tests (getGameView -> CardView, etc);

This commit is contained in:
Oleg Agafonov 2021-07-19 13:07:09 +04:00
parent 26fea5d07b
commit fc0ff6c22d
7 changed files with 59 additions and 22 deletions

View file

@ -11,8 +11,8 @@ import mage.interfaces.callback.ClientCallback;
import mage.interfaces.callback.ClientCallbackMethod; import mage.interfaces.callback.ClientCallbackMethod;
import mage.players.Player; import mage.players.Player;
import mage.server.User; import mage.server.User;
import mage.server.managers.UserManager;
import mage.server.managers.ManagerFactory; import mage.server.managers.ManagerFactory;
import mage.server.managers.UserManager;
import mage.view.*; import mage.view.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -195,6 +195,18 @@ public class GameSessionPlayer extends GameSessionWatcher {
@Override @Override
public GameView getGameView() { public GameView getGameView() {
return prepareGameView(game, playerId, userId);
}
/**
* Prepare client-server data. Can be used in real games or in unit tests
*
* @param game
* @param playerId
* @param userId can be null for tests
* @return
*/
public static GameView prepareGameView(Game game, UUID playerId, UUID userId) {
Player player = game.getPlayer(playerId); Player player = game.getPlayer(playerId);
GameView gameView = new GameView(game.getState(), game, playerId, null); GameView gameView = new GameView(game.getState(), game, playerId, null);
gameView.setHand(new CardsView(game, player.getHand().getCards(game))); gameView.setHand(new CardsView(game, player.getHand().getCards(game)));
@ -202,8 +214,8 @@ public class GameSessionPlayer extends GameSessionWatcher {
gameView.setCanPlayObjects(player.getPlayableObjects(game, Zone.ALL)); gameView.setCanPlayObjects(player.getPlayableObjects(game, Zone.ALL));
} }
processControlledPlayers(player, gameView); processControlledPlayers(game, player, gameView);
processWatchedHands(userId, gameView); processWatchedHands(game, userId, gameView);
//TODO: should player who controls another player's turn be able to look at all these cards? //TODO: should player who controls another player's turn be able to look at all these cards?
List<LookedAtView> list = new ArrayList<>(); List<LookedAtView> list = new ArrayList<>();
@ -215,7 +227,7 @@ public class GameSessionPlayer extends GameSessionWatcher {
return gameView; return gameView;
} }
private void processControlledPlayers(Player player, GameView gameView) { private static void processControlledPlayers(Game game, Player player, GameView gameView) {
if (!player.getPlayersUnderYourControl().isEmpty()) { if (!player.getPlayersUnderYourControl().isEmpty()) {
Map<String, SimpleCardsView> handCards = new HashMap<>(); Map<String, SimpleCardsView> handCards = new HashMap<>();
for (UUID controlledPlayerId : player.getPlayersUnderYourControl()) { for (UUID controlledPlayerId : player.getPlayersUnderYourControl()) {
@ -258,7 +270,7 @@ public class GameSessionPlayer extends GameSessionWatcher {
if (ex.getCause() != null) { if (ex.getCause() != null) {
logger.debug("- Cause: " + (ex.getCause().getMessage() == null ? "null" : ex.getCause().getMessage()), ex); logger.debug("- Cause: " + (ex.getCause().getMessage() == null ? "null" : ex.getCause().getMessage()), ex);
} else { } else {
logger.debug("- ex: " + ex.toString(), ex); logger.debug("- ex: " + ex, ex);
} }
} else { } else {
logger.fatal("Game session game quit exception - null gameId:" + game.getId() + " playerId: " + playerId); logger.fatal("Game session game quit exception - null gameId:" + game.getId() + " playerId: " + playerId);

View file

@ -99,11 +99,11 @@ public class GameSessionWatcher {
public GameView getGameView() { public GameView getGameView() {
GameView gameView = new GameView(game.getState(), game, null, userId); GameView gameView = new GameView(game.getState(), game, null, userId);
processWatchedHands(userId, gameView); processWatchedHands(game, userId, gameView);
return gameView; return gameView;
} }
protected void processWatchedHands(UUID userId, GameView gameView) { protected static void processWatchedHands(Game game, UUID userId, GameView gameView) {
Map<String, SimpleCardsView> handCards = new HashMap<>(); Map<String, SimpleCardsView> handCards = new HashMap<>();
for (Player player : game.getPlayers().values()) { for (Player player : game.getPlayers().values()) {
if (player.hasUserPermissionToSeeHand(userId)) { if (player.hasUserPermissionToSeeHand(userId)) {

View file

@ -23,6 +23,7 @@ import mage.game.GameCommanderImpl;
import mage.game.command.CommandObject; import mage.game.command.CommandObject;
import mage.game.command.Plane; import mage.game.command.Plane;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.game.permanent.token.Token;
import mage.players.Player; import mage.players.Player;
import mage.util.CardUtil; import mage.util.CardUtil;
import mage.util.RandomUtil; import mage.util.RandomUtil;
@ -267,8 +268,8 @@ public final class SystemUtil {
public static void addCardsForTesting(Game game, String fileSource, Player feedbackPlayer) { public static void addCardsForTesting(Game game, String fileSource, Player feedbackPlayer) {
// fake test ability for triggers and events // fake test ability for triggers and events
Ability fakeSourceAbility = new SimpleStaticAbility(Zone.OUTSIDE, new InfoEffect("adding testing cards")); Ability fakeSourceAbilityTemplate = new SimpleStaticAbility(Zone.OUTSIDE, new InfoEffect("adding testing cards"));
fakeSourceAbility.setControllerId(feedbackPlayer.getId()); fakeSourceAbilityTemplate.setControllerId(feedbackPlayer.getId());
try { try {
String fileName = fileSource; String fileName = fileSource;
@ -496,9 +497,12 @@ public final class SystemUtil {
// eg: token:Human:HippoToken:1 // eg: token:Human:HippoToken:1
Class<?> c = Class.forName("mage.game.permanent.token." + command.cardName); Class<?> c = Class.forName("mage.game.permanent.token." + command.cardName);
Constructor<?> cons = c.getConstructor(); Constructor<?> cons = c.getConstructor();
Object token = cons.newInstance(); Object obj = cons.newInstance();
if (token instanceof mage.game.permanent.token.Token) { if (obj instanceof Token) {
((mage.game.permanent.token.Token) token).putOntoBattlefield(command.Amount, game, fakeSourceAbility, player.getId(), false, false); Token token = (Token) obj;
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
fakeSourceAbility.setSourceId(token.getId());
token.putOntoBattlefield(command.Amount, game, fakeSourceAbility, player.getId(), false, false);
continue; continue;
} }
} else if ("emblem".equalsIgnoreCase(command.zone)) { } else if ("emblem".equalsIgnoreCase(command.zone)) {
@ -518,6 +522,8 @@ public final class SystemUtil {
} else if ("loyalty".equalsIgnoreCase(command.zone)) { } else if ("loyalty".equalsIgnoreCase(command.zone)) {
for (Permanent perm : game.getBattlefield().getAllActivePermanents(player.getId())) { for (Permanent perm : game.getBattlefield().getAllActivePermanents(player.getId())) {
if (perm.getName().equals(command.cardName) && perm.getCardType(game).contains(CardType.PLANESWALKER)) { if (perm.getName().equals(command.cardName) && perm.getCardType(game).contains(CardType.PLANESWALKER)) {
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
fakeSourceAbility.setSourceId(perm.getId());
perm.addCounters(CounterType.LOYALTY.createInstance(command.Amount), fakeSourceAbility.getControllerId(), fakeSourceAbility, game); perm.addCounters(CounterType.LOYALTY.createInstance(command.Amount), fakeSourceAbility.getControllerId(), fakeSourceAbility, game);
} }
} }
@ -541,6 +547,8 @@ public final class SystemUtil {
// move card from exile to stack // move card from exile to stack
for (Card card : cardsToLoad) { for (Card card : cardsToLoad) {
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
fakeSourceAbility.setSourceId(card.getId());
putCardToZone(fakeSourceAbility, game, player, card, Zone.STACK); putCardToZone(fakeSourceAbility, game, player, card, Zone.STACK);
} }
@ -616,6 +624,8 @@ public final class SystemUtil {
} else { } else {
// as other card // as other card
for (Card card : cardsToLoad) { for (Card card : cardsToLoad) {
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
fakeSourceAbility.setSourceId(card.getId());
putCardToZone(fakeSourceAbility, game, player, card, gameZone); putCardToZone(fakeSourceAbility, game, player, card, gameZone);
} }
} }

View file

@ -25,6 +25,7 @@ import mage.cards.repository.CardRepository;
import mage.constants.*; import mage.constants.*;
import mage.filter.StaticFilters; import mage.filter.StaticFilters;
import mage.game.Game; import mage.game.Game;
import mage.game.match.Match;
import mage.game.match.MatchType; import mage.game.match.MatchType;
import mage.game.permanent.PermanentCard; import mage.game.permanent.PermanentCard;
import mage.game.tournament.TournamentType; import mage.game.tournament.TournamentType;
@ -92,6 +93,8 @@ public abstract class MageTestPlayerBase {
*/ */
protected static Game currentGame = null; protected static Game currentGame = null;
protected static Match currentMatch = null;
/** /**
* Player thats starts the game first. By default, it is ComputerA. * Player thats starts the game first. By default, it is ComputerA.
*/ */

View file

@ -16,19 +16,19 @@ import mage.counters.CounterType;
import mage.filter.Filter; import mage.filter.Filter;
import mage.filter.FilterCard; import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.NamePredicate; import mage.filter.predicate.mageobject.NamePredicate;
import mage.game.ExileZone; import mage.game.*;
import mage.game.Game;
import mage.game.GameException;
import mage.game.GameOptions;
import mage.game.command.CommandObject; import mage.game.command.CommandObject;
import mage.game.match.MatchOptions;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentCard; import mage.game.permanent.PermanentCard;
import mage.player.ai.ComputerPlayer7; import mage.player.ai.ComputerPlayer7;
import mage.player.ai.ComputerPlayerMCTS; import mage.player.ai.ComputerPlayerMCTS;
import mage.players.ManaPool; import mage.players.ManaPool;
import mage.players.Player; import mage.players.Player;
import mage.server.game.GameSessionPlayer;
import mage.server.util.SystemUtil; import mage.server.util.SystemUtil;
import mage.util.CardUtil; import mage.util.CardUtil;
import mage.view.GameView;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.mage.test.player.PlayerAction; import org.mage.test.player.PlayerAction;
@ -216,6 +216,10 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
currentGame = null; currentGame = null;
} }
// prepare fake match (needs for testing some client-server code)
// always 4 seats
MatchOptions matchOptions = new MatchOptions("test match", "test game type", true, 4);
currentMatch = new FreeForAllMatch(matchOptions);
currentGame = createNewGameAndPlayers(); currentGame = createNewGameAndPlayers();
activePlayer = playerA; activePlayer = playerA;
@ -267,6 +271,7 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
game.loadCards(deck.getCards(), player.getId()); game.loadCards(deck.getCards(), player.getId());
game.loadCards(deck.getSideboard(), player.getId()); game.loadCards(deck.getSideboard(), player.getId());
game.addPlayer(player, deck); game.addPlayer(player, deck);
currentMatch.addPlayer(player, deck); // fake match
return player; return player;
} }
@ -2116,4 +2121,8 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
Assert.assertFalse(player.getName() + " has lost the game.", player.hasLost()); Assert.assertFalse(player.getName() + " has lost the game.", player.hasLost());
} }
public GameView getGameView(Player player) {
// prepare client-server data for tests
return GameSessionPlayer.prepareGameView(currentGame, player.getId(), null);
}
} }

View file

@ -7,13 +7,13 @@ import mage.cards.repository.CardRepository;
import mage.constants.PhaseStep; import mage.constants.PhaseStep;
import mage.constants.Zone; import mage.constants.Zone;
import mage.counters.CounterType; import mage.counters.CounterType;
import mage.game.Game;
import mage.game.mulligan.LondonMulligan; import mage.game.mulligan.LondonMulligan;
import mage.game.permanent.PermanentCard; import mage.game.permanent.PermanentCard;
import mage.game.permanent.PermanentImpl; import mage.game.permanent.PermanentImpl;
import mage.remote.traffic.ZippedObjectImpl; import mage.remote.traffic.ZippedObjectImpl;
import mage.util.CardUtil; import mage.util.CardUtil;
import mage.utils.CompressUtil; import mage.utils.CompressUtil;
import mage.view.GameView;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase; import org.mage.test.serverside.base.CardTestPlayerBase;
@ -76,9 +76,10 @@ public class SerializationTest extends CardTestPlayerBase {
setStopAt(1, PhaseStep.END_TURN); setStopAt(1, PhaseStep.END_TURN);
execute(); execute();
Object compressed = CompressUtil.compress(currentGame); GameView gameView = getGameView(playerA);
Object compressed = CompressUtil.compress(gameView);
Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl); Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl);
Game uncompressed = (Game) CompressUtil.decompress(compressed); GameView uncompressed = (GameView) CompressUtil.decompress(compressed);
Assert.assertEquals("Must be same", 1, uncompressed.getBattlefield().getAllActivePermanents().size()); Assert.assertEquals("Must be same", 1, uncompressed.getPlayers().get(0).getBattlefield().size());
} }
} }

View file

@ -3185,8 +3185,8 @@ public abstract class GameImpl implements Game, Serializable {
@Override @Override
public void cheat(UUID ownerId, List<Card> library, List<Card> hand, List<PermanentCard> battlefield, List<Card> graveyard, List<Card> command) { public void cheat(UUID ownerId, List<Card> library, List<Card> hand, List<PermanentCard> battlefield, List<Card> graveyard, List<Card> command) {
// fake test ability for triggers and events // fake test ability for triggers and events
Ability fakeSourceAbility = new SimpleStaticAbility(Zone.OUTSIDE, new InfoEffect("adding testing cards")); Ability fakeSourceAbilityTemplate = new SimpleStaticAbility(Zone.OUTSIDE, new InfoEffect("adding testing cards"));
fakeSourceAbility.setControllerId(ownerId); fakeSourceAbilityTemplate.setControllerId(ownerId);
Player player = getPlayer(ownerId); Player player = getPlayer(ownerId);
if (player != null) { if (player != null) {
@ -3221,6 +3221,8 @@ public abstract class GameImpl implements Game, Serializable {
} }
for (PermanentCard permanentCard : battlefield) { for (PermanentCard permanentCard : battlefield) {
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
fakeSourceAbility.setSourceId(permanentCard.getId());
CardUtil.putCardOntoBattlefieldWithEffects(fakeSourceAbility, this, permanentCard, player); CardUtil.putCardOntoBattlefieldWithEffects(fakeSourceAbility, this, permanentCard, player);
} }