source: network-game/server/server.cpp@ b73bc28

Last change on this file since b73bc28 was b73bc28, checked in by dportnoy <dmp1488@…>, 11 years ago

Turning in the opposing team's flag now works in individual games and returning your own should as well, but hasn't been tested

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