From 50f28a2bf7fc14d72036f3e3ee51a87a9f28af3e Mon Sep 17 00:00:00 2001 From: Ingmar Goudt Date: Sat, 19 Jan 2019 09:31:50 +0100 Subject: [PATCH] add some Java8 magic to the server --- .../src/main/java/mage/server/Main.java | 12 +++-- .../main/java/mage/server/SessionManager.java | 49 ++++++++----------- .../java/mage/server/TableController.java | 10 ++-- .../src/main/java/mage/server/User.java | 6 +-- .../main/java/mage/server/UserManager.java | 9 +--- .../server/tournament/TournamentManager.java | 4 +- Mage.Sets/src/mage/cards/f/FleshAllergy.java | 12 +++-- 7 files changed, 46 insertions(+), 56 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index 03fe704d7e..06dc38db0f 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -56,9 +56,9 @@ public final class Main { private static final File extensionFolder = new File("extensions"); public static final PluginClassLoader classLoader = new PluginClassLoader(); - public static TransporterServer server; - protected static boolean testMode; - protected static boolean fastDbMode; + private static TransporterServer server; + private static boolean testMode; + private static boolean fastDbMode; /** * @param args the command line arguments @@ -418,8 +418,10 @@ public final class Main { File[] files = directory.listFiles( (dir, name) -> name.endsWith(".game") ); - for (File file : files) { - file.delete(); + if(files != null) { + for (File file : files) { + file.delete(); + } } } diff --git a/Mage.Server/src/main/java/mage/server/SessionManager.java b/Mage.Server/src/main/java/mage/server/SessionManager.java index 66d9c7d8e1..a0a30d2edd 100644 --- a/Mage.Server/src/main/java/mage/server/SessionManager.java +++ b/Mage.Server/src/main/java/mage/server/SessionManager.java @@ -1,14 +1,15 @@ package mage.server; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import javax.annotation.Nonnull; import mage.MageException; import mage.players.net.UserData; import org.apache.log4j.Logger; import org.jboss.remoting.callback.InvokerCallbackHandler; +import javax.annotation.Nonnull; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + /** * @author BetaSteward_at_googlemail.com */ @@ -88,18 +89,15 @@ public enum SessionManager { } public boolean setUserData(String userName, String sessionId, UserData userData, String clientVersion, String userIdStr) throws MageException { - Session session = sessions.get(sessionId); - if (session != null) { - session.setUserData(userName, userData, clientVersion, userIdStr); - return true; - } - return false; + return getSession(sessionId) + .map(session -> session.setUserData(userName,userData, clientVersion, userIdStr)) + .orElse(false); + } public void disconnect(String sessionId, DisconnectReason reason) { - Session session = sessions.get(sessionId); - if (session != null) { - if (!sessions.containsKey(sessionId)) { + getSession(sessionId).ifPresent(session -> { + if (!isValidSession(sessionId)) { // session was removed meanwhile by another thread so we can return return; } @@ -122,11 +120,11 @@ public enum SessionManager { default: logger.trace("endSession: unexpected reason " + reason.toString() + " - sessionId: " + sessionId); } - - } + }); } + /** * Admin requested the disconnect of a user * @@ -150,11 +148,9 @@ public enum SessionManager { } private Optional getUserFromSession(String sessionId) { - Optional session = getSession(sessionId); - if (!session.isPresent()) { - return Optional.empty(); - } - return UserManager.instance.getUser(session.get().getUserId()); + return getSession(sessionId) + .flatMap(s -> UserManager.instance.getUser(s.getUserId())); + } public void endUserSession(String sessionId, String userSessionId) { @@ -164,11 +160,8 @@ public enum SessionManager { } public boolean isAdmin(String sessionId) { - Session admin = sessions.get(sessionId); - if (admin != null) { - return admin.isAdmin(); - } - return false; + return getSession(sessionId).map(Session::isAdmin).orElse(false); + } public boolean isValidSession(@Nonnull String sessionId) { @@ -185,11 +178,9 @@ public enum SessionManager { } public boolean extendUserSession(String sessionId, String pingInfo) { - Session session = sessions.get(sessionId); - if (session != null) { - return UserManager.instance.extendUserSession(session.getUserId(), pingInfo); - } - return false; + return getSession(sessionId) + .map(session -> UserManager.instance.extendUserSession(session.getUserId(), pingInfo)) + .orElse(false); } public void sendErrorMessageToClient(String sessionId, String message) { diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index 90e7638c6e..914bdf3a53 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -927,12 +927,10 @@ public class TableController { public boolean isTournamentStillValid() { if (table.getTournament() != null) { if (table.getState() != TableState.WAITING && table.getState() != TableState.READY_TO_START && table.getState() != TableState.STARTING) { - TournamentController tournamentController = TournamentManager.instance.getTournamentController(table.getTournament().getId()); - if (tournamentController != null) { - return tournamentController.isTournamentStillValid(table.getState()); - } else { - return false; - } + return TournamentManager.instance.getTournamentController(table.getTournament().getId()) + .map(tc -> tc.isTournamentStillValid(table.getState())) + .orElse(false); + } else { // check if table creator is still a valid user, if not removeUserFromAllTablesAndChat table return UserManager.instance.getUser(userId).isPresent(); diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index 0649e7ac20..930133aa73 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -334,10 +334,10 @@ public class User { } for (Iterator> iterator = userTournaments.entrySet().iterator(); iterator.hasNext();) { Entry next = iterator.next(); - TournamentController tournamentController = TournamentManager.instance.getTournamentController(next.getValue()); - if (tournamentController != null) { + Optional tournamentController = TournamentManager.instance.getTournamentController(next.getValue()); + if (tournamentController.isPresent()) { ccTournamentStarted(next.getValue(), next.getKey()); - tournamentController.rejoin(next.getKey()); + tournamentController.get().rejoin(next.getKey()); } else { iterator.remove(); // tournament has ended meanwhile } diff --git a/Mage.Server/src/main/java/mage/server/UserManager.java b/Mage.Server/src/main/java/mage/server/UserManager.java index 4473afb3e0..a3c1c43842 100644 --- a/Mage.Server/src/main/java/mage/server/UserManager.java +++ b/Mage.Server/src/main/java/mage/server/UserManager.java @@ -70,13 +70,8 @@ public enum UserManager { final Lock r = lock.readLock(); r.lock(); try { - Optional u = users.values().stream().filter(user -> user.getName().equals(userName)) + return users.values().stream().filter(user -> user.getName().equals(userName)) .findFirst(); - if (u.isPresent()) { - return u; - } else { - return Optional.empty(); - } } finally { r.unlock(); } @@ -84,7 +79,7 @@ public enum UserManager { } public Collection getUsers() { - ArrayList userList = new ArrayList<>(); + List userList = new ArrayList<>(); final Lock r = lock.readLock(); r.lock(); try { diff --git a/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java b/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java index d9a611d26c..da4844fed9 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentManager.java @@ -16,8 +16,8 @@ public enum TournamentManager { instance; private final ConcurrentHashMap controllers = new ConcurrentHashMap<>(); - public TournamentController getTournamentController(UUID tournamentId) { - return controllers.get(tournamentId); + public Optional getTournamentController(UUID tournamentId) { + return Optional.ofNullable(controllers.get(tournamentId)); } public void createTournamentSession(Tournament tournament, ConcurrentHashMap userPlayerMap, UUID tableId) { diff --git a/Mage.Sets/src/mage/cards/f/FleshAllergy.java b/Mage.Sets/src/mage/cards/f/FleshAllergy.java index 6206824743..b14d2f10ee 100644 --- a/Mage.Sets/src/mage/cards/f/FleshAllergy.java +++ b/Mage.Sets/src/mage/cards/f/FleshAllergy.java @@ -54,13 +54,13 @@ public final class FleshAllergy extends CardImpl { class FleshAllergyWatcher extends Watcher { - public int creaturesDiedThisTurn = 0; + private int creaturesDiedThisTurn = 0; public FleshAllergyWatcher() { super(FleshAllergyWatcher.class, WatcherScope.GAME); } - public FleshAllergyWatcher(final FleshAllergyWatcher watcher) { + private FleshAllergyWatcher(final FleshAllergyWatcher watcher) { super(watcher); } @@ -79,6 +79,10 @@ class FleshAllergyWatcher extends Watcher { } } + public int getCreaturesDiedThisTurn(){ + return creaturesDiedThisTurn; + } + @Override public void reset() { super.reset(); @@ -94,7 +98,7 @@ class FleshAllergyEffect extends OneShotEffect { staticText = "Its controller loses life equal to the number of creatures that died this turn"; } - public FleshAllergyEffect(final FleshAllergyEffect effect) { + private FleshAllergyEffect(final FleshAllergyEffect effect) { super(effect); } @@ -110,7 +114,7 @@ class FleshAllergyEffect extends OneShotEffect { if (permanent != null && watcher != null) { Player player = game.getPlayer(permanent.getControllerId()); if (player != null) { - int amount = watcher.creaturesDiedThisTurn; + int amount = watcher.getCreaturesDiedThisTurn(); if (amount > 0) { player.loseLife(amount, game, false); return true;