package scotlandyard.servlets.games;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import scotlandyard.engine.impl.Engine;
import scotlandyard.engine.impl.Game;
import scotlandyard.engine.spec.IGame;
import scotlandyard.engine.spec.IPlayer;
import scotlandyard.engine.spec.IUser;
import scotlandyard.servlets.HttpServletEx;
/**
* constructs a game panel that shows the
* players and who is mr X
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class show_game extends HttpServletEx {
private static final long serialVersionUID = -5470872681314520656L;
private String gameId,xhash;
private Boolean selected = Boolean.FALSE;
public show_game() {
super();
}
public show_game(String gameId,Boolean selected,String xhash) {
this();
this.gameId=gameId;
this.selected=selected;
this.xhash=xhash;
}
@Override
public void setHeader(HttpServletResponse response) {
response.setHeader("Content-Type", "text/html");
}
@Override
public String processRequest(Map<String, String> parameters, String sid) throws ServletException {
final StringBuffer sb = new StringBuffer();
try{
final String reqGameId=parameters.get("game");
final String reqSelected=parameters.get("selected");
final String reqxHash=parameters.get("xhash");
if(reqGameId!=null){
gameId = reqGameId;
}
if(gameId==null || "".equals(gameId)){
throw new Exception("Game ID is not known");
}
final IGame game = Engine.instance().games.get(gameId);
if(game==null){
throw new Exception("Game is not known");
}
if(reqSelected!=null){
selected = Boolean.valueOf(reqSelected);
}
if(reqxHash!=null){
xhash=reqxHash;
}
if(xhash==null){
throw new Exception("HASH is not known");
}
sb.append("<div id='"+Engine.md5(gameId)+"'><div class='game_header"+(selected?" selectedGameTag":"")+"'>Game : ");
sb.append(" ["+Game.getStatusDefinition(game.getStatus())+"]</div>");
sb.append("<div class='gamecontainer"+(selected?" selectedGamePanel":"")+"'>");
if(!selected){
sb.append("<button onclick='open_game(this.title)' title='"+game.getId()+"'>Open : ");
sb.append(game.getId()+"</button><br/>");
}else if(game.getStatus()=='N'){
sb.append("<button onclick='start_game()'>Start game</button><br/>");
}
sb.append("<ul class='gameplayers'>");
for(IPlayer p:game.getPlayers()){
sb.append("<li class='gamePlayerTag"+(p.isMrx()?"X":"")+"'>");
sb.append("<div>");
sb.append("<img class='PlayerSmallIcon' src='images/icons/"+p.getIcon()+"'/>");
sb.append("</div>");
sb.append("<div>");
if(p.isMrx()){
sb.append("<strong>"+p.getName()+"</strong>");
}else{
sb.append(p.getName());
}
sb.append(" </div>");
sb.append("</li>");
}
sb.append("</ul>");
final IUser user = Engine.instance().getUser(xhash);
if(game.getStatus()==Game.NEW && (user!=null && (game.getPlayers().size()==0 || (game.getPlayers().size()<6 && !game.hasPlayer(user))))){
sb.append("<div id='game_buttons_"+game.getId()+"' align='right'>");
sb.append("<table><tr>");
if(game.getMrX()==null){
sb.append("<td><button onclick='join_game(\""+game.getId()+"\",true)'>");
sb.append("Be MrX</button></td>");
}
sb.append("<td><button onclick='join_game(\""+game.getId()+"\",false)'>");
sb.append("Be Detective </button></td>");
sb.append("</tr></table>");
sb.append("</div>");
}
sb.append("</div></div>");
}catch(Exception e){
sb.setLength(0);
sb.append("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
}
return sb.toString();
}
}