Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged network back to trunk

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 "src/lib/util/loading/factory.h"
19
20#include "player.h"
21#include "playable.h"
22#include "state.h"
23#include "shared_network_data.h"
24
25#include "converter.h"
26
27#include "parser/preferences/preferences.h"
28
29#include "debug.h"
30#include "shell_command.h"
31
32
33
34ObjectListDefinition(PlayerStats);
35CREATE_FACTORY(PlayerStats);
36
37/**
38 * constructor
39 */
40PlayerStats::PlayerStats( int userId )
41{
42  init();
43
44  this->assignedUserId = userId;
45}
46
47/**
48 * constructor
49 */
50PlayerStats::PlayerStats( const TiXmlElement* root )
51{
52  init();
53}
54
55void PlayerStats::init( )
56{
57  this->registerObject(this, PlayerStats::_objectList);
58
59  this->assignedUserId = 0;
60  this->teamId = TEAM_NOTEAM;
61  this->preferedTeamId = TEAM_NOTEAM;
62  this->score = 0;
63  this->playableClassId = 0;
64  this->modelFileName = "";
65  this->nickName = "Player";
66  this->oldNickName = "Player";
67
68  userId_handle = registerVarId( new SynchronizeableInt( &assignedUserId, &assignedUserId, "userId", PERMISSION_MASTER_SERVER ) );
69  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId", PERMISSION_MASTER_SERVER ) );
70  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId", PERMISSION_MASTER_SERVER ) );
71  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score", PERMISSION_MASTER_SERVER ) );
72  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId", PERMISSION_MASTER_SERVER) );
73  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId", PERMISSION_MASTER_SERVER ) );
74  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName", PERMISSION_MASTER_SERVER ) );
75  nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName", PERMISSION_MASTER_SERVER ) );
76
77  MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL );
78
79  PRINTF(5)("PlayerStats created\n");
80}
81
82
83/**
84 * standard deconstructor
85 */
86PlayerStats::~PlayerStats()
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 );
100
101    PRINTF(4)("uniqueID changed %d %d %d\n", assignedUserId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
102  }
103
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());
107    this->oldNickName = nickName;
108  }
109
110  if ( std::find( id.begin(), id.end(), preferedTeamId_handle) != id.end() )
111  {
112    PRINTF(0)("user %s has changed team to %i\n", nickName.c_str(), this->teamId);
113  }
114}
115
116
117
118/**
119 * get stats for user userId
120 * @param userId user's id
121 * @return stats assigned to user
122 */
123PlayerStats * PlayerStats::getStats( int userId )
124{
125  for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin();
126       it != PlayerStats::objectList().end();
127       ++it)
128  {
129    if ( (*it)->getAssignedUserId() == userId )
130    {
131      return (*it);
132    }
133  }
134
135  return NULL;
136}
137
138/**
139 * set playable class id and set playable
140 */
141void PlayerStats::setPlayableUniqueId( int uniqueId )
142{
143  this->playable = NULL;
144  for (ObjectList<Playable>::const_iterator it = Playable::objectList().begin();
145       it != Playable::objectList().end();
146       ++it)
147  {
148    if ( (*it)->getUniqueID() == uniqueId )
149    {
150      this->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 && this->assignedUserId == SharedNetworkData::getInstance()->getHostID() )
158  {
159    State::getPlayer()->setPlayable( this->playable );
160    // also set the team id
161  }
162
163  this->playableUniqueId = uniqueId;
164}
165
166/**
167 * get playable associated to player
168 * @return playable associated to player
169 */
170Playable * PlayerStats::getPlayable()
171{
172  if ( playable )
173    return playable;
174
175  assert( playableUniqueId > 0 );
176
177  setPlayableUniqueId( playableUniqueId );
178
179  assert( playable );
180
181  return playable;
182}
183
184/**
185 * client sends server a message to change nick and server changes nick directly
186 * @param nick new nickname
187 */
188void PlayerStats::setNickName( std::string nick )
189{
190  if ( SharedNetworkData::getInstance()->isMasterServer())
191  {
192    this->nickName = nick;
193    PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str());
194    oldNickName = nickName;
195    return;
196  }
197  else
198  {
199    byte * data = new byte[nick.length()+INTSIZE];
200
201    assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE );
202
203    MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, NET_MASTER_SERVER, MP_HIGHBANDWIDTH );
204    return;
205  }
206}
207
208/**
209 * handler for the nick name
210 * @param messageType type of the message
211 * @param data  data of the message
212 * @param dataLength length of the data
213 * @param someData some additional data
214 * @param senderId userId of the sender
215 * @param destinationId userId of the rec
216 * @return true if handled correctly
217 */
218bool PlayerStats::changeNickHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
219{
220  std::string newNick;
221  int res = Converter::byteArrayToString( data, newNick, dataLength );
222
223  if ( res != dataLength )
224  {
225    PRINTF(2)("invalid message size from user %d\n", senderId);
226    newNick = "invalid";
227  }
228
229  if ( PlayerStats::getStats( senderId) )
230    PlayerStats::getStats( senderId )->setNickName( newNick );
231
232  return true;
233}
234
235/**
236 * sets the nick name from the shell
237 * @param newNick new prefered nick name
238 */
239void PlayerStats::shellNick( const std::string& newNick )
240{
241  if ( getStats( SharedNetworkData::getInstance()->getHostID() ) )
242    getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick );
243
244  Preferences::getInstance()->setString( "multiplayer", "nickname", newNick );
245}
246
247
248
249/**
250 * removes and delets all player stats
251 */
252void PlayerStats::deleteAllPlayerStats( )
253{
254
255  while(!PlayerStats::objectList().empty())
256    delete PlayerStats::objectList().front();
257}
258
259
260
261/**
262 * @return the score list of this player stat
263 */
264ScoreList PlayerStats::getScoreList( )
265{
266  ScoreList result;
267
268  for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin();
269       it != PlayerStats::objectList().end();
270       ++it)
271  {
272    PlayerStats& stats = *(*it);
273
274    TeamScoreList::iterator it = result[stats.getTeamId()].begin();
275
276    while (  it != result[stats.getTeamId()].end() && stats.score > it->score )
277    {
278      it++;
279    }
280
281    PlayerScore score;
282    score.name = stats.getNickName();
283    score.score = stats.getScore();
284
285    result[stats.getTeamId()].insert(it, score);
286  }
287
288  return result;
289}
Note: See TracBrowser for help on using the repository browser.