- Timestamp:
- Dec 24, 2013, 8:06:30 PM (11 years ago)
- Branches:
- master
- Children:
- 9c18cb7
- Parents:
- 68d94de
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/server.cpp
r68d94de r8554263 46 46 // from used to be const. Removed that so I could take a reference 47 47 // and use it to send messages 48 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog); 49 48 void 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 &scoreBlue, int &scoreRed); 49 50 void broadcastMessage(MessageProcessor &msgProcessor, NETWORK_MSG &serverMsg, map<unsigned int, Player*>& players); 50 51 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers); 51 52 Player *findPlayerByName(map<unsigned int, Player*> &m, string name); … … 53 54 void damagePlayer(Player *p, int damage); 54 55 55 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog); 56 57 // this should probably go somewhere in the common folder 58 void error(const char *msg) 59 { 60 perror(msg); 61 exit(0); 62 } 56 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor); 63 57 64 58 void quit(int sig) { … … 68 62 int main(int argc, char *argv[]) 69 63 { 70 int sock, length , n;64 int sock, length; 71 65 struct sockaddr_in server; 72 66 struct sockaddr_in from; // info of client sending the message … … 133 127 set_nonblock(sock); 134 128 135 bool broadcastResponse; 129 msgProcessor = MessageProcessor(sock, &outputLog); 130 136 131 timespec ts; 137 132 int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0; … … 149 144 timeLastUpdated = curTime; 150 145 151 msgProcessor.cleanAckedMessages( &outputLog);152 msgProcessor.resendUnackedMessages( sock, &outputLog);146 msgProcessor.cleanAckedMessages(); 147 msgProcessor.resendUnackedMessages(); 153 148 154 149 map<unsigned int, Player*>::iterator it; … … 157 152 158 153 // set targets for all chasing players (or make them attack if they're close enough) 154 // this should be moved into the games loop 159 155 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 160 156 { … … 197 193 p->serialize(serverMsg.buffer); 198 194 199 map<unsigned int, Player*>::iterator it2; 200 for (it2 = p->currentGame->getPlayers().begin(); it2 != p->currentGame->getPlayers().end(); it2++) 201 { 202 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 203 error("sendMessage"); 204 } 195 broadcastMessage(msgProcessor, serverMsg, p->currentGame->getPlayers()); 205 196 } 206 197 … … 215 206 p->serialize(serverMsg.buffer); 216 207 217 map<unsigned int, Player*>::iterator it2; 218 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 219 { 220 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 221 error("sendMessage"); 222 } 208 broadcastMessage(msgProcessor, serverMsg, playersInGame); 223 209 } 224 210 } … … 257 243 broadcastMove = true; 258 244 cout << "player move processed" << endl; 259 260 245 261 246 WorldMap::ObjectType flagType; … … 348 333 serverMsg.type = MSG_TYPE_OBJECT; 349 334 gameMap->getObjects()->back().serialize(serverMsg.buffer); 350 351 map<unsigned int, Player*>::iterator it2; 352 353 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 354 { 355 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 356 error("sendMessage"); 357 } 335 broadcastMessage(msgProcessor, serverMsg, playersInGame); 358 336 359 337 serverMsg.type = MSG_TYPE_SCORE; 360 338 memcpy(serverMsg.buffer, &scoreBlue, 4); 361 339 memcpy(serverMsg.buffer+4, &scoreRed, 4); 362 363 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 364 { 365 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 366 error("sendMessage"); 367 } 340 broadcastMessage(msgProcessor, serverMsg, playersInGame); 368 341 369 342 // check to see if the game should end … … 385 358 memcpy(serverMsg.buffer+8, &scoreRed, 4); 386 359 strcpy(serverMsg.buffer+12, game->getName().c_str()); 387 388 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 389 { 390 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 391 error("sendMessage"); 392 } 360 broadcastMessage(msgProcessor, serverMsg, playersInGame); 393 361 } 394 362 … … 432 400 serverMsg.type = MSG_TYPE_OBJECT; 433 401 itObjects->serialize(serverMsg.buffer); 434 435 map<unsigned int, Player*>::iterator it2; 436 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 437 { 438 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 439 error("sendMessage"); 440 } 402 broadcastMessage(msgProcessor, serverMsg, playersInGame); 441 403 } 442 404 443 405 if (broadcastMove) 444 406 { 445 cout << "broadcasting player move" << endl;446 407 serverMsg.type = MSG_TYPE_PLAYER; 447 408 p->serialize(serverMsg.buffer); 448 449 // only broadcast message to other players in the same game 450 cout << "about to broadcast move" << endl; 451 452 map<unsigned int, Player*>::iterator it2; 453 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 454 { 455 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 456 error("sendMessage"); 457 } 458 cout << "done broadcasting player move" << endl; 409 broadcastMessage(msgProcessor, serverMsg, playersInGame); 459 410 } 460 411 } … … 474 425 memcpy(serverMsg.buffer, &p->id, 4); 475 426 memcpy(serverMsg.buffer+4, &p->targetPlayer, 4); 476 477 map<unsigned int, Player*>::iterator it2; 478 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 479 { 480 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 481 error("sendMessage"); 482 } 427 broadcastMessage(msgProcessor, serverMsg, playersInGame); 483 428 484 429 if (p->attackType == Player::ATTACK_MELEE) … … 498 443 499 444 if (flagType != WorldMap::OBJECT_NONE) { 500 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor , sock, outputLog);445 addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor); 501 446 } 502 447 } … … 525 470 cout << "Invalid attack type: " << p->attackType << endl; 526 471 527 // broadcast either a PLAYER or PROJECTILE message 528 cout << "Broadcasting player or projectile message" << endl; 529 for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++) 530 { 531 if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 532 error("sendMessage"); 533 } 534 cout << "Done broadcasting" << endl; 472 broadcastMessage(msgProcessor, serverMsg, playersInGame); 535 473 } 536 474 } … … 541 479 serverMsg.type = MSG_TYPE_GAME_INFO; 542 480 memcpy(serverMsg.buffer, &numPlayers, 4); 543 544 map<unsigned int, Player*>::iterator it2; 545 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 546 { 547 if (it2->second->currentGame == game) 548 it2->second->currentGame = NULL; 549 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 550 error("sendMessage"); 551 } 481 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 552 482 553 483 // erase game from server … … 576 506 memcpy(serverMsg.buffer, &itProj->second.id, 4); 577 507 game->removeProjectile(itProj->second.id); 578 579 map<unsigned int, Player*>::iterator it2; 580 cout << "Broadcasting REMOVE_PROJECTILE" << endl; 581 for (it2 = game->getPlayers().begin(); it2 != game->getPlayers().end(); it2++) 582 { 583 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 584 error("sendMessage"); 585 } 586 587 cout << "send a PLAYER message after dealing damage" << endl; 588 // send a PLAYER message after dealing damage 508 broadcastMessage(msgProcessor, serverMsg, game->getPlayers()); 509 589 510 Player* target = game->getPlayers()[itProj->second.target]; 590 591 511 damagePlayer(target, itProj->second.damage); 592 512 … … 600 520 601 521 if (flagType != WorldMap::OBJECT_NONE) 602 addObjectToMap(flagType, target->pos.x, target->pos.y, game->getMap(), game->getPlayers(), msgProcessor , sock, outputLog);522 addObjectToMap(flagType, target->pos.x, target->pos.y, game->getMap(), game->getPlayers(), msgProcessor); 603 523 } 604 524 525 // send a PLAYER message after dealing damage 605 526 serverMsg.type = MSG_TYPE_PLAYER; 606 527 target->serialize(serverMsg.buffer); 607 608 cout << "Sending a PLAYER message" << endl; 609 for (it2 = game->getPlayers().begin(); it2 != game->getPlayers().end(); it2++) 610 { 611 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 ) 612 error("sendMessage"); 613 } 528 broadcastMessage(msgProcessor, serverMsg, game->getPlayers()); 614 529 } 615 cout << "Projectile was not moved" << endl;616 530 } 617 531 } 618 532 } 619 533 620 n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog); 621 622 if (n >= 0) 623 { 624 broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog); 625 626 if (broadcastResponse) 627 { 628 cout << "Should be broadcasting the message" << endl; 629 630 // needs to be updated to use the players from the game 631 map<unsigned int, Player*>::iterator it; 632 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 633 { 634 cout << "Sent message back to " << it->second->name << endl; 635 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 636 error("sendMessage"); 637 } 638 } 639 else 640 { 641 cout << "Should be sending back the message" << endl; 642 643 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 644 error("sendMessage"); 645 } 534 if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0) 535 { 536 processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, scoreBlue, scoreRed); 646 537 647 538 cout << "Finished processing the message" << endl; … … 668 559 } 669 560 670 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog)561 void 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 &scoreBlue, int &scoreRed) 671 562 { 672 563 DataAccess da; … … 677 568 cout << "MSG: type: " << clientMsg.type << endl; 678 569 cout << "MSG contents: " << clientMsg.buffer << endl; 679 680 // maybe we should make a message class and have this be a member681 bool broadcastResponse = false;682 570 683 571 // Check that if an invalid message is sent, the client will correctly … … 691 579 Player::PlayerClass playerClass; 692 580 693 serverMsg.type = MSG_TYPE_REGISTER;694 581 memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4); 695 582 … … 697 584 cout << "password: " << password << endl; 698 585 699 if (playerClass == Player::CLASS_WARRIOR) 700 cout << "class: WARRIOR" << endl; 701 else if (playerClass == Player::CLASS_RANGER) 702 cout << "class: RANGER" << endl; 703 else { 704 cout << "Unknown player class detected" << endl; 586 bool validClass = false; 587 588 switch(playerClass) { 589 case Player::CLASS_WARRIOR: 590 case Player::CLASS_RANGER: 591 validClass = true; 592 break; 593 default: 594 validClass = false; 595 break; 596 } 597 598 serverMsg.type = MSG_TYPE_REGISTER; 599 600 if (validClass) { 601 int error = da.insertPlayer(username, password, playerClass); 602 603 if (error) 604 strcpy(serverMsg.buffer, "Registration failed. Please try again."); 605 else 606 strcpy(serverMsg.buffer, "Registration successful."); 607 }else 705 608 strcpy(serverMsg.buffer, "You didn't select a class"); 706 break; 707 } 708 709 int error = da.insertPlayer(username, password, playerClass); 710 711 if (error) 712 strcpy(serverMsg.buffer, "Registration failed. Please try again."); 713 else 714 strcpy(serverMsg.buffer, "Registration successful."); 609 610 msgProcessor.sendMessage(&serverMsg, &from); 715 611 716 612 break; … … 748 644 749 645 serverMsg.type = MSG_TYPE_PLAYER; 750 751 646 // tell the new player about all the existing players 752 647 cout << "Sending other players to new player" << endl; … … 759 654 cout << "sending info about " << it->second->name << endl; 760 655 cout << "sending id " << it->second->id << endl; 761 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 762 error("sendMessage"); 656 msgProcessor.sendMessage(&serverMsg, &from); 763 657 } 764 658 … … 772 666 itObjects->serialize(serverMsg.buffer); 773 667 cout << "sending item id " << itObjects->id << endl; 774 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 775 error("sendMessage"); 668 msgProcessor.sendMessage(&serverMsg, &from); 776 669 } 777 670 … … 788 681 memcpy(serverMsg.buffer, &numPlayers, 4); 789 682 strcpy(serverMsg.buffer+4, g->getName().c_str()); 790 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 791 error("sendMessage"); 683 msgProcessor.sendMessage(&serverMsg, &from); 792 684 } 793 685 … … 796 688 memcpy(serverMsg.buffer, &scoreBlue, 4); 797 689 memcpy(serverMsg.buffer+4, &scoreRed, 4); 798 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 799 error("sendMessage"); 690 msgProcessor.sendMessage(&serverMsg, &from); 800 691 801 692 serverMsg.type = MSG_TYPE_PLAYER; 802 693 p->serialize(serverMsg.buffer); 803 cout << "Should be broadcasting the message" << endl; 804 805 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 806 { 807 cout << "Sent message back to " << it->second->name << endl; 808 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 809 error("sendMessage"); 810 } 694 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 811 695 812 696 mapPlayers[unusedPlayerId] = p; … … 814 698 815 699 serverMsg.type = MSG_TYPE_LOGIN; 700 msgProcessor.sendMessage(&serverMsg, &from); 816 701 817 702 break; … … 845 730 846 731 if (flagType != WorldMap::OBJECT_NONE) { 847 addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor , sock, outputLog);732 addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor); 848 733 } 849 734 } … … 853 738 memcpy(serverMsg.buffer, &p->id, 4); 854 739 855 map<unsigned int, Player*>::iterator it; 856 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 857 { 858 cout << "Sent message back to " << it->second->name << endl; 859 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 860 error("sendMessage"); 861 } 740 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 862 741 863 742 if (p->id < unusedPlayerId) 864 743 unusedPlayerId = p->id; 744 865 745 mapPlayers.erase(p->id); 866 746 delete p; 747 867 748 strcpy(serverMsg.buffer+4, "You have successfully logged out."); 868 749 } 869 750 870 751 serverMsg.type = MSG_TYPE_LOGOUT; 752 msgProcessor.sendMessage(&serverMsg, &from); 871 753 872 754 break; … … 875 757 { 876 758 cout << "Got a chat message" << endl; 759 760 serverMsg.type = MSG_TYPE_CHAT; 877 761 878 762 Player *p = findPlayerByAddr(mapPlayers, from); … … 881 765 { 882 766 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server."); 767 msgProcessor.sendMessage(&serverMsg, &from); 883 768 } 884 769 else 885 770 { 886 broadcastResponse = true;887 888 771 ostringstream oss; 889 772 oss << p->name << ": " << clientMsg.buffer; 890 773 891 774 strcpy(serverMsg.buffer, oss.str().c_str()); 775 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 892 776 } 893 894 serverMsg.type = MSG_TYPE_CHAT;895 777 896 778 break; … … 911 793 912 794 Player* p = mapPlayers[id]; 795 bool validMessage = false; 913 796 914 797 if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr && … … 922 805 memcpy(serverMsg.buffer+8, &p->target.y, 4); 923 806 924 broadcastResponse = true; 807 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 808 809 validMessage = true; 925 810 } 926 811 else 927 812 cout << "Bad terrain detected" << endl; 928 813 } 929 else // nned to send back a message indicating failure814 else 930 815 cout << "Player id (" << id << ") doesn't match sender" << endl; 816 817 if (!validMessage) 818 msgProcessor.sendMessage(&serverMsg, &from); 931 819 932 820 break; … … 946 834 947 835 if (objectId >= 0) { 836 map<unsigned int, Player*> players = p->currentGame->getPlayers(); 837 948 838 serverMsg.type = MSG_TYPE_REMOVE_OBJECT; 949 839 memcpy(serverMsg.buffer, &objectId, 4); 950 951 map<unsigned int, Player*> players = p->currentGame->getPlayers(); 952 map<unsigned int, Player*>::iterator it; 953 for (it = players.begin(); it != players.end(); it++) 954 { 955 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 956 error("sendMessage"); 957 } 958 959 } 960 961 // if there was no flag to pickup, we really don't need to send a message 962 serverMsg.type = MSG_TYPE_PLAYER; 963 p->serialize(serverMsg.buffer); 964 965 broadcastResponse = true; 840 broadcastMessage(msgProcessor, serverMsg, players); 841 842 serverMsg.type = MSG_TYPE_PLAYER; 843 p->serialize(serverMsg.buffer); 844 broadcastMessage(msgProcessor, serverMsg, players); 845 } 966 846 967 847 break; … … 985 865 flagType = WorldMap::OBJECT_RED_FLAG; 986 866 987 addObjectToMap(flagType, p->pos.x, p->pos.y, p->currentGame->getMap(), p->currentGame->getPlayers(), msgProcessor, sock, outputLog); 867 map<unsigned int, Player*> players = p->currentGame->getPlayers(); 868 869 addObjectToMap(flagType, p->pos.x, p->pos.y, p->currentGame->getMap(), players, msgProcessor); 988 870 989 871 p->hasBlueFlag = false; … … 992 874 serverMsg.type = MSG_TYPE_PLAYER; 993 875 p->serialize(serverMsg.buffer); 994 995 broadcastResponse = true; 876 broadcastMessage(msgProcessor, serverMsg, players); 996 877 997 878 break; … … 1008 889 // need to make sure the target is in the sender's game 1009 890 1010 Player* source = mapPlayers[id]; 1011 source->targetPlayer = targetId; 1012 source->isChasing = true; 1013 1014 // this is irrelevant since the client doesn't even listen for START_ATTACK messages 1015 // actually, the client should not ignore this and should instead perform the same movement 1016 // algorithm on its end (following the target player until in range) that the server does. 1017 // Once the attacker is in range, the client should stop movement and wait for messages 1018 // from the server 891 Player* p = mapPlayers[id]; 892 p->targetPlayer = targetId; 893 p->isChasing = true; 894 895 map<unsigned int, Player*> players = p->currentGame->getPlayers(); 896 1019 897 serverMsg.type = MSG_TYPE_START_ATTACK; 1020 898 memcpy(serverMsg.buffer, &id, 4); 1021 899 memcpy(serverMsg.buffer+4, &targetId, 4); 1022 broadcastResponse = true; 1023 1024 break; 1025 } 1026 case MSG_TYPE_ATTACK: 1027 { 1028 cout << "Received am ATTACK message" << endl; 1029 cout << "ERROR: Clients should not send ATTACK messages" << endl; 900 broadcastMessage(msgProcessor, serverMsg, players); 1030 901 1031 902 break; … … 1042 913 cout << "Error: Game already exists" << endl; 1043 914 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 1044 broadcastResponse = false; 1045 return broadcastResponse; 1046 } 1047 1048 Game* g = new Game(gameName, "../data/map.txt"); 1049 mapGames[gameName] = g; 1050 1051 // add flag objects to the map 1052 WorldMap* m = g->getMap(); 1053 for (int y=0; y<m->height; y++) { 1054 for (int x=0; x<m->width; x++) { 1055 switch (m->getStructure(x, y)) { 1056 case WorldMap::STRUCTURE_BLUE_FLAG: 1057 m->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12); 1058 break; 1059 case WorldMap::STRUCTURE_RED_FLAG: 1060 m->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12); 1061 break; 915 }else { 916 Game* g = new Game(gameName, "../data/map.txt"); 917 mapGames[gameName] = g; 918 919 // add flag objects to the map 920 WorldMap* m = g->getMap(); 921 for (int y=0; y<m->height; y++) { 922 for (int x=0; x<m->width; x++) { 923 switch (m->getStructure(x, y)) { 924 case WorldMap::STRUCTURE_BLUE_FLAG: 925 m->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12); 926 break; 927 case WorldMap::STRUCTURE_RED_FLAG: 928 m->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12); 929 break; 930 } 1062 931 } 1063 932 } 1064 } 1065 1066 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS; 1067 strcpy(serverMsg.buffer, gameName.c_str()); 1068 broadcastResponse = false; 933 934 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS; 935 strcpy(serverMsg.buffer, gameName.c_str()); 936 } 937 938 msgProcessor.sendMessage(&serverMsg, &from); 1069 939 1070 940 break; … … 1081 951 cout << "Error: Game does not exist" << endl; 1082 952 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 1083 broadcastResponse = false; 1084 return broadcastResponse; 1085 } 1086 1087 Game* g = mapGames[gameName]; 1088 map<unsigned int, Player*>& players = g->getPlayers(); 1089 Player* p = findPlayerByAddr(mapPlayers, from); 1090 1091 if (players.find(p->id) != players.end()) { 1092 cout << "Player " << p->name << " trying to join a game he's already in" << endl; 1093 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 1094 broadcastResponse = false; 1095 return broadcastResponse; 1096 } 1097 1098 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS; 1099 strcpy(serverMsg.buffer, gameName.c_str()); 1100 broadcastResponse = false; 953 }else { 954 Game* g = mapGames[gameName]; 955 map<unsigned int, Player*>& players = g->getPlayers(); 956 Player* p = findPlayerByAddr(mapPlayers, from); 957 958 if (players.find(p->id) != players.end()) { 959 cout << "Player " << p->name << " trying to join a game he's already in" << endl; 960 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 961 }else { 962 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS; 963 strcpy(serverMsg.buffer, gameName.c_str()); 964 } 965 } 966 967 msgProcessor.sendMessage(&serverMsg, &from); 1101 968 1102 969 break; … … 1113 980 1114 981 /// should send a response back, maybe a new message type is needed 1115 1116 break; 1117 } 1118 1119 cout << "Game name: " << g->getName() << endl; 1120 p->currentGame = NULL; 1121 1122 serverMsg.type = MSG_TYPE_LEAVE_GAME; 1123 memcpy(serverMsg.buffer, &p->id, 4); 1124 strcpy(serverMsg.buffer+4, g->getName().c_str()); 1125 1126 map<unsigned int, Player*>& players = g->getPlayers(); 1127 1128 map<unsigned int, Player*>::iterator it; 1129 for (it = players.begin(); it != players.end(); it++) 1130 { 1131 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1132 error("sendMessage"); 1133 } 1134 1135 g->removePlayer(p->id); 1136 1137 int numPlayers = g->getNumPlayers(); 1138 1139 serverMsg.type = MSG_TYPE_GAME_INFO; 1140 memcpy(serverMsg.buffer, &numPlayers, 4); 1141 strcpy(serverMsg.buffer+4, g->getName().c_str()); 1142 broadcastResponse = true; 1143 1144 // if there are no more players in the game, remove it 1145 if (numPlayers == 0) { 1146 mapGames.erase(g->getName()); 1147 delete g; 982 // not sure what to do here 983 }else { 984 cout << "Game name: " << g->getName() << endl; 985 p->currentGame = NULL; 986 987 serverMsg.type = MSG_TYPE_LEAVE_GAME; 988 memcpy(serverMsg.buffer, &p->id, 4); 989 strcpy(serverMsg.buffer+4, g->getName().c_str()); 990 broadcastMessage(msgProcessor, serverMsg, g->getPlayers()); 991 992 g->removePlayer(p->id); 993 994 int numPlayers = g->getNumPlayers(); 995 996 serverMsg.type = MSG_TYPE_GAME_INFO; 997 memcpy(serverMsg.buffer, &numPlayers, 4); 998 strcpy(serverMsg.buffer+4, g->getName().c_str()); 999 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 1000 1001 // if there are no more players in the game, remove it 1002 if (numPlayers == 0) { 1003 mapGames.erase(g->getName()); 1004 delete g; 1005 } 1148 1006 } 1149 1007 … … 1160 1018 if (mapGames.find(gameName) == mapGames.end()) { 1161 1019 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE; 1162 broadcastResponse = false; 1163 return broadcastResponse;1020 1021 msgProcessor.sendMessage(&serverMsg, &from); 1164 1022 } 1165 1023 … … 1180 1038 itObjects->serialize(serverMsg.buffer); 1181 1039 cout << "sending item id " << itObjects->id << endl; 1182 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 1183 error("sendMessage"); 1040 msgProcessor.sendMessage(&serverMsg, &from); 1184 1041 } 1185 1042 … … 1193 1050 memcpy(serverMsg.buffer+4, &game_redScore, 4); 1194 1051 1195 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )1196 error("sendMessage"); 1197 1052 msgProcessor.sendMessage(&serverMsg, &from); 1053 1054 // send info to other players 1198 1055 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME; 1199 1056 p->serialize(serverMsg.buffer); 1200 1057 cout << "Should be broadcasting the message" << endl; 1201 1202 map<unsigned int, Player*>& otherPlayers = g->getPlayers(); 1203 map<unsigned int, Player*>::iterator it; 1204 for (it = otherPlayers.begin(); it != otherPlayers.end(); it++) 1205 { 1206 cout << "Sent message back to " << it->second->name << endl; 1207 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1208 error("sendMessage"); 1209 } 1058 broadcastMessage(msgProcessor, serverMsg, g->getPlayers()); 1210 1059 1211 1060 g->addPlayer(p); 1212 1061 1213 map<unsigned int, Player*>& allPlayers = g->getPlayers();1214 1062 1215 1063 // tell the new player about all the players in the game (including himself) … … 1217 1065 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME; 1218 1066 1067 1068 map<unsigned int, Player*>& allPlayers = g->getPlayers(); 1069 map<unsigned int, Player*>::iterator it; 1219 1070 for (it = allPlayers.begin(); it != allPlayers.end(); it++) 1220 1071 { … … 1223 1074 cout << "sending info about " << it->second->name << endl; 1224 1075 cout << "sending id " << it->second->id << endl; 1225 if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 ) 1226 error("sendMessage"); 1076 msgProcessor.sendMessage(&serverMsg, &from); 1227 1077 } 1228 1078 … … 1232 1082 memcpy(serverMsg.buffer, &numPlayers, 4); 1233 1083 strcpy(serverMsg.buffer+4, gameName.c_str()); 1234 broadcast Response = true;1084 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 1235 1085 1236 1086 break; … … 1238 1088 default: 1239 1089 { 1090 // probably want to log the error rather than sending a chat message, 1091 // especially since chat isn't currently visible on all screens 1092 1240 1093 serverMsg.type = MSG_TYPE_CHAT; 1241 1094 strcpy(serverMsg.buffer, "Server error occured. Report this please."); … … 1244 1097 } 1245 1098 } 1246 1247 return broadcastResponse; 1099 } 1100 1101 void broadcastMessage(MessageProcessor &msgProcessor, NETWORK_MSG &serverMsg, map<unsigned int, Player*>& players) { 1102 map<unsigned int, Player*>::iterator it; 1103 for (it = players.begin(); it != players.end(); it++) { 1104 msgProcessor.sendMessage(&serverMsg, &(it->second->addr)); 1105 } 1248 1106 } 1249 1107 … … 1292 1150 } 1293 1151 1294 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor , int sock, ofstream& outputLog) {1152 void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor) { 1295 1153 NETWORK_MSG serverMsg; 1296 1154 1297 1155 gameMap->addObject(objectType, x, y); 1298 1156 1299 // need to send the OBJECT message too1300 1157 serverMsg.type = MSG_TYPE_OBJECT; 1301 1158 gameMap->getObjects()->back().serialize(serverMsg.buffer); 1302 1159 1303 map<unsigned int, Player*>::iterator it; 1304 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 1305 { 1306 if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 ) 1307 error("sendMessage"); 1308 } 1160 broadcastMessage(msgProcessor, serverMsg, mapPlayers); 1309 1161 }
Note:
See TracChangeset
for help on using the changeset viewer.