Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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