Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/player_stats.h @ 8187

Last change on this file since 8187 was 8187, checked in by rennerc, 18 years ago

reverted playerstats. disconnect works now

File size: 3.8 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
26CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS);
27
28/**
29 * constructor
30 */
31PlayerStats::PlayerStats( int userId )
32{
33  init();
34 
35  this->userId = userId;
36}
37
38/**
39 * constructor
40 */
41PlayerStats::PlayerStats( const TiXmlElement* root )
42{
43  init();
44}
45
46void PlayerStats::init( )
47{
48  this->setClassID( CL_PLAYER_STATS, "PlayerStats" );
49
50  this->userId = 0;
51  this->teamId = TEAM_NOTEAM;
52  this->preferedTeamId = TEAM_NOTEAM;
53  this->score = 0;
54  this->playableClassId = 0;
55  this->modelFileName = "";
56 
57  userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) );
58  teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) );
59  preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) );
60  score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) );
61  playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") );
62  playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) );
63  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
64 
65  PRINTF(0)("PlayerStats created\n");
66}
67
68
69/**
70 * standard deconstructor
71 */
72PlayerStats::~PlayerStats()
73{
74}
75
76
77 /**
78 * override this function to be notified on change
79 * of your registred variables.
80 * @param id id's which have changed
81  */
82void PlayerStats::varChangeHandler( std::list< int > & id )
83{
84  if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
85  {
86    this->setPlayableUniqueId( this->playableUniqueId );
87   
88    PRINTF(0)("uniqueID changed %d %d %d\n", userId, getHostID(), getUniqueID());
89  }
90}
91
92/**
93 * get stats for user userId
94 * @param userId user's id
95 * @return stats assigned to user
96 */
97PlayerStats * PlayerStats::getStats( int userId )
98{
99  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
100 
101  if ( !list )
102  {
103    return NULL;
104  }
105 
106  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
107  {
108    if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId )
109    {
110      return dynamic_cast<PlayerStats*>(*it);
111    }
112  }
113 
114  return NULL;
115}
116
117/**
118 * set playable class id and set playable
119 */
120void PlayerStats::setPlayableUniqueId( int uniqueId )
121{
122  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
123 
124  if ( !list )
125  {
126    this->playableUniqueId = uniqueId;
127    return;
128  }
129 
130  this->playable = NULL;
131  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
132  {
133    if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
134    {
135      this->playable = dynamic_cast<Playable*>(*it);
136      break;
137    }
138  }
139 
140  if ( this->playable && userId == getHostID() )
141  {
142    State::getPlayer()->setPlayable( this->playable );
143  }
144 
145  this->playableUniqueId = uniqueId;
146}
147
148/**
149 * get playable associated to player
150 * @return playable associated to player
151 */
152Playable * PlayerStats::getPlayable()
153{
154  if ( playable )
155    return playable;
156 
157  assert( playableUniqueId > 0 );
158 
159  setPlayableUniqueId( playableUniqueId );
160 
161  assert( playable );
162 
163  return playable;
164}
165
166
Note: See TracBrowser for help on using the repository browser.