Index: trunk/src/lib/network/player_stats.cc
===================================================================
--- trunk/src/lib/network/player_stats.cc	(revision 8362)
+++ trunk/src/lib/network/player_stats.cc	(revision 8623)
@@ -24,8 +24,10 @@
 
 #include "debug.h"
+#include "shell_command.h"
 
 
 CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
 
+
 /**
  * constructor
@@ -34,5 +36,5 @@
 {
   init();
-
+  
   this->userId = userId;
 }
@@ -56,5 +58,7 @@
   this->playableClassId = 0;
   this->modelFileName = "";
-
+  this->nickName = "Player";
+  this->oldNickName = "Player";
+  
   userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
   teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
@@ -64,5 +68,8 @@
   playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
   modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
-
+  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) );
+  
+  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
+  
   PRINTF(0)("PlayerStats created\n");
 }
@@ -87,6 +94,12 @@
   {
     this->setPlayableUniqueId( this->playableUniqueId );
-
+    
     PRINTF(0)("uniqueID changed %d %d %d\n", userId, getHostID(), getUniqueID());
+  }
+  
+  if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() )
+  {
+    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
+    oldNickName = nickName;
   }
 }
@@ -100,10 +113,10 @@
 {
   const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
-
+  
   if ( !list )
   {
     return NULL;
   }
-
+  
   for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
   {
@@ -113,5 +126,5 @@
     }
   }
-
+  
   return NULL;
 }
@@ -123,5 +136,5 @@
 {
   const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
-
+  
   if ( !list )
   {
@@ -129,5 +142,5 @@
     return;
   }
-
+  
   this->playable = NULL;
   for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
@@ -141,10 +154,10 @@
     }
   }
-
+  
   if ( this->playable && userId == getHostID() )
   {
     State::getPlayer()->setPlayable( this->playable );
   }
-
+  
   this->playableUniqueId = uniqueId;
 }
@@ -158,13 +171,69 @@
   if ( playable )
     return playable;
-
+  
   assert( playableUniqueId > 0 );
-
+  
   setPlayableUniqueId( playableUniqueId );
-
+  
   assert( playable );
-
+  
   return playable;
 }
 
-
+/**
+ * client sends server a message to change nick and server changes nick directly
+ * @param nick new nickname
+ */
+void PlayerStats::setNickName( std::string nick )
+{
+  if ( isServer() )
+  {
+    this->nickName = nick;
+    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
+    oldNickName = nickName;
+    return;
+  }
+  else
+  {
+    byte * data = new byte[nick.length()+INTSIZE];
+    
+    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
+    
+    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH );
+    return;
+  }
+}
+
+bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
+{
+  std::string newNick;
+  int res = Converter::byteArrayToString( data, newNick, dataLength );
+  
+  if ( res != dataLength )
+  {
+    PRINTF(2)("invalid message size from user %d\n", userId);
+    newNick = "invalid";
+  }
+  
+  if ( PlayerStats::getStats( userId ) )
+    PlayerStats::getStats( userId )->setNickName( newNick );
+  
+  return true;
+}
+
+void PlayerStats::shellNick( const std::string& newNick )
+{
+  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
+    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
+}
+
+
+
+void PlayerStats::deleteAllPlayerStats( )
+{
+  const std::list<BaseObject*> * list;
+  
+  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
+    delete *list->begin();
+}
+
