package scotlandyard.servlets.games;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import scotlandyard.engine.impl.Engine;
import scotlandyard.engine.spec.IGame;
import scotlandyard.engine.spec.IUser;
/**
* allows a user to join a game and becomes a player
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class join_game extends HttpServlet {
private static final long serialVersionUID = -4763811993588066349L;
private String game_id ;
private Boolean mrx ;
private String hash ;
public join_game() {
super();
}
public join_game(String game_id,boolean joinAsMrX,String hash) {
super();
this.game_id=game_id;
this.mrx=joinAsMrX;
this.hash=hash;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final PrintWriter out = response.getWriter();
try{
response.setHeader("Content-Type", "text/plain");
if(request.getParameter("game")!=null){
game_id = request.getParameter("game");
}
if(request.getParameter("mrx")!=null){
mrx = Boolean.valueOf(request.getParameter("mrx"));
}
if(request.getParameter("xhash")!=null){
hash = request.getParameter("xhash");
}
if(hash==null||"".equals(hash)){
throw new Exception("Unknown HASH");
}
final IUser user = Engine.instance().getUser(hash);
if(user==null){
throw new Exception("Unknown User");
}
if(game_id==null || "".equals(game_id)){
throw new Exception("Game ID is missing");
}
final IGame game = Engine.instance().games.get(game_id);
if(game==null){
throw new Exception("Unknown Game");
}
game.addPlayer(user,mrx);
out.print("{\"msg\" : \"OK\"}");
}catch(Exception e){
out.print("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
}
}
}