Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

extended the callback function for message handlers to match the new sender/dest message structure

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