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