Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/player_stats.cc @ 8810

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

merged network back
merged with command:
svn merge -r8625:HEAD https://svn.orxonox.net/orxonox/branches/network .
no conflicts

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