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.Decision;
16  import pl.agh.iosr.ballamigos.common.Match;
17  import pl.agh.iosr.ballamigos.common.MatchState;
18  import pl.agh.iosr.ballamigos.webapp.core.WebApplicationCommunicator;
19  
20  /***
21   * @author wrobel
22   * Servlet that is used by client to send his move to server and 
23   * receive oponent's move.
24   */
25  public class MakeDecisionServlet extends HttpServlet {
26  	private ServletContext context;
27  	 
28      public void init(ServletConfig config) throws ServletException {
29      	this.context = config.getServletContext();
30      }
31  
32      /***
33       * Waits until message describing oponent's move arrives from
34       * server. 
35       * @param current client communicator
36       * @return current match state
37       */
38      private Match waitForMove(WebApplicationCommunicator wac)
39      {	
40      	synchronized(wac){
41      		while (!wac.matchReceived()){
42      			try{
43      				wac.wait();
44      			}catch (InterruptedException ex) {}
45      		}
46      		return wac.getMatch();
47      	}
48      }
49      
50      /***
51       * Sends move result to JavaScript client
52       * @param wac current client communicator
53       * @return string describing move. Refer to JavaScript source code
54       * for description of this string syntax.
55       */
56      private String receiveMove(WebApplicationCommunicator wac)
57      {
58      	String result = "";
59          MatchState state;
60          int goal = 0;
61          //niebieski - host - ten ktory utworzyl gre, bramki ma po lewej
62              
63          try{
64          		Match m = waitForMove(wac);
65          		result +=  m.getMovesNumber();
66          		for(int k = 0; k < m.getMovesNumber(); ++k){        			
67          			state = m.getMove(k);
68          	 		int ballx = (int)state.getBall().getX();
69          			int bally = (int)state.getBall().getY();
70          			
71          			if (state.getScored() == MatchState.BLUE){
72          				if (m.getHost().equals(wac.getPlayerLogin()))
73          					goal = 1;
74          				else
75          					goal = 2;
76          			}
77          			else if (state.getScored() == MatchState.RED){
78          				if (m.getHost().equals(wac.getPlayerLogin()))
79          					goal = 2;
80          				else
81          					goal = 1;
82          			}
83          			
84          			if (m.getHost().equals(wac.getPlayerLogin())){
85  		        		for(int i = 0; i < 3; i++){
86  		        			result += " " + (int)state.getBlue()[i].getX();
87  		        			result += " " + (int)state.getBlue()[i].getY();
88  		        			if (state.getBlue()[i].hasBall()){
89  		        				ballx = (int)state.getBlue()[i].getX();
90  		        				bally = (int)state.getBlue()[i].getY();
91  		        			}
92  		        		}
93  		        		
94  		        		for(int i = 0; i < 3; i++){
95  		        			result += " " + (int)state.getRed()[i].getX();
96  		        			result += " " + (int)state.getRed()[i].getY();
97  		        			if (state.getRed()[i].hasBall()){
98  		        				ballx = (int)state.getRed()[i].getX();
99  		        				bally = (int)state.getRed()[i].getY();
100 		        			}
101 		        		}
102         			}
103         			else{
104 		        		for(int i = 0; i < 3; i++){
105 		        			result += " " + (int)state.getRed()[i].getX();
106 		        			result += " " + (int)state.getRed()[i].getY();
107 		        		}
108 		        		
109         				for(int i = 0; i < 3; i++){
110 		        			result += " " + (int)state.getBlue()[i].getX();
111 		        			result += " " + (int)state.getBlue()[i].getY();
112 		        			if (state.getBlue()[i].hasBall()){
113 		        				ballx = (int)state.getBlue()[i].getX();
114 		        				bally = (int)state.getBlue()[i].getY();
115 		        			}
116 		        		}
117         			}
118         			
119 	        		result += " " + ballx;
120 	        		result += " " + bally;
121         		}		
122         		
123         		return "^" + goal +" "+ result;
124         }catch(Exception ex){
125         	return "Excpetion: " + ex.toString();
126         }
127     }
128     
129     /***
130      * Method used by client to send her moves.
131      */
132     public void doPost(HttpServletRequest request, HttpServletResponse  response)
133         throws IOException, ServletException {
134     	
135         response.setContentType("text/xml");
136         response.setHeader("Cache-Control", "no-cache");
137         
138         try{
139         	WebApplicationCommunicator wac = Helper.getClientCommunicator(request);
140         	Decision d = new Decision();	
141         	
142         	{
143         		String vx1 =  request.getParameter("vx0");
144         		String vy1 = request.getParameter("vy0");
145         		String type1 = request.getParameter("type0");
146         		if (vx1 == null)
147         			throw new Exception("vx0 not set");
148         		if (vy1 == null)
149         			throw new Exception("vy0 not set");
150         		if (type1 == null)
151         			throw new Exception("type0 not set");
152         		d.setVx1(Integer.parseInt(vx1));
153         		d.setVy1(Integer.parseInt(vy1));
154         		d.setType1(Integer.parseInt(type1));
155         	}
156         	
157         	{
158         		String vx2 = request.getParameter("vx1");
159         		String vy2 = request.getParameter("vy1");
160         		String type2 = request.getParameter("type1");
161         		if (vx2 == null)
162         			throw new Exception("vx1 not set");
163         		if (vy2 == null)
164         			throw new Exception("vy1 not set");
165         		if (type2 == null)
166         			throw new Exception("type1 not set");
167         		d.setVx2(Integer.parseInt(vx2));
168         		d.setVy2(Integer.parseInt(vy2));
169         		d.setType2(Integer.parseInt(type2));
170         	}
171         	
172         	{
173         		String vx3 = request.getParameter("vx2");
174         		String vy3 = request.getParameter("vy2");
175         		String type3 = request.getParameter("type2");
176         		if (vx3 == null)
177         			throw new Exception("vx2 not set");
178         		if (vy3 == null)
179         			throw new Exception("vy2 not set");
180         		if (type3 == null)
181         			throw new Exception("type2 not set");
182         		d.setVx3(Integer.parseInt(vx3));
183         		d.setVy3(Integer.parseInt(vy3));
184         		d.setType3(Integer.parseInt(type3));
185         	}
186         	
187         	wac.sendDecision(d);
188         	response.getWriter().write(receiveMove(wac));
189         }catch(Exception ex){
190         	response.getWriter().write("Excpetion: " + ex.toString());
191         }
192     }
193 }