Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/player_stats.cc @ 9504

Last change on this file since 9504 was 9504, checked in by patrick, 18 years ago
  • removing entities from the network works now also on proxies.
  • player team change is handled differently now: handler function
  • there was a chronological error in the change team function call stack
File size: 7.3 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());
[9504]107    this->oldNickName = nickName;
[8623]108  }
[9504]109
110  if ( std::find( id.begin(), id.end(), teamId_handle ) != id.end() )
111  {
112    PRINTF(0)("user %s joins team %i\n", this->nickName.c_str(), this->teamId);
113    this->setPreferedTeamIdHandler( this->teamId);
114  }
115
[7974]116}
117
[9504]118
[7974]119/**
[9504]120 * handler for setting the prefered team id
121 * @param newTeamId: the new team id
122 */
123void PlayerStats::setPreferedTeamIdHandler( int newTeamId)
124{
125
126  if( this->playable == NULL)
127  {
128    PRINTF(0)("could not set prefered team, since the playable is not yet set\n");
129    return;
130  }
131
132  this->playable->setTeam(newTeamId);
133}
134
135
136
137/**
[7974]138 * get stats for user userId
139 * @param userId user's id
140 * @return stats assigned to user
141 */
142PlayerStats * PlayerStats::getStats( int userId )
143{
144  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
[9406]145
[8147]146  if ( !list )
[8228]147  {
[8147]148    return NULL;
[8228]149  }
[9406]150
[7974]151  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
152  {
153    if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId )
154    {
155      return dynamic_cast<PlayerStats*>(*it);
156    }
157  }
[9406]158
[7974]159  return NULL;
160}
161
162/**
163 * set playable class id and set playable
164 */
165void PlayerStats::setPlayableUniqueId( int uniqueId )
166{
167  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[9406]168
[8147]169  if ( !list )
170  {
171    this->playableUniqueId = uniqueId;
172    return;
173  }
[9406]174
[7974]175  this->playable = NULL;
176  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
177  {
178    if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
179    {
180      this->playable = dynamic_cast<Playable*>(*it);
[8228]181      //TODO when OM_PLAYERS is ticked add line:
182      //this->playable->toList( OM_PLAYERS );
[7974]183      break;
184    }
185  }
[9406]186
[8708]187  if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() )
[8228]188  {
[8147]189    State::getPlayer()->setPlayable( this->playable );
[8228]190  }
[9406]191
[7974]192  this->playableUniqueId = uniqueId;
193}
194
195/**
196 * get playable associated to player
197 * @return playable associated to player
198 */
199Playable * PlayerStats::getPlayable()
200{
201  if ( playable )
202    return playable;
[9406]203
[7974]204  assert( playableUniqueId > 0 );
[9406]205
[7974]206  setPlayableUniqueId( playableUniqueId );
[9406]207
[7974]208  assert( playable );
[9406]209
[7974]210  return playable;
211}
212
[8623]213/**
214 * client sends server a message to change nick and server changes nick directly
215 * @param nick new nickname
216 */
217void PlayerStats::setNickName( std::string nick )
218{
[9494]219  if ( SharedNetworkData::getInstance()->isMasterServer())
[8623]220  {
221    this->nickName = nick;
222    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
223    oldNickName = nickName;
224    return;
225  }
226  else
227  {
228    byte * data = new byte[nick.length()+INTSIZE];
[9406]229
[8623]230    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
[9406]231
[8623]232    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH );
233    return;
234  }
235}
[7974]236
[8623]237bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
238{
239  std::string newNick;
240  int res = Converter::byteArrayToString( data, newNick, dataLength );
[9406]241
[8623]242  if ( res != dataLength )
243  {
244    PRINTF(2)("invalid message size from user %d\n", userId);
245    newNick = "invalid";
246  }
[9406]247
[8623]248  if ( PlayerStats::getStats( userId ) )
249    PlayerStats::getStats( userId )->setNickName( newNick );
[9406]250
[8623]251  return true;
252}
253
254void PlayerStats::shellNick( const std::string& newNick )
255{
256  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
257    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
[9406]258
[9235]259  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
[8623]260}
261
262
263
264void PlayerStats::deleteAllPlayerStats( )
265{
266  const std::list<BaseObject*> * list;
[9406]267
[8623]268  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
269    delete *list->begin();
270}
271
[9110]272
273
274ScoreList PlayerStats::getScoreList( )
275{
276  ScoreList result;
[9406]277
[9110]278  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
[9406]279
[9110]280  if ( !list )
281  {
282    return result;
283  }
[9406]284
[9110]285  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
286  {
287    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
[9406]288
[9110]289    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
[9406]290
[9110]291    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
292    {
293      it++;
294    }
[9406]295
[9110]296    PlayerScore score;
297    score.name = stats.getNickName();
298    score.score = stats.getScore();
[9406]299
[9110]300    result[stats.getTeamId()].insert(it, score);
301  }
[9406]302
[9110]303  return result;
304}
Note: See TracBrowser for help on using the repository browser.