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