From ef6d5e826ee8f9ec8449603725902d3bb1bdbd30 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Sun, 10 Mar 2013 15:26:01 +0400 Subject: [PATCH 1/5] Fixed admin pswd not always parsed correctly on server --- Mage.Server/src/main/java/mage/server/Main.java | 2 ++ .../main/java/mage/server/util/SystemUtil.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index af6d9f4e2a..7338b27c9e 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/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") + "|"); + } } From c1375f7b76d0b4c8d2db6d985ca71123bf876881 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Sun, 10 Mar 2013 15:32:06 +0400 Subject: [PATCH 2/5] Job for removing expired tables --- .../main/java/mage/server/TableManager.java | 58 +++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index a2a58ab8f2..bde1f5ca03 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,41 @@ 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(); + /** + * 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(); + } + }, 1, 1, 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 +261,25 @@ 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; + logger.info("Expire = " + diff); + 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); + } + } + } } From a6decdbd14b6e59a797457395959c49308d48b01 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Sun, 10 Mar 2013 15:33:14 +0400 Subject: [PATCH 3/5] Extending running schedule for the job --- Mage.Server/src/main/java/mage/server/TableManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index bde1f5ca03..3a533eeb5d 100644 --- a/Mage.Server/src/main/java/mage/server/TableManager.java +++ b/Mage.Server/src/main/java/mage/server/TableManager.java @@ -86,7 +86,7 @@ public class TableManager { public void run() { checkExpired(); } - }, 1, 1, TimeUnit.MINUTES); + }, 10, 10, TimeUnit.MINUTES); } public Table createTable(UUID roomId, UUID userId, MatchOptions options) { From a3be8073e242db6bca4a94c2dccc55fa5a44a541 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Sun, 10 Mar 2013 15:57:30 +0400 Subject: [PATCH 4/5] Moved job schedule time value to constant --- .../src/main/java/mage/server/TableManager.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/TableManager.java b/Mage.Server/src/main/java/mage/server/TableManager.java index 3a533eeb5d..167334946f 100644 --- a/Mage.Server/src/main/java/mage/server/TableManager.java +++ b/Mage.Server/src/main/java/mage/server/TableManager.java @@ -62,6 +62,13 @@ public class TableManager { 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. @@ -86,7 +93,7 @@ public class TableManager { public void run() { checkExpired(); } - }, 10, 10, TimeUnit.MINUTES); + }, EXPIRE_CHECK_PERIOD, EXPIRE_CHECK_PERIOD, TimeUnit.MINUTES); } public Table createTable(UUID roomId, UUID userId, MatchOptions options) { @@ -268,7 +275,6 @@ public class TableManager { List toRemove = new ArrayList(); for (Table table : tables.values()) { long diff = (now.getTime() - table.getCreateTime().getTime()) / EXPIRE_TIME_UNIT_VALUE; - logger.info("Expire = " + diff); if (diff >= EXPIRE_TIME) { logger.info("Table expired: id = " + table.getId() + ", created_by=" + table.getControllerName() + ". Removing..."); toRemove.add(table.getId()); From 2b7471df3f51d3994c3dbd2c8222463083c3e3ea Mon Sep 17 00:00:00 2001 From: North Date: Sun, 10 Mar 2013 14:17:55 +0200 Subject: [PATCH 5/5] [minor] removed unused imports --- Mage.Common/src/mage/view/CardView.java | 45 +++++-------------- .../mage/sets/darkascension/BloodFeud.java | 5 +-- Mage.Sets/src/mage/sets/iceage/Lhurgoyf.java | 2 - .../src/mage/sets/lorwyn/CrypticCommand.java | 1 - 4 files changed, 14 insertions(+), 39 deletions(-) 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.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;