Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 20, 2006, 1:39:01 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8230:HEAD https://svn.orxonox.net/orxonox/branches/network .
conflicts resolved in favour of the network branche (conflicts were in network)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/network_game_manager.cc

    r8362 r8623  
    2323#include "state.h"
    2424#include "class_list.h"
     25#include "debug.h"
    2526
    2627#include "network_stream.h"
     
    3940#include "network_game_manager.h"
    4041
    41 #include "debug.h"
    42 
    4342
    4443/* using namespace std is default, this needs to be here */
     
    5958
    6059  this->setSynchronized(true);
    61 
     60 
    6261  MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL );
    6362  MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL );
    64 
     63  MessageManager::getInstance()->registerMessageHandler( MSGID_CHATMESSAGE, chatMessageHandler, NULL );
     64 
    6565  this->gameState = 0;
    6666  registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) );
     
    7272NetworkGameManager::~NetworkGameManager()
    7373{
     74  delete MessageManager::getInstance();
     75 
     76  PlayerStats::deleteAllPlayerStats();
    7477}
    7578
     
    7780/**
    7881 * insert new player into game
    79  * @param userId
    80  * @return
     82 * @param userId 
     83 * @return 
    8184 */
    8285bool NetworkGameManager::signalNewPlayer( int userId )
     
    8588  assert( State::getGameRules() );
    8689  assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) );
    87 
     90 
    8891  NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules()));
    89 
     92 
    9093  int team = rules.getTeamForNewUser();
    9194  ClassID playableClassId = rules.getPlayableClassId( userId, team );
    9295  std::string playableModel = rules.getPlayableModelFileName( userId, team, playableClassId );
    93 
     96 
    9497  BaseObject * bo = Factory::fabricate( playableClassId );
    95 
     98 
    9699  assert( bo != NULL );
    97100  assert( bo->isA( CL_PLAYABLE ) );
    98 
     101 
    99102  Playable & playable = *(dynamic_cast<Playable*>(bo));
    100 
    101   playable.loadModel( playableModel );
     103 
     104  if (  playableModel != "" )
     105    playable.loadModel( playableModel );
    102106  playable.setOwner( userId );
    103107  playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
    104108  playable.setSynchronized( true );
    105 
     109 
    106110  PlayerStats * stats = rules.getNewPlayerStats( userId );
    107 
     111 
    108112  stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
    109113  stats->setSynchronized( true );
    110114  stats->setOwner( getHostID() );
    111 
     115 
    112116  stats->setTeamId( team );
    113117  stats->setPlayableClassId( playableClassId );
    114118  stats->setPlayableUniqueId( playable.getUniqueID() );
    115119  stats->setModelFileName( playableModel );
    116 
     120 
    117121  return true;
    118122}
     
    121125/**
    122126 * remove player from game
    123  * @param userID
    124  * @return
     127 * @param userID 
     128 * @return 
    125129 */
    126130bool NetworkGameManager::signalLeftPlayer(int userID)
     
    132136    delete PlayerStats::getStats( userID );
    133137  }
    134 
     138 
    135139  return true;
    136140}
     
    140144/**
    141145 * handler for remove synchronizeable messages
    142  * @param messageId
    143  * @param data
    144  * @param dataLength
    145  * @param someData
    146  * @param userId
     146 * @param messageId 
     147 * @param data 
     148 * @param dataLength 
     149 * @param someData 
     150 * @param userId 
    147151 * @return true on successfull handling else handler will be called again
    148152 */
     
    154158    return true;
    155159  }
    156 
     160 
    157161  int uniqueId = 0;
    158162  int len = Converter::byteArrayToInt( data, &uniqueId );
    159 
     163 
    160164  if ( len != dataLength )
    161165  {
     
    163167    return true;
    164168  }
    165 
     169 
    166170  const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE );
    167 
     171 
    168172  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
    169173  {
     
    175179        return true;
    176180      }
    177 
     181     
    178182      delete dynamic_cast<Synchronizeable*>(*it);
    179183      return true;
    180184    }
    181185  }
    182 
     186 
    183187  return true;
    184188}
     
    191195{
    192196  byte buf[INTSIZE];
    193 
     197 
    194198  assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE );
    195199
     
    201205/**
    202206 * handler for MSGID_PREFEREDTEAM message
    203  * @param messageId
    204  * @param data
    205  * @param dataLength
    206  * @param someData
    207  * @param userId
    208  * @return
     207 * @param messageId 
     208 * @param data 
     209 * @param dataLength 
     210 * @param someData 
     211 * @param userId 
     212 * @return 
    209213 */
    210214bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
    211215{
    212216  assert( NetworkGameManager::getInstance()->isServer() );
    213 
     217 
    214218  int teamId = 0;
    215219  int len = Converter::byteArrayToInt( data, &teamId );
    216 
     220 
    217221  if ( len != dataLength )
    218222  {
     
    220224    return true;
    221225  }
    222 
     226 
    223227  NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId );
    224 
     228 
    225229  return true;
    226230}
     
    230234  if ( !PlayerStats::getStats( userId ) )
    231235    return;
    232 
     236 
    233237  PlayerStats & stats = *(PlayerStats::getStats( userId ));
    234 
     238 
    235239  stats.setPreferedTeamId( teamId );
    236240}
     
    238242/**
    239243 * set prefered team for this host
    240  * @param teamId
     244 * @param teamId 
    241245 */
    242246void NetworkGameManager::prefereTeam( int teamId )
     
    247251  {
    248252    byte buf[INTSIZE];
    249 
     253   
    250254    assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE );
    251 
     255   
    252256    MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH );
    253257  }
     
    278282
    279283
    280 
     284bool NetworkGameManager::chatMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
     285{
     286  assert( State::getGameRules() );
     287  assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) );
     288 
     289  NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules()));
     290 
     291  if ( dataLength < 2*INTSIZE )
     292  {
     293    PRINTF(2)("got too small chatmessage from client %d\n", userId);
     294   
     295    return true;
     296  }
     297 
     298  int messageType = 0;
     299  Converter::byteArrayToInt( data, &messageType );
     300  std::string message;
     301  Converter::byteArrayToString( data+INTSIZE, message, dataLength-INTSIZE );
     302 
     303  rules.handleChatMessage( userId, message, messageType );
     304
     305  return true;
     306}
     307
     308/**
     309 * send chat message
     310 * @param message message text
     311 * @param messageType some int
     312 * @param userId user to send message to -1 = ALL
     313 */
     314void NetworkGameManager::sendChatMessage( const std::string & message, int messageType, int userId )
     315{
     316  byte * buf = new byte[message.length()+2*INTSIZE];
     317
     318  assert( Converter::intToByteArray( messageType, buf, INTSIZE ) == INTSIZE );
     319  assert( Converter::stringToByteArray(message, buf+INTSIZE, message.length()+INTSIZE) == message.length()+INTSIZE );
     320 
     321  if ( userId == -1 )
     322    MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+2*INTSIZE, RT_ALL_ME, 0, MP_HIGHBANDWIDTH );
     323  else
     324    MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+2*INTSIZE, RT_USER, userId, MP_HIGHBANDWIDTH );
     325 
     326  delete [] buf;
     327}
     328
     329
     330
Note: See TracChangeset for help on using the changeset viewer.