Package scotlandyard.servlets.players

Source Code of scotlandyard.servlets.players.current_position_json

package scotlandyard.servlets.players;

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.IPlayer;

/**
* gets the current position of a player
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class current_position_json extends HttpServlet {

  private static final long serialVersionUID = 8654244919922480995L;
  private String gameId,xhash;
  public current_position_json() {
    super();
  }
  public current_position_json(String gameId,String xhash) {
    super();
    this.gameId=gameId;
    this.xhash=xhash;
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final PrintWriter out = response.getWriter();
    response.setHeader("Content-Type", "text/plain");
    try{
      if(request.getParameter("xhash")!=null){
        xhash = request.getParameter("xhash");
      }
      if(xhash==null || "".equals(xhash)){throw new Exception("Unknown HASH");}

      if(request.getParameter("selected_game")!=null){
        gameId  = request.getParameter("selected_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 player = game.getPlayer(xhash);
          if(player==null){throw new Exception("player is unknown");}

          out.print("{\"msg\" : \""+player.getPosition()+"\"}");

      }catch(Exception e){
          out.print("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
      }
  }

}
TOP

Related Classes of scotlandyard.servlets.players.current_position_json

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.