Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/network/player_stats.cc @ 9690

Last change on this file since 9690 was 9690, checked in by bensch, 18 years ago

some network-stuff

File size: 7.9 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#include "class_id.h"
33
34CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
35NewObjectListDefinitionID(PlayerStats, CL_PLAYER_STATS);
36
37/**
38 * constructor
39 */
40PlayerStats::PlayerStats( int userId )
41{
42  init();
43
44  this->assignedUserId = userId;
45}
46
47/**
48 * constructor
49 */
50PlayerStats::PlayerStats( const TiXmlElement* root )
51{
52  init();
53}
54
55void PlayerStats::init( )
56{
57  this->setClassID( CL_PLAYER_STATS, "PlayerStats" );
58
59  this->assignedUserId = 0;
60  this->teamId = TEAM_NOTEAM;
61  this->preferedTeamId = TEAM_NOTEAM;
62  this->score = 0;
63  this->playableClassId = 0;
64  this->modelFileName = "";
65  this->nickName = "Player";
66  this->oldNickName = "Player";
67
68  userId_handle = registerVarId( new SynchronizeableInt( &assignedUserId, &assignedUserId, "userId", PERMISSION_MASTER_SERVER ) );
69  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId", PERMISSION_MASTER_SERVER ) );
70  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId", PERMISSION_MASTER_SERVER ) );
71  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score", PERMISSION_MASTER_SERVER ) );
72  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId", PERMISSION_MASTER_SERVER) );
73  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId", PERMISSION_MASTER_SERVER ) );
74  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName", PERMISSION_MASTER_SERVER ) );
75  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName", PERMISSION_MASTER_SERVER ) );
76
77  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
78
79  PRINTF(5)("PlayerStats created\n");
80}
81
82
83/**
84 * standard deconstructor
85 */
86PlayerStats::~PlayerStats()
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(4)("uniqueID changed %d %d %d\n", assignedUserId, 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(), preferedTeamId_handle) != id.end() )
111  {
112    PRINTF(0)("user %s has changed team to %i\n", nickName.c_str(), this->teamId);
113  }
114}
115
116
117
118/**
119 * get stats for user userId
120 * @param userId user's id
121 * @return stats assigned to user
122 */
123PlayerStats * PlayerStats::getStats( int userId )
124{
125  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
126
127  if ( !list )
128  {
129    return NULL;
130  }
131
132  for ( std::list<BaseObject*>::const_iterator it = list->
133        begin();
134        it != list->end();
135        it++ )
136  {
137
138
139    if ( dynamic_cast<PlayerStats*>(*it)->getAssignedUserId() == userId )
140    {
141      return dynamic_cast<PlayerStats*>(*it);
142    }
143  }
144
145  return NULL;
146}
147
148/**
149 * set playable class id and set playable
150 */
151void PlayerStats::setPlayableUniqueId( int uniqueId )
152{
153  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
154
155  if ( !list )
156  {
157    this->playableUniqueId = uniqueId;
158    return;
159  }
160
161  this->playable = NULL;
162  for ( std::list<BaseObject*>::const_iterator it = list->
163        begin();
164        it != list->end();
165        it++ )
166  {
167    if ( dynamic_cast<Playable*>(*it)->
168         getUniqueID() == uniqueId )
169    {
170      this->playable = dynamic_cast<Playable*>(*it);
171      //TODO when OM_PLAYERS is ticked add line:
172      //this->playable->toList( OM_PLAYERS );
173      break;
174    }
175  }
176
177  if ( this->playable && this->assignedUserId == SharedNetworkData::getInstance()
178       ->getHostID() )
179  {
180    State::getPlayer()->setPlayable( this->playable );
181    // also set the team id
182  }
183
184  this->playableUniqueId = uniqueId;
185}
186
187/**
188 * get playable associated to player
189 * @return playable associated to player
190 */
191Playable * PlayerStats::getPlayable()
192{
193  if ( playable )
194    return playable;
195
196  assert( playableUniqueId > 0 );
197
198  setPlayableUniqueId( playableUniqueId );
199
200  assert( playable );
201
202  return playable;
203}
204
205/**
206 * client sends server a message to change nick and server changes nick directly
207 * @param nick new nickname
208 */
209void PlayerStats::setNickName( std::string nick )
210{
211  if ( SharedNetworkData::getInstance()->isMasterServer())
212  {
213    this->nickName = nick;
214    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
215    oldNickName = nickName;
216    return;
217  }
218  else
219  {
220    byte * data = new byte[nick.length()+INTSIZE];
221
222    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
223
224    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, NET_MASTER_SERVER, MP_HIGHBANDWIDTH );
225    return;
226  }
227}
228
229/**
230 * handler for the nick name
231 * @param messageType type of the message
232 * @param data  data of the message
233 * @param dataLength length of the data
234 * @param someData some additional data
235 * @param senderId userId of the sender
236 * @param destinationId userId of the rec
237 * @return true if handled correctly
238 */
239bool PlayerStats::changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
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", senderId);
247    newNick = "invalid";
248  }
249
250  if ( PlayerStats::getStats( senderId) )
251    PlayerStats::getStats( senderId )->setNickName( newNick );
252
253  return true;
254}
255
256/**
257 * sets the nick name from the shell
258 * @param newNick new prefered nick name
259 */
260void PlayerStats::shellNick( const std::string& newNick )
261{
262  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
263    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
264
265  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
266}
267
268
269
270/**
271 * removes and delets all player stats
272 */
273void PlayerStats::deleteAllPlayerStats( )
274{
275  const std::list<BaseObject*> * list;
276
277  while ( (list  = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() )
278    delete *list->begin();
279}
280
281
282
283/**
284 * @return the score list of this player stat
285 */
286ScoreList PlayerStats::getScoreList( )
287{
288  ScoreList result;
289
290  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
291
292  if ( !list )
293  {
294    return result;
295  }
296
297  for ( std::list<BaseObject*>::const_iterator it = list->
298        begin();
299        it != list->end();
300        it++ )
301  {
302    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
303
304    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
305
306    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
307    {
308      it++;
309    }
310
311    PlayerScore score;
312    score.name = stats.getNickName();
313    score.score = stats.getScore();
314
315    result[stats.getTeamId()].insert(it, score);
316  }
317
318  return result;
319}
Note: See TracBrowser for help on using the repository browser.