Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more documentation. clients now chooses the playable for the proxy:D working on this now

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