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