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