Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8506 was 8301, checked in by rennerc, 18 years ago

fixed master memory leak :D

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