package scotlandyard.servlets.players;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import scotlandyard.engine.impl.Engine;
import scotlandyard.engine.spec.IGame;
import scotlandyard.engine.spec.IPlayer;
import scotlandyard.servlets.HttpServletEx;
/**
* gets the positions of other players
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class get_all_players_positions_json extends HttpServletEx {
private static final long serialVersionUID = 7894831440203189696L;
String gameId,xhash;
public get_all_players_positions_json() {
super();
}
public get_all_players_positions_json(String xhash,String gameId) {
super();
this.xhash=xhash;
this.gameId=gameId;
}
@Override
public void setHeader(HttpServletResponse response) {
response.setHeader("Content-Type", "text/plain");
}
@Override
public String processRequest(Map<String, String> parameters, String sid) throws ServletException {
final StringBuffer sb = new StringBuffer();
try{
if(parameters.get("xhash")!=null){
xhash = parameters.get("xhash");
}
if(xhash==null || "".equals(xhash)){throw new Exception("Unknown HASH");}
if(parameters.get("game")!=null){
gameId = parameters.get("game");
}
if(gameId==null || "".equals(gameId)){throw new Exception("Game Id is unknown");}
final IGame game = Engine.instance().games.get(gameId);
if(game==null){throw new Exception("Game is unknown");}
final IPlayer thisPlayer = game.getPlayer(xhash);
if(thisPlayer==null){throw new Exception("Player is unknown");}
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
final StringBuffer sb3 = new StringBuffer();
final StringBuffer sb4 = new StringBuffer();
//3, 8, 13 and 18 are the times where MrX is exposed
final int round = game.getRound();
final boolean exposeMRX = game.isRoundExposingMrX(round);
for(final IPlayer player:game.getPlayers()){
if(player.equals(thisPlayer)||(!player.isMrx() || exposeMRX)){
sb1.append(",\""+player.getName()+"\"");
sb2.append(",\""+player.getPosition()+"\"");
sb3.append(",\""+player.getIcon()+"\"");
sb4.append(",\""+player.getName()+"\"");
}
}
sb1.append(" ");sb2.append(" ");sb3.append(" ");
final String players_list = sb1.toString().substring(1);
final String positions_list = sb2.toString().substring(1);
final String icons_list = sb3.toString().substring(1);
final String players_names = sb4.toString().substring(1);
sb.append("{");
sb.append(" \"players\" : ["+players_list+"],");
sb.append(" \"positions\" : ["+positions_list+"],");
sb.append(" \"icons\" : ["+icons_list+"],");
sb.append(" \"round\" : "+round+"," );
sb.append(" \"gameStatus\" : \""+game.getStatus()+"\"," );
sb.append(" \"msg\" : \"OK\"," );
sb.append(" \"mrxFace\" : \""+(game.getStatus()=='F'?game.getMrX().getIcon():"-")+"\", ");
sb.append(" \"mrxName\" : \""+(game.getStatus()=='F'?game.getMrX().getName():"")+"\", ");
sb.append(" \"playersNames\" : ["+players_names+"]," );
sb.append(" \"whosTurn\" : \""+game.getPlayers().get(game.getCurrentTurn()).getName()+"\"" );
sb.append("}");
}catch(Exception e){
sb.setLength(0);
sb.append("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
}
return sb.toString();
}
}