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.servlets.HttpServletEx;
import scotlandyard.servlets.players.player_tickets;
/**
* shows the player who's turn is current for a given game
* @author Hussain Al-Mutawa
* @version 3.0
*
*/
public class whos_turn extends HttpServletEx {
private static final long serialVersionUID = -5391578389064688717L;
private String xhash,game_id;
public whos_turn() {
super();
}
public whos_turn(String xhash,String gameId) {
super();
this.xhash = xhash;
this.game_id = gameId;
}
@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{
if(parameters.get("xhash")!=null){
xhash = parameters.get("xhash");
}
if(xhash==null || "".equals(xhash)){throw new Exception("HASH is unknow");}
if(parameters.get("selected_game")!=null){
game_id = parameters.get("selected_game");
}
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("GAME is unknow");}
if(game.getStatus()==Game.NEW){
sb.append("<h3 style='color:darkgreen;background-color:lightgreen' class='whosTurn_text'> The Game Has Not Been Started </h3>");
}
else
{
if(game.getStatus()==Game.FINISHED){
sb.append("<h3 style='color:Yellow;background-color:Red' class='whosTurn_text'>" + game.getWhoWon() + "</h3>");
}
else
{
String firstName = Engine.firstWord(game.getPlayer(Engine.md5(game.getWhosTurn())).getName());
if(game.getPlayers().get(game.getCurrentTurn()).getEmail().equals(Engine.instance().users.get(xhash).email)){
firstName="<h3 class='whosTurn_text'>Your turn</h3>";
//sb.append("");
}else{
firstName = "<h3 class='whosTurn_text' style='color:#1100FF;background-color:#66CCFF'>"+ firstName +"'s Turn</h3>";
}
sb.append(firstName);
sb.append(new player_tickets(xhash, game.getId()).processRequest(parameters,sid));
}
}
}catch(Exception e){
sb.setLength(0);
sb.append("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
}
return sb.toString();
}
}