| 1 | #include <cstdlib>
|
|---|
| 2 | #include <cstdio>
|
|---|
| 3 | #include <unistd.h>
|
|---|
| 4 | #include <string>
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 | #include <sstream>
|
|---|
| 7 | #include <fstream>
|
|---|
| 8 | #include <cstring>
|
|---|
| 9 | #include <cmath>
|
|---|
| 10 |
|
|---|
| 11 | #include <vector>
|
|---|
| 12 | #include <map>
|
|---|
| 13 |
|
|---|
| 14 | #include <csignal>
|
|---|
| 15 |
|
|---|
| 16 | #include <sys/time.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <sys/socket.h>
|
|---|
| 19 | #include <netdb.h>
|
|---|
| 20 | #include <netinet/in.h>
|
|---|
| 21 | #include <arpa/inet.h>
|
|---|
| 22 |
|
|---|
| 23 | #include <crypt.h>
|
|---|
| 24 |
|
|---|
| 25 | /*
|
|---|
| 26 | #include <openssl/bio.h>
|
|---|
| 27 | #include <openssl/ssl.h>
|
|---|
| 28 | #include <openssl/err.h>
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | #include "../common/Compiler.h"
|
|---|
| 32 | #include "../common/Common.h"
|
|---|
| 33 | #include "../common/MessageProcessor.h"
|
|---|
| 34 | #include "../common/WorldMap.h"
|
|---|
| 35 | #include "../common/Player.h"
|
|---|
| 36 | #include "../common/Projectile.h"
|
|---|
| 37 |
|
|---|
| 38 | #include "DataAccess.h"
|
|---|
| 39 |
|
|---|
| 40 | using namespace std;
|
|---|
| 41 |
|
|---|
| 42 | bool done;
|
|---|
| 43 |
|
|---|
| 44 | // from used to be const. Removed that so I could take a reference
|
|---|
| 45 | // and use it to send messages
|
|---|
| 46 | bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog);
|
|---|
| 47 |
|
|---|
| 48 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
|
|---|
| 49 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);
|
|---|
| 50 | void damagePlayer(Player *p, int damage);
|
|---|
| 51 |
|
|---|
| 52 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
|
|---|
| 53 |
|
|---|
| 54 | // this should probably go somewhere in the common folder
|
|---|
| 55 | void error(const char *msg)
|
|---|
| 56 | {
|
|---|
| 57 | perror(msg);
|
|---|
| 58 | exit(0);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | Player *findPlayerByName(map<unsigned int, Player> &m, string name)
|
|---|
| 62 | {
|
|---|
| 63 | map<unsigned int, Player>::iterator it;
|
|---|
| 64 |
|
|---|
| 65 | for (it = m.begin(); it != m.end(); it++)
|
|---|
| 66 | {
|
|---|
| 67 | if ( it->second.name.compare(name) == 0 )
|
|---|
| 68 | return &(it->second);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return NULL;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
|
|---|
| 75 | {
|
|---|
| 76 | map<unsigned int, Player>::iterator it;
|
|---|
| 77 |
|
|---|
| 78 | for (it = m.begin(); it != m.end(); it++)
|
|---|
| 79 | {
|
|---|
| 80 | if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
|---|
| 81 | it->second.addr.sin_port == addr.sin_port )
|
|---|
| 82 | return &(it->second);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | return NULL;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void quit(int sig) {
|
|---|
| 89 | done = true;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | int main(int argc, char *argv[])
|
|---|
| 93 | {
|
|---|
| 94 | int sock, length, n;
|
|---|
| 95 | struct sockaddr_in server;
|
|---|
| 96 | struct sockaddr_in from; // info of client sending the message
|
|---|
| 97 | NETWORK_MSG clientMsg, serverMsg;
|
|---|
| 98 | MessageProcessor msgProcessor;
|
|---|
| 99 | map<unsigned int, Player> mapPlayers;
|
|---|
| 100 | map<unsigned int, Projectile> mapProjectiles;
|
|---|
| 101 | unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
|
|---|
| 102 | int scoreBlue, scoreRed;
|
|---|
| 103 | ofstream outputLog;
|
|---|
| 104 |
|
|---|
| 105 | done = false;
|
|---|
| 106 |
|
|---|
| 107 | scoreBlue = 0;
|
|---|
| 108 | scoreRed = 0;
|
|---|
| 109 |
|
|---|
| 110 | signal(SIGINT, quit);
|
|---|
| 111 |
|
|---|
| 112 | //SSL_load_error_strings();
|
|---|
| 113 | //ERR_load_BIO_strings();
|
|---|
| 114 | //OpenSSL_add_all_algorithms();
|
|---|
| 115 |
|
|---|
| 116 | if (argc < 2) {
|
|---|
| 117 | cerr << "ERROR, no port provided" << endl;
|
|---|
| 118 | exit(1);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | outputLog.open("server.log", ios::app);
|
|---|
| 122 | outputLog << "Started server on " << getCurrentDateTimeString() << endl;
|
|---|
| 123 |
|
|---|
| 124 | WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
|
|---|
| 125 |
|
|---|
| 126 | // add some items to the map. They will be sent out
|
|---|
| 127 | // to players when they login
|
|---|
| 128 | for (int y=0; y<gameMap->height; y++) {
|
|---|
| 129 | for (int x=0; x<gameMap->width; x++) {
|
|---|
| 130 | switch (gameMap->getStructure(x, y)) {
|
|---|
| 131 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
|---|
| 132 | gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
|---|
| 133 | break;
|
|---|
| 134 | case WorldMap::STRUCTURE_RED_FLAG:
|
|---|
| 135 | gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
|---|
| 136 | break;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|---|
| 142 | if (sock < 0)
|
|---|
| 143 | error("Opening socket");
|
|---|
| 144 | length = sizeof(server);
|
|---|
| 145 | bzero(&server,length);
|
|---|
| 146 | server.sin_family=AF_INET;
|
|---|
| 147 | server.sin_port=htons(atoi(argv[1]));
|
|---|
| 148 | server.sin_addr.s_addr=INADDR_ANY;
|
|---|
| 149 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
|---|
| 150 | error("binding");
|
|---|
| 151 |
|
|---|
| 152 | set_nonblock(sock);
|
|---|
| 153 |
|
|---|
| 154 | bool broadcastResponse;
|
|---|
| 155 | timespec ts;
|
|---|
| 156 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
|---|
| 157 | while (!done) {
|
|---|
| 158 |
|
|---|
| 159 | usleep(5000);
|
|---|
| 160 |
|
|---|
| 161 | clock_gettime(CLOCK_REALTIME, &ts);
|
|---|
| 162 | // make the number smaller so millis can fit in an int
|
|---|
| 163 | ts.tv_sec -= 1368000000;
|
|---|
| 164 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
|---|
| 165 |
|
|---|
| 166 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
|
|---|
| 167 | timeLastUpdated = curTime;
|
|---|
| 168 |
|
|---|
| 169 | msgProcessor.cleanAckedMessages(&outputLog);
|
|---|
| 170 | msgProcessor.resendUnackedMessages(sock, &outputLog);
|
|---|
| 171 |
|
|---|
| 172 | map<unsigned int, Player>::iterator it;
|
|---|
| 173 |
|
|---|
| 174 | // set targets for all chasing players (or make them attack if they're close enough)
|
|---|
| 175 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
|---|
| 176 | // check if it's time to revive dead players
|
|---|
| 177 | if (it->second.isDead) {
|
|---|
| 178 | if (getCurrentMillis() - it->second.timeDied >= 10000) {
|
|---|
| 179 | it->second.isDead = false;
|
|---|
| 180 |
|
|---|
| 181 | POSITION spawnPos;
|
|---|
| 182 |
|
|---|
| 183 | switch (it->second.team) {
|
|---|
| 184 | case 0:// blue team
|
|---|
| 185 | spawnPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 186 | break;
|
|---|
| 187 | case 1:// red team
|
|---|
| 188 | spawnPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 189 | break;
|
|---|
| 190 | default:
|
|---|
| 191 | // should never go here
|
|---|
| 192 | cout << "Error: Invalid team" << endl;
|
|---|
| 193 | break;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | // spawn the player to the right of their flag location
|
|---|
| 197 | spawnPos.x = (spawnPos.x+1) * 25 + 12;
|
|---|
| 198 | spawnPos.y = spawnPos.y * 25 + 12;
|
|---|
| 199 |
|
|---|
| 200 | it->second.pos = spawnPos.toFloat();
|
|---|
| 201 | it->second.target = spawnPos;
|
|---|
| 202 | it->second.health = it->second.maxHealth;
|
|---|
| 203 |
|
|---|
| 204 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 205 | it->second.serialize(serverMsg.buffer);
|
|---|
| 206 |
|
|---|
| 207 | map<unsigned int, Player>::iterator it2;
|
|---|
| 208 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 209 | {
|
|---|
| 210 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 211 | error("sendMessage");
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | continue;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | if (it->second.updateTarget(mapPlayers)) {
|
|---|
| 219 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 220 | it->second.serialize(serverMsg.buffer);
|
|---|
| 221 |
|
|---|
| 222 | map<unsigned int, Player>::iterator it2;
|
|---|
| 223 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 224 | {
|
|---|
| 225 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 226 | error("sendMessage");
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | // move all players
|
|---|
| 232 | // maybe put this in a separate method
|
|---|
| 233 | FLOAT_POSITION oldPos;
|
|---|
| 234 | bool broadcastMove = false;
|
|---|
| 235 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
|---|
| 236 | oldPos = it->second.pos;
|
|---|
| 237 | if (it->second.move(gameMap)) {
|
|---|
| 238 |
|
|---|
| 239 | // check if the move needs to be canceled
|
|---|
| 240 | switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
|
|---|
| 241 | case WorldMap::TERRAIN_NONE:
|
|---|
| 242 | case WorldMap::TERRAIN_OCEAN:
|
|---|
| 243 | case WorldMap::TERRAIN_ROCK:
|
|---|
| 244 | {
|
|---|
| 245 | it->second.pos = oldPos;
|
|---|
| 246 | it->second.target.x = it->second.pos.x;
|
|---|
| 247 | it->second.target.y = it->second.pos.y;
|
|---|
| 248 | it->second.isChasing = false;
|
|---|
| 249 | broadcastMove = true;
|
|---|
| 250 | break;
|
|---|
| 251 | }
|
|---|
| 252 | default:
|
|---|
| 253 | // if there are no obstacles, do nothing
|
|---|
| 254 | break;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | WorldMap::ObjectType flagType;
|
|---|
| 258 | POSITION pos;
|
|---|
| 259 | bool flagTurnedIn = false;
|
|---|
| 260 | bool flagReturned = false;
|
|---|
| 261 | bool ownFlagAtBase = false;
|
|---|
| 262 |
|
|---|
| 263 | switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
|
|---|
| 264 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
|---|
| 265 | {
|
|---|
| 266 | if (it->second.team == 0 && it->second.hasRedFlag)
|
|---|
| 267 | {
|
|---|
| 268 | // check that your flag is at your base
|
|---|
| 269 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 270 |
|
|---|
| 271 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 272 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 273 |
|
|---|
| 274 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 275 | if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
|---|
| 276 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
|---|
| 277 | ownFlagAtBase = true;
|
|---|
| 278 | break;
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | if (ownFlagAtBase) {
|
|---|
| 284 | it->second.hasRedFlag = false;
|
|---|
| 285 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 286 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 287 | flagTurnedIn = true;
|
|---|
| 288 | scoreBlue++;
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | break;
|
|---|
| 293 | }
|
|---|
| 294 | case WorldMap::STRUCTURE_RED_FLAG:
|
|---|
| 295 | {
|
|---|
| 296 | if (it->second.team == 1 && it->second.hasBlueFlag)
|
|---|
| 297 | {
|
|---|
| 298 | // check that your flag is at your base
|
|---|
| 299 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 300 |
|
|---|
| 301 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 302 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 303 |
|
|---|
| 304 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 305 | if (itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
|---|
| 306 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
|---|
| 307 | ownFlagAtBase = true;
|
|---|
| 308 | break;
|
|---|
| 309 | }
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | if (ownFlagAtBase) {
|
|---|
| 314 | it->second.hasBlueFlag = false;
|
|---|
| 315 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 316 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 317 | flagTurnedIn = true;
|
|---|
| 318 | scoreRed++;
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | break;
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | if (flagTurnedIn) {
|
|---|
| 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);
|
|---|
| 331 |
|
|---|
| 332 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 333 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
|---|
| 334 |
|
|---|
| 335 | map<unsigned int, Player>::iterator it2;
|
|---|
| 336 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 337 | {
|
|---|
| 338 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 339 | error("sendMessage");
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 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 | {
|
|---|
| 348 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 349 | error("sendMessage");
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | // this means a PLAYER message will be sent
|
|---|
| 353 | broadcastMove = true;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 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 |
|
|---|
| 361 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 362 | POSITION pos = itObjects->pos;
|
|---|
| 363 |
|
|---|
| 364 | if (posDistance(it->second.pos, pos.toFloat()) < 10) {
|
|---|
| 365 | if (it->second.team == 0 &&
|
|---|
| 366 | itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
|---|
| 367 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 368 | flagReturned = true;
|
|---|
| 369 | break;
|
|---|
| 370 | } else if (it->second.team == 1 &&
|
|---|
| 371 | itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
|---|
| 372 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 373 | flagReturned = true;
|
|---|
| 374 | break;
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | if (flagReturned) {
|
|---|
| 380 | itObjects->pos.x = structPos.x*25+12;
|
|---|
| 381 | itObjects->pos.y = structPos.y*25+12;
|
|---|
| 382 |
|
|---|
| 383 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 384 | itObjects->serialize(serverMsg.buffer);
|
|---|
| 385 |
|
|---|
| 386 | map<unsigned int, Player>::iterator it2;
|
|---|
| 387 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 388 | {
|
|---|
| 389 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 390 | error("sendMessage");
|
|---|
| 391 | }
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | if (broadcastMove) {
|
|---|
| 395 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 396 | it->second.serialize(serverMsg.buffer);
|
|---|
| 397 |
|
|---|
| 398 | cout << "about to broadcast move" << endl;
|
|---|
| 399 | map<unsigned int, Player>::iterator it2;
|
|---|
| 400 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 401 | {
|
|---|
| 402 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 403 | error("sendMessage");
|
|---|
| 404 | }
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | // check if the player's attack animation is complete
|
|---|
| 409 | if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) {
|
|---|
| 410 | it->second.isAttacking = false;
|
|---|
| 411 | cout << "Attack animation is complete" << endl;
|
|---|
| 412 |
|
|---|
| 413 | //send everyone an ATTACK message
|
|---|
| 414 | cout << "about to broadcast attack" << endl;
|
|---|
| 415 |
|
|---|
| 416 | serverMsg.type = MSG_TYPE_ATTACK;
|
|---|
| 417 | memcpy(serverMsg.buffer, &it->second.id, 4);
|
|---|
| 418 | memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
|
|---|
| 419 |
|
|---|
| 420 | map<unsigned int, Player>::iterator it2;
|
|---|
| 421 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 422 | {
|
|---|
| 423 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 424 | error("sendMessage");
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | if (it->second.attackType == Player::ATTACK_MELEE) {
|
|---|
| 428 | cout << "Melee attack" << endl;
|
|---|
| 429 |
|
|---|
| 430 | Player* target = &mapPlayers[it->second.targetPlayer];
|
|---|
| 431 | damagePlayer(target, it->second.damage);
|
|---|
| 432 |
|
|---|
| 433 | if (target->isDead) {
|
|---|
| 434 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 435 | if (target->hasBlueFlag)
|
|---|
| 436 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 437 | else if (target->hasRedFlag)
|
|---|
| 438 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 439 |
|
|---|
| 440 | if (flagType != WorldMap::OBJECT_NONE) {
|
|---|
| 441 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 446 | target->serialize(serverMsg.buffer);
|
|---|
| 447 | }else if (it->second.attackType == Player::ATTACK_RANGED) {
|
|---|
| 448 | cout << "Ranged attack" << endl;
|
|---|
| 449 |
|
|---|
| 450 | Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
|
|---|
| 451 | proj.id = unusedProjectileId;
|
|---|
| 452 | updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
|
|---|
| 453 | mapProjectiles[proj.id] = proj;
|
|---|
| 454 |
|
|---|
| 455 | int x = it->second.pos.x;
|
|---|
| 456 | int y = it->second.pos.y;
|
|---|
| 457 |
|
|---|
| 458 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
|---|
| 459 | memcpy(serverMsg.buffer, &proj.id, 4);
|
|---|
| 460 | memcpy(serverMsg.buffer+4, &x, 4);
|
|---|
| 461 | memcpy(serverMsg.buffer+8, &y, 4);
|
|---|
| 462 | memcpy(serverMsg.buffer+12, &it->second.targetPlayer, 4);
|
|---|
| 463 | }else {
|
|---|
| 464 | cout << "Invalid attack type: " << it->second.attackType << endl;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | // broadcast either a PLAYER or PROJECTILE message
|
|---|
| 468 | cout << "Broadcasting player or projectile message" << endl;
|
|---|
| 469 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 470 | {
|
|---|
| 471 | if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 472 | error("sendMessage");
|
|---|
| 473 | }
|
|---|
| 474 | cout << "Done broadcasting" << endl;
|
|---|
| 475 | }
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | // move all projectiles
|
|---|
| 479 | map<unsigned int, Projectile>::iterator itProj;
|
|---|
| 480 | for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
|
|---|
| 481 | cout << "About to call projectile move" << endl;
|
|---|
| 482 | if (itProj->second.move(mapPlayers)) {
|
|---|
| 483 | // send a REMOVE_PROJECTILE message
|
|---|
| 484 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
|---|
| 485 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
|---|
| 486 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
|---|
| 487 | mapProjectiles.erase(itProj->second.id);
|
|---|
| 488 |
|
|---|
| 489 | map<unsigned int, Player>::iterator it2;
|
|---|
| 490 | cout << "Broadcasting REMOVE_PROJECTILE" << endl;
|
|---|
| 491 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 492 | {
|
|---|
| 493 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 494 | error("sendMessage");
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | cout << "send a PLAYER message after dealing damage" << endl;
|
|---|
| 498 | // send a PLAYER message after dealing damage
|
|---|
| 499 | Player* target = &mapPlayers[itProj->second.target];
|
|---|
| 500 |
|
|---|
| 501 | damagePlayer(target, itProj->second.damage);
|
|---|
| 502 |
|
|---|
| 503 | if (target->isDead) {
|
|---|
| 504 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 505 | if (target->hasBlueFlag)
|
|---|
| 506 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 507 | else if (target->hasRedFlag)
|
|---|
| 508 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 509 |
|
|---|
| 510 | if (flagType != WorldMap::OBJECT_NONE) {
|
|---|
| 511 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
|
|---|
| 512 | }
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 516 | target->serialize(serverMsg.buffer);
|
|---|
| 517 |
|
|---|
| 518 | cout << "Sending a PLAYER message" << endl;
|
|---|
| 519 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 520 | {
|
|---|
| 521 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
|
|---|
| 522 | error("sendMessage");
|
|---|
| 523 | }
|
|---|
| 524 | }
|
|---|
| 525 | cout << "Projectile was not moved" << endl;
|
|---|
| 526 | }
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
|
|---|
| 530 |
|
|---|
| 531 | if (n >= 0) {
|
|---|
| 532 | broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog);
|
|---|
| 533 |
|
|---|
| 534 | if (broadcastResponse)
|
|---|
| 535 | {
|
|---|
| 536 | cout << "Should be broadcasting the message" << endl;
|
|---|
| 537 |
|
|---|
| 538 | map<unsigned int, Player>::iterator it;
|
|---|
| 539 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 540 | {
|
|---|
| 541 | cout << "Sent message back to " << it->second.name << endl;
|
|---|
| 542 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
|
|---|
| 543 | error("sendMessage");
|
|---|
| 544 | }
|
|---|
| 545 | }
|
|---|
| 546 | else
|
|---|
| 547 | {
|
|---|
| 548 | cout << "Should be sending back the message" << endl;
|
|---|
| 549 |
|
|---|
| 550 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
|---|
| 551 | error("sendMessage");
|
|---|
| 552 | }
|
|---|
| 553 | }
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
|---|
| 557 | outputLog.close();
|
|---|
| 558 |
|
|---|
| 559 | return 0;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog)
|
|---|
| 563 | {
|
|---|
| 564 | DataAccess da;
|
|---|
| 565 |
|
|---|
| 566 | cout << "Inside processMessage" << endl;
|
|---|
| 567 |
|
|---|
| 568 | cout << "Received message" << endl;
|
|---|
| 569 | cout << "MSG: type: " << clientMsg.type << endl;
|
|---|
| 570 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
|---|
| 571 |
|
|---|
| 572 | // maybe we should make a message class and have this be a member
|
|---|
| 573 | bool broadcastResponse = false;
|
|---|
| 574 |
|
|---|
| 575 | // Check that if an invalid message is sent, the client will correctly
|
|---|
| 576 | // receive and display the response. Maybe make a special error msg type
|
|---|
| 577 | switch(clientMsg.type)
|
|---|
| 578 | {
|
|---|
| 579 | case MSG_TYPE_REGISTER:
|
|---|
| 580 | {
|
|---|
| 581 | string username(clientMsg.buffer);
|
|---|
| 582 | string password(strchr(clientMsg.buffer, '\0')+1);
|
|---|
| 583 | Player::PlayerClass playerClass;
|
|---|
| 584 |
|
|---|
| 585 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
|---|
| 586 | serverMsg.type = MSG_TYPE_REGISTER;
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 | cout << "username: " << username << endl;
|
|---|
| 590 | cout << "password: " << password << endl;
|
|---|
| 591 |
|
|---|
| 592 | if (playerClass == Player::CLASS_WARRIOR)
|
|---|
| 593 | cout << "class: WARRIOR" << endl;
|
|---|
| 594 | else if (playerClass == Player::CLASS_RANGER)
|
|---|
| 595 | cout << "class: RANGER" << endl;
|
|---|
| 596 | else {
|
|---|
| 597 | cout << "Unknown player class detected" << endl;
|
|---|
| 598 | strcpy(serverMsg.buffer, "You didn't select a class");
|
|---|
| 599 | break;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | int error = da.insertPlayer(username, password, playerClass);
|
|---|
| 603 |
|
|---|
| 604 | if (error)
|
|---|
| 605 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
|---|
| 606 | else
|
|---|
| 607 | strcpy(serverMsg.buffer, "Registration successful.");
|
|---|
| 608 |
|
|---|
| 609 | break;
|
|---|
| 610 | }
|
|---|
| 611 | case MSG_TYPE_LOGIN:
|
|---|
| 612 | {
|
|---|
| 613 | cout << "Got login message" << endl;
|
|---|
| 614 |
|
|---|
| 615 | serverMsg.type = MSG_TYPE_LOGIN;
|
|---|
| 616 |
|
|---|
| 617 | string username(clientMsg.buffer);
|
|---|
| 618 | string password(strchr(clientMsg.buffer, '\0')+1);
|
|---|
| 619 |
|
|---|
| 620 | Player* p = da.getPlayer(username);
|
|---|
| 621 |
|
|---|
| 622 | if (p == NULL || !da.verifyPassword(password, p->password))
|
|---|
| 623 | {
|
|---|
| 624 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
|---|
| 625 | }
|
|---|
| 626 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
|---|
| 627 | {
|
|---|
| 628 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
|---|
| 629 | }
|
|---|
| 630 | else
|
|---|
| 631 | {
|
|---|
| 632 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 633 |
|
|---|
| 634 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
|---|
| 635 | p->id = unusedPlayerId;
|
|---|
| 636 | cout << "new player id: " << p->id << endl;
|
|---|
| 637 | p->setAddr(from);
|
|---|
| 638 |
|
|---|
| 639 | // choose a random team (either 0 or 1)
|
|---|
| 640 | p->team = rand() % 2;
|
|---|
| 641 |
|
|---|
| 642 | // tell the new player about all the existing players
|
|---|
| 643 | cout << "Sending other players to new player" << endl;
|
|---|
| 644 |
|
|---|
| 645 | map<unsigned int, Player>::iterator it;
|
|---|
| 646 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 647 | {
|
|---|
| 648 | it->second.serialize(serverMsg.buffer);
|
|---|
| 649 |
|
|---|
| 650 | cout << "sending info about " << it->second.name << endl;
|
|---|
| 651 | cout << "sending id " << it->second.id << endl;
|
|---|
| 652 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
|---|
| 653 | error("sendMessage");
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | // tell the new player about all map objects
|
|---|
| 657 | // (currently just the flags)
|
|---|
| 658 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 659 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 660 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 661 | cout << "sending items" << endl;
|
|---|
| 662 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 663 | itObjects->serialize(serverMsg.buffer);
|
|---|
| 664 | cout << "sending item id " << itObjects->id << endl;
|
|---|
| 665 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
|---|
| 666 | error("sendMessage");
|
|---|
| 667 | }
|
|---|
| 668 |
|
|---|
| 669 | // send the current score
|
|---|
| 670 | serverMsg.type = MSG_TYPE_SCORE;
|
|---|
| 671 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
|---|
| 672 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
|---|
| 673 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
|---|
| 674 | error("sendMessage");
|
|---|
| 675 |
|
|---|
| 676 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 677 | p->serialize(serverMsg.buffer);
|
|---|
| 678 | cout << "Should be broadcasting the message" << endl;
|
|---|
| 679 |
|
|---|
| 680 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 681 | {
|
|---|
| 682 | cout << "Sent message back to " << it->second.name << endl;
|
|---|
| 683 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
|
|---|
| 684 | error("sendMessage");
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | serverMsg.type = MSG_TYPE_LOGIN;
|
|---|
| 688 | mapPlayers[unusedPlayerId] = *p;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | delete(p);
|
|---|
| 692 |
|
|---|
| 693 | break;
|
|---|
| 694 | }
|
|---|
| 695 | case MSG_TYPE_LOGOUT:
|
|---|
| 696 | {
|
|---|
| 697 | string name(clientMsg.buffer);
|
|---|
| 698 | cout << "Player logging out: " << name << endl;
|
|---|
| 699 |
|
|---|
| 700 | Player *p = findPlayerByName(mapPlayers, name);
|
|---|
| 701 |
|
|---|
| 702 | if (p == NULL)
|
|---|
| 703 | {
|
|---|
| 704 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
|---|
| 705 | cout << "Player not logged in" << endl;
|
|---|
| 706 | }
|
|---|
| 707 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
|---|
| 708 | p->addr.sin_port != from.sin_port )
|
|---|
| 709 | {
|
|---|
| 710 | strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
|---|
| 711 | cout << "Player logged in using a different connection" << endl;
|
|---|
| 712 | }
|
|---|
| 713 | else
|
|---|
| 714 | {
|
|---|
| 715 | if (!p->isDead) {
|
|---|
| 716 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 717 | if (p->hasBlueFlag)
|
|---|
| 718 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 719 | else if (p->hasRedFlag)
|
|---|
| 720 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 721 |
|
|---|
| 722 | if (flagType != WorldMap::OBJECT_NONE) {
|
|---|
| 723 | addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
|
|---|
| 724 | }
|
|---|
| 725 | }
|
|---|
| 726 |
|
|---|
| 727 | if (p->id < unusedPlayerId)
|
|---|
| 728 | unusedPlayerId = p->id;
|
|---|
| 729 | mapPlayers.erase(p->id);
|
|---|
| 730 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | serverMsg.type = MSG_TYPE_LOGOUT;
|
|---|
| 734 |
|
|---|
| 735 | break;
|
|---|
| 736 | }
|
|---|
| 737 | case MSG_TYPE_CHAT:
|
|---|
| 738 | {
|
|---|
| 739 | cout << "Got a chat message" << endl;
|
|---|
| 740 |
|
|---|
| 741 | Player *p = findPlayerByAddr(mapPlayers, from);
|
|---|
| 742 |
|
|---|
| 743 | if (p == NULL)
|
|---|
| 744 | {
|
|---|
| 745 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
|---|
| 746 | }
|
|---|
| 747 | else
|
|---|
| 748 | {
|
|---|
| 749 | broadcastResponse = true;
|
|---|
| 750 |
|
|---|
| 751 | ostringstream oss;
|
|---|
| 752 | oss << p->name << ": " << clientMsg.buffer;
|
|---|
| 753 |
|
|---|
| 754 | strcpy(serverMsg.buffer, oss.str().c_str());
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | serverMsg.type = MSG_TYPE_CHAT;
|
|---|
| 758 |
|
|---|
| 759 | break;
|
|---|
| 760 | }
|
|---|
| 761 | case MSG_TYPE_PLAYER_MOVE:
|
|---|
| 762 | {
|
|---|
| 763 | cout << "PLAYER_MOVE" << endl;
|
|---|
| 764 |
|
|---|
| 765 | int id, x, y;
|
|---|
| 766 |
|
|---|
| 767 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 768 | memcpy(&x, clientMsg.buffer+4, 4);
|
|---|
| 769 | memcpy(&y, clientMsg.buffer+8, 4);
|
|---|
| 770 |
|
|---|
| 771 | cout << "x: " << x << endl;
|
|---|
| 772 | cout << "y: " << y << endl;
|
|---|
| 773 | cout << "id: " << id << endl;
|
|---|
| 774 |
|
|---|
| 775 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
|---|
| 776 | mapPlayers[id].addr.sin_port == from.sin_port )
|
|---|
| 777 | {
|
|---|
| 778 | // we need to make sure the player can move here
|
|---|
| 779 | if (0 <= x && x < gameMap->width*25 && 0 <= y && y < gameMap->height*25 &&
|
|---|
| 780 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
|---|
| 781 | {
|
|---|
| 782 | cout << "valid terrain" << endl;
|
|---|
| 783 |
|
|---|
| 784 | mapPlayers[id].target.x = x;
|
|---|
| 785 | mapPlayers[id].target.y = y;
|
|---|
| 786 |
|
|---|
| 787 | mapPlayers[id].isChasing = false;
|
|---|
| 788 | mapPlayers[id].isAttacking = false;
|
|---|
| 789 |
|
|---|
| 790 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
|---|
| 791 |
|
|---|
| 792 | memcpy(serverMsg.buffer, &id, 4);
|
|---|
| 793 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
|---|
| 794 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
|---|
| 795 |
|
|---|
| 796 | broadcastResponse = true;
|
|---|
| 797 | }
|
|---|
| 798 | else
|
|---|
| 799 | cout << "Bad terrain detected" << endl;
|
|---|
| 800 | }
|
|---|
| 801 | else // nned to send back a message indicating failure
|
|---|
| 802 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
|---|
| 803 |
|
|---|
| 804 | break;
|
|---|
| 805 | }
|
|---|
| 806 | case MSG_TYPE_PICKUP_FLAG:
|
|---|
| 807 | {
|
|---|
| 808 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
|---|
| 809 | cout << "PICKUP_FLAG" << endl;
|
|---|
| 810 |
|
|---|
| 811 | int id;
|
|---|
| 812 |
|
|---|
| 813 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 814 | cout << "id: " << id << endl;
|
|---|
| 815 |
|
|---|
| 816 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 817 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 818 |
|
|---|
| 819 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
|---|
| 820 | POSITION pos = itObjects->pos;
|
|---|
| 821 | bool gotFlag = false;
|
|---|
| 822 |
|
|---|
| 823 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
|---|
| 824 | switch (itObjects->type) {
|
|---|
| 825 | case WorldMap::OBJECT_BLUE_FLAG:
|
|---|
| 826 | if (mapPlayers[id].team == 1) {
|
|---|
| 827 | gotFlag = true;
|
|---|
| 828 | mapPlayers[id].hasBlueFlag = true;
|
|---|
| 829 | broadcastResponse = true;
|
|---|
| 830 | }
|
|---|
| 831 | break;
|
|---|
| 832 | case WorldMap::OBJECT_RED_FLAG:
|
|---|
| 833 | if (mapPlayers[id].team == 0) {
|
|---|
| 834 | gotFlag = true;
|
|---|
| 835 | mapPlayers[id].hasRedFlag = true;
|
|---|
| 836 | broadcastResponse = true;
|
|---|
| 837 | }
|
|---|
| 838 | break;
|
|---|
| 839 | }
|
|---|
| 840 |
|
|---|
| 841 | if (gotFlag) {
|
|---|
| 842 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
|---|
| 843 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
|---|
| 844 |
|
|---|
| 845 | map<unsigned int, Player>::iterator it;
|
|---|
| 846 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 847 | {
|
|---|
| 848 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
|
|---|
| 849 | error("sendMessage");
|
|---|
| 850 | }
|
|---|
| 851 |
|
|---|
| 852 | // remove the object from the server-side map
|
|---|
| 853 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
|---|
| 854 | itObjects = vctObjects->erase(itObjects);
|
|---|
| 855 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
|---|
| 856 | }
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | if (!gotFlag)
|
|---|
| 860 | itObjects++;
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 864 | mapPlayers[id].serialize(serverMsg.buffer);
|
|---|
| 865 |
|
|---|
| 866 | break;
|
|---|
| 867 | }
|
|---|
| 868 | case MSG_TYPE_DROP_FLAG:
|
|---|
| 869 | {
|
|---|
| 870 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
|---|
| 871 | cout << "DROP_FLAG" << endl;
|
|---|
| 872 |
|
|---|
| 873 | int id;
|
|---|
| 874 |
|
|---|
| 875 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 876 | cout << "id: " << id << endl;
|
|---|
| 877 |
|
|---|
| 878 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 879 | if (mapPlayers[id].hasBlueFlag)
|
|---|
| 880 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 881 | else if (mapPlayers[id].hasRedFlag)
|
|---|
| 882 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 883 |
|
|---|
| 884 | addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
|
|---|
| 885 |
|
|---|
| 886 | mapPlayers[id].hasBlueFlag = false;
|
|---|
| 887 | mapPlayers[id].hasRedFlag = false;
|
|---|
| 888 |
|
|---|
| 889 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 890 | mapPlayers[id].serialize(serverMsg.buffer);
|
|---|
| 891 |
|
|---|
| 892 | broadcastResponse = true;
|
|---|
| 893 |
|
|---|
| 894 | break;
|
|---|
| 895 | }
|
|---|
| 896 | case MSG_TYPE_START_ATTACK:
|
|---|
| 897 | {
|
|---|
| 898 | cout << "Received a START_ATTACK message" << endl;
|
|---|
| 899 |
|
|---|
| 900 | int id, targetId;
|
|---|
| 901 |
|
|---|
| 902 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 903 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
|---|
| 904 |
|
|---|
| 905 | Player* source = &mapPlayers[id];
|
|---|
| 906 | source->targetPlayer = targetId;
|
|---|
| 907 | source->isChasing = true;
|
|---|
| 908 |
|
|---|
| 909 | // this is irrelevant since the client doesn't even listen for START_ATTACK messages
|
|---|
| 910 | // actually, the client should not ignore this and should instead perform the same movement
|
|---|
| 911 | // algorithm on its end (following the target player until in range) that the server does.
|
|---|
| 912 | // Once the attacker is in range, the client should stop movement and wait for messages
|
|---|
| 913 | // from the server
|
|---|
| 914 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
|---|
| 915 | memcpy(serverMsg.buffer, &id, 4);
|
|---|
| 916 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
|---|
| 917 | broadcastResponse = true;
|
|---|
| 918 |
|
|---|
| 919 | break;
|
|---|
| 920 | }
|
|---|
| 921 | case MSG_TYPE_ATTACK:
|
|---|
| 922 | {
|
|---|
| 923 | cout << "Received am ATTACK message" << endl;
|
|---|
| 924 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
|---|
| 925 |
|
|---|
| 926 | break;
|
|---|
| 927 | }
|
|---|
| 928 | default:
|
|---|
| 929 | {
|
|---|
| 930 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
|---|
| 931 |
|
|---|
| 932 | serverMsg.type = MSG_TYPE_CHAT;
|
|---|
| 933 |
|
|---|
| 934 | break;
|
|---|
| 935 | }
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | return broadcastResponse;
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
|---|
| 942 | {
|
|---|
| 943 | while (mapPlayers.find(id) != mapPlayers.end())
|
|---|
| 944 | id++;
|
|---|
| 945 | }
|
|---|
| 946 |
|
|---|
| 947 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
|
|---|
| 948 | {
|
|---|
| 949 | while (mapProjectiles.find(id) != mapProjectiles.end())
|
|---|
| 950 | id++;
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | void damagePlayer(Player *p, int damage) {
|
|---|
| 954 | p->health -= damage;
|
|---|
| 955 | if (p->health < 0)
|
|---|
| 956 | p->health = 0;
|
|---|
| 957 | if (p->health == 0) {
|
|---|
| 958 | cout << "Player died" << endl;
|
|---|
| 959 | p->isDead = true;
|
|---|
| 960 | p->timeDied = getCurrentMillis();
|
|---|
| 961 | }
|
|---|
| 962 | }
|
|---|
| 963 |
|
|---|
| 964 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
|
|---|
| 965 | NETWORK_MSG serverMsg;
|
|---|
| 966 |
|
|---|
| 967 | gameMap->addObject(objectType, x, y);
|
|---|
| 968 |
|
|---|
| 969 | // need to send the OBJECT message too
|
|---|
| 970 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 971 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
|---|
| 972 |
|
|---|
| 973 | map<unsigned int, Player>::iterator it;
|
|---|
| 974 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 975 | {
|
|---|
| 976 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
|
|---|
| 977 | error("sendMessage");
|
|---|
| 978 | }
|
|---|
| 979 | }
|
|---|