[refactoring][minor] Replaced all tabs with four spaces.

This commit is contained in:
North 2012-06-19 23:50:20 +03:00
parent e646e4768d
commit 239a4fb100
2891 changed files with 79411 additions and 79411 deletions

View file

@ -38,24 +38,24 @@ import org.apache.log4j.Logger;
*/
public class Config {
private final static Logger logger = Logger.getLogger(Config.class);
private final static Logger logger = Logger.getLogger(Config.class);
static {
Properties p = new Properties();
try {
p.load(Config.class.getResourceAsStream("resources/config.properties"));
} catch (IOException ex) {
logger.fatal("Config error", ex);
}
port = Integer.parseInt(p.getProperty("port"));
remoteServer = p.getProperty("remote-server");
maxGameThreads = Integer.parseInt(p.getProperty("max-game-threads"));
maxSecondsIdle = Integer.parseInt(p.getProperty("max-seconds-idle"));
}
static {
Properties p = new Properties();
try {
p.load(Config.class.getResourceAsStream("resources/config.properties"));
} catch (IOException ex) {
logger.fatal("Config error", ex);
}
port = Integer.parseInt(p.getProperty("port"));
remoteServer = p.getProperty("remote-server");
maxGameThreads = Integer.parseInt(p.getProperty("max-game-threads"));
maxSecondsIdle = Integer.parseInt(p.getProperty("max-seconds-idle"));
}
public static final String remoteServer;
public static final int port;
public static final int maxGameThreads;
public static final int maxSecondsIdle;
public static final String remoteServer;
public static final int port;
public static final int maxGameThreads;
public static final int maxSecondsIdle;
}

View file

@ -44,59 +44,59 @@ import org.apache.log4j.Logger;
*/
public class ConfigSettings {
private final static Logger logger = Logger.getLogger(ConfigSettings.class);
private final static ConfigSettings INSTANCE = new ConfigSettings();
private Config config;
private final static Logger logger = Logger.getLogger(ConfigSettings.class);
private final static ConfigSettings INSTANCE = new ConfigSettings();
public static ConfigSettings getInstance() {
return INSTANCE;
}
private Config config;
private ConfigSettings() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance("mage.server.util.config");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
config = (Config) unmarshaller.unmarshal(new File("config/config.xml"));
} catch (JAXBException ex) {
logger.fatal("ConfigSettings error", ex);
}
}
public static ConfigSettings getInstance() {
return INSTANCE;
}
public String getServerAddress() {
return config.getServer().getServerAddress();
}
private ConfigSettings() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance("mage.server.util.config");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
config = (Config) unmarshaller.unmarshal(new File("config/config.xml"));
} catch (JAXBException ex) {
logger.fatal("ConfigSettings error", ex);
}
}
public String getServerName() {
return config.getServer().getServerName();
}
public String getServerAddress() {
return config.getServer().getServerAddress();
}
public int getPort() {
return config.getServer().getPort().intValue();
}
public String getServerName() {
return config.getServer().getServerName();
}
public int getMaxGameThreads() {
return config.getServer().getMaxGameThreads().intValue();
}
public int getPort() {
return config.getServer().getPort().intValue();
}
public int getMaxSecondsIdle() {
return config.getServer().getMaxSecondsIdle().intValue();
}
public int getMaxGameThreads() {
return config.getServer().getMaxGameThreads().intValue();
}
public List<Plugin> getPlayerTypes() {
return config.getPlayerTypes().getPlayerType();
}
public int getMaxSecondsIdle() {
return config.getServer().getMaxSecondsIdle().intValue();
}
public List<GamePlugin> getGameTypes() {
return config.getGameTypes().getGameType();
}
public List<Plugin> getPlayerTypes() {
return config.getPlayerTypes().getPlayerType();
}
public List<GamePlugin> getTournamentTypes() {
return config.getTournamentTypes().getTournamentType();
}
public List<GamePlugin> getGameTypes() {
return config.getGameTypes().getGameType();
}
public List<Plugin> getDeckTypes() {
return config.getDeckTypes().getDeckType();
}
public List<GamePlugin> getTournamentTypes() {
return config.getTournamentTypes().getTournamentType();
}
public List<Plugin> getDeckTypes() {
return config.getDeckTypes().getDeckType();
}
}

View file

