[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"
|
---|
[c9f6a1c] | 38 | #include "../common/GameSummary.h"
|
---|
[b53c6b3] | 39 |
|
---|
[36082e8] | 40 | #include "DataAccess.h"
|
---|
[d2b411a] | 41 |
|
---|
[e3535b3] | 42 | using namespace std;
|
---|
| 43 |
|
---|
[d05086b] | 44 | bool done;
|
---|
| 45 |
|
---|
[d211210] | 46 | // from used to be const. Removed that so I could take a reference
|
---|
| 47 | // and use it to send messages
|
---|
[95ffe57] | 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);
|
---|
[01d0d00] | 49 |
|
---|
[95ffe57] | 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);
|
---|
[c76134b] | 53 | void damagePlayer(Player *p, int damage);
|
---|
[8e540f4] | 54 |
|
---|
[95ffe57] | 55 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
|
---|
[411c1ae] | 56 |
|
---|
[73f75c1] | 57 | // this should probably go somewhere in the common folder
|
---|
[e3535b3] | 58 | void error(const char *msg)
|
---|
| 59 | {
|
---|
| 60 | perror(msg);
|
---|
| 61 | exit(0);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[d05086b] | 64 | void quit(int sig) {
|
---|
| 65 | done = true;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[e3535b3] | 68 | int main(int argc, char *argv[])
|
---|
| 69 | {
|
---|
| 70 | int sock, length, n;
|
---|
| 71 | struct sockaddr_in server;
|
---|
[3b1efcc] | 72 | struct sockaddr_in from; // info of client sending the message
|
---|
[e084950] | 73 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[9b5d30b] | 74 | MessageProcessor msgProcessor;
|
---|
[95ffe57] | 75 | map<unsigned int, Player*> mapPlayers;
|
---|
[8dad966] | 76 | map<unsigned int, Projectile> mapProjectiles;
|
---|
[f41a7f9] | 77 | map<string, Game*> mapGames;
|
---|
[8dad966] | 78 | unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
|
---|
[b8601ee] | 79 | int scoreBlue, scoreRed;
|
---|
[d05086b] | 80 | ofstream outputLog;
|
---|
| 81 |
|
---|
| 82 | done = false;
|
---|
[b8601ee] | 83 |
|
---|
| 84 | scoreBlue = 0;
|
---|
| 85 | scoreRed = 0;
|
---|
[e084950] | 86 |
|
---|
[d05086b] | 87 | signal(SIGINT, quit);
|
---|
| 88 |
|
---|
[edfd1d0] | 89 | //SSL_load_error_strings();
|
---|
| 90 | //ERR_load_BIO_strings();
|
---|
| 91 | //OpenSSL_add_all_algorithms();
|
---|
[e3535b3] | 92 |
|
---|
[95ffe57] | 93 | if (argc != 2)
|
---|
| 94 | {
|
---|
| 95 | cerr << "ERROR, expected server [domain] [port]" << endl;
|
---|
[73f75c1] | 96 | exit(1);
|
---|
[e3535b3] | 97 | }
|
---|
[60017fc] | 98 |
|
---|
[d05086b] | 99 | outputLog.open("server.log", ios::app);
|
---|
| 100 | outputLog << "Started server on " << getCurrentDateTimeString() << endl;
|
---|
| 101 |
|
---|
[7b43385] | 102 | WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
|
---|
[6e66ffd] | 103 |
|
---|
| 104 | // add some items to the map. They will be sent out
|
---|
| 105 | // to players when they login
|
---|
[95ffe57] | 106 | for (int y=0; y<gameMap->height; y++)
|
---|
| 107 | {
|
---|
| 108 | for (int x=0; x<gameMap->width; x++)
|
---|
| 109 | {
|
---|
| 110 | switch (gameMap->getStructure(x, y))
|
---|
| 111 | {
|
---|
[5f868c0] | 112 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
| 113 | gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
| 114 | break;
|
---|
| 115 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
| 116 | gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
| 117 | break;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[6e66ffd] | 121 |
|
---|
[371ce29] | 122 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[5b1e31e] | 123 | if (sock < 0)
|
---|
| 124 | error("Opening socket");
|
---|
[e3535b3] | 125 | length = sizeof(server);
|
---|
| 126 | bzero(&server,length);
|
---|
| 127 | server.sin_family=AF_INET;
|
---|
| 128 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 129 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 130 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 131 | error("binding");
|
---|
[73f75c1] | 132 |
|
---|
[371ce29] | 133 | set_nonblock(sock);
|
---|
| 134 |
|
---|
[da692b9] | 135 | bool broadcastResponse;
|
---|
[d211210] | 136 | timespec ts;
|
---|
[430c80e] | 137 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
---|
[95ffe57] | 138 | while (!done)
|
---|
| 139 | {
|
---|
[371ce29] | 140 | usleep(5000);
|
---|
| 141 |
|
---|
[d211210] | 142 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
[430c80e] | 143 | // make the number smaller so millis can fit in an int
|
---|
[d69eb32] | 144 | ts.tv_sec -= 1368000000;
|
---|
[430c80e] | 145 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
[d211210] | 146 |
|
---|
[95ffe57] | 147 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
|
---|
| 148 | {
|
---|
[d211210] | 149 | timeLastUpdated = curTime;
|
---|
| 150 |
|
---|
[d05086b] | 151 | msgProcessor.cleanAckedMessages(&outputLog);
|
---|
| 152 | msgProcessor.resendUnackedMessages(sock, &outputLog);
|
---|
[9b5d30b] | 153 |
|
---|
[95ffe57] | 154 | map<unsigned int, Player*>::iterator it;
|
---|
[11d21ee] | 155 |
|
---|
[ce2bb87] | 156 | cout << "Updating player targets and respawning dead players" << endl;
|
---|
| 157 |
|
---|
[11d21ee] | 158 | // set targets for all chasing players (or make them attack if they're close enough)
|
---|
[95ffe57] | 159 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 160 | {
|
---|
[ffadc8e] | 161 | Player* p = it->second;
|
---|
| 162 |
|
---|
[c76134b] | 163 | // check if it's time to revive dead players
|
---|
[ffadc8e] | 164 | if (p->isDead)
|
---|
[95ffe57] | 165 | {
|
---|
[e1af80c] | 166 | cout << "Player is dead" << endl;
|
---|
[35f6097] | 167 |
|
---|
[ffadc8e] | 168 | if (getCurrentMillis() - p->timeDied >= 10000)
|
---|
[95ffe57] | 169 | {
|
---|
[ffadc8e] | 170 | p->isDead = false;
|
---|
[c76134b] | 171 |
|
---|
| 172 | POSITION spawnPos;
|
---|
| 173 |
|
---|
[ffadc8e] | 174 | switch (p->team)
|
---|
[95ffe57] | 175 | {
|
---|
[c76134b] | 176 | case 0:// blue team
|
---|
[35f6097] | 177 | spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
[c76134b] | 178 | break;
|
---|
| 179 | case 1:// red team
|
---|
[35f6097] | 180 | spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
[c76134b] | 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 |
|
---|
[ffadc8e] | 192 | p->pos = spawnPos.toFloat();
|
---|
| 193 | p->target = spawnPos;
|
---|
| 194 | p->health = p->maxHealth;
|
---|
[c76134b] | 195 |
|
---|
| 196 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[ffadc8e] | 197 | p->serialize(serverMsg.buffer);
|
---|
[c76134b] | 198 |
|
---|
[95ffe57] | 199 | map<unsigned int, Player*>::iterator it2;
|
---|
[35f6097] | 200 | for (it2 = p->currentGame->getPlayers().begin(); it2 != p->currentGame->getPlayers().end(); it2++)
|
---|
[c76134b] | 201 | {
|
---|
[35f6097] | 202 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
[c76134b] | 203 | error("sendMessage");
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | continue;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[ffadc8e] | 210 | if (p->currentGame != NULL) {
|
---|
| 211 | map<unsigned int, Player*> playersInGame = p->currentGame->getPlayers();
|
---|
| 212 | if (p->updateTarget(playersInGame))
|
---|
[5b1e31e] | 213 | {
|
---|
[ffadc8e] | 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 | }
|
---|
[11d21ee] | 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[ce2bb87] | 227 | cout << "Processing players in a game" << endl;
|
---|
| 228 |
|
---|
[402cf86] | 229 | // process players currently in a game
|
---|
[23559e7] | 230 | FLOAT_POSITION oldPos;
|
---|
[e62b56c] | 231 | map<string, Game*>::iterator itGames;
|
---|
| 232 | Game* game = NULL;
|
---|
| 233 | WorldMap* gameMap = NULL;
|
---|
[e5b96e2] | 234 | bool gameFinished;
|
---|
[ce2bb87] | 235 |
|
---|
[e5b96e2] | 236 | for (itGames = mapGames.begin(); itGames != mapGames.end();) {
|
---|
[e62b56c] | 237 | game = itGames->second;
|
---|
| 238 | gameMap = game->getMap();
|
---|
| 239 | map<unsigned int, Player*>& playersInGame = game->getPlayers();
|
---|
[e5b96e2] | 240 | gameFinished = false;
|
---|
[402cf86] | 241 |
|
---|
[e62b56c] | 242 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
|
---|
| 243 | {
|
---|
| 244 | Player* p = it->second;
|
---|
[483a2cb] | 245 |
|
---|
[e62b56c] | 246 | cout << "moving player" << endl;
|
---|
| 247 | bool broadcastMove = false;
|
---|
[23559e7] | 248 |
|
---|
[e62b56c] | 249 | // xompute playersInGame here
|
---|
[23559e7] | 250 |
|
---|
[e62b56c] | 251 | // move player and perform associated tasks
|
---|
| 252 | oldPos = p->pos;
|
---|
| 253 | if (p->move(gameMap)) {
|
---|
[b73bc28] | 254 |
|
---|
[e62b56c] | 255 | cout << "player moved" << endl;
|
---|
| 256 | if (game->processPlayerMovement(p, oldPos))
|
---|
| 257 | broadcastMove = true;
|
---|
| 258 | cout << "player move processed" << endl;
|
---|
[b73bc28] | 259 |
|
---|
[e62b56c] | 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))
|
---|
[e4c60ba] | 270 | {
|
---|
[e62b56c] | 271 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
[7553db9] | 272 | {
|
---|
[e62b56c] | 273 | if (p->team == 0 && p->hasRedFlag)
|
---|
[95ffe57] | 274 | {
|
---|
[e62b56c] | 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++)
|
---|
[95ffe57] | 282 | {
|
---|
[e62b56c] | 283 | if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
|
---|
[95ffe57] | 284 | {
|
---|
[e62b56c] | 285 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
|
---|
| 286 | {
|
---|
| 287 | ownFlagAtBase = true;
|
---|
| 288 | break;
|
---|
| 289 | }
|
---|
[446dc65] | 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[e62b56c] | 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 | }
|
---|
[446dc65] | 301 | }
|
---|
[7553db9] | 302 |
|
---|
[e62b56c] | 303 | break;
|
---|
| 304 | }
|
---|
| 305 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
[7553db9] | 306 | {
|
---|
[e62b56c] | 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);
|
---|
[446dc65] | 311 |
|
---|
[e62b56c] | 312 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
| 313 | vector<WorldMap::Object>::iterator itObjects;
|
---|
[446dc65] | 314 |
|
---|
[e62b56c] | 315 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
|
---|
[95ffe57] | 316 | {
|
---|
[e62b56c] | 317 | if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
|
---|
[95ffe57] | 318 | {
|
---|
[e62b56c] | 319 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
|
---|
| 320 | {
|
---|
| 321 | ownFlagAtBase = true;
|
---|
| 322 | break;
|
---|
| 323 | }
|
---|
[446dc65] | 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[e62b56c] | 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 | }
|
---|
[446dc65] | 335 | }
|
---|
[e4c60ba] | 336 |
|
---|
[e62b56c] | 337 | break;
|
---|
| 338 | }
|
---|
[7553db9] | 339 | }
|
---|
[e4c60ba] | 340 |
|
---|
[e62b56c] | 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);
|
---|
[e4c60ba] | 347 |
|
---|
[e62b56c] | 348 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
| 349 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
[e4c60ba] | 350 |
|
---|
[e62b56c] | 351 | map<unsigned int, Player*>::iterator it2;
|
---|
[b73bc28] | 352 |
|
---|
[e62b56c] | 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 | }
|
---|
[7553db9] | 358 |
|
---|
[e62b56c] | 359 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
| 360 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
| 361 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
[b8601ee] | 362 |
|
---|
[e62b56c] | 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 | }
|
---|
[b8601ee] | 368 |
|
---|
[e5b96e2] | 369 | // check to see if the game should end
|
---|
| 370 | // move to its own method
|
---|
[e62b56c] | 371 | if (scoreBlue == 3 || scoreRed == 3) {
|
---|
[e5b96e2] | 372 | gameFinished = true;
|
---|
[e4c60ba] | 373 |
|
---|
[e62b56c] | 374 | unsigned int winningTeam;
|
---|
| 375 | if (scoreBlue == 3)
|
---|
| 376 | winningTeam = 0;
|
---|
| 377 | else if (scoreRed == 3)
|
---|
| 378 | winningTeam = 1;
|
---|
[446dc65] | 379 |
|
---|
[e62b56c] | 380 | serverMsg.type = MSG_TYPE_FINISH_GAME;
|
---|
[c9f6a1c] | 381 |
|
---|
| 382 | // I should create an instance of the GameSummary object here and just serialize it into this message
|
---|
[778d0c9] | 383 | memcpy(serverMsg.buffer, &winningTeam, 4);
|
---|
[e62b56c] | 384 | memcpy(serverMsg.buffer+4, &scoreBlue, 4);
|
---|
| 385 | memcpy(serverMsg.buffer+8, &scoreRed, 4);
|
---|
[778d0c9] | 386 | strcpy(serverMsg.buffer+12, game->getName().c_str());
|
---|
[e62b56c] | 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++)
|
---|
[95ffe57] | 405 | {
|
---|
[e62b56c] | 406 | POSITION pos = itObjects->pos;
|
---|
| 407 |
|
---|
| 408 | if (posDistance(p->pos, pos.toFloat()) < 10)
|
---|
[95ffe57] | 409 | {
|
---|
[e62b56c] | 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 | }
|
---|
[95ffe57] | 424 | }
|
---|
[e62b56c] | 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++)
|
---|
[95ffe57] | 437 | {
|
---|
[e62b56c] | 438 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
| 439 | error("sendMessage");
|
---|
[446dc65] | 440 | }
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[e62b56c] | 443 | if (broadcastMove)
|
---|
| 444 | {
|
---|
| 445 | cout << "broadcasting player move" << endl;
|
---|
| 446 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 447 | p->serialize(serverMsg.buffer);
|
---|
[446dc65] | 448 |
|
---|
[e62b56c] | 449 | // only broadcast message to other players in the same game
|
---|
| 450 | cout << "about to broadcast move" << endl;
|
---|
[446dc65] | 451 |
|
---|
[e62b56c] | 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;
|
---|
[446dc65] | 459 | }
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[e62b56c] | 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())
|
---|
[95ffe57] | 466 | {
|
---|
[e62b56c] | 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;
|
---|
[23559e7] | 472 |
|
---|
[e62b56c] | 473 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
| 474 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
| 475 | memcpy(serverMsg.buffer+4, &p->targetPlayer, 4);
|
---|
[402cf86] | 476 |
|
---|
[95ffe57] | 477 | map<unsigned int, Player*>::iterator it2;
|
---|
[ce2bb87] | 478 | for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
|
---|
[23559e7] | 479 | {
|
---|
[95ffe57] | 480 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
[23559e7] | 481 | error("sendMessage");
|
---|
| 482 | }
|
---|
[8dad966] | 483 |
|
---|
[e62b56c] | 484 | if (p->attackType == Player::ATTACK_MELEE)
|
---|
| 485 | {
|
---|
| 486 | cout << "Melee attack" << endl;
|
---|
[ff2133a] | 487 |
|
---|
[e62b56c] | 488 | Player* target = playersInGame[p->targetPlayer];
|
---|
| 489 | damagePlayer(target, p->damage);
|
---|
[8dad966] | 490 |
|
---|
[e62b56c] | 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;
|
---|
[411c1ae] | 498 |
|
---|
[e62b56c] | 499 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
| 500 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor, sock, outputLog);
|
---|
| 501 | }
|
---|
[411c1ae] | 502 | }
|
---|
| 503 |
|
---|
[e62b56c] | 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;
|
---|
[ff2133a] | 510 |
|
---|
[e62b56c] | 511 | Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
|
---|
| 512 | game->assignProjectileId(&proj);
|
---|
| 513 | game->addProjectile(proj);
|
---|
[8dad966] | 514 |
|
---|
[e62b56c] | 515 | int x = p->pos.x;
|
---|
| 516 | int y = p->pos.y;
|
---|
[8795a38] | 517 |
|
---|
[e62b56c] | 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;
|
---|
[8dad966] | 526 |
|
---|
[e62b56c] | 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;
|
---|
[8dad966] | 535 | }
|
---|
| 536 | }
|
---|
[e5b96e2] | 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;
|
---|
[3b6f46b] | 545 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
[e5b96e2] | 546 | {
|
---|
[e1af80c] | 547 | if (it2->second->currentGame == game)
|
---|
| 548 | it2->second->currentGame = NULL;
|
---|
[e5b96e2] | 549 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
| 550 | error("sendMessage");
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | // erase game from server
|
---|
[8ce793b] | 554 | mapGames.erase(itGames++);
|
---|
[3b6f46b] | 555 | delete game;
|
---|
[e5b96e2] | 556 | }else
|
---|
| 557 | itGames++;
|
---|
[8dad966] | 558 | }
|
---|
| 559 |
|
---|
[ce2bb87] | 560 | cout << "Processing projectiles" << endl;
|
---|
| 561 |
|
---|
[8dad966] | 562 | // move all projectiles
|
---|
[483a2cb] | 563 | // see if this can be moved inside the game class
|
---|
[45734ff] | 564 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
[8dad966] | 565 | map<unsigned int, Projectile>::iterator itProj;
|
---|
[5ae8dca] | 566 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
| 567 | game = itGames->second;
|
---|
| 568 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
[95ffe57] | 569 | {
|
---|
[5ae8dca] | 570 | cout << "About to call projectile move" << endl;
|
---|
| 571 | if (itProj->second.move(game->getPlayers()))
|
---|
[8dad966] | 572 | {
|
---|
[5ae8dca] | 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 | }
|
---|
[8dad966] | 586 |
|
---|
[5ae8dca] | 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];
|
---|
[8dad966] | 590 |
|
---|
[5ae8dca] | 591 | damagePlayer(target, itProj->second.damage);
|
---|
[8dad966] | 592 |
|
---|
[5ae8dca] | 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;
|
---|
[411c1ae] | 600 |
|
---|
[5ae8dca] | 601 | if (flagType != WorldMap::OBJECT_NONE)
|
---|
| 602 | addObjectToMap(flagType, target->pos.x, target->pos.y, game->getMap(), game->getPlayers(), msgProcessor, sock, outputLog);
|
---|
| 603 | }
|
---|
[8dad966] | 604 |
|
---|
[5ae8dca] | 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 | }
|
---|
[8dad966] | 614 | }
|
---|
[5ae8dca] | 615 | cout << "Projectile was not moved" << endl;
|
---|
[8dad966] | 616 | }
|
---|
[d211210] | 617 | }
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[d05086b] | 620 | n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
|
---|
[8e540f4] | 621 |
|
---|
[95ffe57] | 622 | if (n >= 0)
|
---|
| 623 | {
|
---|
[99afbb8] | 624 | broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog);
|
---|
[371ce29] | 625 |
|
---|
[da692b9] | 626 | if (broadcastResponse)
|
---|
[3b1efcc] | 627 | {
|
---|
[da692b9] | 628 | cout << "Should be broadcasting the message" << endl;
|
---|
| 629 |
|
---|
[ce2bb87] | 630 | // needs to be updated to use the players from the game
|
---|
[95ffe57] | 631 | map<unsigned int, Player*>::iterator it;
|
---|
[01d0d00] | 632 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
[3b1efcc] | 633 | {
|
---|
[95ffe57] | 634 | cout << "Sent message back to " << it->second->name << endl;
|
---|
| 635 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
[3b1efcc] | 636 | error("sendMessage");
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 | else
|
---|
| 640 | {
|
---|
[da692b9] | 641 | cout << "Should be sending back the message" << endl;
|
---|
| 642 |
|
---|
[d05086b] | 643 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
[3b1efcc] | 644 | error("sendMessage");
|
---|
| 645 | }
|
---|
[ce2bb87] | 646 |
|
---|
| 647 | cout << "Finished processing the message" << endl;
|
---|
[7b43385] | 648 | }
|
---|
[8e540f4] | 649 | }
|
---|
[371ce29] | 650 |
|
---|
[d05086b] | 651 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
| 652 | outputLog.close();
|
---|
| 653 |
|
---|
[f41a7f9] | 654 | // delete all games
|
---|
| 655 | map<string, Game*>::iterator itGames;
|
---|
[95ffe57] | 656 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
| 657 | {
|
---|
[f41a7f9] | 658 | delete itGames->second;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
[95ffe57] | 661 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
| 662 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
| 663 | {
|
---|
| 664 | delete itPlayers->second;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[8e540f4] | 667 | return 0;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
[95ffe57] | 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)
|
---|
[8e540f4] | 671 | {
|
---|
[41ad8ed] | 672 | DataAccess da;
|
---|
| 673 |
|
---|
[9a4fa04] | 674 | cout << "Inside processMessage" << endl;
|
---|
| 675 |
|
---|
[b8cb03f] | 676 | cout << "Received message" << endl;
|
---|
[8e540f4] | 677 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 678 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 679 |
|
---|
[da692b9] | 680 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 681 | bool broadcastResponse = false;
|
---|
| 682 |
|
---|
[8e540f4] | 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:
|
---|
[d2b411a] | 688 | {
|
---|
[8e540f4] | 689 | string username(clientMsg.buffer);
|
---|
| 690 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[521c88b] | 691 | Player::PlayerClass playerClass;
|
---|
| 692 |
|
---|
[c4c2a3c] | 693 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[4509648] | 694 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
[c4c2a3c] | 695 |
|
---|
[8e540f4] | 696 | cout << "username: " << username << endl;
|
---|
| 697 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 698 |
|
---|
[521c88b] | 699 | if (playerClass == Player::CLASS_WARRIOR)
|
---|
| 700 | cout << "class: WARRIOR" << endl;
|
---|
| 701 | else if (playerClass == Player::CLASS_RANGER)
|
---|
| 702 | cout << "class: RANGER" << endl;
|
---|
[c4c2a3c] | 703 | else {
|
---|
[521c88b] | 704 | cout << "Unknown player class detected" << endl;
|
---|
[c4c2a3c] | 705 | strcpy(serverMsg.buffer, "You didn't select a class");
|
---|
| 706 | break;
|
---|
| 707 | }
|
---|
[521c88b] | 708 |
|
---|
| 709 | int error = da.insertPlayer(username, password, playerClass);
|
---|
[41ad8ed] | 710 |
|
---|
[c4c2a3c] | 711 | if (error)
|
---|
[3b1efcc] | 712 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[c4c2a3c] | 713 | else
|
---|
| 714 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
[d2b411a] | 715 |
|
---|
[8e540f4] | 716 | break;
|
---|
| 717 | }
|
---|
| 718 | case MSG_TYPE_LOGIN:
|
---|
| 719 | {
|
---|
[60017fc] | 720 | cout << "Got login message" << endl;
|
---|
| 721 |
|
---|
[8e540f4] | 722 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 723 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 724 |
|
---|
[41ad8ed] | 725 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 726 |
|
---|
[b128109] | 727 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 728 | {
|
---|
| 729 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
[95ffe57] | 730 | if (p != NULL)
|
---|
| 731 | delete(p);
|
---|
[41ad8ed] | 732 | }
|
---|
[01d0d00] | 733 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 734 | {
|
---|
| 735 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
[95ffe57] | 736 | delete(p);
|
---|
[41ad8ed] | 737 | }
|
---|
| 738 | else
|
---|
[8e540f4] | 739 | {
|
---|
[8dad966] | 740 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
| 741 | p->id = unusedPlayerId;
|
---|
[d211210] | 742 | cout << "new player id: " << p->id << endl;
|
---|
[df79cfd] | 743 | p->setAddr(from);
|
---|
[ce2bb87] | 744 | p->currentGame = NULL;
|
---|
[df79cfd] | 745 |
|
---|
| 746 | // choose a random team (either 0 or 1)
|
---|
| 747 | p->team = rand() % 2;
|
---|
[d211210] | 748 |
|
---|
[f203c5c] | 749 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 750 |
|
---|
[d211210] | 751 | // tell the new player about all the existing players
|
---|
| 752 | cout << "Sending other players to new player" << endl;
|
---|
| 753 |
|
---|
[95ffe57] | 754 | map<unsigned int, Player*>::iterator it;
|
---|
[d211210] | 755 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 756 | {
|
---|
[95ffe57] | 757 | it->second->serialize(serverMsg.buffer);
|
---|
[d211210] | 758 |
|
---|
[95ffe57] | 759 | cout << "sending info about " << it->second->name << endl;
|
---|
| 760 | cout << "sending id " << it->second->id << endl;
|
---|
[d05086b] | 761 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
[5f868c0] | 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;
|
---|
[e487381] | 768 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
[5f868c0] | 769 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 770 | cout << "sending items" << endl;
|
---|
[e487381] | 771 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
[5f868c0] | 772 | itObjects->serialize(serverMsg.buffer);
|
---|
| 773 | cout << "sending item id " << itObjects->id << endl;
|
---|
[d05086b] | 774 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
[d211210] | 775 | error("sendMessage");
|
---|
[d3efa1a] | 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");
|
---|
[d211210] | 792 | }
|
---|
[59061f6] | 793 |
|
---|
[b8601ee] | 794 | // send the current score
|
---|
| 795 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
| 796 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
| 797 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
[d05086b] | 798 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
[b8601ee] | 799 | error("sendMessage");
|
---|
| 800 |
|
---|
| 801 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[594d2e9] | 802 | p->serialize(serverMsg.buffer);
|
---|
[d211210] | 803 | cout << "Should be broadcasting the message" << endl;
|
---|
| 804 |
|
---|
| 805 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 806 | {
|
---|
[95ffe57] | 807 | cout << "Sent message back to " << it->second->name << endl;
|
---|
| 808 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
[d211210] | 809 | error("sendMessage");
|
---|
| 810 | }
|
---|
| 811 |
|
---|
[95ffe57] | 812 | mapPlayers[unusedPlayerId] = p;
|
---|
[07028b9] | 813 | }
|
---|
| 814 |
|
---|
[f203c5c] | 815 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[07028b9] | 816 |
|
---|
[8e540f4] | 817 | break;
|
---|
| 818 | }
|
---|
| 819 | case MSG_TYPE_LOGOUT:
|
---|
| 820 | {
|
---|
| 821 | string name(clientMsg.buffer);
|
---|
| 822 | cout << "Player logging out: " << name << endl;
|
---|
| 823 |
|
---|
[01d0d00] | 824 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 825 |
|
---|
[8e540f4] | 826 | if (p == NULL)
|
---|
| 827 | {
|
---|
[90eaad2] | 828 | strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 829 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 830 | }
|
---|
[01d0d00] | 831 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 832 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 833 | {
|
---|
[90eaad2] | 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.");
|
---|
[8a3ef42] | 835 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 836 | }
|
---|
[8e540f4] | 837 | else
|
---|
[2488852] | 838 | {
|
---|
[411c1ae] | 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) {
|
---|
[d05086b] | 847 | addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
|
---|
[411c1ae] | 848 | }
|
---|
| 849 | }
|
---|
| 850 |
|
---|
[1a47469] | 851 | // broadcast to all players before deleting p from the map
|
---|
[4509648] | 852 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
| 853 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
| 854 |
|
---|
[1a47469] | 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 |
|
---|
[8dad966] | 863 | if (p->id < unusedPlayerId)
|
---|
| 864 | unusedPlayerId = p->id;
|
---|
[01d0d00] | 865 | mapPlayers.erase(p->id);
|
---|
[95ffe57] | 866 | delete p;
|
---|
[90eaad2] | 867 | strcpy(serverMsg.buffer+4, "You have successfully logged out.");
|
---|
[8e540f4] | 868 | }
|
---|
[07028b9] | 869 |
|
---|
[d211210] | 870 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
[8a3ef42] | 871 |
|
---|
[8e540f4] | 872 | break;
|
---|
| 873 | }
|
---|
| 874 | case MSG_TYPE_CHAT:
|
---|
| 875 | {
|
---|
[da692b9] | 876 | cout << "Got a chat message" << endl;
|
---|
| 877 |
|
---|
[01d0d00] | 878 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 879 |
|
---|
[8e540f4] | 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.");
|
---|
[2488852] | 883 | }
|
---|
[8e540f4] | 884 | else
|
---|
| 885 | {
|
---|
[3b1efcc] | 886 | broadcastResponse = true;
|
---|
| 887 |
|
---|
[b128109] | 888 | ostringstream oss;
|
---|
| 889 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 890 |
|
---|
[b128109] | 891 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[8e540f4] | 892 | }
|
---|
| 893 |
|
---|
| 894 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 895 |
|
---|
| 896 | break;
|
---|
[e084950] | 897 | }
|
---|
[b128109] | 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);
|
---|
[7b43385] | 907 |
|
---|
[b128109] | 908 | cout << "x: " << x << endl;
|
---|
| 909 | cout << "y: " << y << endl;
|
---|
| 910 | cout << "id: " << id << endl;
|
---|
[7b43385] | 911 |
|
---|
[95ffe57] | 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 )
|
---|
[b128109] | 916 | {
|
---|
[0129700] | 917 | if (p->currentGame->startPlayerMovement(id, x, y)) {
|
---|
[60017fc] | 918 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
| 919 |
|
---|
| 920 | memcpy(serverMsg.buffer, &id, 4);
|
---|
[95ffe57] | 921 | memcpy(serverMsg.buffer+4, &p->target.x, 4);
|
---|
| 922 | memcpy(serverMsg.buffer+8, &p->target.y, 4);
|
---|
[60017fc] | 923 |
|
---|
| 924 | broadcastResponse = true;
|
---|
| 925 | }
|
---|
| 926 | else
|
---|
| 927 | cout << "Bad terrain detected" << endl;
|
---|
[b128109] | 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 | }
|
---|
[5299436] | 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 |
|
---|
[95ffe57] | 944 | Player* p = mapPlayers[id];
|
---|
[ce2bb87] | 945 | int objectId = p->currentGame->processFlagPickupRequest(p);
|
---|
[95ffe57] | 946 |
|
---|
[ce2bb87] | 947 | if (objectId >= 0) {
|
---|
| 948 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
| 949 | memcpy(serverMsg.buffer, &objectId, 4);
|
---|
[5c84d54] | 950 |
|
---|
[ce2bb87] | 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");
|
---|
[5c84d54] | 957 | }
|
---|
| 958 |
|
---|
| 959 | }
|
---|
| 960 |
|
---|
[ce2bb87] | 961 | // if there was no flag to pickup, we really don't need to send a message
|
---|
[5c84d54] | 962 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[95ffe57] | 963 | p->serialize(serverMsg.buffer);
|
---|
[5c84d54] | 964 |
|
---|
[ad1e2fc] | 965 | broadcastResponse = true;
|
---|
| 966 |
|
---|
[5299436] | 967 | break;
|
---|
| 968 | }
|
---|
[e487381] | 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 |
|
---|
[95ffe57] | 979 | Player* p = mapPlayers[id];
|
---|
| 980 |
|
---|
[e487381] | 981 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
[95ffe57] | 982 | if (p->hasBlueFlag)
|
---|
[e487381] | 983 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
[95ffe57] | 984 | else if (p->hasRedFlag)
|
---|
[e487381] | 985 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 986 |
|
---|
[b73bc28] | 987 | addObjectToMap(flagType, p->pos.x, p->pos.y, p->currentGame->getMap(), p->currentGame->getPlayers(), msgProcessor, sock, outputLog);
|
---|
[e487381] | 988 |
|
---|
[95ffe57] | 989 | p->hasBlueFlag = false;
|
---|
| 990 | p->hasRedFlag = false;
|
---|
[e487381] | 991 |
|
---|
| 992 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[95ffe57] | 993 | p->serialize(serverMsg.buffer);
|
---|
[e487381] | 994 |
|
---|
| 995 | broadcastResponse = true;
|
---|
| 996 |
|
---|
| 997 | break;
|
---|
| 998 | }
|
---|
[4b4b153] | 999 | case MSG_TYPE_START_ATTACK:
|
---|
| 1000 | {
|
---|
| 1001 | cout << "Received a START_ATTACK message" << endl;
|
---|
| 1002 |
|
---|
[8a4ed74] | 1003 | int id, targetId;
|
---|
| 1004 |
|
---|
| 1005 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 1006 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
| 1007 |
|
---|
[ffadc8e] | 1008 | // need to make sure the target is in the sender's game
|
---|
| 1009 |
|
---|
[95ffe57] | 1010 | Player* source = mapPlayers[id];
|
---|
[8dad966] | 1011 | source->targetPlayer = targetId;
|
---|
[11d21ee] | 1012 | source->isChasing = true;
|
---|
[8dad966] | 1013 |
|
---|
[11d21ee] | 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
|
---|
[4b4b153] | 1019 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
---|
[8dad966] | 1020 | memcpy(serverMsg.buffer, &id, 4);
|
---|
| 1021 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
[4b4b153] | 1022 | broadcastResponse = true;
|
---|
[8a4ed74] | 1023 |
|
---|
| 1024 | break;
|
---|
[4b4b153] | 1025 | }
|
---|
| 1026 | case MSG_TYPE_ATTACK:
|
---|
| 1027 | {
|
---|
| 1028 | cout << "Received am ATTACK message" << endl;
|
---|
[8dad966] | 1029 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
---|
[8a4ed74] | 1030 |
|
---|
| 1031 | break;
|
---|
[4b4b153] | 1032 | }
|
---|
[b8f789d] | 1033 | case MSG_TYPE_CREATE_GAME:
|
---|
[8e540f4] | 1034 | {
|
---|
[b8f789d] | 1035 | cout << "Received a CREATE_GAME message" << endl;
|
---|
| 1036 |
|
---|
| 1037 | string gameName(clientMsg.buffer);
|
---|
| 1038 | cout << "Game name: " << gameName << endl;
|
---|
| 1039 |
|
---|
[b48ef09] | 1040 | // check if this game already exists
|
---|
| 1041 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
[3ef8cf4] | 1042 | cout << "Error: Game already exists" << endl;
|
---|
[b48ef09] | 1043 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
| 1044 | broadcastResponse = false;
|
---|
| 1045 | return broadcastResponse;
|
---|
| 1046 | }
|
---|
[b92e6a7] | 1047 |
|
---|
[a6fe73d] | 1048 | Game* g = new Game(gameName, "../data/map.txt");
|
---|
[f41a7f9] | 1049 | mapGames[gameName] = g;
|
---|
[b92e6a7] | 1050 |
|
---|
[70fc3e8] | 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 |
|
---|
[7d8d5d3] | 1066 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
[b48ef09] | 1067 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
| 1068 | broadcastResponse = false;
|
---|
[b92e6a7] | 1069 |
|
---|
[b48ef09] | 1070 | break;
|
---|
| 1071 | }
|
---|
| 1072 | case MSG_TYPE_JOIN_GAME:
|
---|
| 1073 | {
|
---|
| 1074 | cout << "Received a JOIN_GAME message" << endl;
|
---|
[b92e6a7] | 1075 |
|
---|
[b48ef09] | 1076 | string gameName(clientMsg.buffer);
|
---|
| 1077 | cout << "Game name: " << gameName << endl;
|
---|
[b92e6a7] | 1078 |
|
---|
[b48ef09] | 1079 | // check if this game already exists
|
---|
| 1080 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
[3ef8cf4] | 1081 | cout << "Error: Game does not exist" << endl;
|
---|
[b48ef09] | 1082 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
| 1083 | broadcastResponse = false;
|
---|
| 1084 | return broadcastResponse;
|
---|
[b92e6a7] | 1085 | }
|
---|
| 1086 |
|
---|
[b48ef09] | 1087 | Game* g = mapGames[gameName];
|
---|
| 1088 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
| 1089 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
[f41a7f9] | 1090 |
|
---|
[b48ef09] | 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;
|
---|
[b92e6a7] | 1096 | }
|
---|
| 1097 |
|
---|
[7d8d5d3] | 1098 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
[b48ef09] | 1099 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
| 1100 | broadcastResponse = false;
|
---|
[b8f789d] | 1101 |
|
---|
| 1102 | break;
|
---|
| 1103 | }
|
---|
[ab8fd40] | 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;
|
---|
[3ef8cf4] | 1120 | p->currentGame = NULL;
|
---|
[95ffe57] | 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 |
|
---|
[3ef8cf4] | 1135 | g->removePlayer(p->id);
|
---|
[ab8fd40] | 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 |
|
---|
[1248984] | 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 |
|
---|
[ab8fd40] | 1150 | break;
|
---|
| 1151 | }
|
---|
[b48ef09] | 1152 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
[b8f789d] | 1153 | {
|
---|
[b48ef09] | 1154 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
[e084950] | 1155 |
|
---|
[b8f789d] | 1156 | string gameName(clientMsg.buffer);
|
---|
| 1157 | cout << "Game name: " << gameName << endl;
|
---|
| 1158 |
|
---|
[b48ef09] | 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 | }
|
---|
[b92e6a7] | 1165 |
|
---|
[f41a7f9] | 1166 | Game* g = mapGames[gameName];
|
---|
[b92e6a7] | 1167 |
|
---|
[b48ef09] | 1168 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
[f41a7f9] | 1169 | p->team = rand() % 2; // choose a random team (either 0 or 1)
|
---|
[e62b56c] | 1170 | p->currentGame = g;
|
---|
[b92e6a7] | 1171 |
|
---|
| 1172 | // tell the new player about all map objects
|
---|
| 1173 | // (currently just the flags)
|
---|
| 1174 |
|
---|
| 1175 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
[f41a7f9] | 1176 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
[b92e6a7] | 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 |
|
---|
[f41a7f9] | 1190 | int game_blueScore = g->getBlueScore();
|
---|
| 1191 | int game_redScore = g->getRedScore();
|
---|
[b92e6a7] | 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 |
|
---|
[453087e] | 1198 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
[b92e6a7] | 1199 | p->serialize(serverMsg.buffer);
|
---|
| 1200 | cout << "Should be broadcasting the message" << endl;
|
---|
| 1201 |
|
---|
[453087e] | 1202 | map<unsigned int, Player*>& otherPlayers = g->getPlayers();
|
---|
[2d78e03] | 1203 | map<unsigned int, Player*>::iterator it;
|
---|
[b92e6a7] | 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 | }
|
---|
[b8f789d] | 1210 |
|
---|
[b48ef09] | 1211 | g->addPlayer(p);
|
---|
[453087e] | 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 |
|
---|
[b48ef09] | 1229 | int numPlayers = g->getNumPlayers();
|
---|
| 1230 |
|
---|
[b8f789d] | 1231 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 1232 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 1233 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
| 1234 | broadcastResponse = true;
|
---|
[ab8fd40] | 1235 |
|
---|
[b8f789d] | 1236 | break;
|
---|
| 1237 | }
|
---|
| 1238 | default:
|
---|
| 1239 | {
|
---|
[8e540f4] | 1240 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[b8f789d] | 1241 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 1242 |
|
---|
[8e540f4] | 1243 | break;
|
---|
| 1244 | }
|
---|
[e3535b3] | 1245 | }
|
---|
[da692b9] | 1246 |
|
---|
| 1247 | return broadcastResponse;
|
---|
[e3535b3] | 1248 | }
|
---|
[da692b9] | 1249 |
|
---|
[95ffe57] | 1250 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
|
---|
[01d0d00] | 1251 | {
|
---|
[1106210] | 1252 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 1253 | id++;
|
---|
[01d0d00] | 1254 | }
|
---|
[8dad966] | 1255 |
|
---|
[95ffe57] | 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 |
|
---|
[c76134b] | 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) {
|
---|
[66c4ec4] | 1288 | cout << "Player died" << endl;
|
---|
[c76134b] | 1289 | p->isDead = true;
|
---|
| 1290 | p->timeDied = getCurrentMillis();
|
---|
| 1291 | }
|
---|
| 1292 | }
|
---|
[411c1ae] | 1293 |
|
---|
[95ffe57] | 1294 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
|
---|
[411c1ae] | 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 |
|
---|
[95ffe57] | 1303 | map<unsigned int, Player*>::iterator it;
|
---|
[411c1ae] | 1304 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 1305 | {
|
---|
[95ffe57] | 1306 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
[411c1ae] | 1307 | error("sendMessage");
|
---|
| 1308 | }
|
---|
| 1309 | }
|
---|