Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9486 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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