Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9745 was 9656, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy bache back with no conflicts

File size: 7.9 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
[9656]43  this->assignedUserId = userId;
[7974]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
[9656]58  this->assignedUserId = 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
[9656]67  userId_handle = registerVarId( new SynchronizeableInt( &assignedUserId, &assignedUserId, "userId", PERMISSION_MASTER_SERVER ) );
68  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId", PERMISSION_MASTER_SERVER ) );
69  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId", PERMISSION_MASTER_SERVER ) );
70  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score", PERMISSION_MASTER_SERVER ) );
71  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId", PERMISSION_MASTER_SERVER) );
72  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId", PERMISSION_MASTER_SERVER ) );
73  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName", PERMISSION_MASTER_SERVER ) );
74  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName", PERMISSION_MASTER_SERVER ) );
[9406]75
[8623]76  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
[9406]77
[9656]78  PRINTF(5)("PlayerStats created\n");
[7974]79}
80
81
82/**
83 * standard deconstructor
84 */
85PlayerStats::~PlayerStats()
[9656]86{}
[7974]87
88
[9656]89/**
90* override this function to be notified on change
91* of your registred variables.
92* @param id id's which have changed
93 */
[7974]94void PlayerStats::varChangeHandler( std::list< int > & id )
95{
96  if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
97  {
98    this->setPlayableUniqueId( this->playableUniqueId );
[9406]99
[9656]100    PRINTF(4)("uniqueID changed %d %d %d\n", assignedUserId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
[7974]101  }
[9406]102
[8623]103  if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() )
104  {
105    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
[9656]106    this->oldNickName = nickName;
[8623]107  }
[9656]108
109  if ( std::find( id.begin(), id.end(), preferedTeamId_handle) != id.end() )
110  {
111    PRINTF(0)("user %s has changed team to %i\n", nickName.c_str(), this->teamId);
112  }
[7974]113}
114
[9656]115
116
[7974]117/**
118 * get stats for user userId
119 * @param userId user's id
120 * @return stats assigned to user
121 */
122PlayerStats * PlayerStats::getStats( int userId )
123{
124  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
[9406]125
[8147]126  if ( !list )
[8228]127  {
[8147]128    return NULL;
[8228]129  }
[9406]130
[9656]131  for ( std::list<BaseObject*>::const_iterator it = list->
132        begin();
133        it != list->end();
134        it++ )
[7974]135  {
[9656]136
137
138    if ( dynamic_cast<PlayerStats*>(*it)->getAssignedUserId() == userId )
[7974]139    {
140      return dynamic_cast<PlayerStats*>(*it);
141    }
142  }
[9406]143
[7974]144  return NULL;
145}
146
147/**
148 * set playable class id and set playable
149 */
150void PlayerStats::setPlayableUniqueId( int uniqueId )
151{
152  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[9406]153
[8147]154  if ( !list )
155  {
156    this->playableUniqueId = uniqueId;
157    return;
158  }
[9406]159
[7974]160  this->playable = NULL;
[9656]161  for ( std::list<BaseObject*>::const_iterator it = list->
162        begin();
163        it != list->end();
164        it++ )
[7974]165  {
[9656]166    if ( dynamic_cast<Playable*>(*it)->
167         getUniqueID() == uniqueId )
[7974]168    {
169      this->playable = dynamic_cast<Playable*>(*it);
[8228]170      //TODO when OM_PLAYERS is ticked add line:
171      //this->playable->toList( OM_PLAYERS );
[7974]172      break;
173    }
174  }
[9406]175
[9656]176  if ( this->playable && this->assignedUserId == SharedNetworkData::getInstance()
177       ->getHostID() )
[8228]178  {
[8147]179    State::getPlayer()->setPlayable( this->playable );
[9656]180    // also set the team id
[8228]181  }
[9406]182
[7974]183  this->playableUniqueId = uniqueId;
184}
185
186/**
187 * get playable associated to player
188 * @return playable associated to player
189 */
190Playable * PlayerStats::getPlayable()
191{
192  if ( playable )
193    return playable;
[9406]194
[7974]195  assert( playableUniqueId > 0 );
[9406]196
[7974]197  setPlayableUniqueId( playableUniqueId );
[9406]198
[7974]199  assert( playable );
[9406]200
[7974]201  return playable;
202}
203
[8623]204/**
205 * client sends server a message to change nick and server changes nick directly
206 * @param nick new nickname
207 */
208void PlayerStats::setNickName( std::string nick )
209{
[9494]210  if ( SharedNetworkData::getInstance()->isMasterServer())
[8623]211  {
212    this->nickName = nick;
213    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
214    oldNickName = nickName;
215    return;
216  }
217  else
218  {
219    byte * data = new byte[nick.length()+INTSIZE];
[9406]220
[8623]221    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
[9406]222
[9656]223    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, NET_MASTER_SERVER, MP_HIGHBANDWIDTH );
[8623]224    return;
225  }
226}
[7974]227
[9656]228/**
229 * handler for the nick name
230 * @param messageType type of the message
231 * @param data  data of the message
232 * @param dataLength length of the data
233 * @param someData some additional data
234 * @param senderId userId of the sender
235 * @param destinationId userId of the rec
236 * @return true if handled correctly
237 */
238bool PlayerStats::changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
[8623]239{
240  std::string newNick;
241  int res = Converter::byteArrayToString( data, newNick, dataLength );
[9406]242
[8623]243  if ( res != dataLength )
244  {
[9656]245    PRINTF(2)("invalid message size from user %d\n", senderId);
[8623]246    newNick = "invalid";
247  }
[9406]248
[9656]249  if ( PlayerStats::getStats( senderId) )
250    PlayerStats::getStats( senderId )->setNickName( newNick );
[9406]251
[8623]252  return true;
253}
254
[9656]255/**
256 * sets the nick name from the shell
257 * @param newNick new prefered nick name
258 */
[8623]259void PlayerStats::shellNick( const std::string& newNick )
260{
261  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
262    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
[9406]263
[9235]264  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
[8623]265}
266
267
268
[9656]269/**
270 * removes and delets all player stats
271 */
[8623]272void PlayerStats::deleteAllPlayerStats( )
273{
274  const std::list<BaseObject*> * list;
[9406]275
[8623]276  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
277    delete *list->begin();
278}
279
[9110]280
281
[9656]282/**
283 * @return the score list of this player stat
284 */
[9110]285ScoreList PlayerStats::getScoreList( )
286{
287  ScoreList result;
[9406]288
[9110]289  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
[9406]290
[9110]291  if ( !list )
292  {
293    return result;
294  }
[9406]295
[9656]296  for ( std::list<BaseObject*>::const_iterator it = list->
297        begin();
298        it != list->end();
299        it++ )
[9110]300  {
301    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
[9406]302
[9110]303    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
[9406]304
[9110]305    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
306    {
307      it++;
308    }
[9406]309
[9110]310    PlayerScore score;
311    score.name = stats.getNickName();
312    score.score = stats.getScore();
[9406]313
[9110]314    result[stats.getTeamId()].insert(it, score);
315  }
[9406]316
[9110]317  return result;
318}
Note: See TracBrowser for help on using the repository browser.