Code clean

This commit is contained in:
Oleg Agafonov 2023-05-08 22:59:30 +04:00
parent 1dea98cf1f
commit 6882a9a49e
6 changed files with 56 additions and 33 deletions

View file

@ -24,6 +24,7 @@ import mage.game.command.Plane;
import mage.game.draft.RateCard;
import mage.game.permanent.PermanentToken;
import mage.game.permanent.token.Token;
import mage.game.permanent.token.TokenImpl;
import mage.view.*;
import org.apache.log4j.Logger;
import org.mage.card.arcane.ManaSymbols;
@ -236,17 +237,11 @@ public class MageBook extends JComponent {
.filter(token -> token.getSetCode().equals(currentSet))
.collect(Collectors.toList());
allTokens.forEach(token -> {
try {
Class<?> c = Class.forName(token.getFullClassFileName());
Constructor<?> cons = c.getConstructor();
Object newToken = cons.newInstance();
if (newToken instanceof Token) {
((Token) newToken).setExpansionSetCode(currentSet);
((Token) newToken).setImageNumber(token.getImageNumber());
res.add(newToken);
}
} catch (Exception e) {
// ignore error
TokenImpl newToken = TokenImpl.createTokenByClassName(token.getFullClassFileName());
if (newToken != null) {
newToken.setExpansionSetCode(currentSet);
newToken.setImageNumber(token.getImageNumber());
res.add(newToken);
}
});

View file

@ -8,6 +8,7 @@ import mage.abilities.effects.common.CreateTokenEffect;
import mage.cards.Card;
import mage.cards.repository.TokenRepository;
import mage.constants.PhaseStep;
import mage.constants.SubType;
import mage.constants.Zone;
import mage.game.permanent.PermanentToken;
import mage.game.permanent.token.HumanToken;
@ -21,7 +22,6 @@ import mage.view.GameView;
import mage.view.PermanentView;
import mage.view.PlayerView;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
@ -553,15 +553,13 @@ public class TokenImagesTest extends CardTestPlayerBase {
}
@Test
@Ignore
// TODO: implement auto-generate creature token images from public tokens (by name, type, color, PT, abilities)
public void test_CreatureToken_MustGetDefaultImage() {
Ability ability = new SimpleActivatedAbility(
Zone.ALL,
new CreateTokenEffect(new CreatureToken(2, 2), 10),
new CreateTokenEffect(new CreatureToken(2, 2, "", SubType.HUMAN), 10),
new ManaCostsImpl<>("")
);
addCustomCardWithAbility("test", playerA, ability);
addCustomCardWithAbility("40K-test", playerA, ability);
activateAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "create ten");
waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
@ -573,7 +571,7 @@ public class TokenImagesTest extends CardTestPlayerBase {
assertPermanentCount(playerA, 1 + 10); // 1 test card + 10 tokens
assert_Inner("test", 0, 0, 1,
"", 10, false, "XXX=10");
"Human Token", 10, false, "XMAGE=10");
}
@Test

View file

@ -280,27 +280,37 @@ public enum TokenRepository {
/**
* Try to find random image info by related set code
*
* @param className full class name of the token or other object
* @param setCode primary set code for possible image
* @param possibleList all possible tokens e.g. by clas name
* @param preferredSetCode primary set code for possible image (if not found then will use any set)
* @return
*/
public TokenInfo generateTokenInfoBySetCode(String className, String setCode) {
private TokenInfo findPreferredTokenInfo(List<TokenInfo> possibleList, String preferredSetCode) {
// search by set code
List<TokenInfo> possibleInfo = TokenRepository.instance.getByClassName(className)
.stream()
.filter(info -> info.getSetCode().equals(setCode))
List<TokenInfo> needList = possibleList.stream()
.filter(info -> info.getSetCode().equals(preferredSetCode))
.collect(Collectors.toList());
// search by random set
if (possibleInfo.isEmpty()) {
possibleInfo = new ArrayList<>(TokenRepository.instance.getByClassName(className));
// search by all sets
if (needList.isEmpty()) {
needList = possibleList;
}
// also weill return diff image number for tokens
if (possibleInfo.size() > 0) {
return RandomUtil.randomFromCollection(possibleInfo);
// also will return diff image number for tokens
if (needList.size() > 0) {
return RandomUtil.randomFromCollection(needList);
} else {
return null;
}
}
/**
* Try to find random image info by related set code
*
* @param className full class name of the token or other object
* @param preferredSetCode primary set code for possible image (if not found then will use any set)
* @return
*/
public TokenInfo findPreferredTokenInfoForClass(String className, String preferredSetCode) {
return findPreferredTokenInfo(TokenRepository.instance.getByClassName(className), preferredSetCode);
}
}

View file

@ -66,7 +66,7 @@ public abstract class Emblem extends CommandObjectImpl {
this.sourceObject = sourceObject;
// choose set code due source
TokenInfo foundInfo = TokenRepository.instance.generateTokenInfoBySetCode(this.getClass().getName(), sourceObject.getExpansionSetCode());
TokenInfo foundInfo = TokenRepository.instance.findPreferredTokenInfoForClass(this.getClass().getName(), sourceObject.getExpansionSetCode());
if (foundInfo != null) {
this.setExpansionSetCode(foundInfo.getSetCode());
this.setCardNumber("");

View file

@ -71,7 +71,7 @@ public abstract class Plane extends CommandObjectImpl {
this.sourceObject = null;
// choose set code due source
TokenInfo foundInfo = TokenRepository.instance.generateTokenInfoBySetCode(this.getClass().getName(), null);
TokenInfo foundInfo = TokenRepository.instance.findPreferredTokenInfoForClass(this.getClass().getName(), null);
if (foundInfo != null) {
this.setExpansionSetCode(foundInfo.getSetCode());
this.setCardNumber("");

View file

@ -21,9 +21,11 @@ import mage.game.events.CreatedTokensEvent;
import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentToken;
import mage.game.permanent.token.custom.CreatureToken;
import mage.players.Player;
import mage.target.Target;
import java.lang.reflect.Constructor;
import java.util.*;
/**
@ -175,12 +177,16 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
}
// search by set code
TokenInfo foundInfo = TokenRepository.instance.generateTokenInfoBySetCode(token.getClass().getName(), setCode);
TokenInfo foundInfo = TokenRepository.instance.findPreferredTokenInfoForClass(token.getClass().getName(), setCode);
if (foundInfo != null) {
return foundInfo;
}
// TODO: implement auto-generate images for CreatureToken (search public tokens for same characteristics)
// auto-image for creature token (it's a private token without official image, so try to find same paper image)
if (token instanceof CreatureToken) {
// TODO: return default creature token image
}
// TODO: implement Copy image
// TODO: implement Manifest image
// TODO: implement Morph image
@ -605,4 +611,18 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
public boolean isEntersTransformed() {
return this.entersTransformed && this.backFace != null;
}
public static TokenImpl createTokenByClassName(String fullClassName) {
try {
Class<?> c = Class.forName(fullClassName);
Constructor<?> cons = c.getConstructor();
Object newToken = cons.newInstance();
if (newToken instanceof Token) {
return (TokenImpl) newToken;
}
} catch (Exception e) {
// ignore error
}
return null;
}
}