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