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