Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/player_stats.cc @ 9395

Last change on this file since 9395 was 9235, checked in by bensch, 18 years ago

merged the presentation back

File size: 6.8 KB
RevLine 
[7974]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: ...
14*/
15
16#include "player_stats.h"
17
18#include "class_list.h"
19#include "src/lib/util/loading/factory.h"
20
[7984]21#include "player.h"
22#include "state.h"
[8228]23#include "shared_network_data.h"
[7974]24
[9235]25#include "preferences.h"
26
[8362]27#include "debug.h"
[8623]28#include "shell_command.h"
[7984]29
[8362]30
[7974]31CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
32
[8623]33
[7974]34/**
35 * constructor
36 */
37PlayerStats::PlayerStats( int userId )
38{
39  init();
[8623]40 
[7974]41  this->userId = userId;
42}
43
44/**
45 * constructor
46 */
47PlayerStats::PlayerStats( const TiXmlElement* root )
48{
49  init();
50}
51
52void PlayerStats::init( )
53{
54  this->setClassID( CL_PLAYER_STATS, "PlayerStats" );
55
56  this->userId = 0;
[8067]57  this->teamId = TEAM_NOTEAM;
58  this->preferedTeamId = TEAM_NOTEAM;
[7974]59  this->score = 0;
60  this->playableClassId = 0;
61  this->modelFileName = "";
[8623]62  this->nickName = "Player";
63  this->oldNickName = "Player";
64 
[7974]65  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
[8067]66  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
67  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) );
[7974]68  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) );
69  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") );
70  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
71  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
[8623]72  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) );
73 
74  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
75 
[8014]76  PRINTF(0)("PlayerStats created\n");
[7974]77}
78
79
80/**
81 * standard deconstructor
82 */
83PlayerStats::~PlayerStats()
84{
85}
86
87
88 /**
89 * override this function to be notified on change
90 * of your registred variables.
91 * @param id id's which have changed
92  */
93void PlayerStats::varChangeHandler( std::list< int > & id )
94{
95  if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
96  {
97    this->setPlayableUniqueId( this->playableUniqueId );
[8623]98   
[8708]99    PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
[7974]100  }
[8623]101 
102  if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() )
103  {
104    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
105    oldNickName = nickName;
106  }
[7974]107}
108
109/**
110 * get stats for user userId
111 * @param userId user's id
112 * @return stats assigned to user
113 */
114PlayerStats * PlayerStats::getStats( int userId )
115{
116  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
[8623]117 
[8147]118  if ( !list )
[8228]119  {
[8147]120    return NULL;
[8228]121  }
[8623]122 
[7974]123  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
124  {
125    if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId )
126    {
127      return dynamic_cast<PlayerStats*>(*it);
128    }
129  }
[8623]130 
[7974]131  return NULL;
132}
133
134/**
135 * set playable class id and set playable
136 */
137void PlayerStats::setPlayableUniqueId( int uniqueId )
138{
139  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[8623]140 
[8147]141  if ( !list )
142  {
143    this->playableUniqueId = uniqueId;
144    return;
145  }
[8623]146 
[7974]147  this->playable = NULL;
148  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
149  {
150    if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
151    {
152      this->playable = dynamic_cast<Playable*>(*it);
[8228]153      //TODO when OM_PLAYERS is ticked add line:
154      //this->playable->toList( OM_PLAYERS );
[7974]155      break;
156    }
157  }
[8623]158 
[8708]159  if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() )
[8228]160  {
[8147]161    State::getPlayer()->setPlayable( this->playable );
[8228]162  }
[8623]163 
[7974]164  this->playableUniqueId = uniqueId;
165}
166
167/**
168 * get playable associated to player
169 * @return playable associated to player
170 */
171Playable * PlayerStats::getPlayable()
172{
173  if ( playable )
174    return playable;
[8623]175 
[7974]176  assert( playableUniqueId > 0 );
[8623]177 
[7974]178  setPlayableUniqueId( playableUniqueId );
[8623]179 
[7974]180  assert( playable );
[8623]181 
[7974]182  return playable;
183}
184
[8623]185/**
186 * client sends server a message to change nick and server changes nick directly
187 * @param nick new nickname
188 */
189void PlayerStats::setNickName( std::string nick )
190{
[9235]191  if ( SharedNetworkData::getInstance()->isGameServer() )
[8623]192  {
193    this->nickName = nick;
194    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
195    oldNickName = nickName;
196    return;
197  }
198  else
199  {
200    byte * data = new byte[nick.length()+INTSIZE];
201   
202    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
203   
204    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH );
205    return;
206  }
207}
[7974]208
[8623]209bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
210{
211  std::string newNick;
212  int res = Converter::byteArrayToString( data, newNick, dataLength );
213 
214  if ( res != dataLength )
215  {
216    PRINTF(2)("invalid message size from user %d\n", userId);
217    newNick = "invalid";
218  }
219 
220  if ( PlayerStats::getStats( userId ) )
221    PlayerStats::getStats( userId )->setNickName( newNick );
222 
223  return true;
224}
225
226void PlayerStats::shellNick( const std::string& newNick )
227{
228  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
229    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
[9235]230   
231  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
[8623]232}
233
234
235
236void PlayerStats::deleteAllPlayerStats( )
237{
238  const std::list<BaseObject*> * list;
239 
240  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
241    delete *list->begin();
242}
243
[9110]244
245
246ScoreList PlayerStats::getScoreList( )
247{
248  ScoreList result;
249 
250  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
251 
252  if ( !list )
253  {
254    return result;
255  }
256 
257  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
258  {
259    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
260   
261    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
262   
263    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
264    {
265      it++;
266    }
267   
268    PlayerScore score;
269    score.name = stats.getNickName();
270    score.score = stats.getScore();
271   
272    result[stats.getTeamId()].insert(it, score);
273  }
274 
275  return result;
276}
Note: See TracBrowser for help on using the repository browser.