package scotlandyard.server.services;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import scotlandyard.client.network.rpc.PreGameService;
import scotlandyard.server.GameServer;
import scotlandyard.server.MainServer;
import scotlandyard.shared.Player;
import scotlandyard.shared.ScotConstants;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class PreGameServiceImpl extends RemoteServiceServlet implements
PreGameService {
@Override
public int login(String playerName, String password) {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player player = new Player();
player.setName(playerName);
session.setAttribute("PlayerInstance", player);
//Did player DCed?
if (MainServer.getMainServerInstance().hasPlayerDCed(player)){
System.out.println("Player "+player.getName()+" trying to reconnect!");
return ScotConstants.LOGIN_RECONNECTED;
}
// For now everyone can login
session.setAttribute("Login.isDone", playerName);
//codigo para consultar o bd
/*boolean resultado = false;
String pesquisa = "";
query = "select senha from usuario where login = "+playerName;
try {
prepared = connect.prepareStatement(query);
resultado = prepared.executeQuery();
while (resultado.next()) {
pesquisa = resultado.getString("senha");
if(pesquisa==password){
resultado = true;
}
}
} catch (SQLException ex) {
System.out.println("Erro ");
ex.printStackTrace();
}
return resultado;*/
return ScotConstants.LOGIN_CONNECTED;
}
@Override
public void logout() {
// TODO Auto-generated method stub
}
@Override
public void joinGame(String gameName) {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player playerName = (Player)session.getAttribute("PlayerInstance");
System.out.println(playerName);
GameServer game = MainServer.getMainServerInstance().getGameServerByGameName(gameName);
session.setAttribute("GameServerInstance",game );
//session.setAttribute("PlayerName", playerName);
// see MainServerManagementTest
game.joinGame(playerName);
}
@Override
public void quitGame(String gameName) {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player playerName = (Player)session.getAttribute("PlayerInstance");
GameServer game = (GameServer)session.getAttribute("GameServerInstance");
game.quitGame(playerName);
session.removeAttribute("GameServerInstance");
}
@Override
public String[] getGameList() {
// see MainServerManagementTest
return MainServer.getMainServerInstance().getAllGameNames();
}
@Override
public void openGame(String gameName, int caseNumber) {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player playerName = (Player)session.getAttribute("PlayerInstance");
// see MainServerManagementTest
MainServer.getMainServerInstance().openGame(gameName);
GameServer game = MainServer.getMainServerInstance().getGameServerByGameName(gameName);
session.setAttribute("GameServerInstance", game);
game.joinGame(playerName);
}
@Override
public void closeGame(String gameName) {
// see MainServerManagementTest
MainServer.getMainServerInstance().closeGame(gameName);
}
@Override
public String[] getPlayerList(String gameName) {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
GameServer game = (GameServer)session.getAttribute("GameServerInstance");
return game.getPlayerNames();
}
@Override
public void readyStartGame() {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player player = (Player)session.getAttribute("PlayerInstance");
GameServer game = (GameServer)session.getAttribute("GameServerInstance");
game.readyStartGame(player);
System.out.println(player.getName() + " pediu para comecar o jogo.");
//Player asked, try to startGame
}
@Override
public void stopCometServer() {
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
GameServer game = (GameServer)session.getAttribute("GameServerInstance");
//System.out.println("PreGameService: pedindo para parar MyCometServlet");
//game.stopServer = true;
}
@Override
public void notifyDisconnection() {
//So he disconnected?
////Was he Playing?
//Then wait for reconnection!
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player player = (Player)session.getAttribute("PlayerInstance");
GameServer game = (GameServer)session.getAttribute("GameServerInstance");
//Se ele est� ligado a um jogo
//E se esse jogo j� come�ou
if (game != null && game.isRunning()){
System.out.println("Player "+player.getName()+" disconnected/closed the browser while playing!");
MainServer.getMainServerInstance().playerDisconnected(player, game);
}
else{
System.out.println("Closed browser BUT is not playing");
}
}
@Override
public String notifyReconnection() {
//Player reconnected
HttpServletRequest request = this.getThreadLocalRequest();
HttpSession session = request.getSession();
Player player = (Player)session.getAttribute("PlayerInstance");
GameServer game = MainServer.getMainServerInstance().getGameForReconnection(player);
session.setAttribute("GameServerInstance", game);
game.switchPlayer(player);
return game.getGameName();
}
}