mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 04:52:07 -08:00
Merge branch 'master' of https://github.com/magefree/mage.git
This commit is contained in:
commit
ec546a5ae6
7 changed files with 92 additions and 43 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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<UUID, TableController> controllers = new ConcurrentHashMap<UUID, TableController>();
|
||||
private ConcurrentHashMap<UUID, Table> tables = new ConcurrentHashMap<UUID, Table>();
|
||||
|
||||
/**
|
||||
* 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<UUID> toRemove = new ArrayList<UUID>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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") + "|");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue