| 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 |  | 
|---|
| 26 | CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS); | 
|---|
| 27 |  | 
|---|
| 28 | /** | 
|---|
| 29 |  * constructor | 
|---|
| 30 |  */ | 
|---|
| 31 | PlayerStats::PlayerStats( int userId ) | 
|---|
| 32 | { | 
|---|
| 33 |   init(); | 
|---|
| 34 |    | 
|---|
| 35 |   this->userId = userId; | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | /** | 
|---|
| 39 |  * constructor | 
|---|
| 40 |  */ | 
|---|
| 41 | PlayerStats::PlayerStats( const TiXmlElement* root ) | 
|---|
| 42 | { | 
|---|
| 43 |   init(); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | void 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 |  */ | 
|---|
| 72 | PlayerStats::~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 |   */ | 
|---|
| 82 | void 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 |  */ | 
|---|
| 97 | PlayerStats * 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 |  */ | 
|---|
| 120 | void 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 |       //TODO when OM_PLAYERS is ticked add line: | 
|---|
| 137 |       //this->playable->toList( OM_PLAYERS ); | 
|---|
| 138 |       break; | 
|---|
| 139 |     } | 
|---|
| 140 |   } | 
|---|
| 141 |    | 
|---|
| 142 |   if ( this->playable && userId == getHostID() ) | 
|---|
| 143 |   { | 
|---|
| 144 |     State::getPlayer()->setPlayable( this->playable ); | 
|---|
| 145 |   } | 
|---|
| 146 |    | 
|---|
| 147 |   this->playableUniqueId = uniqueId; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** | 
|---|
| 151 |  * get playable associated to player | 
|---|
| 152 |  * @return playable associated to player | 
|---|
| 153 |  */ | 
|---|
| 154 | Playable * PlayerStats::getPlayable() | 
|---|
| 155 | { | 
|---|
| 156 |   if ( playable ) | 
|---|
| 157 |     return playable; | 
|---|
| 158 |    | 
|---|
| 159 |   assert( playableUniqueId > 0 ); | 
|---|
| 160 |    | 
|---|
| 161 |   setPlayableUniqueId( playableUniqueId ); | 
|---|
| 162 |    | 
|---|
| 163 |   assert( playable ); | 
|---|
| 164 |    | 
|---|
| 165 |   return playable; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|