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.GoalPosition;
16 import pl.agh.iosr.ballamigos.common.MatchProperties;
17 import pl.agh.iosr.ballamigos.common.Obstacle;
18 import pl.agh.iosr.ballamigos.webapp.core.WebApplicationCommunicator;
19
20 /***
21 * @author wrobel
22 * Servlet used by client to send arena to server and to wait for oponent to
23 * join a game.
24 */
25 public class NewArenaServlet extends HttpServlet {
26 private ServletContext context;
27
28 public void init(ServletConfig config) throws ServletException {
29 this.context = config.getServletContext();
30 }
31
32 /***
33 * Parse arena description from JavaScript client and send it to rmiserver.
34 * Return only on error or when oponent joins game.
35 */
36 public void doPost(HttpServletRequest request, HttpServletResponse response)
37 throws IOException, ServletException {
38
39 response.setContentType("text/xml");
40 response.setHeader("Cache-Control", "no-cache");
41
42 String roomName = request.getParameter("roomName");
43
44
45 if (roomName == null){
46 response.getWriter().write("roomName not set\n");
47 return;
48 }
49
50 String playerName = request.getParameter("playerName");
51 if (playerName == null){
52 response.getWriter().write("playerName not set\n");
53 return;
54 }
55
56 String playerPassword = request.getParameter("playerPassword");
57 if (playerPassword == null){
58 response.getWriter().write("playerPassword not set\n");
59 return;
60 }
61
62 MatchProperties match = new MatchProperties();
63 String s = request.getParameter("fieldWidth");
64 if (s == null){
65 response.getWriter().write("fieldWidth not set\n");
66 return;
67 }
68 match.setSizeX(Integer.parseInt(s));
69
70 s = request.getParameter("fieldHeight");
71 if (s == null){
72 response.getWriter().write("fieldHeight not set\n");
73 return;
74 }
75 match.setSizeY(Integer.parseInt(s));
76
77
78 s = request.getParameter("goalFromTop");
79 if (s == null){
80 response.getWriter().write("goalFromTop not set\n");
81 return;
82 }
83 int offset = Integer.parseInt(s);
84
85 s = request.getParameter("goalSize");
86 if (s == null){
87 response.getWriter().write("goalSize not set\n");
88 return;
89 }
90 GoalPosition goal = new GoalPosition(offset, Integer.parseInt(s));
91 match.setLeftGoalPosition(goal);
92
93 int bluePositions[][] = new int[3][2];
94
95 int redPositions[][] = new int[3][2];
96
97
98 for(int i=0;i<3;i++){
99 s = request.getParameter("player" + i + "x");
100 if (s == null){
101 response.getWriter().write("player" + i + "x" + "not set\n");
102 return;
103 }
104 int x = Integer.parseInt(s);
105
106 s = request.getParameter("player" + i + "y");
107 if (s == null){
108 response.getWriter().write("player" + i + "y" + "not set\n");
109 return;
110 }
111 int y = Integer.parseInt(s);
112
113 bluePositions[i][0] = x;
114 bluePositions[i][1] = y;
115
116 redPositions[i][0] = match.getSizeX() - x;
117 redPositions[i][1] = match.getSizeY() - y;
118
119 }
120 match.setBluePositions(bluePositions);
121
122
123 s = request.getParameter("amountOfObstacles");
124 if (s == null){
125 response.getWriter().write("amountOfObstacles not set");
126 return;
127 }
128
129 int amountOfObstacles = Integer.parseInt(s);
130 for(int i = 0; i < amountOfObstacles; i++){
131 Obstacle obstacle = new Obstacle(0, -1, -1, -1, -1);
132
133 s = request.getParameter("obstacleType" + i);
134 if (s == null){
135 response.getWriter().write("obstacleType"+i + "not set\n");
136 return;
137 }
138 int type = Integer.parseInt(s);
139 if (type == 1)
140 type = 3;
141 else if (type == 0)
142 type = 1;
143
144 obstacle.setObstacleType(type);
145
146 String x1 = request.getParameter("obstacle"+ i + "x1");
147 String x2 = request.getParameter("obstacle"+ i + "x2");
148 String y1 = request.getParameter("obstacle"+ i + "y1");
149 String y2 = request.getParameter("obstacle"+ i + "y2");
150
151 if (x1 == null){
152 response.getWriter().write("obstacle"+ i + "x1" + "not set\n");
153 return;
154 }
155 if (x2 == null){
156 response.getWriter().write("obstacle" + i + "x2" + "not set\n");
157 return;
158 }
159 if (y1 == null){
160 response.getWriter().write("obstacle" + i + "y1" + "not set\n");
161 return;
162 }
163 if (y2 == null){
164 response.getWriter().write("obstacle" + i + "y2" + "not set\n");
165 return;
166 }
167 int x1i = Integer.parseInt(x1);
168 int x2i = Integer.parseInt(x2);
169 int y1i = Integer.parseInt(y1);
170 int y2i = Integer.parseInt(y2);
171
172 obstacle.setPositionX(Math.min(x1i, x2i));
173 obstacle.setSizeX(Math.abs(x1i - x2i));
174 obstacle.setPositionY(Math.min(y1i, y2i));
175 obstacle.setSizeY(Math.abs(y1i - y2i));
176 match.addObstacle(obstacle);
177
178 }
179 try{
180 WebApplicationCommunicator wac = Helper.getClientCommunicator(request);
181 match.setMaxScore(5);
182
183 wac.changeMatchProperties(match);
184
185 waitForPlayer(wac);
186 Thread.sleep(2000);
187 wac.sendStartMatch();
188
189 }catch (Exception e){
190 response.getWriter().write("Exception: " + e.toString() +"\n" + e.getStackTrace());
191 return;
192 }
193 return;
194 }
195
196 /***
197 * Wait for oponent to join the server
198 * @param wac current client communicator
199 */
200 private void waitForPlayer(WebApplicationCommunicator wac)
201 {
202 synchronized(wac){
203 while(!wac.playerEntered()){
204
205 try{
206 wac.wait();
207 }catch (InterruptedException ex) {}
208 }
209 }
210 }
211 }