Changeset b92e6a7 in network-game for server


Ignore:
Timestamp:
Sep 26, 2013, 8:29:14 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
f41a7f9
Parents:
321fbbc
Message:

The Game class now has a WorldMap. When a client creates or joins a game, the server sends out all the basic game info (the map and info about all players in the game)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r321fbbc rb92e6a7  
    935935         cout << "Game name: " << gameName << endl;
    936936
     937         Player* p = findPlayerByAddr(mapPlayers, from);
     938
    937939         mapGames[gameName] = Game(gameName);
    938          mapGames[gameName].addPlayer(findPlayerByAddr(mapPlayers, from));
    939          int numPlayers = mapGames[gameName].getNumPlayers();
     940         Game& g = mapGames[gameName];
     941         g.addPlayer(p);
     942         int numPlayers = g.getNumPlayers();
     943
     944         map<unsigned int, Player*>& otherPlayers = g.getPlayers();
     945
     946         // choose a random team (either 0 or 1)
     947         p->team = rand() % 2;
     948
     949         // tell the new player about all the existing players
     950         cout << "Sending other players to new player" << endl;
     951
     952         map<unsigned int, Player*>::iterator it;
     953         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
     954         {
     955            it->second->serialize(serverMsg.buffer);
     956
     957            cout << "sending info about " << it->second->name  << endl;
     958            cout << "sending id " << it->second->id  << endl;
     959            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     960               error("sendMessage");
     961         }
     962
     963         // tell the new player about all map objects
     964         // (currently just the flags)
     965
     966         serverMsg.type = MSG_TYPE_OBJECT;
     967         vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects();
     968         vector<WorldMap::Object>::iterator itObjects;
     969         cout << "sending items" << endl;
     970         for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
     971            itObjects->serialize(serverMsg.buffer);
     972            cout << "sending item id " << itObjects->id  << endl;
     973            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     974               error("sendMessage");
     975         }
     976
     977         // send the current score
     978         serverMsg.type = MSG_TYPE_SCORE;
     979
     980         int game_blueScore = g.getBlueScore();
     981         int game_redScore = g.getRedScore();
     982         memcpy(serverMsg.buffer, &game_blueScore, 4);
     983         memcpy(serverMsg.buffer+4, &game_redScore, 4);
     984
     985         if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     986            error("sendMessage");
     987
     988         serverMsg.type = MSG_TYPE_PLAYER;
     989         p->serialize(serverMsg.buffer);
     990         cout << "Should be broadcasting the message" << endl;
     991
     992         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
     993         {
     994            cout << "Sent message back to " << it->second->name << endl;
     995            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
     996               error("sendMessage");
     997         }
    940998
    941999         serverMsg.type = MSG_TYPE_GAME_INFO;
     
    9531011         cout << "Game name: " << gameName << endl;
    9541012
    955          mapGames[gameName].addPlayer(findPlayerByAddr(mapPlayers, from));
    956          int numPlayers = mapGames[gameName].getNumPlayers();
     1013         Player* p = findPlayerByAddr(mapPlayers, from);
     1014
     1015         Game& g = mapGames[gameName];
     1016         if (!g.addPlayer(p))
     1017            cout << "Player " << p->name << " trying to join a game he's already in" << endl;
     1018         int numPlayers = g.getNumPlayers();
     1019
     1020         map<unsigned int, Player*>& otherPlayers = g.getPlayers();
     1021
     1022         // choose a random team (either 0 or 1)
     1023         p->team = rand() % 2;
     1024
     1025         // tell the new player about all the existing players
     1026         cout << "Sending other players to new player" << endl;
     1027
     1028         map<unsigned int, Player*>::iterator it;
     1029         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
     1030         {
     1031            it->second->serialize(serverMsg.buffer);
     1032
     1033            cout << "sending info about " << it->second->name  << endl;
     1034            cout << "sending id " << it->second->id  << endl;
     1035            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     1036               error("sendMessage");
     1037         }
     1038
     1039         // tell the new player about all map objects
     1040         // (currently just the flags)
     1041
     1042         serverMsg.type = MSG_TYPE_OBJECT;
     1043         vector<WorldMap::Object>* vctObjects = g.getMap()->getObjects();
     1044         vector<WorldMap::Object>::iterator itObjects;
     1045         cout << "sending items" << endl;
     1046         for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
     1047            itObjects->serialize(serverMsg.buffer);
     1048            cout << "sending item id " << itObjects->id  << endl;
     1049            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     1050               error("sendMessage");
     1051         }
     1052
     1053
     1054         // send the current score
     1055         serverMsg.type = MSG_TYPE_SCORE;
     1056
     1057         int game_blueScore = g.getBlueScore();
     1058         int game_redScore = g.getRedScore();
     1059         memcpy(serverMsg.buffer, &game_blueScore, 4);
     1060         memcpy(serverMsg.buffer+4, &game_redScore, 4);
     1061
     1062         if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
     1063            error("sendMessage");
     1064
     1065         serverMsg.type = MSG_TYPE_PLAYER;
     1066         p->serialize(serverMsg.buffer);
     1067         cout << "Should be broadcasting the message" << endl;
     1068
     1069         for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
     1070         {
     1071            cout << "Sent message back to " << it->second->name << endl;
     1072            if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
     1073               error("sendMessage");
     1074         }
    9571075
    9581076         serverMsg.type = MSG_TYPE_GAME_INFO;
Note: See TracChangeset for help on using the changeset viewer.