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