Package scotlandyard.servlets.players

Source Code of scotlandyard.servlets.players.player_tickets

package scotlandyard.servlets.players;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import scotlandyard.engine.impl.BMap;
import scotlandyard.engine.impl.Engine;
import scotlandyard.engine.spec.IGame;
import scotlandyard.engine.spec.IPlayer;
import scotlandyard.servlets.HttpServletEx;
/**
* shows the tickets for a player
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class player_tickets extends HttpServletEx {

  private static final long serialVersionUID = 187504394150478371L;
  private String xhash,game_id;
  public player_tickets() {super();}
  public player_tickets(String xhash,String gameId) {
    super();
    this.xhash = xhash;
    this.game_id = gameId;
  }

  private int pow2(int n){
    int result = 1;
    while(n>0){
      result*=2;
      n--;
    }
    return result;
  }
  @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");}

          final IPlayer player = game.getPlayer(xhash);
          if(player==null){throw new Exception("UNKNOWN Player");}

          sb.append("<div class='player_tickets'><table cellpadding='2' class='tokens'>");
          final StringBuffer sb1 = new StringBuffer();
          final StringBuffer sb2 = new StringBuffer();
          for(int i=0;i<5;i++){
              final String tr = BMap.convTransport(i);
              final String url = "http://scotlandyard.comule.com/get_icon.php?icon=";
              sb1.append("<td><img src='"+url+pow2(i)+"' alt='"+tr+"' title='"+tr+"' id='transport_"+i+"'/></td>");
              sb2.append("<th>"+player.getTickets(i)+"</th>");
          }
          sb.append("<tr>"+sb1.toString()+"</tr>");
          sb.append("<tr>"+sb2.toString()+"</tr>");
          sb.append("</table>");
      }catch(Exception e){
        sb.setLength(0);
        sb.append("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
      }
    return sb.toString();
  }
}
TOP

Related Classes of scotlandyard.servlets.players.player_tickets

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.