mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 13:02:06 -08:00
separated User from Session - will be need for reconnecting
This commit is contained in:
parent
b55dd5d787
commit
bdd8a2a28e
6 changed files with 46 additions and 29 deletions
|
|
@ -219,7 +219,7 @@ public class MageServerImpl implements MageServer {
|
|||
try {
|
||||
List<String> players = new ArrayList<String>();
|
||||
for (Session session : SessionManager.getInstance().getSessions().values()) {
|
||||
players.add(session.getUsername());
|
||||
players.add(session.getUser().getName());
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ public class Main {
|
|||
public void handleConnectionException(Throwable throwable, Client client) {
|
||||
Session session = SessionManager.getInstance().getSession(client.getSessionId());
|
||||
if (session != null) {
|
||||
String sessionName = session.getUsername() + " at " + session.getHost();
|
||||
String sessionName = session.getUser().getName() + " at " + session.getHost();
|
||||
if (throwable instanceof ClientDisconnectedException) {
|
||||
logger.info("client disconnected - " + sessionName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ package mage.server;
|
|||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import mage.MageException;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.server.game.GameManager;
|
||||
|
|
@ -49,7 +50,7 @@ public class Session {
|
|||
private final static Logger logger = Logger.getLogger(Session.class);
|
||||
|
||||
private String sessionId;
|
||||
private String username;
|
||||
private User user;
|
||||
private String host;
|
||||
private Date timeConnected;
|
||||
private boolean isAdmin = false;
|
||||
|
|
@ -62,14 +63,27 @@ public class Session {
|
|||
this.timeConnected = new Date();
|
||||
}
|
||||
|
||||
public void registerUser(String userName) {
|
||||
public void registerUser(String userName) throws MageException {
|
||||
this.isAdmin = false;
|
||||
this.username = userName;
|
||||
User user = UserManager.getInstance().findUser(userName);
|
||||
if (user == null) {
|
||||
user = UserManager.getInstance().createUser(userName, host);
|
||||
}
|
||||
else {
|
||||
if (user.getHost().equals(host)) {
|
||||
logger.info("Reconnecting session for " + userName);
|
||||
}
|
||||
else {
|
||||
throw new MageException("User name already in use");
|
||||
}
|
||||
}
|
||||
UserManager.getInstance().connectToSession(sessionId, userName);
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public void registerAdmin() {
|
||||
this.isAdmin = true;
|
||||
this.username = "Admin";
|
||||
this.user = UserManager.getInstance().createUser("Admin", host);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
|
|
@ -80,6 +94,7 @@ public class Session {
|
|||
TableManager.getInstance().removeSession(sessionId);
|
||||
GameManager.getInstance().removeSession(sessionId);
|
||||
ChatManager.getInstance().removeSession(sessionId);
|
||||
UserManager.getInstance().disconnect(user.getName());
|
||||
}
|
||||
|
||||
public synchronized void fireCallback(final ClientCallback call) {
|
||||
|
|
@ -118,8 +133,8 @@ public class Session {
|
|||
fireCallback(new ClientCallback("replayGame", gameId));
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import mage.MageException;
|
||||
import mage.view.UserView;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jboss.remoting.callback.InvokerCallbackHandler;
|
||||
|
|
@ -62,7 +63,7 @@ public class SessionManager {
|
|||
sessions.put(sessionId, session);
|
||||
}
|
||||
|
||||
public boolean registerUser(String sessionId, String userName) {
|
||||
public boolean registerUser(String sessionId, String userName) throws MageException {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
session.registerUser(userName);
|
||||
|
|
@ -103,7 +104,7 @@ public class SessionManager {
|
|||
Session admin = sessions.get(sessionId);
|
||||
if (admin != null && admin.isAdmin()) {
|
||||
for (Session session: sessions.values()) {
|
||||
users.add(new UserView(session.getUsername(), "", session.getId(), session.getConnectionTime()));
|
||||
users.add(new UserView(session.getUser().getName(), "", session.getId(), session.getConnectionTime()));
|
||||
}
|
||||
}
|
||||
return users;
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public class TableController {
|
|||
match = GameFactory.getInstance().createMatch(options.getGameType(), options);
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null)
|
||||
controllerName = session.getUsername();
|
||||
controllerName = session.getUser().getName();
|
||||
else
|
||||
controllerName = "System";
|
||||
table = new Table(roomId, options.getGameType(), options.getName(), controllerName, DeckValidatorFactory.getInstance().createDeckValidator(options.getDeckType()), options.getPlayerTypes(), match);
|
||||
|
|
@ -96,7 +96,7 @@ public class TableController {
|
|||
tournament = TournamentFactory.getInstance().createTournament(options.getTournamentType(), options);
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null)
|
||||
controllerName = session.getUsername();
|
||||
controllerName = session.getUser().getName();
|
||||
else
|
||||
controllerName = "System";
|
||||
table = new Table(roomId, options.getTournamentType(), options.getName(), controllerName, DeckValidatorFactory.getInstance().createDeckValidator(options.getMatchOptions().getDeckType()), options.getPlayerTypes(), tournament);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue