Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

switched another interface funciton

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