forked from External/mage
[app-wiring-refactor]: Remove static initialisation
- Remove all enum static managers - Introduce interfaces for the managers - Define new application wiring class (`ManagerFactory`) - Externalise the configuration
This commit is contained in:
parent
cf3dd2d94c
commit
d0c2135e17
48 changed files with 1385 additions and 911 deletions
|
|
@ -1,5 +1,3 @@
|
|||
|
||||
|
||||
package mage.server.draft;
|
||||
|
||||
import mage.MageException;
|
||||
|
|
@ -9,10 +7,8 @@ import mage.game.events.Listener;
|
|||
import mage.game.events.PlayerQueryEvent;
|
||||
import mage.game.events.TableEvent;
|
||||
import mage.players.Player;
|
||||
import mage.server.TableManager;
|
||||
import mage.server.UserManager;
|
||||
import mage.server.game.GameController;
|
||||
import mage.server.util.ThreadExecutor;
|
||||
import mage.server.managers.ManagerFactory;
|
||||
import mage.view.DraftPickView;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
|
@ -25,7 +21,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class DraftController {
|
||||
|
|
@ -33,6 +28,7 @@ public class DraftController {
|
|||
private static final Logger logger = Logger.getLogger(GameController.class);
|
||||
public static final String INIT_FILE_PATH = "config" + File.separator + "init.txt";
|
||||
|
||||
private final ManagerFactory managerFactory;
|
||||
private final ConcurrentMap<UUID, DraftSession> draftSessions = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<UUID, UUID> userPlayerMap;
|
||||
private final UUID draftSessionId;
|
||||
|
|
@ -40,7 +36,8 @@ public class DraftController {
|
|||
private final UUID tableId;
|
||||
private final UUID markedCard;
|
||||
|
||||
public DraftController(Draft draft, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
|
||||
public DraftController(ManagerFactory managerFactory, Draft draft, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
|
||||
this.managerFactory = managerFactory;
|
||||
draftSessionId = UUID.randomUUID();
|
||||
this.userPlayerMap = userPlayerMap;
|
||||
this.draft = draft;
|
||||
|
|
@ -61,8 +58,7 @@ public class DraftController {
|
|||
endDraft();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (MageException ex) {
|
||||
} catch (MageException ex) {
|
||||
logger.fatal("Table event listener error", ex);
|
||||
}
|
||||
}
|
||||
|
|
@ -75,13 +71,12 @@ public class DraftController {
|
|||
pickCard(event.getPlayerId(), event.getMax());
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (MageException ex) {
|
||||
} catch (MageException ex) {
|
||||
logger.fatal("Table event listener error", ex);
|
||||
}
|
||||
}
|
||||
);
|
||||
for (DraftPlayer player: draft.getPlayers()) {
|
||||
for (DraftPlayer player : draft.getPlayers()) {
|
||||
if (!player.getPlayer().isHuman()) {
|
||||
player.setJoined();
|
||||
logger.debug("player " + player.getPlayer().getId() + " has joined draft " + draft.getId());
|
||||
|
|
@ -96,13 +91,13 @@ public class DraftController {
|
|||
|
||||
public void join(UUID userId) {
|
||||
UUID playerId = userPlayerMap.get(userId);
|
||||
DraftSession draftSession = new DraftSession(draft, userId, playerId);
|
||||
DraftSession draftSession = new DraftSession(managerFactory, draft, userId, playerId);
|
||||
draftSessions.put(playerId, draftSession);
|
||||
UserManager.instance.getUser(userId).ifPresent(user-> {
|
||||
user.addDraft(playerId, draftSession);
|
||||
logger.debug("User " + user.getName() + " has joined draft " + draft.getId());
|
||||
draft.getPlayer(playerId).setJoined();
|
||||
});
|
||||
managerFactory.userManager().getUser(userId).ifPresent(user -> {
|
||||
user.addDraft(playerId, draftSession);
|
||||
logger.debug("User " + user.getName() + " has joined draft " + draft.getId());
|
||||
draft.getPlayer(playerId).setJoined();
|
||||
});
|
||||
checkStart();
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +109,7 @@ public class DraftController {
|
|||
}
|
||||
|
||||
public boolean replacePlayer(Player oldPlayer, Player newPlayer) {
|
||||
if (draft.replacePlayer(oldPlayer, newPlayer)) {
|
||||
if (draft.replacePlayer(oldPlayer, newPlayer)) {
|
||||
DraftSession draftSession = draftSessions.get(oldPlayer.getId());
|
||||
if (draftSession != null) {
|
||||
draftSession.draftOver(); // closes the draft panel of the replaced player
|
||||
|
|
@ -128,12 +123,12 @@ public class DraftController {
|
|||
private synchronized void checkStart() {
|
||||
if (!draft.isStarted() && allJoined()) {
|
||||
draft.setStarted();
|
||||
ThreadExecutor.instance.getCallExecutor().execute(this::startDraft);
|
||||
managerFactory.threadExecutor().getCallExecutor().execute(this::startDraft);
|
||||
}
|
||||
}
|
||||
|
||||
private void startDraft() {
|
||||
for (final Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
|
||||
for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
|
||||
if (!entry.getValue().init()) {
|
||||
logger.fatal("Unable to initialize client for playerId " + entry.getKey());
|
||||
//TODO: generate client error message
|
||||
|
|
@ -147,7 +142,7 @@ public class DraftController {
|
|||
if (!draft.allJoined()) {
|
||||
return false;
|
||||
}
|
||||
for (DraftPlayer player: draft.getPlayers()) {
|
||||
for (DraftPlayer player : draft.getPlayers()) {
|
||||
if (player.getPlayer().isHuman() && !draftSessions.containsKey(player.getPlayer().getId())) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -160,12 +155,12 @@ public class DraftController {
|
|||
}
|
||||
|
||||
private void endDraft() throws MageException {
|
||||
for (final DraftSession draftSession: draftSessions.values()) {
|
||||
for (final DraftSession draftSession : draftSessions.values()) {
|
||||
draftSession.draftOver();
|
||||
draftSession.removeDraft();
|
||||
}
|
||||
TableManager.instance.endDraft(tableId, draft);
|
||||
DraftManager.instance.removeDraft(draft.getId());
|
||||
managerFactory.tableManager().endDraft(tableId, draft);
|
||||
managerFactory.draftManager().removeDraft(draft.getId());
|
||||
}
|
||||
|
||||
public void kill(UUID userId) {
|
||||
|
|
@ -210,7 +205,7 @@ public class DraftController {
|
|||
}
|
||||
|
||||
private synchronized void updateDraft() throws MageException {
|
||||
for (final Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
|
||||
for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
|
||||
entry.getValue().update();
|
||||
}
|
||||
}
|
||||
|
|
@ -229,7 +224,7 @@ public class DraftController {
|
|||
draft.setAbort(true);
|
||||
try {
|
||||
endDraft();
|
||||
} catch(MageException ex) {
|
||||
} catch (MageException ex) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,69 +1,83 @@
|
|||
|
||||
|
||||
package mage.server.draft;
|
||||
|
||||
import mage.game.draft.Draft;
|
||||
import mage.server.managers.IDraftManager;
|
||||
import mage.server.managers.ManagerFactory;
|
||||
import mage.view.DraftPickView;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import mage.game.draft.Draft;
|
||||
import mage.view.DraftPickView;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public enum DraftManager {
|
||||
instance;
|
||||
public class DraftManager implements IDraftManager {
|
||||
|
||||
private final ManagerFactory managerFactory;
|
||||
private final ConcurrentMap<UUID, DraftController> draftControllers = new ConcurrentHashMap<>();
|
||||
|
||||
public DraftManager(ManagerFactory managerFactory) {
|
||||
this.managerFactory = managerFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID createDraftSession(Draft draft, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
|
||||
DraftController draftController = new DraftController(draft, userPlayerMap, tableId);
|
||||
DraftController draftController = new DraftController(managerFactory, draft, userPlayerMap, tableId);
|
||||
draftControllers.put(draft.getId(), draftController);
|
||||
return draftController.getSessionId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void joinDraft(UUID draftId, UUID userId) {
|
||||
draftControllers.get(draftId).join(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyChatSession(UUID gameId) {
|
||||
draftControllers.remove(gameId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DraftPickView sendCardPick(UUID draftId, UUID userId, UUID cardId, Set<UUID> hiddenCards) {
|
||||
return draftControllers.get(draftId).sendCardPick(userId, cardId, hiddenCards);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendCardMark(UUID draftId, UUID userId, UUID cardId) {
|
||||
draftControllers.get(draftId).sendCardMark(userId, cardId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSession(UUID userId) {
|
||||
for (DraftController controller: draftControllers.values()) {
|
||||
for (DraftController controller : draftControllers.values()) {
|
||||
controller.kill(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kill(UUID draftId, UUID userId) {
|
||||
draftControllers.get(draftId).kill(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void timeout(UUID gameId, UUID userId) {
|
||||
draftControllers.get(gameId).timeout(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeDraft(UUID draftId) {
|
||||
draftControllers.remove(draftId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DraftController getControllerByDraftId(UUID draftId) {
|
||||
return draftControllers.get(draftId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DraftController> getController(UUID tableId) {
|
||||
return draftControllers.values().stream().filter(controller -> controller.getTableId().equals(tableId)).findFirst();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,15 @@
|
|||
|
||||
package mage.server.draft;
|
||||
|
||||
import mage.game.draft.Draft;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.interfaces.callback.ClientCallbackMethod;
|
||||
import mage.server.User;
|
||||
import mage.server.managers.ManagerFactory;
|
||||
import mage.view.DraftClientMessage;
|
||||
import mage.view.DraftPickView;
|
||||
import mage.view.DraftView;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
|
@ -8,16 +17,6 @@ import java.util.UUID;
|
|||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import mage.game.draft.Draft;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.interfaces.callback.ClientCallbackMethod;
|
||||
import mage.server.User;
|
||||
import mage.server.UserManager;
|
||||
import mage.server.util.ThreadExecutor;
|
||||
import mage.view.DraftClientMessage;
|
||||
import mage.view.DraftPickView;
|
||||
import mage.view.DraftView;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
|
|
@ -26,6 +25,7 @@ public class DraftSession {
|
|||
|
||||
protected static final Logger logger = Logger.getLogger(DraftSession.class);
|
||||
|
||||
private final ManagerFactory managerFactory;
|
||||
protected final UUID userId;
|
||||
protected final UUID playerId;
|
||||
protected final Draft draft;
|
||||
|
|
@ -33,9 +33,11 @@ public class DraftSession {
|
|||
protected UUID markedCard;
|
||||
|
||||
private ScheduledFuture<?> futureTimeout;
|
||||
protected static final ScheduledExecutorService timeoutExecutor = ThreadExecutor.instance.getTimeoutExecutor();
|
||||
protected final ScheduledExecutorService timeoutExecutor;
|
||||
|
||||
public DraftSession(Draft draft, UUID userId, UUID playerId) {
|
||||
public DraftSession(ManagerFactory managerFactory, Draft draft, UUID userId, UUID playerId) {
|
||||
this.managerFactory = managerFactory;
|
||||
this.timeoutExecutor = managerFactory.threadExecutor().getTimeoutExecutor();
|
||||
this.userId = userId;
|
||||
this.draft = draft;
|
||||
this.playerId = playerId;
|
||||
|
|
@ -44,12 +46,12 @@ public class DraftSession {
|
|||
|
||||
public boolean init() {
|
||||
if (!killed) {
|
||||
Optional<User> user = UserManager.instance.getUser(userId);
|
||||
Optional<User> user = managerFactory.userManager().getUser(userId);
|
||||
if (user.isPresent()) {
|
||||
if (futureTimeout != null && !futureTimeout.isDone()) {
|
||||
int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS);
|
||||
user.get().fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_INIT, draft.getId(),
|
||||
new DraftClientMessage(getDraftView(), getDraftPickView(remaining))));
|
||||
new DraftClientMessage(getDraftView(), getDraftPickView(remaining))));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -59,16 +61,16 @@ public class DraftSession {
|
|||
|
||||
public void update() {
|
||||
if (!killed) {
|
||||
UserManager.instance
|
||||
managerFactory.userManager()
|
||||
.getUser(userId).
|
||||
ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_UPDATE, draft.getId(),
|
||||
new DraftClientMessage(getDraftView(), null))));
|
||||
new DraftClientMessage(getDraftView(), null))));
|
||||
}
|
||||
}
|
||||
|
||||
public void draftOver() {
|
||||
if (!killed) {
|
||||
UserManager.instance
|
||||
managerFactory.userManager()
|
||||
.getUser(userId)
|
||||
.ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_OVER, draft.getId())));
|
||||
}
|
||||
|
|
@ -77,10 +79,10 @@ public class DraftSession {
|
|||
public void pickCard(int timeout) {
|
||||
if (!killed) {
|
||||
setupTimeout(timeout);
|
||||
UserManager.instance
|
||||
managerFactory.userManager()
|
||||
.getUser(userId)
|
||||
.ifPresent(user -> user.fireCallback(new ClientCallback(ClientCallbackMethod.DRAFT_PICK, draft.getId(),
|
||||
new DraftClientMessage(getDraftView(), getDraftPickView(timeout)))));
|
||||
new DraftClientMessage(getDraftView(), getDraftPickView(timeout)))));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -89,7 +91,7 @@ public class DraftSession {
|
|||
cancelTimeout();
|
||||
if (seconds > 0) {
|
||||
futureTimeout = timeoutExecutor.schedule(
|
||||
() -> DraftManager.instance.timeout(draft.getId(), userId),
|
||||
() -> managerFactory.draftManager().timeout(draft.getId(), userId),
|
||||
seconds, TimeUnit.SECONDS
|
||||
);
|
||||
}
|
||||
|
|
@ -103,7 +105,7 @@ public class DraftSession {
|
|||
|
||||
protected void handleRemoteException(RemoteException ex) {
|
||||
logger.fatal("DraftSession error ", ex);
|
||||
DraftManager.instance.kill(draft.getId(), userId);
|
||||
managerFactory.draftManager().kill(draft.getId(), userId);
|
||||
}
|
||||
|
||||
public void setKilled() {
|
||||
|
|
@ -119,7 +121,7 @@ public class DraftSession {
|
|||
}
|
||||
|
||||
public void removeDraft() {
|
||||
UserManager.instance.getUser(userId).ifPresent(user -> user.removeDraft(playerId));
|
||||
managerFactory.userManager().getUser(userId).ifPresent(user -> user.removeDraft(playerId));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue