diff --git a/Mage.Common/src/mage/view/CardView.java b/Mage.Common/src/mage/view/CardView.java index 06d5864af2..df7fd22d01 100644 --- a/Mage.Common/src/mage/view/CardView.java +++ b/Mage.Common/src/mage/view/CardView.java @@ -35,6 +35,7 @@ import mage.MageObject; import mage.ObjectColor; import mage.abilities.Mode; import mage.cards.Card; +import mage.counters.Counter; import mage.counters.CounterType; import mage.game.permanent.Permanent; import mage.game.permanent.PermanentToken; @@ -47,8 +48,6 @@ import mage.target.Targets; import java.util.ArrayList; import java.util.List; import java.util.UUID; -import mage.counters.Counter; -import mage.counters.Counters; /** * @author BetaSteward_at_googlemail.com @@ -100,10 +99,11 @@ public class CardView extends SimpleCardView { this.name = card.getName(); this.rules = card.getRules(); if (card instanceof Permanent) { + Permanent permanent = (Permanent)card; this.power = Integer.toString(card.getPower().getValue()); this.toughness = Integer.toString(card.getToughness().getValue()); - this.loyalty = Integer.toString(((Permanent) card).getCounters().getCount(CounterType.LOYALTY)); - this.pairedCard = ((Permanent)card).getPairedCard(); + this.loyalty = Integer.toString(permanent.getCounters().getCount(CounterType.LOYALTY)); + this.pairedCard = permanent.getPairedCard(); } else { this.power = card.getPower().toString(); this.toughness = card.getToughness().toString(); @@ -169,21 +169,13 @@ public class CardView extends SimpleCardView { this.color = card.getColor(); this.manaCost = card.getManaCost().getSymbols(); this.convertedManaCost = card.getManaCost().convertedManaCost(); -// if (card instanceof Card) { -// Counters cardCounters = ((Card) card).getCounters(); -// if (cardCounters != null && !cardCounters.isEmpty()) { -// counters = new ArrayList(); -// for (Counter counter: cardCounters.values()) { -// counters.add(new CounterView(counter)); -// } -// } -// } if (card instanceof PermanentToken) { + PermanentToken permanentToken = (PermanentToken) card; this.rarity = Rarity.COMMON; - this.expansionSetCode = ((PermanentToken) card).getExpansionSetCode(); - this.rules = ((PermanentToken) card).getRules(); - this.type = ((PermanentToken)card).getToken().getTokenType(); + this.expansionSetCode = permanentToken.getExpansionSetCode(); + this.rules = permanentToken.getRules(); + this.type = permanentToken.getToken().getTokenType(); } if (this.rarity == null && card instanceof StackAbility) { StackAbility stackAbility = (StackAbility)card; @@ -245,34 +237,21 @@ public class CardView extends SimpleCardView { this.manaCost = token.getManaCost().getSymbols(); this.rarity = Rarity.NA; this.type = token.getTokenType(); - //this.expansionSetCode = ""; } - protected void setTargets(Targets targets) { + protected final void setTargets(Targets targets) { for (Target target : targets) { if (target.isChosen()) { for (UUID targetUUID : target.getTargets()) { - if (this.targets == null) this.targets = new ArrayList(); + if (this.targets == null) { + this.targets = new ArrayList(); + } this.targets.add(targetUUID); } } } } -// protected List formatRules(List rules) { -// List newRules = new ArrayList(); -// for (String rule: rules) { -// newRules.add(formatRule(rule)); -// } -// return newRules; -// } -// -// protected String formatRule(String rule) { -// String replace = rule.replace("{this}", this.name); -// replace = replace.replace("{source}", this.name); -// return replace; -// } - public String getName() { return name; } diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index c911a1dd94..77dd7f193b 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -40,6 +40,7 @@ import mage.server.tournament.TournamentFactory; import mage.server.util.ConfigSettings; import mage.server.util.PluginClassLoader; import mage.server.util.ServerMessagesUtil; +import mage.server.util.SystemUtil; import mage.server.util.config.GamePlugin; import mage.server.util.config.Plugin; import mage.utils.MageVersion; @@ -106,6 +107,7 @@ public class Main { } else if (arg.startsWith(adminPasswordArg)) { adminPassword = arg.replace(adminPasswordArg, ""); + adminPassword = SystemUtil.sanitize(adminPassword); } } Connection connection = new Connection(); diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index a2a58ab8f2..167334946f 100644 --- a/Mage.Server/src/main/java/mage/server/TableManager.java +++ b/Mage.Server/src/main/java/mage/server/TableManager.java @@ -28,9 +28,6 @@ package mage.server; -import java.util.Collection; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; import mage.MageException; import mage.cards.decks.Deck; import mage.cards.decks.DeckCardLists; @@ -43,6 +40,13 @@ import mage.game.tournament.Tournament; import mage.game.tournament.TournamentOptions; import mage.players.Player; import mage.server.game.GamesRoomManager; +import org.apache.log4j.Logger; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * @@ -50,16 +54,48 @@ import mage.server.game.GamesRoomManager; */ public class TableManager { + protected static ScheduledExecutorService expireExecutor = Executors.newSingleThreadScheduledExecutor(); + private final static TableManager INSTANCE = new TableManager(); - //private final static Logger logger = Logger.getLogger(TableManager.class); + private final static Logger logger = Logger.getLogger(TableManager.class); private ConcurrentHashMap controllers = new ConcurrentHashMap(); private ConcurrentHashMap tables = new ConcurrentHashMap(); + /** + * Defines how often checking process should be run on server. + * + * In minutes. + */ + private static final int EXPIRE_CHECK_PERIOD = 10; + + /** + * This parameters defines when table can be counted as expired. + * Uses EXPIRE_TIME_UNIT_VALUE as unit of measurement. + * + * The time pass is calculated as (table_created_at - now) / EXPIRE_TIME_UNIT_VALUE. + * Then this values is compared to EXPIRE_TIME. + */ + private static final int EXPIRE_TIME = 3; + + /** + * Defines unit of measurement for expiration time of tables created. + */ + private static final int EXPIRE_TIME_UNIT_VALUE = 1000 * 60 * 60; // 1 hour + public static TableManager getInstance() { return INSTANCE; } + private TableManager() { + expireExecutor.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + checkExpired(); + } + }, EXPIRE_CHECK_PERIOD, EXPIRE_CHECK_PERIOD, TimeUnit.MINUTES); + } + public Table createTable(UUID roomId, UUID userId, MatchOptions options) { TableController tableController = new TableController(roomId, userId, options); controllers.put(tableController.getTable().getId(), tableController); @@ -232,4 +268,24 @@ public class TableManager { } } + private void checkExpired() { + logger.info("Table expire checking..."); + + Date now = new Date(); + List toRemove = new ArrayList(); + for (Table table : tables.values()) { + long diff = (now.getTime() - table.getCreateTime().getTime()) / EXPIRE_TIME_UNIT_VALUE; + if (diff >= EXPIRE_TIME) { + logger.info("Table expired: id = " + table.getId() + ", created_by=" + table.getControllerName() + ". Removing..."); + toRemove.add(table.getId()); + } + } + for (UUID tableId : toRemove) { + try { + removeTable(tableId); + } catch (Exception e) { + logger.error(e); + } + } + } } diff --git a/Mage.Server/src/main/java/mage/server/util/SystemUtil.java b/Mage.Server/src/main/java/mage/server/util/SystemUtil.java index 43dbcac95f..e2d6d55a66 100644 --- a/Mage.Server/src/main/java/mage/server/util/SystemUtil.java +++ b/Mage.Server/src/main/java/mage/server/util/SystemUtil.java @@ -148,4 +148,20 @@ public class SystemUtil { } return null; } + + public static String sanitize(String input) { + //Pattern pattern = Pattern.compile("[^0-9a-zA-Z]"); + //Matcher matcher = pattern.matcher(input); + //return matcher.replaceAll(""); + return input.replaceAll("[^a-zA-Z0-9]", ""); + } + + public static void main(String... args) { + System.out.println(sanitize("123")); + System.out.println(sanitize("AaAaD_123")); + System.out.println(sanitize("--sas-")); + System.out.println(sanitize("anPlsdf123_") + "|"); + System.out.println(sanitize("anPlsdf123 ") + "|"); + System.out.println(sanitize("anPlsdf123\r\n") + "|"); + } } diff --git a/Mage.Sets/src/mage/sets/darkascension/BloodFeud.java b/Mage.Sets/src/mage/sets/darkascension/BloodFeud.java index 1d56a2d910..810a2ca6be 100644 --- a/Mage.Sets/src/mage/sets/darkascension/BloodFeud.java +++ b/Mage.Sets/src/mage/sets/darkascension/BloodFeud.java @@ -33,7 +33,6 @@ import mage.Constants.Rarity; import mage.abilities.Ability; import mage.abilities.effects.common.FightTargetsEffect; import mage.cards.CardImpl; -import mage.filter.common.FilterCreaturePermanent; import mage.game.Game; import mage.target.common.TargetCreaturePermanent; @@ -52,7 +51,7 @@ public class BloodFeud extends CardImpl { // Target creature fights another target creature. this.getSpellAbility().addEffect(new FightTargetsEffect()); this.getSpellAbility().addTarget(new TargetCreaturePermanent(true)); - this.getSpellAbility().addTarget(new TargetOtherCreaturePermanent(true)); + this.getSpellAbility().addTarget(new TargetOtherCreaturePermanent()); } public BloodFeud(final BloodFeud card) { @@ -67,7 +66,7 @@ public class BloodFeud extends CardImpl { class TargetOtherCreaturePermanent extends TargetCreaturePermanent { - public TargetOtherCreaturePermanent(boolean required) { + public TargetOtherCreaturePermanent() { super(true); } diff --git a/Mage.Sets/src/mage/sets/iceage/Lhurgoyf.java b/Mage.Sets/src/mage/sets/iceage/Lhurgoyf.java index 62096ddc47..3ce4d6e583 100644 --- a/Mage.Sets/src/mage/sets/iceage/Lhurgoyf.java +++ b/Mage.Sets/src/mage/sets/iceage/Lhurgoyf.java @@ -36,8 +36,6 @@ import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.effects.ContinuousEffectImpl; import mage.cards.CardImpl; -import mage.cards.Cards; -import mage.cards.CardsImpl; import mage.filter.common.FilterCreatureCard; import mage.game.Game; import mage.game.permanent.Permanent; diff --git a/Mage.Sets/src/mage/sets/lorwyn/CrypticCommand.java b/Mage.Sets/src/mage/sets/lorwyn/CrypticCommand.java index f30384d236..15b1de21b3 100644 --- a/Mage.Sets/src/mage/sets/lorwyn/CrypticCommand.java +++ b/Mage.Sets/src/mage/sets/lorwyn/CrypticCommand.java @@ -27,7 +27,6 @@ */ package mage.sets.lorwyn; -import java.util.List; import java.util.UUID; import mage.Constants; import mage.Constants.CardType;