Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9505 was 9505, checked in by patrick, 18 years ago

changing the team color now works and a little cleanup

File size: 7.4 KB
Line 
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
21#include "player.h"
22#include "state.h"
23#include "shared_network_data.h"
24
25#include "converter.h"
26
27#include "preferences.h"
28
29#include "debug.h"
30#include "shell_command.h"
31
32
33CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
34
35
36/**
37 * constructor
38 */
39PlayerStats::PlayerStats( int userId )
40{
41  init();
42
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;
59  this->teamId = TEAM_NOTEAM;
60  this->preferedTeamId = TEAM_NOTEAM;
61  this->score = 0;
62  this->playableClassId = 0;
63  this->modelFileName = "";
64  this->nickName = "Player";
65  this->oldNickName = "Player";
66
67  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
68  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
69  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) );
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" ) );
74  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) );
75
76  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
77
78  PRINTF(0)("PlayerStats created\n");
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 );
100
101    PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
102  }
103
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    this->oldNickName = nickName;
108  }
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
116}
117
118
119/**
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/**
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 );
145
146  if ( !list )
147  {
148    return NULL;
149  }
150
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  }
158
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 );
168
169  if ( !list )
170  {
171    this->playableUniqueId = uniqueId;
172    return;
173  }
174
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);
181      //TODO when OM_PLAYERS is ticked add line:
182      //this->playable->toList( OM_PLAYERS );
183      break;
184    }
185  }
186
187  if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() )
188  {
189    State::getPlayer()->setPlayable( this->playable );
190    // also set the team id
191    this->playable->setTeam(this->getTeamId());
192  }
193
194  this->playableUniqueId = uniqueId;
195}
196
197/**
198 * get playable associated to player
199 * @return playable associated to player
200 */
201Playable * PlayerStats::getPlayable()
202{
203  if ( playable )
204    return playable;
205
206  assert( playableUniqueId > 0 );
207
208  setPlayableUniqueId( playableUniqueId );
209
210  assert( playable );
211
212  return playable;
213}
214
215/**
216 * client sends server a message to change nick and server changes nick directly
217 * @param nick new nickname
218 */
219void PlayerStats::setNickName( std::string nick )
220{
221  if ( SharedNetworkData::getInstance()->isMasterServer())
222  {
223    this->nickName = nick;
224    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
225    oldNickName = nickName;
226    return;
227  }
228  else
229  {
230    byte * data = new byte[nick.length()+INTSIZE];
231
232    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
233
234    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH );
235    return;
236  }
237}
238
239bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
240{
241  std::string newNick;
242  int res = Converter::byteArrayToString( data, newNick, dataLength );
243
244  if ( res != dataLength )
245  {
246    PRINTF(2)("invalid message size from user %d\n", userId);
247    newNick = "invalid";
248  }
249
250  if ( PlayerStats::getStats( userId ) )
251    PlayerStats::getStats( userId )->setNickName( newNick );
252
253  return true;
254}
255
256void PlayerStats::shellNick( const std::string& newNick )
257{
258  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
259    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
260
261  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
262}
263
264
265
266void PlayerStats::deleteAllPlayerStats( )
267{
268  const std::list<BaseObject*> * list;
269
270  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
271    delete *list->begin();
272}
273
274
275
276ScoreList PlayerStats::getScoreList( )
277{
278  ScoreList result;
279
280  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
281
282  if ( !list )
283  {
284    return result;
285  }
286
287  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
288  {
289    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
290
291    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
292
293    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
294    {
295      it++;
296    }
297
298    PlayerScore score;
299    score.name = stats.getNickName();
300    score.score = stats.getScore();
301
302    result[stats.getTeamId()].insert(it, score);
303  }
304
305  return result;
306}
Note: See TracBrowser for help on using the repository browser.