1 | #include <cstdlib>
|
---|
2 | #include <cstdio>
|
---|
3 | #include <unistd.h>
|
---|
4 | #include <string>
|
---|
5 | #include <iostream>
|
---|
6 | #include <sstream>
|
---|
7 | #include <fstream>
|
---|
8 | #include <cstring>
|
---|
9 | #include <cmath>
|
---|
10 |
|
---|
11 | #include <vector>
|
---|
12 | #include <map>
|
---|
13 |
|
---|
14 | #include <csignal>
|
---|
15 |
|
---|
16 | #include <sys/time.h>
|
---|
17 |
|
---|
18 | #include <sys/socket.h>
|
---|
19 | #include <netdb.h>
|
---|
20 | #include <netinet/in.h>
|
---|
21 | #include <arpa/inet.h>
|
---|
22 |
|
---|
23 | #include <crypt.h>
|
---|
24 |
|
---|
25 | /*
|
---|
26 | #include <openssl/bio.h>
|
---|
27 | #include <openssl/ssl.h>
|
---|
28 | #include <openssl/err.h>
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include "../common/Compiler.h"
|
---|
32 | #include "../common/Common.h"
|
---|
33 | #include "../common/MessageProcessor.h"
|
---|
34 | #include "../common/WorldMap.h"
|
---|
35 | #include "../common/Player.h"
|
---|
36 | #include "../common/Projectile.h"
|
---|
37 | #include "../common/Game.h"
|
---|
38 | #include "../common/GameSummary.h"
|
---|
39 |
|
---|
40 | #include "DataAccess.h"
|
---|
41 |
|
---|
42 | using namespace std;
|
---|
43 |
|
---|
44 | // from used to be const. Removed that so I could take a reference
|
---|
45 | // and use it to send messages
|
---|
46 | void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId);
|
---|
47 |
|
---|
48 | bool handleGameEvents(Game* game, map<unsigned int, Player*>& mapPlayers, MessageProcessor& msgPocessor);
|
---|
49 | bool handlePlayerEvents(Player* p, Game* game, MessageProcessor& msgProcessor);
|
---|
50 |
|
---|
51 | void broadcastMessage(MessageProcessor &msgProcessor, NETWORK_MSG &serverMsg, map<unsigned int, Player*>& players);
|
---|
52 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
|
---|
53 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
|
---|
54 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
|
---|
55 | void damagePlayer(Player *p, int damage);
|
---|
56 |
|
---|
57 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor);
|
---|
58 |
|
---|
59 | void quit(int sig);
|
---|
60 |
|
---|
61 | bool done;
|
---|
62 |
|
---|
63 | int main(int argc, char *argv[])
|
---|
64 | {
|
---|
65 | int sock, length;
|
---|
66 | struct sockaddr_in server;
|
---|
67 | struct sockaddr_in from; // info of client sending the message
|
---|
68 | NETWORK_MSG clientMsg, serverMsg;
|
---|
69 | MessageProcessor msgProcessor;
|
---|
70 | map<unsigned int, Player*> mapPlayers;
|
---|
71 | map<unsigned int, Projectile> mapProjectiles;
|
---|
72 | map<string, Game*> mapGames;
|
---|
73 | unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
|
---|
74 | ofstream outputLog;
|
---|
75 |
|
---|
76 | done = false;
|
---|
77 |
|
---|
78 | signal(SIGINT, quit);
|
---|
79 |
|
---|
80 | //SSL_load_error_strings();
|
---|
81 | //ERR_load_BIO_strings();
|
---|
82 | //OpenSSL_add_all_algorithms();
|
---|
83 |
|
---|
84 | if (argc != 2)
|
---|
85 | {
|
---|
86 | cerr << "ERROR, expected server [domain] [port]" << endl;
|
---|
87 | exit(1);
|
---|
88 | }
|
---|
89 |
|
---|
90 | outputLog.open("server.log", ios::app);
|
---|
91 | outputLog << "Started server on " << getCurrentDateTimeString() << endl;
|
---|
92 |
|
---|
93 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
94 | if (sock < 0)
|
---|
95 | error("Opening socket");
|
---|
96 | length = sizeof(server);
|
---|
97 | bzero(&server,length);
|
---|
98 | server.sin_family=AF_INET;
|
---|
99 | server.sin_port=htons(atoi(argv[1]));
|
---|
100 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
101 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
102 | error("binding");
|
---|
103 |
|
---|
104 | set_nonblock(sock);
|
---|
105 |
|
---|
106 | msgProcessor = MessageProcessor(sock, &outputLog);
|
---|
107 |
|
---|
108 | timespec ts;
|
---|
109 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
---|
110 | while (!done)
|
---|
111 | {
|
---|
112 | usleep(5000);
|
---|
113 |
|
---|
114 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
115 | // make the number smaller so millis can fit in an int
|
---|
116 | ts.tv_sec -= 1368000000;
|
---|
117 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
118 |
|
---|
119 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
|
---|
120 | {
|
---|
121 | timeLastUpdated = curTime;
|
---|
122 |
|
---|
123 | msgProcessor.cleanAckedMessages();
|
---|
124 | msgProcessor.resendUnackedMessages();
|
---|
125 |
|
---|
126 | map<unsigned int, Player*>::iterator it;
|
---|
127 |
|
---|
128 | cout << "Updating player targets and respawning dead players" << endl;
|
---|
129 |
|
---|
130 | // set targets for all chasing players (or make them attack if they're close enough)
|
---|
131 | // this should be moved into the games loop
|
---|
132 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
133 | {
|
---|
134 | Player* p = it->second;
|
---|
135 |
|
---|
136 | // check if it's time to revive dead players
|
---|
137 | if (p->isDead)
|
---|
138 | {
|
---|
139 | cout << "Player is dead" << endl;
|
---|
140 |
|
---|
141 | if (getCurrentMillis() - p->timeDied >= 10000)
|
---|
142 | {
|
---|
143 | p->isDead = false;
|
---|
144 |
|
---|
145 | POSITION spawnPos;
|
---|
146 |
|
---|
147 | switch (p->team)
|
---|
148 | {
|
---|
149 | case 0:// blue team
|
---|
150 | spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
151 | break;
|
---|
152 | case 1:// red team
|
---|
153 | spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | // should never go here
|
---|
157 | cout << "Error: Invalid team" << endl;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // spawn the player to the right of their flag location
|
---|
162 | spawnPos.x = (spawnPos.x+1) * 25 + 12;
|
---|
163 | spawnPos.y = spawnPos.y * 25 + 12;
|
---|
164 |
|
---|
165 | p->pos = spawnPos.toFloat();
|
---|
166 | p->target = spawnPos;
|
---|
167 | p->health = p->maxHealth;
|
---|
168 |
|
---|
169 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
170 | p->serialize(serverMsg.buffer);
|
---|
171 |
|
---|
172 | broadcastMessage(msgProcessor, serverMsg, p->currentGame->getPlayers());
|
---|
173 | }
|
---|
174 |
|
---|
175 | continue;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (p->currentGame != NULL) {
|
---|
179 | map<unsigned int, Player*> playersInGame = p->currentGame->getPlayers();
|
---|
180 | if (p->updateTarget(playersInGame))
|
---|
181 | {
|
---|
182 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
183 | p->serialize(serverMsg.buffer);
|
---|
184 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | cout << "Processing players in a game" << endl;
|
---|
190 |
|
---|
191 | // process players currently in a game
|
---|
192 | map<string, Game*>::iterator itGames;
|
---|
193 | Game* game = NULL;
|
---|
194 |
|
---|
195 | for (itGames = mapGames.begin(); itGames != mapGames.end();) {
|
---|
196 | if (handleGameEvents(itGames->second, mapPlayers, msgProcessor)) {
|
---|
197 | delete itGames->second;
|
---|
198 | mapGames.erase(itGames++);
|
---|
199 | }else
|
---|
200 | itGames++;
|
---|
201 | }
|
---|
202 |
|
---|
203 | cout << "Processing projectiles" << endl;
|
---|
204 |
|
---|
205 | // move all projectiles
|
---|
206 | // see if this can be moved inside the game class
|
---|
207 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
208 | map<unsigned int, Projectile>::iterator itProj;
|
---|
209 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
210 | game = itGames->second;
|
---|
211 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
212 | {
|
---|
213 | cout << "About to call projectile move" << endl;
|
---|
214 | if (itProj->second.move(game->getPlayers()))
|
---|
215 | {
|
---|
216 | // send a REMOVE_PROJECTILE message
|
---|
217 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
218 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
219 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
220 | game->removeProjectile(itProj->second.id);
|
---|
221 | broadcastMessage(msgProcessor, serverMsg, game->getPlayers());
|
---|
222 |
|
---|
223 | Player* target = game->getPlayers()[itProj->second.target];
|
---|
224 | damagePlayer(target, itProj->second.damage);
|
---|
225 |
|
---|
226 | if (target->isDead)
|
---|
227 | {
|
---|
228 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
229 | if (target->hasBlueFlag)
|
---|
230 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
231 | else if (target->hasRedFlag)
|
---|
232 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
233 |
|
---|
234 | if (flagType != WorldMap::OBJECT_NONE)
|
---|
235 | addObjectToMap(flagType, target->pos.x, target->pos.y, game->getMap(), game->getPlayers(), msgProcessor);
|
---|
236 | }
|
---|
237 |
|
---|
238 | // send a PLAYER message after dealing damage
|
---|
239 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
240 | target->serialize(serverMsg.buffer);
|
---|
241 | broadcastMessage(msgProcessor, serverMsg, game->getPlayers());
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
|
---|
248 | {
|
---|
249 | processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, unusedPlayerId);
|
---|
250 |
|
---|
251 | cout << "Finished processing the message" << endl;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
256 | outputLog.close();
|
---|
257 |
|
---|
258 | // delete all games
|
---|
259 | map<string, Game*>::iterator itGames;
|
---|
260 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
261 | {
|
---|
262 | delete itGames->second;
|
---|
263 | }
|
---|
264 |
|
---|
265 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
266 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
267 | {
|
---|
268 | delete itPlayers->second;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId)
|
---|
275 | {
|
---|
276 | NETWORK_MSG serverMsg;
|
---|
277 | DataAccess da;
|
---|
278 |
|
---|
279 | cout << "Inside processMessage" << endl;
|
---|
280 |
|
---|
281 | cout << "Received message" << endl;
|
---|
282 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
283 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
284 |
|
---|
285 | // Check that if an invalid message is sent, the client will correctly
|
---|
286 | // receive and display the response. Maybe make a special error msg type
|
---|
287 | switch(clientMsg.type)
|
---|
288 | {
|
---|
289 | case MSG_TYPE_REGISTER:
|
---|
290 | {
|
---|
291 | string username(clientMsg.buffer);
|
---|
292 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
293 | Player::PlayerClass playerClass;
|
---|
294 |
|
---|
295 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
296 |
|
---|
297 | cout << "username: " << username << endl;
|
---|
298 | cout << "password: " << password << endl;
|
---|
299 |
|
---|
300 | bool validClass = false;
|
---|
301 |
|
---|
302 | switch(playerClass) {
|
---|
303 | case Player::CLASS_WARRIOR:
|
---|
304 | case Player::CLASS_RANGER:
|
---|
305 | validClass = true;
|
---|
306 | break;
|
---|
307 | default:
|
---|
308 | validClass = false;
|
---|
309 | break;
|
---|
310 | }
|
---|
311 |
|
---|
312 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
313 |
|
---|
314 | if (validClass) {
|
---|
315 | int error = da.insertPlayer(username, password, playerClass);
|
---|
316 |
|
---|
317 | if (error)
|
---|
318 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
319 | else
|
---|
320 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
321 | }else
|
---|
322 | strcpy(serverMsg.buffer, "You didn't select a class");
|
---|
323 |
|
---|
324 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
325 |
|
---|
326 | break;
|
---|
327 | }
|
---|
328 | case MSG_TYPE_LOGIN:
|
---|
329 | {
|
---|
330 | cout << "Got login message" << endl;
|
---|
331 |
|
---|
332 | string username(clientMsg.buffer);
|
---|
333 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
334 |
|
---|
335 | Player* p = da.getPlayer(username);
|
---|
336 |
|
---|
337 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
338 | {
|
---|
339 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
340 | if (p != NULL)
|
---|
341 | delete(p);
|
---|
342 | }
|
---|
343 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
344 | {
|
---|
345 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
346 | delete(p);
|
---|
347 | }
|
---|
348 | else
|
---|
349 | {
|
---|
350 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
351 | p->id = unusedPlayerId;
|
---|
352 | cout << "new player id: " << p->id << endl;
|
---|
353 | p->setAddr(from);
|
---|
354 | p->currentGame = NULL;
|
---|
355 |
|
---|
356 | // choose a random team (either 0 or 1)
|
---|
357 | p->team = rand() % 2;
|
---|
358 |
|
---|
359 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
360 | // tell the new player about all the existing players
|
---|
361 | cout << "Sending other players to new player" << endl;
|
---|
362 |
|
---|
363 | map<unsigned int, Player*>::iterator it;
|
---|
364 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
365 | {
|
---|
366 | it->second->serialize(serverMsg.buffer);
|
---|
367 |
|
---|
368 | cout << "sending info about " << it->second->name << endl;
|
---|
369 | cout << "sending id " << it->second->id << endl;
|
---|
370 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
371 | }
|
---|
372 |
|
---|
373 | // send info about existing games to new player
|
---|
374 | map<string, Game*>::iterator itGames;
|
---|
375 | Game* g;
|
---|
376 | int numPlayers;
|
---|
377 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
378 |
|
---|
379 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
380 | {
|
---|
381 | g = itGames->second;
|
---|
382 | numPlayers = g->getNumPlayers();
|
---|
383 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
384 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
385 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
386 | }
|
---|
387 |
|
---|
388 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
389 | p->serialize(serverMsg.buffer);
|
---|
390 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
391 |
|
---|
392 | mapPlayers[unusedPlayerId] = p;
|
---|
393 | }
|
---|
394 |
|
---|
395 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
396 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
397 |
|
---|
398 | break;
|
---|
399 | }
|
---|
400 | case MSG_TYPE_LOGOUT:
|
---|
401 | {
|
---|
402 | string name(clientMsg.buffer);
|
---|
403 | cout << "Player logging out: " << name << endl;
|
---|
404 |
|
---|
405 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
406 |
|
---|
407 | if (p == NULL)
|
---|
408 | {
|
---|
409 | strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
410 | cout << "Player not logged in" << endl;
|
---|
411 | }
|
---|
412 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
413 | p->addr.sin_port != from.sin_port )
|
---|
414 | {
|
---|
415 | strcpy(serverMsg.buffer+4, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
---|
416 | cout << "Player logged in using a different connection" << endl;
|
---|
417 | }
|
---|
418 | else
|
---|
419 | {
|
---|
420 | // broadcast to all players before deleting p from the map
|
---|
421 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
422 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
423 |
|
---|
424 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
425 |
|
---|
426 | if (p->id < unusedPlayerId)
|
---|
427 | unusedPlayerId = p->id;
|
---|
428 |
|
---|
429 | mapPlayers.erase(p->id);
|
---|
430 | delete p;
|
---|
431 |
|
---|
432 | strcpy(serverMsg.buffer+4, "You have successfully logged out.");
|
---|
433 | }
|
---|
434 |
|
---|
435 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
436 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
437 |
|
---|
438 | break;
|
---|
439 | }
|
---|
440 | case MSG_TYPE_CHAT:
|
---|
441 | {
|
---|
442 | cout << "Got a chat message" << endl;
|
---|
443 |
|
---|
444 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
445 |
|
---|
446 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
447 |
|
---|
448 | if (p == NULL)
|
---|
449 | {
|
---|
450 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
451 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
452 | }
|
---|
453 | else
|
---|
454 | {
|
---|
455 | ostringstream oss;
|
---|
456 | oss << p->name << ": " << clientMsg.buffer;
|
---|
457 |
|
---|
458 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
459 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
460 | }
|
---|
461 |
|
---|
462 | break;
|
---|
463 | }
|
---|
464 | case MSG_TYPE_PLAYER_MOVE:
|
---|
465 | {
|
---|
466 | cout << "PLAYER_MOVE" << endl;
|
---|
467 |
|
---|
468 | int id, x, y;
|
---|
469 |
|
---|
470 | memcpy(&id, clientMsg.buffer, 4);
|
---|
471 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
472 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
473 |
|
---|
474 | cout << "x: " << x << endl;
|
---|
475 | cout << "y: " << y << endl;
|
---|
476 | cout << "id: " << id << endl;
|
---|
477 |
|
---|
478 | Player* p = mapPlayers[id];
|
---|
479 | bool validMessage = false;
|
---|
480 |
|
---|
481 | if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
482 | p->addr.sin_port == from.sin_port )
|
---|
483 | {
|
---|
484 | if (p->currentGame->startPlayerMovement(id, x, y)) {
|
---|
485 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
486 |
|
---|
487 | memcpy(serverMsg.buffer, &id, 4);
|
---|
488 | memcpy(serverMsg.buffer+4, &p->target.x, 4);
|
---|
489 | memcpy(serverMsg.buffer+8, &p->target.y, 4);
|
---|
490 |
|
---|
491 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
492 |
|
---|
493 | validMessage = true;
|
---|
494 | }
|
---|
495 | else
|
---|
496 | cout << "Bad terrain detected" << endl;
|
---|
497 | }
|
---|
498 | else
|
---|
499 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
500 |
|
---|
501 | if (!validMessage)
|
---|
502 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
503 |
|
---|
504 | break;
|
---|
505 | }
|
---|
506 | case MSG_TYPE_PICKUP_FLAG:
|
---|
507 | {
|
---|
508 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
509 | cout << "PICKUP_FLAG" << endl;
|
---|
510 |
|
---|
511 | int id;
|
---|
512 |
|
---|
513 | memcpy(&id, clientMsg.buffer, 4);
|
---|
514 | cout << "id: " << id << endl;
|
---|
515 |
|
---|
516 | Player* p = mapPlayers[id];
|
---|
517 | int objectId = p->currentGame->processFlagPickupRequest(p);
|
---|
518 |
|
---|
519 | if (objectId >= 0) {
|
---|
520 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
521 |
|
---|
522 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
523 | memcpy(serverMsg.buffer, &objectId, 4);
|
---|
524 | broadcastMessage(msgProcessor, serverMsg, players);
|
---|
525 |
|
---|
526 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
527 | p->serialize(serverMsg.buffer);
|
---|
528 | broadcastMessage(msgProcessor, serverMsg, players);
|
---|
529 | }
|
---|
530 |
|
---|
531 | break;
|
---|
532 | }
|
---|
533 | case MSG_TYPE_DROP_FLAG:
|
---|
534 | {
|
---|
535 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
536 | cout << "DROP_FLAG" << endl;
|
---|
537 |
|
---|
538 | int id;
|
---|
539 |
|
---|
540 | memcpy(&id, clientMsg.buffer, 4);
|
---|
541 | cout << "id: " << id << endl;
|
---|
542 |
|
---|
543 | Player* p = mapPlayers[id];
|
---|
544 |
|
---|
545 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
546 | if (p->hasBlueFlag)
|
---|
547 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
548 | else if (p->hasRedFlag)
|
---|
549 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
550 |
|
---|
551 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
552 |
|
---|
553 | addObjectToMap(flagType, p->pos.x, p->pos.y, p->currentGame->getMap(), players, msgProcessor);
|
---|
554 |
|
---|
555 | p->hasBlueFlag = false;
|
---|
556 | p->hasRedFlag = false;
|
---|
557 |
|
---|
558 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
559 | p->serialize(serverMsg.buffer);
|
---|
560 | broadcastMessage(msgProcessor, serverMsg, players);
|
---|
561 |
|
---|
562 | break;
|
---|
563 | }
|
---|
564 | case MSG_TYPE_ATTACK:
|
---|
565 | {
|
---|
566 | cout << "Received a START_ATTACK message" << endl;
|
---|
567 |
|
---|
568 | int id, targetId;
|
---|
569 |
|
---|
570 | memcpy(&id, clientMsg.buffer, 4);
|
---|
571 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
572 |
|
---|
573 | // need to make sure the target is in the sender's game
|
---|
574 |
|
---|
575 | Player* p = mapPlayers[id];
|
---|
576 | p->targetPlayer = targetId;
|
---|
577 | p->isChasing = true;
|
---|
578 |
|
---|
579 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
580 |
|
---|
581 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
582 | memcpy(serverMsg.buffer, &id, 4);
|
---|
583 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
584 | broadcastMessage(msgProcessor, serverMsg, players);
|
---|
585 |
|
---|
586 | break;
|
---|
587 | }
|
---|
588 | case MSG_TYPE_CREATE_GAME:
|
---|
589 | {
|
---|
590 | cout << "Received a CREATE_GAME message" << endl;
|
---|
591 |
|
---|
592 | string gameName(clientMsg.buffer);
|
---|
593 | cout << "Game name: " << gameName << endl;
|
---|
594 |
|
---|
595 | // check if this game already exists
|
---|
596 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
597 | cout << "Error: Game already exists" << endl;
|
---|
598 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
599 | }else {
|
---|
600 | Game* g = new Game(gameName, "../data/map.txt");
|
---|
601 | mapGames[gameName] = g;
|
---|
602 |
|
---|
603 | // add flag objects to the map
|
---|
604 | WorldMap* m = g->getMap();
|
---|
605 | for (int y=0; y<m->height; y++) {
|
---|
606 | for (int x=0; x<m->width; x++) {
|
---|
607 | switch (m->getStructure(x, y)) {
|
---|
608 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
609 | m->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
610 | break;
|
---|
611 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
612 | m->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
613 | break;
|
---|
614 | }
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
619 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
620 | }
|
---|
621 |
|
---|
622 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
623 |
|
---|
624 | break;
|
---|
625 | }
|
---|
626 | case MSG_TYPE_JOIN_GAME:
|
---|
627 | {
|
---|
628 | cout << "Received a JOIN_GAME message" << endl;
|
---|
629 |
|
---|
630 | string gameName(clientMsg.buffer);
|
---|
631 | cout << "Game name: " << gameName << endl;
|
---|
632 |
|
---|
633 | // check if this game already exists
|
---|
634 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
635 | cout << "Error: Game does not exist" << endl;
|
---|
636 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
637 | }else {
|
---|
638 | Game* g = mapGames[gameName];
|
---|
639 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
640 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
641 |
|
---|
642 | if (players.find(p->id) != players.end()) {
|
---|
643 | cout << "Player " << p->name << " trying to join a game he's already in" << endl;
|
---|
644 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
645 | }else {
|
---|
646 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
647 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
652 |
|
---|
653 | break;
|
---|
654 | }
|
---|
655 | case MSG_TYPE_LEAVE_GAME:
|
---|
656 | {
|
---|
657 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
658 |
|
---|
659 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
660 | Game* g = p->currentGame;
|
---|
661 |
|
---|
662 | if (g == NULL) {
|
---|
663 | cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
|
---|
664 |
|
---|
665 | /// should send a response back, maybe a new message type is needed
|
---|
666 | // not sure what to do here
|
---|
667 | }else {
|
---|
668 | cout << "Game name: " << g->getName() << endl;
|
---|
669 |
|
---|
670 | if (!p->isDead) {
|
---|
671 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
672 | if (p->hasBlueFlag)
|
---|
673 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
674 | else if (p->hasRedFlag)
|
---|
675 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
676 |
|
---|
677 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
678 | addObjectToMap(flagType, p->pos.x, p->pos.y, g->getMap(), g->getPlayers(), msgProcessor);
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | p->currentGame = NULL;
|
---|
683 | g->removePlayer(p->id);
|
---|
684 |
|
---|
685 | serverMsg.type = MSG_TYPE_LEAVE_GAME;
|
---|
686 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
687 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
688 | broadcastMessage(msgProcessor, serverMsg, g->getPlayers());
|
---|
689 |
|
---|
690 | int numPlayers = g->getNumPlayers();
|
---|
691 |
|
---|
692 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
693 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
694 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
695 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
696 |
|
---|
697 | // if there are no more players in the game, remove it
|
---|
698 | if (numPlayers == 0) {
|
---|
699 | mapGames.erase(g->getName());
|
---|
700 | delete g;
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | break;
|
---|
705 | }
|
---|
706 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
707 | {
|
---|
708 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
709 |
|
---|
710 | string gameName(clientMsg.buffer);
|
---|
711 | cout << "Game name: " << gameName << endl;
|
---|
712 |
|
---|
713 | // check if this game already exists
|
---|
714 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
715 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
716 |
|
---|
717 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
718 | }
|
---|
719 |
|
---|
720 | Game* g = mapGames[gameName];
|
---|
721 |
|
---|
722 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
723 | p->team = rand() % 2; // choose a random team (either 0 or 1)
|
---|
724 | p->currentGame = g;
|
---|
725 |
|
---|
726 | // tell the new player about all map objects
|
---|
727 | // (currently just the flags)
|
---|
728 |
|
---|
729 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
730 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
731 | vector<WorldMap::Object>::iterator itObjects;
|
---|
732 | cout << "sending items" << endl;
|
---|
733 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
734 | itObjects->serialize(serverMsg.buffer);
|
---|
735 | cout << "sending item id " << itObjects->id << endl;
|
---|
736 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
737 | }
|
---|
738 |
|
---|
739 |
|
---|
740 | // send the current score
|
---|
741 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
742 |
|
---|
743 | int blueScore = g->getBlueScore();
|
---|
744 | int redScore = g->getRedScore();
|
---|
745 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
746 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
747 |
|
---|
748 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
749 |
|
---|
750 | // send info to other players
|
---|
751 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
752 | p->serialize(serverMsg.buffer);
|
---|
753 | cout << "Should be broadcasting the message" << endl;
|
---|
754 | broadcastMessage(msgProcessor, serverMsg, g->getPlayers());
|
---|
755 |
|
---|
756 | g->addPlayer(p);
|
---|
757 |
|
---|
758 |
|
---|
759 | // tell the new player about all the players in the game (including himself)
|
---|
760 | cout << "Sending other players to new player" << endl;
|
---|
761 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
762 |
|
---|
763 |
|
---|
764 | map<unsigned int, Player*>& allPlayers = g->getPlayers();
|
---|
765 | map<unsigned int, Player*>::iterator it;
|
---|
766 | for (it = allPlayers.begin(); it != allPlayers.end(); it++)
|
---|
767 | {
|
---|
768 | it->second->serialize(serverMsg.buffer);
|
---|
769 |
|
---|
770 | cout << "sending info about " << it->second->name << endl;
|
---|
771 | cout << "sending id " << it->second->id << endl;
|
---|
772 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
773 | }
|
---|
774 |
|
---|
775 | int numPlayers = g->getNumPlayers();
|
---|
776 |
|
---|
777 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
778 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
779 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
780 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
781 |
|
---|
782 | break;
|
---|
783 | }
|
---|
784 | default:
|
---|
785 | {
|
---|
786 | // probably want to log the error rather than sending a chat message,
|
---|
787 | // especially since chat isn't currently visible on all screens
|
---|
788 |
|
---|
789 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
790 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
791 |
|
---|
792 | break;
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | bool handleGameEvents(Game* game, map<unsigned int, Player*>& mapPlayers, MessageProcessor& msgProcessor) {
|
---|
798 | NETWORK_MSG serverMsg;
|
---|
799 | map<unsigned int, Player*>::iterator it;
|
---|
800 | bool gameFinished = false;
|
---|
801 |
|
---|
802 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
|
---|
803 | {
|
---|
804 | gameFinished = gameFinished ||
|
---|
805 | handlePlayerEvents(it->second, game, msgProcessor);
|
---|
806 | }
|
---|
807 |
|
---|
808 | // set each player's current game to null
|
---|
809 | if (gameFinished) {
|
---|
810 | // send a GAME_INFO message with 0 players to force clients to delete the game
|
---|
811 | int numPlayers = 0;
|
---|
812 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
813 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
814 | strcpy(serverMsg.buffer+4, game->getName().c_str());
|
---|
815 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
816 |
|
---|
817 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
|
---|
818 | {
|
---|
819 | it->second->currentGame = NULL;
|
---|
820 | }
|
---|
821 | }
|
---|
822 |
|
---|
823 | return gameFinished;
|
---|
824 | }
|
---|
825 |
|
---|
826 | bool handlePlayerEvents(Player* p, Game* game, MessageProcessor& msgProcessor) {
|
---|
827 | NETWORK_MSG serverMsg;
|
---|
828 | WorldMap* gameMap = game->getMap();
|
---|
829 | map<unsigned int, Player*>& playersInGame = game->getPlayers();
|
---|
830 | bool gameFinished = false;
|
---|
831 | FLOAT_POSITION oldPos;
|
---|
832 |
|
---|
833 | cout << "moving player" << endl;
|
---|
834 | bool broadcastMove = false;
|
---|
835 |
|
---|
836 | // move player and perform associated tasks
|
---|
837 | oldPos = p->pos;
|
---|
838 | if (p->move(gameMap)) {
|
---|
839 |
|
---|
840 | cout << "player moved" << endl;
|
---|
841 | if (game->processPlayerMovement(p, oldPos))
|
---|
842 | broadcastMove = true;
|
---|
843 | cout << "player move processed" << endl;
|
---|
844 |
|
---|
845 | WorldMap::ObjectType flagType;
|
---|
846 | POSITION pos;
|
---|
847 | bool flagTurnedIn = false;
|
---|
848 | bool flagReturned = false;
|
---|
849 | bool ownFlagAtBase = false;
|
---|
850 |
|
---|
851 | // need to figure out how to move this to a different file
|
---|
852 | // while still sending back flag type and position
|
---|
853 | switch(gameMap->getStructure(p->pos.x/25, p->pos.y/25))
|
---|
854 | {
|
---|
855 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
856 | {
|
---|
857 | if (p->team == 0 && p->hasRedFlag)
|
---|
858 | {
|
---|
859 | // check that your flag is at your base
|
---|
860 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
861 |
|
---|
862 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
863 | vector<WorldMap::Object>::iterator itObjects;
|
---|
864 |
|
---|
865 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
|
---|
866 | {
|
---|
867 | if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
|
---|
868 | {
|
---|
869 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
|
---|
870 | {
|
---|
871 | ownFlagAtBase = true;
|
---|
872 | break;
|
---|
873 | }
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 | if (ownFlagAtBase)
|
---|
878 | {
|
---|
879 | p->hasRedFlag = false;
|
---|
880 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
881 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
882 | flagTurnedIn = true;
|
---|
883 | game->setBlueScore(game->getBlueScore()+1);
|
---|
884 | }
|
---|
885 | }
|
---|
886 |
|
---|
887 | break;
|
---|
888 | }
|
---|
889 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
890 | {
|
---|
891 | if (p->team == 1 && p->hasBlueFlag)
|
---|
892 | {
|
---|
893 | // check that your flag is at your base
|
---|
894 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
895 |
|
---|
896 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
897 | vector<WorldMap::Object>::iterator itObjects;
|
---|
898 |
|
---|
899 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
|
---|
900 | {
|
---|
901 | if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
|
---|
902 | {
|
---|
903 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
|
---|
904 | {
|
---|
905 | ownFlagAtBase = true;
|
---|
906 | break;
|
---|
907 | }
|
---|
908 | }
|
---|
909 | }
|
---|
910 |
|
---|
911 | if (ownFlagAtBase)
|
---|
912 | {
|
---|
913 | p->hasBlueFlag = false;
|
---|
914 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
915 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
916 | flagTurnedIn = true;
|
---|
917 | game->setRedScore(game->getRedScore()+1);
|
---|
918 | }
|
---|
919 | }
|
---|
920 |
|
---|
921 | break;
|
---|
922 | }
|
---|
923 | }
|
---|
924 |
|
---|
925 | if (flagTurnedIn)
|
---|
926 | {
|
---|
927 | unsigned int blueScore = game->getBlueScore();
|
---|
928 | unsigned int redScore = game->getRedScore();
|
---|
929 |
|
---|
930 | // send an OBJECT message to add the flag back to its spawn point
|
---|
931 | pos.x = pos.x*25+12;
|
---|
932 | pos.y = pos.y*25+12;
|
---|
933 | gameMap->addObject(flagType, pos.x, pos.y);
|
---|
934 |
|
---|
935 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
936 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
937 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
938 |
|
---|
939 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
940 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
941 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
942 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
943 |
|
---|
944 | // check to see if the game should end
|
---|
945 | // move to its own method
|
---|
946 | if (game->getBlueScore() == 3 || game->getRedScore() == 3) {
|
---|
947 | gameFinished = true;
|
---|
948 |
|
---|
949 | unsigned int winningTeam;
|
---|
950 | if (game->getBlueScore() == 3)
|
---|
951 | winningTeam = 0;
|
---|
952 | else if (game->getRedScore() == 3)
|
---|
953 | winningTeam = 1;
|
---|
954 |
|
---|
955 | serverMsg.type = MSG_TYPE_FINISH_GAME;
|
---|
956 |
|
---|
957 | // I should create an instance of the GameSummary object here and just serialize it into this message
|
---|
958 | memcpy(serverMsg.buffer, &winningTeam, 4);
|
---|
959 | memcpy(serverMsg.buffer+4, &blueScore, 4);
|
---|
960 | memcpy(serverMsg.buffer+8, &redScore, 4);
|
---|
961 | strcpy(serverMsg.buffer+12, game->getName().c_str());
|
---|
962 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
963 | }
|
---|
964 |
|
---|
965 | // this means a PLAYER message will be sent
|
---|
966 | broadcastMove = true;
|
---|
967 | }
|
---|
968 |
|
---|
969 | // go through all objects and check if the player is close to one and if its their flag
|
---|
970 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
971 | vector<WorldMap::Object>::iterator itObjects;
|
---|
972 | POSITION structPos;
|
---|
973 |
|
---|
974 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
|
---|
975 | {
|
---|
976 | POSITION pos = itObjects->pos;
|
---|
977 |
|
---|
978 | if (posDistance(p->pos, pos.toFloat()) < 10)
|
---|
979 | {
|
---|
980 | if (p->team == 0 &&
|
---|
981 | itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
|
---|
982 | {
|
---|
983 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
984 | flagReturned = true;
|
---|
985 | break;
|
---|
986 | }
|
---|
987 | else if (p->team == 1 &&
|
---|
988 | itObjects->type == WorldMap::OBJECT_RED_FLAG)
|
---|
989 | {
|
---|
990 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
991 | flagReturned = true;
|
---|
992 | break;
|
---|
993 | }
|
---|
994 | }
|
---|
995 | }
|
---|
996 |
|
---|
997 | if (flagReturned)
|
---|
998 | {
|
---|
999 | itObjects->pos.x = structPos.x*25+12;
|
---|
1000 | itObjects->pos.y = structPos.y*25+12;
|
---|
1001 |
|
---|
1002 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
1003 | itObjects->serialize(serverMsg.buffer);
|
---|
1004 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | if (broadcastMove)
|
---|
1008 | {
|
---|
1009 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
1010 | p->serialize(serverMsg.buffer);
|
---|
1011 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | cout << "processing player attack" << endl;
|
---|
1016 |
|
---|
1017 | // check if the player's attack animation is complete
|
---|
1018 | if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis())
|
---|
1019 | {
|
---|
1020 | p->isAttacking = false;
|
---|
1021 | cout << "Attack animation is complete" << endl;
|
---|
1022 |
|
---|
1023 | //send everyone an ATTACK message
|
---|
1024 | cout << "about to broadcast attack" << endl;
|
---|
1025 |
|
---|
1026 | if (p->attackType == Player::ATTACK_MELEE)
|
---|
1027 | {
|
---|
1028 | cout << "Melee attack" << endl;
|
---|
1029 |
|
---|
1030 | Player* target = playersInGame[p->targetPlayer];
|
---|
1031 | damagePlayer(target, p->damage);
|
---|
1032 |
|
---|
1033 | if (target->isDead)
|
---|
1034 | {
|
---|
1035 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
1036 | if (target->hasBlueFlag)
|
---|
1037 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
1038 | else if (target->hasRedFlag)
|
---|
1039 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
1040 |
|
---|
1041 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
1042 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor);
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
1047 | target->serialize(serverMsg.buffer);
|
---|
1048 | }
|
---|
1049 | else if (p->attackType == Player::ATTACK_RANGED)
|
---|
1050 | {
|
---|
1051 | cout << "Ranged attack" << endl;
|
---|
1052 |
|
---|
1053 | Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
|
---|
1054 | game->assignProjectileId(&proj);
|
---|
1055 | game->addProjectile(proj);
|
---|
1056 |
|
---|
1057 | int x = p->pos.x;
|
---|
1058 | int y = p->pos.y;
|
---|
1059 |
|
---|
1060 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
1061 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
1062 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
1063 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
1064 | memcpy(serverMsg.buffer+12, &p->targetPlayer, 4);
|
---|
1065 | }
|
---|
1066 | else
|
---|
1067 | cout << "Invalid attack type: " << p->attackType << endl;
|
---|
1068 |
|
---|
1069 | broadcastMessage(msgProcessor, serverMsg, playersInGame);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | return gameFinished;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | void broadcastMessage(MessageProcessor &msgProcessor, NETWORK_MSG &serverMsg, map<unsigned int, Player*>& players) {
|
---|
1076 | map<unsigned int, Player*>::iterator it;
|
---|
1077 | for (it = players.begin(); it != players.end(); it++) {
|
---|
1078 | msgProcessor.sendMessage(&serverMsg, &(it->second->addr));
|
---|
1079 | }
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
|
---|
1083 | {
|
---|
1084 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
1085 | id++;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
|
---|
1089 | {
|
---|
1090 | map<unsigned int, Player*>::iterator it;
|
---|
1091 |
|
---|
1092 | for (it = m.begin(); it != m.end(); it++)
|
---|
1093 | {
|
---|
1094 | if ( it->second->name.compare(name) == 0 )
|
---|
1095 | return it->second;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | return NULL;
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
|
---|
1102 | {
|
---|
1103 | map<unsigned int, Player*>::iterator it;
|
---|
1104 |
|
---|
1105 | for (it = m.begin(); it != m.end(); it++)
|
---|
1106 | {
|
---|
1107 | if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
1108 | it->second->addr.sin_port == addr.sin_port )
|
---|
1109 | return it->second;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | return NULL;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | void damagePlayer(Player *p, int damage) {
|
---|
1116 | p->health -= damage;
|
---|
1117 | if (p->health < 0)
|
---|
1118 | p->health = 0;
|
---|
1119 | if (p->health == 0) {
|
---|
1120 | cout << "Player died" << endl;
|
---|
1121 | p->isDead = true;
|
---|
1122 | p->timeDied = getCurrentMillis();
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor) {
|
---|
1127 | NETWORK_MSG serverMsg;
|
---|
1128 |
|
---|
1129 | gameMap->addObject(objectType, x, y);
|
---|
1130 |
|
---|
1131 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
1132 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
1133 |
|
---|
1134 | broadcastMessage(msgProcessor, serverMsg, mapPlayers);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | void quit(int sig) {
|
---|
1138 | done = true;
|
---|
1139 | }
|
---|