Changeset 95ffe57 in network-game
- Timestamp:
- Oct 4, 2013, 5:06:20 PM (11 years ago)
- Branches:
- master
- Children:
- 90eaad2
- Parents:
- e6c26b8
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
re6c26b8 r95ffe57 674 674 { 675 675 wndCurrent = wndLobby; 676 677 Player p("", ""); 678 p.deserialize(msg.buffer); 679 680 if (mapPlayers.find(p.id) != mapPlayers.end()) 681 delete mapPlayers[p.id]; 682 mapPlayers[p.id] = new Player(p); 683 curPlayerId = p.id; 676 677 // this message should only be sent when a player first logs in so they know their id 678 679 Player* p = new Player("", ""); 680 p->deserialize(msg.buffer); 681 682 if (mapPlayers.find(p->id) != mapPlayers.end()) 683 delete mapPlayers[p->id]; 684 mapPlayers[p->id] = p; 685 curPlayerId = p->id; 684 686 685 687 cout << "Got a valid login response with the player" << endl; 686 688 cout << "Player id: " << curPlayerId << endl; 687 cout << "Player health: " << p .health << endl;689 cout << "Player health: " << p->health << endl; 688 690 cout << "player map size: " << mapPlayers.size() << endl; 689 691 } … … 708 710 cout << "Received MSG_TYPE_PLAYER" << endl; 709 711 710 Player p("", "");711 p .deserialize(msg.buffer);712 p .timeLastUpdated = getCurrentMillis();713 p .isChasing = false;714 if (p .health <= 0)715 p .isDead = true;712 Player* p = new Player("", ""); 713 p->deserialize(msg.buffer); 714 p->timeLastUpdated = getCurrentMillis(); 715 p->isChasing = false; 716 if (p->health <= 0) 717 p->isDead = true; 716 718 else 717 p .isDead = false;718 719 if (mapPlayers.find(p .id) != mapPlayers.end())720 delete mapPlayers[p .id];721 mapPlayers[p .id] = new Player(p);719 p->isDead = false; 720 721 if (mapPlayers.find(p->id) != mapPlayers.end()) 722 delete mapPlayers[p->id]; 723 mapPlayers[p->id] = p; 722 724 723 725 break; -
server/server.cpp
re6c26b8 r95ffe57 45 45 // from used to be const. Removed that so I could take a reference 46 46 // and use it to send messages 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);48 49 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player >& mapPlayers);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); 48 49 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers); 50 50 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles); 51 Player *findPlayerByName(map<unsigned int, Player*> &m, string name); 52 Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr); 51 53 void damagePlayer(Player *p, int damage); 52 54 53 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player >& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);55 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog); 54 56 55 57 // this should probably go somewhere in the common folder … … 58 60 perror(msg); 59 61 exit(0); 60 }61 62 Player *findPlayerByName(map<unsigned int, Player> &m, string name)63 {64 map<unsigned int, Player>::iterator it;65 66 for (it = m.begin(); it != m.end(); it++)67 {68 if ( it->second.name.compare(name) == 0 )69 return &(it->second);70 }71 72 return NULL;73 }74 75 Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)76 {77 map<unsigned int, Player>::iterator it;78 79 for (it = m.begin(); it != m.end(); it++)80 {81 if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&82 it->second.addr.sin_port == addr.sin_port )83 return &(it->second);84 }85 86 return NULL;87 62 } 88 63 … … 98 73 NETWORK_MSG clientMsg, serverMsg; 99 74 MessageProcessor msgProcessor; 100 map<unsigned int, Player > mapPlayers;75 map<unsigned int, Player*> mapPlayers; 101 76 map<unsigned int, Projectile> mapProjectiles; 102 77 map<string, Game*> mapGames; … … 116 91 //OpenSSL_add_all_algorithms(); 117 92 118 if (argc < 2) { 119 cerr << "ERROR, no port provided" << endl; 93 if (argc != 2) 94 { 95 cerr << "ERROR, expected server [domain] [port]" << endl; 120 96 exit(1); 121 97 } … … 128 104 // add some items to the map. They will be sent out 129 105 // to players when they login 130 for (int y=0; y<gameMap->height; y++) { 131 for (int x=0; x<gameMap->width; x++) { 132 switch (gameMap->getStructure(x, y)) { 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 { 133 112 case WorldMap::STRUCTURE_BLUE_FLAG: 134 113 gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12); … … 157 136 timespec ts; 158 137 int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0; 159 while (!done) { 138 while (!done) 139 { 160 140 161 141 usleep(5000); … … 166 146 curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000; 167 147 168 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) { 148 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) 149 { 169 150 timeLastUpdated = curTime; 170 151 … … 172 153 msgProcessor.resendUnackedMessages(sock, &outputLog); 173 154 174 map<unsigned int, Player >::iterator it;155 map<unsigned int, Player*>::iterator it; 175 156 176 157 // set targets for all chasing players (or make them attack if they're close enough) 177 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 158 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 159 { 178 160 // check if it's time to revive dead players 179 if (it->second.isDead) { 180 if (getCurrentMillis() - it->second.timeDied >= 10000) { 181 it->second.isDead = false; 161 if (it->second->isDead) 162 { 163 if (getCurrentMillis() - it->second->timeDied >= 10000) 164 { 165 it->second->isDead = false; 182 166 183 167 POSITION spawnPos; 184 168 185 switch (it->second.team) { 169 switch (it->second->team) 170 { 186 171 case 0:// blue team 187 172 spawnPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG); … … 200 185 spawnPos.y = spawnPos.y * 25 + 12; 201 186 202 it->second .pos = spawnPos.toFloat();203 it->second .target = spawnPos;204 it->second .health = it->second.maxHealth;187 it->second->pos = spawnPos.toFloat(); 188 it->second->target = spawnPos; 189 it->second->health = it->second->maxHealth; 205 190 206 191 serverMsg.type = MSG_TYPE_PLAYER; 207 it->second .serialize(serverMsg.buffer);208 209 map<unsigned int, Player >::iterator it2;192 it->second->serialize(serverMsg.buffer); 193 194 map<unsigned int, Player*>::iterator it2; 210 195 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 211 196 { 212 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )197 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 213 198 error("sendMessage"); 214 199 } … … 218 203 } 219 204 220 if (it->second.updateTarget(mapPlayers)) { 205 if (it->second->updateTarget(mapPlayers)) 206 { 221 207 serverMsg.type = MSG_TYPE_PLAYER; 222 it->second .serialize(serverMsg.buffer);223 224 map<unsigned int, Player >::iterator it2;208 it->second->serialize(serverMsg.buffer); 209 210 map<unsigned int, Player*>::iterator it2; 225 211 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 226 212 { 227 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )213 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 228 214 error("sendMessage"); 229 215 } … … 235 221 FLOAT_POSITION oldPos; 236 222 bool broadcastMove = false; 237 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 238 oldPos = it->second.pos; 239 if (it->second.move(gameMap)) { 223 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 224 { 225 oldPos = it->second->pos; 226 if (it->second->move(gameMap)) { 240 227 241 228 // check if the move needs to be canceled 242 switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) { 229 switch(gameMap->getElement(it->second->pos.x/25, it->second->pos.y/25)) 230 { 243 231 case WorldMap::TERRAIN_NONE: 244 232 case WorldMap::TERRAIN_OCEAN: 245 233 case WorldMap::TERRAIN_ROCK: 246 234 { 247 it->second .pos = oldPos;248 it->second .target.x = it->second.pos.x;249 it->second .target.y = it->second.pos.y;250 it->second .isChasing = false;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; 251 239 broadcastMove = true; 252 240 break; … … 263 251 bool ownFlagAtBase = false; 264 252 265 switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) { 253 switch(gameMap->getStructure(it->second->pos.x/25, it->second->pos.y/25)) 254 { 266 255 case WorldMap::STRUCTURE_BLUE_FLAG: 267 256 { 268 if (it->second .team == 0 && it->second.hasRedFlag)257 if (it->second->team == 0 && it->second->hasRedFlag) 269 258 { 270 259 // check that your flag is at your base … … 274 263 vector<WorldMap::Object>::iterator itObjects; 275 264 276 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) { 277 if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) { 278 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) { 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 { 279 271 ownFlagAtBase = true; 280 272 break; … … 283 275 } 284 276 285 if (ownFlagAtBase) { 286 it->second.hasRedFlag = false; 277 if (ownFlagAtBase) 278 { 279 it->second->hasRedFlag = false; 287 280 flagType = WorldMap::OBJECT_RED_FLAG; 288 281 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG); … … 296 289 case WorldMap::STRUCTURE_RED_FLAG: 297 290 { 298 if (it->second .team == 1 && it->second.hasBlueFlag)291 if (it->second->team == 1 && it->second->hasBlueFlag) 299 292 { 300 293 // check that your flag is at your base … … 304 297 vector<WorldMap::Object>::iterator itObjects; 305 298 306 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) { 307 if (itObjects->type == WorldMap::OBJECT_RED_FLAG) { 308 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) { 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 { 309 305 ownFlagAtBase = true; 310 306 break; … … 313 309 } 314 310 315 if (ownFlagAtBase) { 316 it->second.hasBlueFlag = false; 311 if (ownFlagAtBase) 312 { 313 it->second->hasBlueFlag = false; 317 314 flagType = WorldMap::OBJECT_BLUE_FLAG; 318 315 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG); … … 326 323 } 327 324 328 if (flagTurnedIn) { 325 if (flagTurnedIn) 326 { 329 327 // send an OBJECT message to add the flag back to its spawn point 330 328 pos.x = pos.x*25+12; … … 335 333 gameMap->getObjects()->back().serialize(serverMsg.buffer); 336 334 337 map<unsigned int, Player >::iterator it2;335 map<unsigned int, Player*>::iterator it2; 338 336 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 339 337 { 340 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )338 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 341 339 error("sendMessage"); 342 340 } … … 348 346 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 349 347 { 350 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )348 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 351 349 error("sendMessage"); 352 350 } … … 361 359 POSITION structPos; 362 360 363 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) { 361 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) 362 { 364 363 POSITION pos = itObjects->pos; 365 364 366 if (posDistance(it->second.pos, pos.toFloat()) < 10) { 367 if (it->second.team == 0 && 368 itObjects->type == WorldMap::OBJECT_BLUE_FLAG) { 365 if (posDistance(it->second->pos, pos.toFloat()) < 10) 366 { 367 if (it->second->team == 0 && 368 itObjects->type == WorldMap::OBJECT_BLUE_FLAG) 369 { 369 370 structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG); 370 371 flagReturned = true; 371 372 break; 372 } else if (it->second.team == 1 && 373 itObjects->type == WorldMap::OBJECT_RED_FLAG) { 373 } 374 else if (it->second->team == 1 && 375 itObjects->type == WorldMap::OBJECT_RED_FLAG) 376 { 374 377 structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG); 375 378 flagReturned = true; … … 379 382 } 380 383 381 if (flagReturned) { 384 if (flagReturned) 385 { 382 386 itObjects->pos.x = structPos.x*25+12; 383 387 itObjects->pos.y = structPos.y*25+12; … … 386 390 itObjects->serialize(serverMsg.buffer); 387 391 388 map<unsigned int, Player >::iterator it2;392 map<unsigned int, Player*>::iterator it2; 389 393 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 390 394 { 391 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )395 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 392 396 error("sendMessage"); 393 397 } 394 398 } 395 399 396 if (broadcastMove) { 400 if (broadcastMove) 401 { 397 402 serverMsg.type = MSG_TYPE_PLAYER; 398 it->second .serialize(serverMsg.buffer);403 it->second->serialize(serverMsg.buffer); 399 404 400 405 cout << "about to broadcast move" << endl; 401 map<unsigned int, Player >::iterator it2;406 map<unsigned int, Player*>::iterator it2; 402 407 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 403 408 { 404 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )409 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 405 410 error("sendMessage"); 406 411 } … … 409 414 410 415 // check if the player's attack animation is complete 411 if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) { 412 it->second.isAttacking = false; 416 if (it->second->isAttacking && it->second->timeAttackStarted+it->second->attackCooldown <= getCurrentMillis()) 417 { 418 it->second->isAttacking = false; 413 419 cout << "Attack animation is complete" << endl; 414 420 … … 417 423 418 424 serverMsg.type = MSG_TYPE_ATTACK; 419 memcpy(serverMsg.buffer, &it->second .id, 4);420 memcpy(serverMsg.buffer+4, &it->second .targetPlayer, 4);421 422 map<unsigned int, Player >::iterator it2;425 memcpy(serverMsg.buffer, &it->second->id, 4); 426 memcpy(serverMsg.buffer+4, &it->second->targetPlayer, 4); 427 428 map<unsigned int, Player*>::iterator it2; 423 429 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 424 430 { 425 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )431 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 426 432 error("sendMessage"); 427 433 } 428 434 429 if (it->second.attackType == Player::ATTACK_MELEE) { 435 if (it->second->attackType == Player::ATTACK_MELEE) 436 { 430 437 cout << "Melee attack" << endl; 431 438 432 Player* target = &mapPlayers[it->second.targetPlayer]; 433 damagePlayer(target, it->second.damage); 434 435 if (target->isDead) { 439 Player* target = mapPlayers[it->second->targetPlayer]; 440 damagePlayer(target, it->second->damage); 441 442 if (target->isDead) 443 { 436 444 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE; 437 445 if (target->hasBlueFlag) … … 447 455 serverMsg.type = MSG_TYPE_PLAYER; 448 456 target->serialize(serverMsg.buffer); 449 }else if (it->second.attackType == Player::ATTACK_RANGED) { 457 } 458 else if (it->second->attackType == Player::ATTACK_RANGED) 459 { 450 460 cout << "Ranged attack" << endl; 451 461 452 Projectile proj(it->second .pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);462 Projectile proj(it->second->pos.x, it->second->pos.y, it->second->targetPlayer, it->second->damage); 453 463 proj.id = unusedProjectileId; 454 464 updateUnusedProjectileId(unusedProjectileId, mapProjectiles); 455 465 mapProjectiles[proj.id] = proj; 456 466 457 int x = it->second .pos.x;458 int y = it->second .pos.y;467 int x = it->second->pos.x; 468 int y = it->second->pos.y; 459 469 460 470 serverMsg.type = MSG_TYPE_PROJECTILE; … … 462 472 memcpy(serverMsg.buffer+4, &x, 4); 463 473 memcpy(serverMsg.buffer+8, &y, 4); 464 memcpy(serverMsg.buffer+12, &it->second .targetPlayer, 4);465 } else {466 cout << "Invalid attack type: " << it->second.attackType << endl;467 }474 memcpy(serverMsg.buffer+12, &it->second->targetPlayer, 4); 475 } 476 else 477 cout << "Invalid attack type: " << it->second->attackType << endl; 468 478 469 479 // broadcast either a PLAYER or PROJECTILE message … … 471 481 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 472 482 { 473 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )483 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 474 484 error("sendMessage"); 475 485 } … … 480 490 // move all projectiles 481 491 map<unsigned int, Projectile>::iterator itProj; 482 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) { 492 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) 493 { 483 494 cout << "About to call projectile move" << endl; 484 if (itProj->second.move(mapPlayers)) { 495 if (itProj->second.move(mapPlayers)) 496 { 485 497 // send a REMOVE_PROJECTILE message 486 498 cout << "send a REMOVE_PROJECTILE message" << endl; … … 489 501 mapProjectiles.erase(itProj->second.id); 490 502 491 map<unsigned int, Player >::iterator it2;503 map<unsigned int, Player*>::iterator it2; 492 504 cout << "Broadcasting REMOVE_PROJECTILE" << endl; 493 505 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 494 506 { 495 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )507 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 496 508 error("sendMessage"); 497 509 } … … 499 511 cout << "send a PLAYER message after dealing damage" << endl; 500 512 // send a PLAYER message after dealing damage 501 Player* target = &mapPlayers[itProj->second.target];513 Player* target = mapPlayers[itProj->second.target]; 502 514 503 515 damagePlayer(target, itProj->second.damage); 504 516 505 if (target->isDead) { 517 if (target->isDead) 518 { 506 519 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE; 507 520 if (target->hasBlueFlag) … … 510 523 flagType = WorldMap::OBJECT_RED_FLAG; 511 524 512 if (flagType != WorldMap::OBJECT_NONE) {525 if (flagType != WorldMap::OBJECT_NONE) 513 526 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog); 514 }515 527 } 516 528 … … 521 533 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 522 534 { 523 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second .addr), &outputLog) < 0 )535 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 524 536 error("sendMessage"); 525 537 } … … 531 543 n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog); 532 544 533 if (n >= 0) { 545 if (n >= 0) 546 { 534 547 broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog); 535 548 … … 538 551 cout << "Should be broadcasting the message" << endl; 539 552 540 map<unsigned int, Player >::iterator it;553 map<unsigned int, Player*>::iterator it; 541 554 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 542 555 { 543 cout << "Sent message back to " << it->second .name << endl;544 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second .addr), &outputLog) < 0 )556 cout << "Sent message back to " << it->second->name << endl; 557 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 545 558 error("sendMessage"); 546 559 } … … 561 574 // delete all games 562 575 map<string, Game*>::iterator itGames; 563 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) { 576 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) 577 { 564 578 delete itGames->second; 565 579 } 566 580 581 map<unsigned int, Player*>::iterator itPlayers; 582 for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++) 583 { 584 delete itPlayers->second; 585 } 586 567 587 return 0; 568 588 } 569 589 570 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)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) 571 591 { 572 592 DataAccess da; … … 593 613 memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4); 594 614 serverMsg.type = MSG_TYPE_REGISTER; 595 596 615 597 616 cout << "username: " << username << endl; … … 629 648 { 630 649 strcpy(serverMsg.buffer, "Incorrect username or password"); 650 if (p != NULL) 651 delete(p); 631 652 } 632 653 else if(findPlayerByName(mapPlayers, username) != NULL) 633 654 { 634 655 strcpy(serverMsg.buffer, "Player has already logged in."); 656 delete(p); 635 657 } 636 658 else … … 649 671 cout << "Sending other players to new player" << endl; 650 672 651 map<unsigned int, Player >::iterator it;673 map<unsigned int, Player*>::iterator it; 652 674 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 653 675 { 654 it->second .serialize(serverMsg.buffer);655 656 cout << "sending info about " << it->second .name << endl;657 cout << "sending id " << it->second .id << endl;676 it->second->serialize(serverMsg.buffer); 677 678 cout << "sending info about " << it->second->name << endl; 679 cout << "sending id " << it->second->id << endl; 658 680 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 659 681 error("sendMessage"); … … 686 708 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 687 709 { 688 cout << "Sent message back to " << it->second .name << endl;689 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second .addr), &outputLog) < 0 )710 cout << "Sent message back to " << it->second->name << endl; 711 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 690 712 error("sendMessage"); 691 713 } 692 714 693 mapPlayers[unusedPlayerId] = *p;715 mapPlayers[unusedPlayerId] = p; 694 716 } 695 717 696 718 serverMsg.type = MSG_TYPE_LOGIN; 697 delete(p);698 719 699 720 break; … … 734 755 unusedPlayerId = p->id; 735 756 mapPlayers.erase(p->id); 757 delete p; 736 758 strcpy(serverMsg.buffer, "You have successfully logged out."); 737 759 } … … 779 801 cout << "id: " << id << endl; 780 802 781 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr && 782 mapPlayers[id].addr.sin_port == from.sin_port ) 803 Player* p = mapPlayers[id]; 804 805 if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr && 806 p->addr.sin_port == from.sin_port ) 783 807 { 784 808 // we need to make sure the player can move here … … 788 812 cout << "valid terrain" << endl; 789 813 790 mapPlayers[id].target.x = x;791 mapPlayers[id].target.y = y;792 793 mapPlayers[id].isChasing = false;794 mapPlayers[id].isAttacking = false;814 p->target.x = x; 815 p->target.y = y; 816 817 p->isChasing = false; 818 p->isAttacking = false; 795 819 796 820 serverMsg.type = MSG_TYPE_PLAYER_MOVE; 797 821 798 822 memcpy(serverMsg.buffer, &id, 4); 799 memcpy(serverMsg.buffer+4, & mapPlayers[id].target.x, 4);800 memcpy(serverMsg.buffer+8, & mapPlayers[id].target.y, 4);823 memcpy(serverMsg.buffer+4, &p->target.x, 4); 824 memcpy(serverMsg.buffer+8, &p->target.y, 4); 801 825 802 826 broadcastResponse = true; … … 820 844 cout << "id: " << id << endl; 821 845 846 Player* p = mapPlayers[id]; 847 822 848 vector<WorldMap::Object>* vctObjects = gameMap->getObjects(); 823 849 vector<WorldMap::Object>::iterator itObjects; … … 827 853 bool gotFlag = false; 828 854 829 if (posDistance( mapPlayers[id].pos, pos.toFloat()) < 10) {855 if (posDistance(p->pos, pos.toFloat()) < 10) { 830 856 switch (itObjects->type) { 831 857 case WorldMap::OBJECT_BLUE_FLAG: 832 if ( mapPlayers[id].team == 1) {858 if (p->team == 1) { 833 859 gotFlag = true; 834 mapPlayers[id].hasBlueFlag = true;860 p->hasBlueFlag = true; 835 861 broadcastResponse = true; 836 862 } 837 863 break; 838 864 case WorldMap::OBJECT_RED_FLAG: 839 if ( mapPlayers[id].team == 0) {865 if (p->team == 0) { 840 866 gotFlag = true; 841 mapPlayers[id].hasRedFlag = true;867 p->hasRedFlag = true; 842 868 broadcastResponse = true; 843 869 } … … 849 875 memcpy(serverMsg.buffer, &itObjects->id, 4); 850 876 851 map<unsigned int, Player >::iterator it;877 map<unsigned int, Player*>::iterator it; 852 878 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 853 879 { 854 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second .addr), &outputLog) < 0 )880 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 855 881 error("sendMessage"); 856 882 } … … 868 894 869 895 serverMsg.type = MSG_TYPE_PLAYER; 870 mapPlayers[id].serialize(serverMsg.buffer);896 p->serialize(serverMsg.buffer); 871 897 872 898 break; … … 882 908 cout << "id: " << id << endl; 883 909 910 Player* p = mapPlayers[id]; 911 884 912 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE; 885 if ( mapPlayers[id].hasBlueFlag)913 if (p->hasBlueFlag) 886 914 flagType = WorldMap::OBJECT_BLUE_FLAG; 887 else if ( mapPlayers[id].hasRedFlag)915 else if (p->hasRedFlag) 888 916 flagType = WorldMap::OBJECT_RED_FLAG; 889 917 890 addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);891 892 mapPlayers[id].hasBlueFlag = false;893 mapPlayers[id].hasRedFlag = false;918 addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog); 919 920 p->hasBlueFlag = false; 921 p->hasRedFlag = false; 894 922 895 923 serverMsg.type = MSG_TYPE_PLAYER; 896 mapPlayers[id].serialize(serverMsg.buffer);924 p->serialize(serverMsg.buffer); 897 925 898 926 broadcastResponse = true; … … 909 937 memcpy(&targetId, clientMsg.buffer+4, 4); 910 938 911 Player* source = &mapPlayers[id];939 Player* source = mapPlayers[id]; 912 940 source->targetPlayer = targetId; 913 941 source->isChasing = true; … … 1010 1038 cout << "Game name: " << g->getName() << endl; 1011 1039 p->currentGame = NULL; 1040 1041 serverMsg.type = MSG_TYPE_LEAVE_GAME; 1042 memcpy(serverMsg.buffer, &p->id, 4); 1043 strcpy(serverMsg.buffer+4, g->getName().c_str()); 1044 1045 map<unsigned int, Player*>& players = g->getPlayers(); 1046 1047 map<unsigned int, Player*>::iterator it; 1048 for (it = players.begin(); it != players.end(); it++) 1049 { 1050 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1051 error("sendMessage"); 1052 } 1053 1012 1054 g->removePlayer(p->id); 1013 1055 … … 1048 1090 // tell the new player about all the existing players 1049 1091 cout << "Sending other players to new player" << endl; 1050 serverMsg.type = MSG_TYPE_ LOGIN;1092 serverMsg.type = MSG_TYPE_PLAYER; 1051 1093 1052 1094 map<unsigned int, Player*>::iterator it; … … 1120 1162 } 1121 1163 1122 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player >& mapPlayers)1164 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers) 1123 1165 { 1124 1166 while (mapPlayers.find(id) != mapPlayers.end()) … … 1130 1172 while (mapProjectiles.find(id) != mapProjectiles.end()) 1131 1173 id++; 1174 } 1175 1176 Player *findPlayerByName(map<unsigned int, Player*> &m, string name) 1177 { 1178 map<unsigned int, Player*>::iterator it; 1179 1180 for (it = m.begin(); it != m.end(); it++) 1181 { 1182 if ( it->second->name.compare(name) == 0 ) 1183 return it->second; 1184 } 1185 1186 return NULL; 1187 } 1188 1189 Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr) 1190 { 1191 map<unsigned int, Player*>::iterator it; 1192 1193 for (it = m.begin(); it != m.end(); it++) 1194 { 1195 if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr && 1196 it->second->addr.sin_port == addr.sin_port ) 1197 return it->second; 1198 } 1199 1200 return NULL; 1132 1201 } 1133 1202 … … 1143 1212 } 1144 1213 1145 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player >& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {1214 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) { 1146 1215 NETWORK_MSG serverMsg; 1147 1216 … … 1152 1221 gameMap->getObjects()->back().serialize(serverMsg.buffer); 1153 1222 1154 map<unsigned int, Player >::iterator it;1223 map<unsigned int, Player*>::iterator it; 1155 1224 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 1156 1225 { 1157 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second .addr), &outputLog) < 0 )1226 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1158 1227 error("sendMessage"); 1159 1228 }
Note:
See TracChangeset
for help on using the changeset viewer.