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