Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more framework switching

File size: 6.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 "preferences.h"
26
27#include "debug.h"
28#include "shell_command.h"
29
30
31CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
32
33
34/**
35 * constructor
36 */
37PlayerStats::PlayerStats( int userId )
38{
39  init();
40
41  this->userId = userId;
42}
43
44/**
45 * constructor
46 */
47PlayerStats::PlayerStats( const TiXmlElement* root )
48{
49  init();
50}
51
52void PlayerStats::init( )
53{
54  this->setClassID( CL_PLAYER_STATS, "PlayerStats" );
55
56  this->userId = 0;
57  this->teamId = TEAM_NOTEAM;
58  this->preferedTeamId = TEAM_NOTEAM;
59  this->score = 0;
60  this->playableClassId = 0;
61  this->modelFileName = "";
62  this->nickName = "Player";
63  this->oldNickName = "Player";
64
65  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
66  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
67  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) );
68  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) );
69  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") );
70  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
71  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
72  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) );
73
74  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
75
76  PRINTF(0)("PlayerStats created\n");
77}
78
79
80/**
81 * standard deconstructor
82 */
83PlayerStats::~PlayerStats()
84{
85}
86
87
88 /**
89 * override this function to be notified on change
90 * of your registred variables.
91 * @param id id's which have changed
92  */
93void PlayerStats::varChangeHandler( std::list< int > & id )
94{
95  if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
96  {
97    this->setPlayableUniqueId( this->playableUniqueId );
98
99    PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
100  }
101
102  if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() )
103  {
104    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
105    oldNickName = nickName;
106  }
107}
108
109/**
110 * get stats for user userId
111 * @param userId user's id
112 * @return stats assigned to user
113 */
114PlayerStats * PlayerStats::getStats( int userId )
115{
116  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
117
118  if ( !list )
119  {
120    return NULL;
121  }
122
123  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
124  {
125    if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId )
126    {
127      return dynamic_cast<PlayerStats*>(*it);
128    }
129  }
130
131  return NULL;
132}
133
134/**
135 * set playable class id and set playable
136 */
137void PlayerStats::setPlayableUniqueId( int uniqueId )
138{
139  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
140
141  if ( !list )
142  {
143    this->playableUniqueId = uniqueId;
144    return;
145  }
146
147  this->playable = NULL;
148  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
149  {
150    if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
151    {
152      this->playable = dynamic_cast<Playable*>(*it);
153      //TODO when OM_PLAYERS is ticked add line:
154      //this->playable->toList( OM_PLAYERS );
155      break;
156    }
157  }
158
159  if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() )
160  {
161    State::getPlayer()->setPlayable( this->playable );
162  }
163
164  this->playableUniqueId = uniqueId;
165}
166
167/**
168 * get playable associated to player
169 * @return playable associated to player
170 */
171Playable * PlayerStats::getPlayable()
172{
173  if ( playable )
174    return playable;
175
176  assert( playableUniqueId > 0 );
177
178  setPlayableUniqueId( playableUniqueId );
179
180  assert( playable );
181
182  return playable;
183}
184
185/**
186 * client sends server a message to change nick and server changes nick directly
187 * @param nick new nickname
188 */
189void PlayerStats::setNickName( std::string nick )
190{
191  if ( SharedNetworkData::getInstance()->isMasterServer() )
192  {
193    this->nickName = nick;
194    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
195    oldNickName = nickName;
196    return;
197  }
198  else
199  {
200    byte * data = new byte[nick.length()+INTSIZE];
201
202    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
203
204    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH );
205    return;
206  }
207}
208
209bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
210{
211  std::string newNick;
212  int res = Converter::byteArrayToString( data, newNick, dataLength );
213
214  if ( res != dataLength )
215  {
216    PRINTF(2)("invalid message size from user %d\n", userId);
217    newNick = "invalid";
218  }
219
220  if ( PlayerStats::getStats( userId ) )
221    PlayerStats::getStats( userId )->setNickName( newNick );
222
223  return true;
224}
225
226void PlayerStats::shellNick( const std::string& newNick )
227{
228  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
229    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
230
231  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
232}
233
234
235
236void PlayerStats::deleteAllPlayerStats( )
237{
238  const std::list<BaseObject*> * list;
239
240  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
241    delete *list->begin();
242}
243
244
245
246ScoreList PlayerStats::getScoreList( )
247{
248  ScoreList result;
249
250  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
251
252  if ( !list )
253  {
254    return result;
255  }
256
257  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
258  {
259    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
260
261    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
262
263    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
264    {
265      it++;
266    }
267
268    PlayerScore score;
269    score.name = stats.getNickName();
270    score.score = stats.getScore();
271
272    result[stats.getTeamId()].insert(it, score);
273  }
274
275  return result;
276}
Note: See TracBrowser for help on using the repository browser.