Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

better debug output now

File size: 7.7 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->assignedUserId = 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->assignedUserId = 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( &assignedUserId, &assignedUserId, "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(5)("PlayerStats created\n");
79}
80
81
82/**
83 * standard deconstructor
84 */
85PlayerStats::~PlayerStats()
86{}
87
88
89/**
90* override this function to be notified on change
91* of your registred variables.
92* @param id id's which have changed
93 */
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 );
99
100    PRINTF(4)("uniqueID changed %d %d %d\n", assignedUserId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
101  }
102
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());
106    this->oldNickName = nickName;
107  }
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  }
113}
114
115
116
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 );
125
126  if ( !list )
127  {
128    return NULL;
129  }
130
131  for ( std::list<BaseObject*>::const_iterator it = list->
132        begin();
133        it != list->end();
134        it++ )
135  {
136
137
138    if ( dynamic_cast<PlayerStats*>(*it)->getAssignedUserId() == userId )
139    {
140      return dynamic_cast<PlayerStats*>(*it);
141    }
142  }
143
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 );
153
154  if ( !list )
155  {
156    this->playableUniqueId = uniqueId;
157    return;
158  }
159
160  this->playable = NULL;
161  for ( std::list<BaseObject*>::const_iterator it = list->
162        begin();
163        it != list->end();
164        it++ )
165  {
166    if ( dynamic_cast<Playable*>(*it)->
167         getUniqueID() == uniqueId )
168    {
169      this->playable = dynamic_cast<Playable*>(*it);
170      //TODO when OM_PLAYERS is ticked add line:
171      //this->playable->toList( OM_PLAYERS );
172      break;
173    }
174  }
175
176  if ( this->playable && this->assignedUserId == SharedNetworkData::getInstance()
177       ->getHostID() )
178  {
179    State::getPlayer()->setPlayable( this->playable );
180    // also set the team id
181  }
182
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;
194
195  assert( playableUniqueId > 0 );
196
197  setPlayableUniqueId( playableUniqueId );
198
199  assert( playable );
200
201  return playable;
202}
203
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{
210  if ( SharedNetworkData::getInstance()->isMasterServer())
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];
220
221    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
222
223    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, NET_MASTER_SERVER, MP_HIGHBANDWIDTH );
224    return;
225  }
226}
227
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  )
239{
240  std::string newNick;
241  int res = Converter::byteArrayToString( data, newNick, dataLength );
242
243  if ( res != dataLength )
244  {
245    PRINTF(2)("invalid message size from user %d\n", senderId);
246    newNick = "invalid";
247  }
248
249  if ( PlayerStats::getStats( senderId) )
250    PlayerStats::getStats( senderId )->setNickName( newNick );
251
252  return true;
253}
254
255/**
256 * sets the nick name from the shell
257 * @param newNick new prefered nick name
258 */
259void PlayerStats::shellNick( const std::string& newNick )
260{
261  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
262    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
263
264  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
265}
266
267
268
269/**
270 * removes and delets all player stats
271 */
272void PlayerStats::deleteAllPlayerStats( )
273{
274  const std::list<BaseObject*> * list;
275
276  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
277    delete *list->begin();
278}
279
280
281
282/**
283 * @return the score list of this player stat
284 */
285ScoreList PlayerStats::getScoreList( )
286{
287  ScoreList result;
288
289  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
290
291  if ( !list )
292  {
293    return result;
294  }
295
296  for ( std::list<BaseObject*>::const_iterator it = list->
297        begin();
298        it != list->end();
299        it++ )
300  {
301    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
302
303    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
304
305    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
306    {
307      it++;
308    }
309
310    PlayerScore score;
311    score.name = stats.getNickName();
312    score.score = stats.getScore();
313
314    result[stats.getTeamId()].insert(it, score);
315  }
316
317  return result;
318}
Note: See TracBrowser for help on using the repository browser.