better use of the CardsImpl constructor

This commit is contained in:
Ingmar Goudt 2019-02-03 21:08:31 +01:00
parent dc112215d4
commit 2cd9e25229
89 changed files with 248 additions and 402 deletions

View file

@ -260,13 +260,10 @@ public class MageServerImpl implements MageServer {
@Override
public void removeTable(final String sessionId, final UUID roomId, final UUID tableId) throws MageException {
execute("removeTable", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
TableManager.instance.removeTable(userId, tableId);
}
});
});
}
@ -277,7 +274,6 @@ public class MageServerImpl implements MageServer {
public Boolean execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
}
UUID userId = session.get().getUserId();
@ -288,7 +284,6 @@ public class MageServerImpl implements MageServer {
}
Optional<GamesRoom> room = GamesRoomManager.instance.getRoom(roomId);
if (!room.isPresent()) {
logger.error("room not found : " + roomId);
return false;
}
return room.get().joinTable(userId, tableId, name, playerType, skill, deckList, password);
@ -304,7 +299,6 @@ public class MageServerImpl implements MageServer {
public Boolean execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
}
UUID userId = session.get().getUserId();
@ -333,7 +327,6 @@ public class MageServerImpl implements MageServer {
public Boolean execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
} else {
UUID userId = session.get().getUserId();
@ -380,12 +373,7 @@ public class MageServerImpl implements MageServer {
//FIXME: why no sessionId here???
public List<MatchView> getFinishedMatches(UUID roomId) throws MageException {
try {
Optional<GamesRoom> room = GamesRoomManager.instance.getRoom(roomId);
if (room.isPresent()) {
return room.get().getFinished();
} else {
return new ArrayList<>();
}
return GamesRoomManager.instance.getRoom(roomId).map(GamesRoom::getFinished).orElse(new ArrayList<>());
} catch (Exception ex) {
handleException(ex);
}
@ -515,13 +503,11 @@ public class MageServerImpl implements MageServer {
@Override
public void joinChat(final UUID chatId, final String sessionId, final String userName) throws MageException {
execute("joinChat", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
ChatManager.instance.joinChat(chatId, userId);
}
});
});
}
@ -529,13 +515,10 @@ public class MageServerImpl implements MageServer {
public void leaveChat(final UUID chatId, final String sessionId) throws MageException {
execute("leaveChat", sessionId, () -> {
if (chatId != null) {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
ChatManager.instance.leaveChat(chatId, userId);
}
});
}
});
}
@ -574,7 +557,6 @@ public class MageServerImpl implements MageServer {
public Boolean execute() {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
} else {
UUID userId = session.get().getUserId();
@ -587,13 +569,10 @@ public class MageServerImpl implements MageServer {
@Override
public void swapSeats(final String sessionId, final UUID roomId, final UUID tableId, final int seatNum1, final int seatNum2) throws MageException {
execute("swapSeats", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
TableManager.instance.swapSeats(tableId, userId, seatNum1, seatNum2);
}
});
});
}
@ -607,19 +586,12 @@ public class MageServerImpl implements MageServer {
return false;
}
execute("leaveTable", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
Optional<GamesRoom> room = GamesRoomManager.instance.getRoom(roomId);
if (!room.isPresent()) {
logger.error("room not found : " + roomId);
} else {
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
GamesRoomManager.instance.getRoom(roomId).ifPresent(room ->
room.leaveTable(userId, tableId));
room.get().leaveTable(userId, tableId);
}
}
});
});
} else {
// this can happen if a game ends and a player quits XMage or a match nearly at the same time as the game ends
@ -642,26 +614,20 @@ public class MageServerImpl implements MageServer {
@Override
public void joinGame(final UUID gameId, final String sessionId) throws MageException {
execute("joinGame", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
GameManager.instance.joinGame(gameId, userId);
}
});
});
}
@Override
public void joinDraft(final UUID draftId, final String sessionId) throws MageException {
execute("joinDraft", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
DraftManager.instance.joinDraft(draftId, userId);
}
});
});
}
@ -769,13 +735,10 @@ public class MageServerImpl implements MageServer {
@Override
public void sendCardMark(final UUID draftId, final String sessionId, final UUID cardPick) throws MageException {
execute("sendCardMark", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
DraftManager.instance.sendCardMark(draftId, userId, cardPick);
}
});
});
}
@ -785,14 +748,10 @@ public class MageServerImpl implements MageServer {
try {
callExecutor.execute(
() -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
GameManager.instance.quitMatch(gameId, userId);
}
});
}
);
} catch (Exception ex) {
@ -807,14 +766,11 @@ public class MageServerImpl implements MageServer {
try {
callExecutor.execute(
() -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
TournamentManager.instance.quit(tournamentId, userId);
}
});
}
);
} catch (Exception ex) {
@ -830,18 +786,16 @@ public class MageServerImpl implements MageServer {
try {
callExecutor.execute(
() -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
UUID tableId = DraftManager.instance.getControllerByDraftId(draftId).getTableId();
Table table = TableManager.instance.getTable(tableId);
if (table.isTournament()) {
UUID tournamentId = table.getTournament().getId();
TournamentManager.instance.quit(tournamentId, userId);
}
}
SessionManager.instance.getSession(sessionId).ifPresent(
session -> {
UUID userId = session.getUserId();
UUID tableId = DraftManager.instance.getControllerByDraftId(draftId).getTableId();
Table table = TableManager.instance.getTable(tableId);
if (table.isTournament()) {
UUID tournamentId = table.getTournament().getId();
TournamentManager.instance.quit(tournamentId, userId);
}
});
}
);
} catch (Exception ex) {
@ -854,13 +808,10 @@ public class MageServerImpl implements MageServer {
@Override
public void sendPlayerAction(final PlayerAction playerAction, final UUID gameId, final String sessionId, final Object data) throws MageException {
execute("sendPlayerAction", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
GameManager.instance.sendPlayerAction(playerAction, gameId, userId, data);
}
});
});
}
@ -895,14 +846,11 @@ public class MageServerImpl implements MageServer {
return executeWithResult("watchGame", sessionId, new ActionWithResult<Boolean>() {
@Override
public Boolean execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
} else {
UUID userId = session.get().getUserId();
return GameManager.instance.watchGame(gameId, userId);
}
return SessionManager.instance.getSession(sessionId)
.map(session -> {
UUID userId = session.getUserId();
return GameManager.instance.watchGame(gameId, userId);
}).orElse(false);
}
@Override
@ -915,16 +863,13 @@ public class MageServerImpl implements MageServer {
@Override
public void stopWatching(final UUID gameId, final String sessionId) throws MageException {
execute("stopWatching", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
UserManager.instance.getUser(userId).ifPresent(user -> {
GameManager.instance.stopWatching(gameId, userId);
user.removeGameWatchInfo(gameId);
});
}
});
});
}
@ -932,26 +877,20 @@ public class MageServerImpl implements MageServer {
@Override
public void replayGame(final UUID gameId, final String sessionId) throws MageException {
execute("replayGame", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
ReplayManager.instance.replayGame(gameId, userId);
}
});
});
}
@Override
public void startReplay(final UUID gameId, final String sessionId) throws MageException {
execute("startReplay", sessionId, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
ReplayManager.instance.startReplay(gameId, userId);
}
});
});
}
@ -1032,13 +971,10 @@ public class MageServerImpl implements MageServer {
public void cheat(final UUID gameId, final String sessionId, final UUID playerId, final DeckCardLists deckList) throws MageException {
execute("cheat", sessionId, () -> {
if (testMode) {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
SessionManager.instance.getSession(sessionId).ifPresent(session -> {
UUID userId = session.getUserId();
GameManager.instance.cheat(gameId, userId, playerId, deckList);
}
});
}
});
}
@ -1327,7 +1263,6 @@ public class MageServerImpl implements MageServer {
public Boolean execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
} else {
UUID userId = session.get().getUserId();
@ -1378,7 +1313,6 @@ public class MageServerImpl implements MageServer {
public TableView execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return null;
}
UUID userId = session.get().getUserId();