@ -40,37 +40,37 @@ import java.net.URLClassLoader;
*/
public class PluginClassLoader extends URLClassLoader {
public PluginClassLoader(){
super(new URL[0], PluginClassLoader.class.getClassLoader());
}
public PluginClassLoader(){
super(new URL[0], PluginClassLoader.class.getClassLoader());
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
// First check whether it's already been loaded, if so use it
Class loadedClass = findLoadedClass(name);
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
// First check whether it's already been loaded, if so use it
Class loadedClass = findLoadedClass(name);
// Not loaded, try to load it
if (loadedClass == null) {
try {
// Ignore parent delegation and just try to load locally
loadedClass = findClass(name);
} catch (ClassNotFoundException e) {
// Swallow exception - does not exist locally
}
// Not loaded, try to load it
if (loadedClass == null) {
try {
// Ignore parent delegation and just try to load locally
loadedClass = findClass(name);
} catch (ClassNotFoundException e) {
// Swallow exception - does not exist locally
}
// If not found locally, use normal parent delegation in URLClassloader
if (loadedClass == null) {
// throws ClassNotFoundException if not found in delegation hierarchy at all
loadedClass = super.loadClass(name);
}
}
// will never return null (ClassNotFoundException will be thrown)
return loadedClass;
// If not found locally, use normal parent delegation in URLClassloader
if (loadedClass == null) {
// throws ClassNotFoundException if not found in delegation hierarchy at all
loadedClass = super.loadClass(name);
}
}
// will never return null (ClassNotFoundException will be thrown)
return loadedClass;
}

View file

@ -54,7 +54,7 @@ public class ServerMessagesUtil {
private static final Logger log = Logger.getLogger(ServerMessagesUtil.class);
private static final String SERVER_MSG_TXT_FILE = "server.msg.txt";
private static ScheduledExecutorService updateExecutor;
private static ScheduledExecutorService updateExecutor;
private List<String> messages = new ArrayList<String>();
private ReadWriteLock lock = new ReentrantReadWriteLock();
@ -72,12 +72,12 @@ public class ServerMessagesUtil {
public ServerMessagesUtil() {
updateExecutor = Executors.newSingleThreadScheduledExecutor();
updateExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
reloadMessages();
}
}, 5, 5 * 60, TimeUnit.SECONDS);
updateExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
reloadMessages();
}
}, 5, 5 * 60, TimeUnit.SECONDS);
}
public static ServerMessagesUtil getInstance() {

View file

@ -12,13 +12,13 @@ import java.util.UUID;
*/
public class Splitter {
public static List<UUID> split(Game game, UUID playerId) {
List<UUID> players = new ArrayList<UUID>();
//players.add(playerId); // add original player
Player player = game.getPlayer(playerId);
if (player != null && player.getTurnControlledBy() != null) {
players.add(player.getTurnControlledBy());
}
return players;
}
public static List<UUID> split(Game game, UUID playerId) {
List<UUID> players = new ArrayList<UUID>();
//players.add(playerId); // add original player
Player player = game.getPlayer(playerId);
if (player != null && player.getTurnControlledBy() != null) {
players.add(player.getTurnControlledBy());
}
return players;
}
}

View file

@ -18,117 +18,117 @@ import java.util.regex.Pattern;
*/
public class SystemUtil {
private static final String INIT_FILE_PATH = "config" + File.separator + "init.txt";
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SystemUtil.class);
private static final String INIT_FILE_PATH = "config" + File.separator + "init.txt";
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SystemUtil.class);
/**
* Replaces cards in player's hands by specified in config/init.txt.<br/>
* <br/>
* <b>Implementation note:</b><br/>
* 1. Read init.txt line by line<br/>
* 2. Parse line using the following format: line ::= <zone>:<nickname>:<card name>:<amount><br/>
* 3. If zone equals to 'hand', add card to player's library<br/>
* 3a. Then swap added card with any card in player's hand<br/>
* 3b. Parse next line (go to 2.), If EOF go to 4.<br/>
* 4. Log message to all players that cards were added (to prevent unfair play).<br/>
* 5. Exit<br/>
*/
public static void addCardsForTesting(Game game) {
try {
File f = new File(INIT_FILE_PATH);
Pattern pattern = Pattern.compile("([a-zA-Z]*):([\\w]*):([a-zA-Z ,\\-.!'\\d]*):([\\d]*)");
if (!f.exists()) {
logger.warn("Couldn't find init file: " + INIT_FILE_PATH);
return;
}
/**
* Replaces cards in player's hands by specified in config/init.txt.<br/>
* <br/>
* <b>Implementation note:</b><br/>
* 1. Read init.txt line by line<br/>
* 2. Parse line using the following format: line ::= <zone>:<nickname>:<card name>:<amount><br/>
* 3. If zone equals to 'hand', add card to player's library<br/>
* 3a. Then swap added card with any card in player's hand<br/>
* 3b. Parse next line (go to 2.), If EOF go to 4.<br/>
* 4. Log message to all players that cards were added (to prevent unfair play).<br/>
* 5. Exit<br/>
*/
public static void addCardsForTesting(Game game) {
try {
File f = new File(INIT_FILE_PATH);
Pattern pattern = Pattern.compile("([a-zA-Z]*):([\\w]*):([a-zA-Z ,\\-.!'\\d]*):([\\d]*)");
if (!f.exists()) {
logger.warn("Couldn't find init file: " + INIT_FILE_PATH);
return;
}
logger.info("Parsing init.txt... ");
logger.info("Parsing init.txt... ");
Scanner scanner = new Scanner(f);
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.trim().length() == 0 || line.startsWith("#")) continue;
Matcher m = pattern.matcher(line);
if (m.matches()) {
Scanner scanner = new Scanner(f);
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.trim().length() == 0 || line.startsWith("#")) continue;
Matcher m = pattern.matcher(line);
if (m.matches()) {
String zone = m.group(1);
String nickname = m.group(2);
String zone = m.group(1);
String nickname = m.group(2);
Player player = findPlayer(game, nickname);
if (player != null) {
Constants.Zone gameZone;
if ("hand".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.HAND;
} else if ("battlefield".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.BATTLEFIELD;
} else if ("graveyard".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.GRAVEYARD;
} else if ("library".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.LIBRARY;
} else {
continue; // go parse next line
}
Player player = findPlayer(game, nickname);
if (player != null) {
Constants.Zone gameZone;
if ("hand".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.HAND;
} else if ("battlefield".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.BATTLEFIELD;
} else if ("graveyard".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.GRAVEYARD;
} else if ("library".equalsIgnoreCase(zone)) {
gameZone = Constants.Zone.LIBRARY;
} else {
continue; // go parse next line
}
String cardName = m.group(3);
Integer amount = Integer.parseInt(m.group(4));
for (int i = 0; i < amount; i++) {
Card card = Sets.findCard(cardName, true);
if (card != null) {
Set<Card> cards = new HashSet<Card>();
cards.add(card);
game.loadCards(cards, player.getId());
swapWithAnyCard(game, player, card, gameZone);
} else {
logger.fatal("Couldn't find a card: " + cardName);
}
}
} else {
logger.warn("Was skipped: " + line);
}
} else {
logger.warn("Init string wasn't parsed: " + line);
}
}
}
finally {
scanner.close();
}
} catch (Exception e) {
logger.fatal("", e);
}
}
String cardName = m.group(3);
Integer amount = Integer.parseInt(m.group(4));
for (int i = 0; i < amount; i++) {
Card card = Sets.findCard(cardName, true);
if (card != null) {
Set<Card> cards = new HashSet<Card>();
cards.add(card);
game.loadCards(cards, player.getId());
swapWithAnyCard(game, player, card, gameZone);
} else {
logger.fatal("Couldn't find a card: " + cardName);
}
}
} else {
logger.warn("Was skipped: " + line);
}
} else {
logger.warn("Init string wasn't parsed: " + line);
}
}
}
finally {
scanner.close();
}
} catch (Exception e) {
logger.fatal("", e);
}
}
/**
* Swap cards between specified card from library and any hand card.
*
* @param game
* @param card Card to put to player's hand
*/
private static void swapWithAnyCard(Game game, Player player, Card card, Constants.Zone zone) {
if (zone.equals(Constants.Zone.BATTLEFIELD)) {
card.putOntoBattlefield(game, Constants.Zone.OUTSIDE, null, player.getId());
} else if (zone.equals(Constants.Zone.LIBRARY)) {
/**
* Swap cards between specified card from library and any hand card.
*
* @param game
* @param card Card to put to player's hand
*/
private static void swapWithAnyCard(Game game, Player player, Card card, Constants.Zone zone) {
if (zone.equals(Constants.Zone.BATTLEFIELD)) {
card.putOntoBattlefield(game, Constants.Zone.OUTSIDE, null, player.getId());
} else if (zone.equals(Constants.Zone.LIBRARY)) {
game.setZone(card.getId(), Constants.Zone.LIBRARY);
player.getLibrary().putOnTop(card, game);
} else {
card.moveToZone(zone, null, game, false);
}
logger.info("Added card to player's " + zone.toString() + ": " + card.getName() +", player = " + player.getName());
}
card.moveToZone(zone, null, game, false);
}
logger.info("Added card to player's " + zone.toString() + ": " + card.getName() +", player = " + player.getName());
}
/**
* Find player by name.
*
* @param game
* @param name
* @return
*/
private static Player findPlayer(Game game, String name) {
for (Player player: game.getPlayers().values()) {
if (player.getName().equals(name))
return player;
}
return null;
}
/**
* Find player by name.
*
* @param game
* @param name
* @return
*/
private static Player findPlayer(Game game, String name) {
for (Player player: game.getPlayers().values()) {
if (player.getName().equals(name))
return player;
}
return null;
}
}

