Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/lib/network/player_stats.cc @ 10637

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

orxonox/trunk: merged the Presentation back

File size: 6.7 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 "debug.h"
26#include "shell_command.h"
27
28
29CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
30
31
32/**
33 * constructor
34 */
35PlayerStats::PlayerStats( int userId )
36{
37  init();
38 
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;
55  this->teamId = TEAM_NOTEAM;
56  this->preferedTeamId = TEAM_NOTEAM;
57  this->score = 0;
58  this->playableClassId = 0;
59  this->modelFileName = "";
60  this->nickName = "Player";
61  this->oldNickName = "Player";
62 
63  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
64  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
65  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) );
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" ) );
70  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) );
71 
72  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
73 
74  PRINTF(0)("PlayerStats created\n");
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 );
96   
97    PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
98  }
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  }
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 );
115 
116  if ( !list )
117  {
118    return NULL;
119  }
120 
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  }
128 
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 );
138 
139  if ( !list )
140  {
141    this->playableUniqueId = uniqueId;
142    return;
143  }
144 
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);
151      //TODO when OM_PLAYERS is ticked add line:
152      //this->playable->toList( OM_PLAYERS );
153      break;
154    }
155  }
156 
157  if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() )
158  {
159    State::getPlayer()->setPlayable( this->playable );
160  }
161 
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;
173 
174  assert( playableUniqueId > 0 );
175 
176  setPlayableUniqueId( playableUniqueId );
177 
178  assert( playable );
179 
180  return playable;
181}
182
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}
206
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
240
241
242ScoreList PlayerStats::getScoreList( )
243{
244  ScoreList result;
245 
246  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
247 
248  if ( !list )
249  {
250    return result;
251  }
252 
253  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
254  {
255    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
256   
257    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
258   
259    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
260    {
261      it++;
262    }
263   
264    PlayerScore score;
265    score.name = stats.getNickName();
266    score.score = stats.getScore();
267   
268    result[stats.getTeamId()].insert(it, score);
269  }
270 
271  return result;
272}
Note: See TracBrowser for help on using the repository browser.