View Javadoc

1   /***
2    * 
3    */
4   package pl.agh.iosr.ballamigos.webapp.jscommunication;
5   
6   import java.io.IOException;
7   
8   import javax.servlet.ServletConfig;
9   import javax.servlet.ServletContext;
10  import javax.servlet.ServletException;
11  import javax.servlet.http.HttpServlet;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import pl.agh.iosr.ballamigos.common.MatchProperties;
16  import pl.agh.iosr.ballamigos.common.Obstacle;
17  import pl.agh.iosr.ballamigos.webapp.core.WebApplicationCommunicator;
18  
19  /***
20   * @author wrobel
21   * Servlet that is used by client to get arena for the game she is assigned to.
22   */
23  public class LoadArenaServlet extends HttpServlet {
24  	   	private ServletContext context;
25  	 
26  	    public void init(ServletConfig config) throws ServletException {
27  	    	this.context = config.getServletContext();
28  	    }
29  
30  	    public void doPost(HttpServletRequest request, HttpServletResponse  response)
31  	        throws IOException, ServletException {
32  	    	String saved;
33  	    	
34  	        response.setContentType("text/xml");
35  	        response.setHeader("Cache-Control", "no-cache");
36  	        
37  	        try{
38  	        	WebApplicationCommunicator wac = Helper.getClientCommunicator(request);
39  	        	MatchProperties properties = wac.getMatch().getProperties();
40  	        	/***
41  	        	 * format of the arena string is defined in JavaScript source code
42  	        	 */
43  		        saved = "^";
44  		        saved += "" + properties.getSizeX();
45  		        saved += " " + properties.getSizeY();
46  		        saved += " " + properties.getLeftGoalPosition().getOffsetFromCorner();
47  		        saved += " " + properties.getLeftGoalPosition().getSize();
48  		        saved += " " + properties.getBluePositionY(0) + " " + properties.getBluePositionX(0);
49  		        saved += " " + properties.getBluePositionY(1) + " " + properties.getBluePositionX(1);
50  		        saved += " " + properties.getBluePositionY(2) + " " + properties.getBluePositionX(2);		        
51  		        
52  		        int nobst = properties.getObstaclesNumber();
53  		        saved += " " + nobst; 
54  		        for (int i = 0; i < nobst; ++i){
55  		        	Obstacle o = properties.getObstacle(i);
56  		        	int type =  o.getObstacleType();
57  		        	if (type == 3)
58  		        		type = 1;
59  		        	else if (type == 1)
60  		        		type = 0;
61  		        	saved += " " + type;
62  		        	saved += " " + o.getPositionX();
63  		        	saved += " " + (o.getPositionX() + o.getSizeX());
64  		        	saved += " " + o.getPositionY();
65  		        	saved += " " + (o.getPositionY() + o.getSizeY());
66  		    		        
67  		        }
68  		        response.getWriter().write(saved);
69  	        
70  	        }catch(Exception ex){
71  	        	response.getWriter().write("Excpetion: " + ex.toString());
72  	        }
73  	        
74  	    }
75  }