View file

@ -36,37 +36,37 @@ import java.util.concurrent.*;
*/
public class ThreadExecutor {
private static final ExecutorService callExecutor = Executors.newCachedThreadPool();
private static final ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads());
private static final ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5);
private static final ExecutorService callExecutor = Executors.newCachedThreadPool();
private static final ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads());
private static final ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5);
static {
((ThreadPoolExecutor)callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)callExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)gameExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)timeoutExecutor).allowCoreThreadTimeOut(true);
}
static {
((ThreadPoolExecutor)callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)callExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)gameExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor)timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor)timeoutExecutor).allowCoreThreadTimeOut(true);
}
private final static ThreadExecutor INSTANCE = new ThreadExecutor();
private final static ThreadExecutor INSTANCE = new ThreadExecutor();
public static ThreadExecutor getInstance() {
return INSTANCE;
}
public static ThreadExecutor getInstance() {
return INSTANCE;
}
private ThreadExecutor() {}
private ThreadExecutor() {}
public ExecutorService getCallExecutor() {
return callExecutor;
}
public ExecutorService getCallExecutor() {
return callExecutor;
}
public ExecutorService getGameExecutor() {
return gameExecutor;
}
public ExecutorService getGameExecutor() {
return gameExecutor;
}
public ScheduledExecutorService getTimeoutExecutor() {
return timeoutExecutor;
}
public ScheduledExecutorService getTimeoutExecutor() {
return timeoutExecutor;
}
}

