Narrowed variables scope.

This commit is contained in:
vraskulin 2017-02-15 16:05:18 +03:00
parent 09da478b38
commit f1ef3bf68b
20 changed files with 42 additions and 60 deletions

View file

@ -271,8 +271,7 @@ public class ChatManager {
}
public ArrayList<ChatSession> getChatSessions() {
ArrayList<ChatSession> chatSessionList = new ArrayList<>(chatSessions.values());
return chatSessionList;
return new ArrayList<>(chatSessions.values());
}

View file

@ -28,7 +28,6 @@
package mage.server;
import mage.interfaces.callback.ClientCallback;
import mage.server.exceptions.UserNotFoundException;
import mage.view.ChatMessage;
import mage.view.ChatMessage.MessageColor;
import mage.view.ChatMessage.MessageType;
@ -36,7 +35,6 @@ import mage.view.ChatMessage.SoundToPlay;
import org.apache.log4j.Logger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Optional;

View file

@ -227,13 +227,13 @@ public class MageServerImpl implements MageServer {
// check AI players max
String maxAiOpponents = ConfigSettings.getInstance().getMaxAiOpponents();
if (maxAiOpponents != null) {
int max = Integer.parseInt(maxAiOpponents);
int aiPlayers = 0;
for (String playerType : options.getPlayerTypes()) {
if (!playerType.equals("Human")) {
aiPlayers++;
}
}
int max = Integer.parseInt(maxAiOpponents);
if (aiPlayers > max) {
user.showUserMessage("Create tournament", "It's only allowed to use a maximum of " + max + " AI players.");
throw new MageException("No message");

View file

@ -37,8 +37,7 @@ public class MailClient {
message.setSubject(subject);
message.setText(text);
Transport trnsport;
trnsport = session.getTransport("smtps");
Transport trnsport = session.getTransport("smtps");
trnsport.connect(null, properties.getProperty("mail.password"));
message.saveChanges();
trnsport.sendMessage(message, message.getAllRecipients());

View file

@ -116,13 +116,13 @@ public class Main {
}
logger.info("Loading extension packages...");
List<ExtensionPackage> extensions = new ArrayList<>();
if (!extensionFolder.exists()) {
if (!extensionFolder.mkdirs()) {
logger.error("Could not create extensions directory.");
}
}
File[] extensionDirectories = extensionFolder.listFiles();
List<ExtensionPackage> extensions = new ArrayList<>();
if (extensionDirectories != null) {
for (File f : extensionDirectories) {
if (f.isDirectory()) {

View file

@ -101,11 +101,11 @@ public class Session {
return returnMessage;
}
AuthorizedUserRepository.instance.add(userName, password, email);
String subject = "XMage Registration Completed";
String text = "You are successfully registered as " + userName + '.';
text += " Your initial, generated password is: " + password;
boolean success;
String subject = "XMage Registration Completed";
if (!ConfigSettings.getInstance().getMailUser().isEmpty()) {
success = MailClient.sendMessage(email, subject, text);
} else {

View file

@ -173,8 +173,9 @@ public class SessionManager {
*/
public void disconnectUser(String sessionId, String userSessionId) {
if (isAdmin(sessionId)) {
User userAdmin, user;
User userAdmin;
if ((userAdmin = getUserFromSession(sessionId)) != null) {
User user;
if ((user = getUserFromSession(userSessionId)) != null) {
user.showUserMessage("Admin operation", "Your session was disconnected by Admin.");
userAdmin.showUserMessage("Admin action", "User" + user.getName() + " was disconnected.");

View file

@ -911,9 +911,6 @@ public class TableController {
public boolean isMatchTableStillValid() {
// check only normal match table with state != Finished
if (!table.isTournament()) {
int humanPlayers = 0;
int aiPlayers = 0;
int validHumanPlayers = 0;
if (!(table.getState() == TableState.WAITING || table.getState() == TableState.STARTING || table.getState() == TableState.READY_TO_START)) {
if (match == null) {
logger.debug("- Match table with no match:");
@ -927,6 +924,9 @@ public class TableController {
}
}
// check for active players
int validHumanPlayers = 0;
int aiPlayers = 0;
int humanPlayers = 0;
for (Map.Entry<UUID, UUID> userPlayerEntry : userPlayerMap.entrySet()) {
MatchPlayer matchPlayer = match.getPlayer(userPlayerEntry.getValue());
if (matchPlayer == null) {

View file

@ -55,9 +55,8 @@ public class CubeFactory {
public DraftCube createDraftCube(String draftCubeName) {
DraftCube draftCube;
Constructor<?> con;
try {
con = draftCubes.get(draftCubeName).getConstructor();
Constructor<?> con = draftCubes.get(draftCubeName).getConstructor();
draftCube = (DraftCube)con.newInstance();
} catch (Exception ex) {
logger.fatal("CubeFactory error", ex);
@ -71,9 +70,8 @@ public class CubeFactory {
public DraftCube createDeckDraftCube(String draftCubeName, Deck cubeFromDeck) {
DraftCube draftCube;
Constructor<?> con;
try {
con = draftCubes.get(draftCubeName).getConstructor(Deck.class);
Constructor<?> con = draftCubes.get(draftCubeName).getConstructor(Deck.class);
draftCube = (DraftCube)con.newInstance(cubeFromDeck);
} catch (Exception ex) {
logger.fatal("CubeFactory error", ex);

View file

@ -55,9 +55,8 @@ public class DeckValidatorFactory {
public DeckValidator createDeckValidator(String deckType) {
DeckValidator validator;
Constructor<?> con;
try {
con = deckTypes.get(deckType).getConstructor();
Constructor<?> con = deckTypes.get(deckType).getConstructor();
validator = (DeckValidator)con.newInstance();
} catch (Exception ex) {
logger.fatal("DeckValidatorFactory error", ex);

View file

@ -621,9 +621,8 @@ public class GameController implements GameCallback {
}
public void cheat(UUID userId, UUID playerId, DeckCardLists deckList) {
Deck deck;
try {
deck = Deck.load(deckList, false, false);
Deck deck = Deck.load(deckList, false, false);
game.loadCards(deck.getCards(), playerId);
for (Card card : deck.getCards()) {
card.putOntoBattlefield(game, Zone.OUTSIDE, null, playerId);

View file

@ -62,9 +62,8 @@ public class GameFactory {
public Match createMatch(String gameType, MatchOptions options) {
Match match;
Constructor<Match> con;
try {
con = games.get(gameType).getConstructor(MatchOptions.class);
Constructor<Match> con = games.get(gameType).getConstructor(MatchOptions.class);
match = con.newInstance(options);
} catch (Exception ex) {
logger.fatal("Error creating match - " + gameType, ex);

View file

@ -29,7 +29,6 @@ package mage.server.game;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
@ -89,10 +88,10 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
private void update() {
ArrayList<TableView> tableList = new ArrayList<>();
ArrayList<MatchView> matchList = new ArrayList<>();
List<Table> allTables = new ArrayList<>(tables.values());
allTables.sort(new TableListSorter());
ArrayList<MatchView> matchList = new ArrayList<>();
ArrayList<TableView> tableList = new ArrayList<>();
for (Table table : allTables) {
if (table.getState() != TableState.FINISHED) {
tableList.add(new TableView(table));

View file

@ -54,13 +54,11 @@ public class PlayerFactory {
private PlayerFactory() {}
public Player createPlayer(String playerType, String name, RangeOfInfluence range, int skill) {
Player player;
Constructor<?> con;
try {
Class playerTypeClass = playerTypes.get(playerType);
if (playerTypeClass != null) {
con = playerTypeClass.getConstructor(String.class, RangeOfInfluence.class, int.class);
player = (Player)con.newInstance(name, range, skill);
Constructor<?> con = playerTypeClass.getConstructor(String.class, RangeOfInfluence.class, int.class);
Player player = (Player) con.newInstance(name, range, skill);
logger.trace("Player created: " + name + " - " + player.getId());
return player;
}

View file

@ -33,7 +33,6 @@ import java.util.UUID;
import mage.game.Game;
import mage.game.GameState;
import mage.interfaces.callback.ClientCallback;
import mage.server.User;
import mage.server.UserManager;
import mage.view.GameView;

View file

@ -418,10 +418,10 @@ public class TournamentController {
}
// replace player that quits with draft bot
if (humans > 1) {
String replacePlayerName = "Draftbot";
Optional<User> user = UserManager.getInstance().getUser(userId);
TableController tableController = TableManager.getInstance().getController(tableId);
if (tableController != null) {
String replacePlayerName = "Draftbot";
if (user.isPresent()) {
replacePlayerName = "Draftbot (" + user.get().getName() + ')';
}

View file

@ -64,9 +64,8 @@ public class TournamentFactory {
public Tournament createTournament(String tournamentType, TournamentOptions options) {
Tournament tournament;
Constructor<Tournament> con;
try {
con = tournaments.get(tournamentType).getConstructor(TournamentOptions.class);
Constructor<Tournament> con = tournaments.get(tournamentType).getConstructor(TournamentOptions.class);
tournament = con.newInstance(options);
// transfer set information, create short info string for included sets
tournament.setTournamentType(tournamentTypes.get(tournamentType));

View file

@ -45,7 +45,6 @@ public class SystemUtil {
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;
@ -54,6 +53,7 @@ public class SystemUtil {
logger.info("Parsing init.txt... ");
try (Scanner scanner = new Scanner(f)) {
Pattern pattern = Pattern.compile("([a-zA-Z]+):([\\w]+):([a-zA-Z ,\\/\\-.!'\\d:]+?):(\\d+)");
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.isEmpty() || line.startsWith("#")) {