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