View file

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Config.xsd">
<server serverAddress="0.0.0.0" serverName="mage-server" port="17171" maxGameThreads="10" maxSecondsIdle="600"/>
<playerTypes>
<playerType name="Human" jar="Mage.Player.Human.jar" className="mage.player.human.HumanPlayer"/>
<playerType name="Computer - default" jar="Mage.Player.AI.jar" className="mage.player.ai.ComputerPlayer"/>
</playerTypes>
<gameTypes>
<gameType name="Two Player Duel" jar="Mage.Game.TwoPlayerDuel.jar" className="mage.game.TwoPlayerDuel" typeName="mage.game.TwoPlayerDuelType"/>
</gameTypes>
<deckTypes>
<deckType name="Constructed" jar="Mage.Deck.Constructed.jar" className="mage.deck.Constructed"/>
</deckTypes>
<server serverAddress="0.0.0.0" serverName="mage-server" port="17171" maxGameThreads="10" maxSecondsIdle="600"/>
<playerTypes>
<playerType name="Human" jar="Mage.Player.Human.jar" className="mage.player.human.HumanPlayer"/>
<playerType name="Computer - default" jar="Mage.Player.AI.jar" className="mage.player.ai.ComputerPlayer"/>
</playerTypes>
<gameTypes>
<gameType name="Two Player Duel" jar="Mage.Game.TwoPlayerDuel.jar" className="mage.game.TwoPlayerDuel" typeName="mage.game.TwoPlayerDuelType"/>
</gameTypes>
<deckTypes>
<deckType name="Constructed" jar="Mage.Deck.Constructed.jar" className="mage.deck.Constructed"/>
</deckTypes>
</config>