mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 02:30:08 -08:00
Test framework: added support to test client side data in unit tests (getGameView -> CardView, etc);
This commit is contained in:
parent
26fea5d07b
commit
fc0ff6c22d
7 changed files with 59 additions and 22 deletions
|
|
@ -11,8 +11,8 @@ import mage.interfaces.callback.ClientCallback;
|
|||
import mage.interfaces.callback.ClientCallbackMethod;
|
||||
import mage.players.Player;
|
||||
import mage.server.User;
|
||||
import mage.server.managers.UserManager;
|
||||
import mage.server.managers.ManagerFactory;
|
||||
import mage.server.managers.UserManager;
|
||||
import mage.view.*;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
|
@ -195,6 +195,18 @@ public class GameSessionPlayer extends GameSessionWatcher {
|
|||
|
||||
@Override
|
||||
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);
|
||||
GameView gameView = new GameView(game.getState(), game, playerId, null);
|
||||
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));
|
||||
}
|
||||
|
||||
processControlledPlayers(player, gameView);
|
||||
processWatchedHands(userId, gameView);
|
||||
processControlledPlayers(game, player, gameView);
|
||||
processWatchedHands(game, userId, gameView);
|
||||
//TODO: should player who controls another player's turn be able to look at all these cards?
|
||||
|
||||
List<LookedAtView> list = new ArrayList<>();
|
||||
|
|
@ -215,7 +227,7 @@ public class GameSessionPlayer extends GameSessionWatcher {
|
|||
return gameView;
|
||||
}
|
||||
|
||||
private void processControlledPlayers(Player player, GameView gameView) {
|
||||
private static void processControlledPlayers(Game game, Player player, GameView gameView) {
|
||||
if (!player.getPlayersUnderYourControl().isEmpty()) {
|
||||
Map<String, SimpleCardsView> handCards = new HashMap<>();
|
||||
for (UUID controlledPlayerId : player.getPlayersUnderYourControl()) {
|
||||
|
|
@ -258,7 +270,7 @@ public class GameSessionPlayer extends GameSessionWatcher {
|
|||
if (ex.getCause() != null) {
|
||||
logger.debug("- Cause: " + (ex.getCause().getMessage() == null ? "null" : ex.getCause().getMessage()), ex);
|
||||
} else {
|
||||
logger.debug("- ex: " + ex.toString(), ex);
|
||||
logger.debug("- ex: " + ex, ex);
|
||||
}
|
||||
} else {
|
||||
logger.fatal("Game session game quit exception - null gameId:" + game.getId() + " playerId: " + playerId);
|
||||
|
|
|
|||
|
|
@ -99,11 +99,11 @@ public class GameSessionWatcher {
|
|||
|
||||
public GameView getGameView() {
|
||||
GameView gameView = new GameView(game.getState(), game, null, userId);
|
||||
processWatchedHands(userId, gameView);
|
||||
processWatchedHands(game, userId, 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<>();
|
||||
for (Player player : game.getPlayers().values()) {
|
||||
if (player.hasUserPermissionToSeeHand(userId)) {
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import mage.game.GameCommanderImpl;
|
|||
import mage.game.command.CommandObject;
|
||||
import mage.game.command.Plane;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.permanent.token.Token;
|
||||
import mage.players.Player;
|
||||
import mage.util.CardUtil;
|
||||
import mage.util.RandomUtil;
|
||||
|
|
@ -267,8 +268,8 @@ public final class SystemUtil {
|
|||
public static void addCardsForTesting(Game game, String fileSource, Player feedbackPlayer) {
|
||||
|
||||
// fake test ability for triggers and events
|
||||
Ability fakeSourceAbility = new SimpleStaticAbility(Zone.OUTSIDE, new InfoEffect("adding testing cards"));
|
||||
fakeSourceAbility.setControllerId(feedbackPlayer.getId());
|
||||
Ability fakeSourceAbilityTemplate = new SimpleStaticAbility(Zone.OUTSIDE, new InfoEffect("adding testing cards"));
|
||||
fakeSourceAbilityTemplate.setControllerId(feedbackPlayer.getId());
|
||||
|
||||
try {
|
||||
String fileName = fileSource;
|
||||
|
|
@ -496,9 +497,12 @@ public final class SystemUtil {
|
|||
// eg: token:Human:HippoToken:1
|
||||
Class<?> c = Class.forName("mage.game.permanent.token." + command.cardName);
|
||||
Constructor<?> cons = c.getConstructor();
|
||||
Object token = cons.newInstance();
|
||||
if (token instanceof mage.game.permanent.token.Token) {
|
||||
((mage.game.permanent.token.Token) token).putOntoBattlefield(command.Amount, game, fakeSourceAbility, player.getId(), false, false);
|
||||
Object obj = cons.newInstance();
|
||||
if (obj instanceof Token) {
|
||||
Token token = (Token) obj;
|
||||
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
|
||||
fakeSourceAbility.setSourceId(token.getId());
|
||||
token.putOntoBattlefield(command.Amount, game, fakeSourceAbility, player.getId(), false, false);
|
||||
continue;
|
||||
}
|
||||
} else if ("emblem".equalsIgnoreCase(command.zone)) {
|
||||
|
|
@ -518,6 +522,8 @@ public final class SystemUtil {
|
|||
} else if ("loyalty".equalsIgnoreCase(command.zone)) {
|
||||
for (Permanent perm : game.getBattlefield().getAllActivePermanents(player.getId())) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -541,6 +547,8 @@ public final class SystemUtil {
|
|||
|
||||
// move card from exile to stack
|
||||
for (Card card : cardsToLoad) {
|
||||
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
|
||||
fakeSourceAbility.setSourceId(card.getId());
|
||||
putCardToZone(fakeSourceAbility, game, player, card, Zone.STACK);
|
||||
}
|
||||
|
||||
|
|
@ -616,6 +624,8 @@ public final class SystemUtil {
|
|||
} else {
|
||||
// as other card
|
||||
for (Card card : cardsToLoad) {
|
||||
Ability fakeSourceAbility = fakeSourceAbilityTemplate.copy();
|
||||
fakeSourceAbility.setSourceId(card.getId());
|
||||
putCardToZone(fakeSourceAbility, game, player, card, gameZone);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue