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