Package scotlandyard.servlets.games

Source Code of scotlandyard.servlets.games.leave_game

package scotlandyard.servlets.games;

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.IUser;
/**
* can be used to remove a player from a game that has not started already
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class leave_game extends HttpServlet {

  private static final long serialVersionUID = 5272990315728466636L;
  private String game_id ;
  private String hash ;
  public leave_game() {
    super();
  }
  public leave_game(String game_id,String hash) {
    super();
    this.game_id=game_id;
    this.hash=hash;
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final PrintWriter out = response.getWriter();
    try{
      response.setHeader("Content-Type", "text/plain");
      if(request.getParameter("game")!=null){
        game_id   = request.getParameter("game");
      }
     
      if(request.getParameter("xhash")!=null){
        hash = request.getParameter("xhash");
      }

      if(hash==null||"".equals(hash)){
        throw new Exception("Unknown HASH");
      }
     
      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("Unknown Game");
      }

        final IUser user = Engine.instance().getUser(hash);

        if(user==null){
          throw new Exception("Unknown USER");
        }
        if(game.getStatus()!=IGame.NEW){
          throw new Exception("the game has already started.");
        }
        game.removePlayer("{\"msg\" : \""+user.getEmail()+"\"}");
       
      }catch(Exception e){
        out.print("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
      }
  }

}
TOP

Related Classes of scotlandyard.servlets.games.leave_game

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.