Changeset c666518 in network-game


Ignore:
Timestamp:
Oct 17, 2014, 1:19:28 AM (10 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
master
Children:
aee0634
Parents:
84754c0
Message:

Move handleGameEvents and handlePlayers events from the Game class to the server

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • common/Game.cpp

    r84754c0 rc666518  
    235235}
    236236
    237 bool Game::handleGameEvents() {
    238    map<unsigned int, Player*>::iterator it;
    239    bool gameFinished = false;
    240 
    241    for (it = this->getPlayers().begin(); it != this->getPlayers().end(); it++) {
    242       gameFinished = gameFinished || this->handlePlayerEvents(it->second);
    243    }
    244 
    245    if (gameFinished) {
    246       for (it = this->players.begin(); it != this->players.end(); it++) {
    247          it->second->currentGame = NULL;
    248       }
    249    }
    250 
    251    return gameFinished;
    252 }
    253 
    254 bool Game::handlePlayerEvents(Player* p) {
    255    NETWORK_MSG serverMsg;
    256    FLOAT_POSITION oldPos;
    257    bool gameFinished = false;
    258    bool broadcastMove = false;
    259 
    260    cout << "moving player" << endl;
    261 
    262    // move player and perform associated tasks
    263    oldPos = p->pos;
    264    if (p->move(this->worldMap)) {
    265 
    266       cout << "player moved" << endl;
    267       if (this->processPlayerMovement(p, oldPos))
    268          broadcastMove = true;
    269       cout << "player move processed" << endl;
    270 
    271       ObjectType flagType;
    272       POSITION pos;
    273       bool flagTurnedIn = false;
    274       bool flagReturned = false;
    275       bool ownFlagAtBase = false;
    276 
    277       switch(this->worldMap->getStructure(p->pos.x/25, p->pos.y/25)) {
    278          case STRUCTURE_BLUE_FLAG:
    279          {
    280             if (p->team == 0 && p->hasRedFlag) {
    281                // check that your flag is at your base
    282                pos = this->worldMap->getStructureLocation(STRUCTURE_BLUE_FLAG);
    283                            
    284                vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
    285                vector<WorldMap::Object>::iterator itObjects;
    286 
    287                for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
    288                   if (itObjects->type == OBJECT_BLUE_FLAG) {
    289                      if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
    290                         ownFlagAtBase = true;
    291                         break;
    292                      }
    293                   }
    294                }
    295 
    296                if (ownFlagAtBase) {
    297                   p->hasRedFlag = false;
    298                   flagType = OBJECT_RED_FLAG;
    299                   pos = this->worldMap->getStructureLocation(STRUCTURE_RED_FLAG);
    300                   flagTurnedIn = true;
    301                   this->blueScore++;
    302                }
    303             }
    304 
    305             break;
    306          }
    307          case STRUCTURE_RED_FLAG:
    308          {
    309             if (p->team == 1 && p->hasBlueFlag) {
    310                // check that your flag is at your base
    311                pos = this->worldMap->getStructureLocation(STRUCTURE_RED_FLAG);
    312                        
    313                vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
    314                vector<WorldMap::Object>::iterator itObjects;
    315 
    316                for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
    317                   if (itObjects->type == OBJECT_RED_FLAG) {
    318                      if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
    319                         ownFlagAtBase = true;
    320                         break;
    321                      }
    322                   }
    323                }
    324 
    325                if (ownFlagAtBase) {
    326                   p->hasBlueFlag = false;
    327                   flagType = OBJECT_BLUE_FLAG;
    328                   pos = this->worldMap->getStructureLocation(STRUCTURE_BLUE_FLAG);
    329                   flagTurnedIn = true;
    330                   this->redScore++;
    331                }
    332             }
    333 
    334             break;
    335          }
    336          default:
    337          {
    338             break;
    339          }
    340       }
    341 
    342       if (flagTurnedIn) {
    343          unsigned int blueScore = this->blueScore;
    344          unsigned int redScore = this->redScore;
    345 
    346          pos.x = pos.x*25+12;
    347          pos.y = pos.y*25+12;
    348          this->addObjectToMap(flagType, pos.x, pos.y);
    349 
    350          serverMsg.type = MSG_TYPE_SCORE;
    351          memcpy(serverMsg.buffer, &blueScore, 4);
    352          memcpy(serverMsg.buffer+4, &redScore, 4);
    353          msgProcessor->broadcastMessage(serverMsg, this->players);
    354 
    355          // check to see if the game should end
    356          // move to its own method
    357          if (this->blueScore == 3 || this->redScore == 3) {
    358             gameFinished = true;
    359 
    360             unsigned int winningTeam;
    361             if (this->blueScore == 3)
    362                winningTeam = 0;
    363             else if (this->redScore == 3)
    364                winningTeam = 1;
    365 
    366             serverMsg.type = MSG_TYPE_FINISH_GAME;
    367 
    368             // I should create an instance of the GameSummary object here and just serialize it into this message
    369             memcpy(serverMsg.buffer, &winningTeam, 4);
    370             memcpy(serverMsg.buffer+4, &blueScore, 4);
    371             memcpy(serverMsg.buffer+8, &redScore, 4);
    372             strcpy(serverMsg.buffer+12, this->getName().c_str());
    373             msgProcessor->broadcastMessage(serverMsg, this->players);
    374          }
    375 
    376          // this means a PLAYER message will be sent
    377          broadcastMove = true;
    378       }
    379 
    380       // go through all objects and check if the player is close to one and if its their flag
    381       vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
    382       vector<WorldMap::Object>::iterator itObjects;
    383       POSITION structPos;
    384 
    385       for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
    386          POSITION pos = itObjects->pos;
    387 
    388          if (posDistance(p->pos, pos.toFloat()) < 10) {
    389             if (p->team == 0 && itObjects->type == OBJECT_BLUE_FLAG) {
    390                structPos = this->worldMap->getStructureLocation(STRUCTURE_BLUE_FLAG);
    391                flagReturned = true;
    392                break;
    393             } else if (p->team == 1 && itObjects->type == OBJECT_RED_FLAG) {
    394                structPos = this->worldMap->getStructureLocation(STRUCTURE_RED_FLAG);
    395                flagReturned = true;
    396                break;
    397             }
    398          }
    399       }
    400 
    401       if (flagReturned) {
    402          itObjects->pos.x = structPos.x*25+12;
    403          itObjects->pos.y = structPos.y*25+12;
    404 
    405          serverMsg.type = MSG_TYPE_OBJECT;
    406          itObjects->serialize(serverMsg.buffer);
    407          msgProcessor->broadcastMessage(serverMsg, this->players);
    408       }
    409 
    410       if (broadcastMove) {
    411          serverMsg.type = MSG_TYPE_PLAYER;
    412          p->serialize(serverMsg.buffer);
    413          msgProcessor->broadcastMessage(serverMsg, this->players);
    414       }
    415    }
    416 
    417    cout << "processing player attack" << endl;
    418 
    419    // check if the player's attack animation is complete
    420    if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis()) {
    421       p->isAttacking = false;
    422       cout << "Attack animation is complete" << endl;
    423 
    424       //send everyone an ATTACK message
    425       cout << "about to broadcast attack" << endl;
    426 
    427       if (p->attackType == Player::ATTACK_MELEE) {
    428          cout << "Melee attack" << endl;
    429 
    430          Player* target = players[p->getTargetPlayer()];
    431          this->dealDamageToPlayer(target, p->damage);
    432       } else if (p->attackType == Player::ATTACK_RANGED) {
    433          cout << "Ranged attack" << endl;
    434 
    435          Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
    436          this->assignProjectileId(&proj);
    437          this->addProjectile(proj);
    438 
    439          int x = p->pos.x;
    440          int y = p->pos.y;
    441          unsigned int targetId = p->getTargetPlayer();
    442 
    443          serverMsg.type = MSG_TYPE_PROJECTILE;
    444          memcpy(serverMsg.buffer, &proj.id, 4);
    445          memcpy(serverMsg.buffer+4, &x, 4);
    446          memcpy(serverMsg.buffer+8, &y, 4);
    447          memcpy(serverMsg.buffer+12, &targetId, 4);
    448          msgProcessor->broadcastMessage(serverMsg, players);
    449       } else
    450          cout << "Invalid attack type: " << p->attackType << endl;
    451    }
    452 
    453    return gameFinished;
    454 }
    455 
    456237void Game::assignProjectileId(Projectile* p) {
    457238   p->id = unusedProjectileId;
  • common/Game.h

    r84754c0 rc666518  
    6161   void dealDamageToPlayer(Player* p, int damage);
    6262
    63    bool handleGameEvents();
    64    bool handlePlayerEvents(Player* p);
    65 
    6663   void assignProjectileId(Projectile* p);
    6764   void updateUnusedProjectileId();
  • server/server.cpp

    r84754c0 rc666518  
    4747Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
    4848Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
     49
     50bool handleGameEvents(Game* game, MessageProcessor* msgProcessor, DataAccess* da);
     51bool handlePlayerEvents(Game* game, Player* p, MessageProcessor* msgProcessor, DataAccess* da);
    4952
    5053void quit(int sig);
     
    178181         map<string, Game*>::iterator itGames;
    179182         Game* game = NULL;
     183         DataAccess da;
    180184
    181185         for (itGames = mapGames.begin(); itGames != mapGames.end();) {
    182186            game = itGames->second;
    183             if (game->handleGameEvents()) {
     187            bool gameFinished = handleGameEvents(game, &msgProcessor, &da);
     188
     189            if (gameFinished) {
    184190               // save game record
    185191               int winningTeam = -1;
     
    193199               else {
    194200                  map<unsigned int, Player*>::iterator it;
    195                   DataAccess da;
    196201
    197202                  for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++) {
    198                       da.saveGameHistory(it->second->getId(), winningTeam, game->getBlueScore(), game->getRedScore());
     203                      Player* p = it->second;
     204                      cout << "winning team: " << winningTeam << endl;
     205                      cout << "player team: " << p->team << endl;
     206                      cout << "player id: " << p->getId() << endl;
     207                     
     208                      if (winningTeam == p->team)
     209                          p->wins++;
     210                      else
     211                          p->losses++;
     212                      da.updatePlayer(p);
     213                      da.saveGameHistory(p->getId(), winningTeam, game->getBlueScore(), game->getRedScore());
    199214                  }
    200215               }
     
    585600         int* playerRecord = da.getPlayerRecord(id);
    586601
    587          int honorPoints = playerRecord[0];
    588          int wins = playerRecord[1];
    589          int losses = playerRecord[2];
     602         // playerRecord[0] is level
     603         // playerRecord[1] is experience
     604         int honorPoints = playerRecord[2];
     605         int wins = playerRecord[3];
     606         int losses = playerRecord[4];
    590607
    591608         serverMsg.type = MSG_TYPE_PROFILE;
     
    860877}
    861878
     879bool handleGameEvents(Game* game, MessageProcessor* msgProcessor, DataAccess* da) {
     880   map<unsigned int, Player*> players = game->getPlayers();
     881   map<unsigned int, Player*>::iterator it;
     882   bool gameFinished = false;
     883
     884   for (it = players.begin(); it != players.end(); it++) {
     885      gameFinished = gameFinished || handlePlayerEvents(game, it->second, msgProcessor, da);
     886   }
     887
     888   if (gameFinished) {
     889      for (it = players.begin(); it != players.end(); it++) {
     890         it->second->currentGame = NULL;
     891      }
     892   }
     893
     894   return gameFinished;
     895}
     896
     897bool handlePlayerEvents(Game* game, Player* p, MessageProcessor* msgProcessor, DataAccess* da) {
     898   NETWORK_MSG serverMsg;
     899   FLOAT_POSITION oldPos;
     900   bool gameFinished = false;
     901   bool broadcastMove = false;
     902
     903   cout << "moving player" << endl;
     904
     905   // move player and perform associated tasks
     906   oldPos = p->pos;
     907   if (p->move(game->getMap())) {
     908
     909      cout << "player moved" << endl;
     910      if (game->processPlayerMovement(p, oldPos))
     911         broadcastMove = true;
     912      cout << "player move processed" << endl;
     913
     914      ObjectType flagType;
     915      POSITION pos;
     916      bool flagTurnedIn = false;
     917      bool flagReturned = false;
     918      bool ownFlagAtBase = false;
     919
     920      switch(game->getMap()->getStructure(p->pos.x/25, p->pos.y/25)) {
     921         case STRUCTURE_BLUE_FLAG:
     922         {
     923            if (p->team == 0 && p->hasRedFlag) {
     924               // check that your flag is at your base
     925               pos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
     926                           
     927               vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
     928               vector<WorldMap::Object>::iterator itObjects;
     929
     930               for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
     931                  if (itObjects->type == OBJECT_BLUE_FLAG) {
     932                     if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
     933                        ownFlagAtBase = true;
     934                        break;
     935                     }
     936                  }
     937               }
     938
     939               if (ownFlagAtBase) {
     940                  p->hasRedFlag = false;
     941                  flagType = OBJECT_RED_FLAG;
     942                  pos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
     943                  flagTurnedIn = true;
     944                  game->setBlueScore(game->getBlueScore()+1);
     945               }
     946            }
     947
     948            break;
     949         }
     950         case STRUCTURE_RED_FLAG:
     951         {
     952            if (p->team == 1 && p->hasBlueFlag) {
     953               // check that your flag is at your base
     954               pos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
     955                       
     956               vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
     957               vector<WorldMap::Object>::iterator itObjects;
     958
     959               for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
     960                  if (itObjects->type == OBJECT_RED_FLAG) {
     961                     if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
     962                        ownFlagAtBase = true;
     963                        break;
     964                     }
     965                  }
     966               }
     967
     968               if (ownFlagAtBase) {
     969                  p->hasBlueFlag = false;
     970                  flagType = OBJECT_BLUE_FLAG;
     971                  pos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
     972                  flagTurnedIn = true;
     973                  game->setRedScore(game->getRedScore()+1);
     974               }
     975            }
     976
     977            break;
     978         }
     979         default:
     980         {
     981            break;
     982         }
     983      }
     984
     985      if (flagTurnedIn) {
     986         unsigned int blueScore = game->getBlueScore();
     987         unsigned int redScore = game->getRedScore();
     988
     989         pos.x = pos.x*25+12;
     990         pos.y = pos.y*25+12;
     991         game->addObjectToMap(flagType, pos.x, pos.y);
     992
     993         serverMsg.type = MSG_TYPE_SCORE;
     994         memcpy(serverMsg.buffer, &blueScore, 4);
     995         memcpy(serverMsg.buffer+4, &redScore, 4);
     996         msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
     997
     998         // give honor to everyone on the winning team
     999         map<unsigned int, Player*>::iterator itPlayers;
     1000         for (itPlayers = game->getPlayers().begin(); itPlayers != game->getPlayers().end(); itPlayers++) {
     1001            if (itPlayers->second->team == p->team) {
     1002               itPlayers->second->honor += 50;
     1003               da->updatePlayer(itPlayers->second);
     1004            }
     1005         }
     1006
     1007         // check to see if the game should end
     1008         // move to its own method
     1009         if (game->getBlueScore() == 3 || game->getRedScore() == 3) {
     1010            gameFinished = true;
     1011
     1012            unsigned int winningTeam;
     1013            if (game->getBlueScore() == 3)
     1014               winningTeam = 0;
     1015            else if (game->getRedScore() == 3)
     1016               winningTeam = 1;
     1017
     1018            serverMsg.type = MSG_TYPE_FINISH_GAME;
     1019
     1020            // I should create an instance of the GameSummary object here and just serialize it into this message
     1021            memcpy(serverMsg.buffer, &winningTeam, 4);
     1022            memcpy(serverMsg.buffer+4, &blueScore, 4);
     1023            memcpy(serverMsg.buffer+8, &redScore, 4);
     1024            strcpy(serverMsg.buffer+12, game->getName().c_str());
     1025            msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
     1026
     1027            // give honor to everyone on the winning team
     1028            for (itPlayers = game->getPlayers().begin(); itPlayers != game->getPlayers().end(); itPlayers++) {
     1029               if (itPlayers->second->team == p->team) {
     1030                  itPlayers->second->honor += 150;
     1031                  da->updatePlayer(itPlayers->second);
     1032               }
     1033            }
     1034         }
     1035
     1036         // this means a PLAYER message will be sent
     1037         broadcastMove = true;
     1038      }
     1039
     1040      // go through all objects and check if the player is close to one and if its their flag
     1041      vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
     1042      vector<WorldMap::Object>::iterator itObjects;
     1043      POSITION structPos;
     1044
     1045      for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
     1046         POSITION pos = itObjects->pos;
     1047
     1048         if (posDistance(p->pos, pos.toFloat()) < 10) {
     1049            if (p->team == 0 && itObjects->type == OBJECT_BLUE_FLAG) {
     1050               structPos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
     1051               flagReturned = true;
     1052               break;
     1053            } else if (p->team == 1 && itObjects->type == OBJECT_RED_FLAG) {
     1054               structPos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
     1055               flagReturned = true;
     1056               break;
     1057            }
     1058         }
     1059      }
     1060
     1061      if (flagReturned) {
     1062         itObjects->pos.x = structPos.x*25+12;
     1063         itObjects->pos.y = structPos.y*25+12;
     1064
     1065         serverMsg.type = MSG_TYPE_OBJECT;
     1066         itObjects->serialize(serverMsg.buffer);
     1067         msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
     1068      }
     1069
     1070      if (broadcastMove) {
     1071         serverMsg.type = MSG_TYPE_PLAYER;
     1072         p->serialize(serverMsg.buffer);
     1073         msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
     1074      }
     1075   }
     1076
     1077   cout << "processing player attack" << endl;
     1078
     1079   // check if the player's attack animation is complete
     1080   if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis()) {
     1081      p->isAttacking = false;
     1082      cout << "Attack animation is complete" << endl;
     1083
     1084      //send everyone an ATTACK message
     1085      cout << "about to broadcast attack" << endl;
     1086
     1087      if (p->attackType == Player::ATTACK_MELEE) {
     1088         cout << "Melee attack" << endl;
     1089
     1090         Player* target = game->getPlayers()[p->getTargetPlayer()];
     1091         game->dealDamageToPlayer(target, p->damage);
     1092      } else if (p->attackType == Player::ATTACK_RANGED) {
     1093         cout << "Ranged attack" << endl;
     1094
     1095         Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
     1096         game->assignProjectileId(&proj);
     1097         game->addProjectile(proj);
     1098
     1099         int x = p->pos.x;
     1100         int y = p->pos.y;
     1101         unsigned int targetId = p->getTargetPlayer();
     1102
     1103         serverMsg.type = MSG_TYPE_PROJECTILE;
     1104         memcpy(serverMsg.buffer, &proj.id, 4);
     1105         memcpy(serverMsg.buffer+4, &x, 4);
     1106         memcpy(serverMsg.buffer+8, &y, 4);
     1107         memcpy(serverMsg.buffer+12, &targetId, 4);
     1108         msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
     1109      } else
     1110         cout << "Invalid attack type: " << p->attackType << endl;
     1111   }
     1112
     1113   return gameFinished;
     1114}
     1115
    8621116void quit(int sig) {
    8631117   done = true;
Note: See TracChangeset for help on using the changeset viewer.