Package scotlandyard.servlets.games

Source Code of scotlandyard.servlets.games.create_game

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;

/**
* creates new game and add it to the list of games in the game engine
* @author Hussain Al-Mutawa
* @version 3.0
*/
public class create_game extends HttpServletEx {

  private static final long serialVersionUID = 3214023844784335889L;

  @Override
  public void setHeader(HttpServletResponse response) {
    response.setHeader("Content-Type", "text/plain");
   
  }

  @Override
  public String processRequest(Map<String, String> parameters, String sid)
      throws ServletException {
    try{
      String game_id = parameters.get("game");
      final String map_path = "/maps/"+parameters.get("selected_map");
      final String mp = this.getServletContext().getRealPath(map_path);
      game_id =  parameters.get("selected_map").replace(".xml", "") ;//(game_id==null||"optional".equals(game_id)?System.currentTimeMillis()+"":game_id);
      int counter=0;
      for(final IGame game:Engine.instance().games.values()){
        counter+=(game.getMapPath().equals(map_path)?1:0);
      }
      counter++;
      final Game game = new Game(game_id+" ("+counter+")",mp);
     
      Engine.instance().games.put(game.getId(),game);
        game.setMapPath(map_path);
       
        return "{\"msg\":\"OK\"}";
       
    }catch(Exception e){
      return ("{\"msg\" : \"EXCEPTION : "+(e.getMessage()+"").replace("\"", "'")+"\", \"className\" : \""+getClass().getName()+"\"}");
    }

  }
    

}
TOP

Related Classes of scotlandyard.servlets.games.create_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.