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
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  }
191
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;
203
204  assert( playableUniqueId > 0 );
205
206  setPlayableUniqueId( playableUniqueId );
207
208  assert( playable );
209
210  return playable;
211}
212
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{
219  if ( SharedNetworkData::getInstance()->isMasterServer())
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];
229
230    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
231
232    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH );
233    return;
234  }
235}
236
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 );
241
242  if ( res != dataLength )
243  {
244    PRINTF(2)("invalid message size from user %d\n", userId);
245    newNick = "invalid";
246  }
247
248  if ( PlayerStats::getStats( userId ) )
249    PlayerStats::getStats( userId )->setNickName( newNick );
250
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 );
258
259  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
260}
261
262
263
264void PlayerStats::deleteAllPlayerStats( )
265{
266  const std::list<BaseObject*> * list;
267
268  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
269    delete *list->begin();
270}
271
272
273
274ScoreList PlayerStats::getScoreList( )
275{
276  ScoreList result;
277
278  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
279
280  if ( !list )
281  {
282    return result;
283  }
284
285  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
286  {
287    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
288
289    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
290
291    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
292    {
293      it++;
294    }
295
296    PlayerScore score;
297    score.name = stats.getNickName();
298    score.score = stats.getScore();
299
300    result[stats.getTeamId()].insert(it, score);
301  }
302
303  return result;
304}
Note: See TracBrowser for help on using the repository browser.