[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" |
---|
[8228] | 23 | #include "shared_network_data.h" |
---|
[7974] | 24 | |
---|
[9406] | 25 | #include "converter.h" |
---|
| 26 | |
---|
[9235] | 27 | #include "preferences.h" |
---|
| 28 | |
---|
[8362] | 29 | #include "debug.h" |
---|
[8623] | 30 | #include "shell_command.h" |
---|
[7984] | 31 | |
---|
[8362] | 32 | |
---|
[7974] | 33 | CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS); |
---|
| 34 | |
---|
[8623] | 35 | |
---|
[7974] | 36 | /** |
---|
| 37 | * constructor |
---|
| 38 | */ |
---|
| 39 | PlayerStats::PlayerStats( int userId ) |
---|
| 40 | { |
---|
| 41 | init(); |
---|
[9406] | 42 | |
---|
[7974] | 43 | this->userId = userId; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * constructor |
---|
| 48 | */ |
---|
| 49 | PlayerStats::PlayerStats( const TiXmlElement* root ) |
---|
| 50 | { |
---|
| 51 | init(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void PlayerStats::init( ) |
---|
| 55 | { |
---|
| 56 | this->setClassID( CL_PLAYER_STATS, "PlayerStats" ); |
---|
| 57 | |
---|
| 58 | this->userId = 0; |
---|
[8067] | 59 | this->teamId = TEAM_NOTEAM; |
---|
| 60 | this->preferedTeamId = TEAM_NOTEAM; |
---|
[7974] | 61 | this->score = 0; |
---|
| 62 | this->playableClassId = 0; |
---|
| 63 | this->modelFileName = ""; |
---|
[8623] | 64 | this->nickName = "Player"; |
---|
| 65 | this->oldNickName = "Player"; |
---|
[9406] | 66 | |
---|
[7974] | 67 | userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) ); |
---|
[8067] | 68 | teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) ); |
---|
| 69 | preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) ); |
---|
[7974] | 70 | score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) ); |
---|
| 71 | playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") ); |
---|
| 72 | playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) ); |
---|
| 73 | modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) ); |
---|
[8623] | 74 | nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) ); |
---|
[9406] | 75 | |
---|
[8623] | 76 | MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL ); |
---|
[9406] | 77 | |
---|
[8014] | 78 | PRINTF(0)("PlayerStats created\n"); |
---|
[7974] | 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * standard deconstructor |
---|
| 84 | */ |
---|
| 85 | PlayerStats::~PlayerStats() |
---|
| 86 | { |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * override this function to be notified on change |
---|
| 92 | * of your registred variables. |
---|
| 93 | * @param id id's which have changed |
---|
| 94 | */ |
---|
| 95 | void PlayerStats::varChangeHandler( std::list< int > & id ) |
---|
| 96 | { |
---|
| 97 | if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() ) |
---|
| 98 | { |
---|
| 99 | this->setPlayableUniqueId( this->playableUniqueId ); |
---|
[9406] | 100 | |
---|
[8708] | 101 | PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID()); |
---|
[7974] | 102 | } |
---|
[9406] | 103 | |
---|
[8623] | 104 | if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() ) |
---|
| 105 | { |
---|
| 106 | PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str()); |
---|
[9504] | 107 | this->oldNickName = nickName; |
---|
[8623] | 108 | } |
---|
[9504] | 109 | |
---|
| 110 | if ( std::find( id.begin(), id.end(), teamId_handle ) != id.end() ) |
---|
| 111 | { |
---|
| 112 | PRINTF(0)("user %s joins team %i\n", this->nickName.c_str(), this->teamId); |
---|
| 113 | this->setPreferedTeamIdHandler( this->teamId); |
---|
| 114 | } |
---|
| 115 | |
---|
[7974] | 116 | } |
---|
| 117 | |
---|
[9504] | 118 | |
---|
[7974] | 119 | /** |
---|
[9504] | 120 | * handler for setting the prefered team id |
---|
| 121 | * @param newTeamId: the new team id |
---|
| 122 | */ |
---|
| 123 | void PlayerStats::setPreferedTeamIdHandler( int newTeamId) |
---|
| 124 | { |
---|
| 125 | |
---|
| 126 | if( this->playable == NULL) |
---|
| 127 | { |
---|
| 128 | PRINTF(0)("could not set prefered team, since the playable is not yet set\n"); |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | this->playable->setTeam(newTeamId); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | /** |
---|
[7974] | 138 | * get stats for user userId |
---|
| 139 | * @param userId user's id |
---|
| 140 | * @return stats assigned to user |
---|
| 141 | */ |
---|
| 142 | PlayerStats * PlayerStats::getStats( int userId ) |
---|
| 143 | { |
---|
| 144 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
[9406] | 145 | |
---|
[8147] | 146 | if ( !list ) |
---|
[8228] | 147 | { |
---|
[8147] | 148 | return NULL; |
---|
[8228] | 149 | } |
---|
[9406] | 150 | |
---|
[7974] | 151 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 152 | { |
---|
| 153 | if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId ) |
---|
| 154 | { |
---|
| 155 | return dynamic_cast<PlayerStats*>(*it); |
---|
| 156 | } |
---|
| 157 | } |
---|
[9406] | 158 | |
---|
[7974] | 159 | return NULL; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * set playable class id and set playable |
---|
| 164 | */ |
---|
| 165 | void PlayerStats::setPlayableUniqueId( int uniqueId ) |
---|
| 166 | { |
---|
| 167 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); |
---|
[9406] | 168 | |
---|
[8147] | 169 | if ( !list ) |
---|
| 170 | { |
---|
| 171 | this->playableUniqueId = uniqueId; |
---|
| 172 | return; |
---|
| 173 | } |
---|
[9406] | 174 | |
---|
[7974] | 175 | this->playable = NULL; |
---|
| 176 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 177 | { |
---|
| 178 | if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId ) |
---|
| 179 | { |
---|
| 180 | this->playable = dynamic_cast<Playable*>(*it); |
---|
[8228] | 181 | //TODO when OM_PLAYERS is ticked add line: |
---|
| 182 | //this->playable->toList( OM_PLAYERS ); |
---|
[7974] | 183 | break; |
---|
| 184 | } |
---|
| 185 | } |
---|
[9406] | 186 | |
---|
[8708] | 187 | if ( this->playable && userId == SharedNetworkData::getInstance()->getHostID() ) |
---|
[8228] | 188 | { |
---|
[8147] | 189 | State::getPlayer()->setPlayable( this->playable ); |
---|
[9505] | 190 | // also set the team id |
---|
| 191 | this->playable->setTeam(this->getTeamId()); |
---|
[8228] | 192 | } |
---|
[9406] | 193 | |
---|
[7974] | 194 | this->playableUniqueId = uniqueId; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * get playable associated to player |
---|
| 199 | * @return playable associated to player |
---|
| 200 | */ |
---|
| 201 | Playable * PlayerStats::getPlayable() |
---|
| 202 | { |
---|
| 203 | if ( playable ) |
---|
| 204 | return playable; |
---|
[9406] | 205 | |
---|
[7974] | 206 | assert( playableUniqueId > 0 ); |
---|
[9406] | 207 | |
---|
[7974] | 208 | setPlayableUniqueId( playableUniqueId ); |
---|
[9406] | 209 | |
---|
[7974] | 210 | assert( playable ); |
---|
[9406] | 211 | |
---|
[7974] | 212 | return playable; |
---|
| 213 | } |
---|
| 214 | |
---|
[8623] | 215 | /** |
---|
| 216 | * client sends server a message to change nick and server changes nick directly |
---|
| 217 | * @param nick new nickname |
---|
| 218 | */ |
---|
| 219 | void PlayerStats::setNickName( std::string nick ) |
---|
| 220 | { |
---|
[9494] | 221 | if ( SharedNetworkData::getInstance()->isMasterServer()) |
---|
[8623] | 222 | { |
---|
| 223 | this->nickName = nick; |
---|
| 224 | PRINTF(0)("user %s is now known as %s\n", oldNickName.c_str(), nickName.c_str()); |
---|
| 225 | oldNickName = nickName; |
---|
| 226 | return; |
---|
| 227 | } |
---|
| 228 | else |
---|
| 229 | { |
---|
| 230 | byte * data = new byte[nick.length()+INTSIZE]; |
---|
[9406] | 231 | |
---|
[8623] | 232 | assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE ); |
---|
[9406] | 233 | |
---|
[8623] | 234 | MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH ); |
---|
| 235 | return; |
---|
| 236 | } |
---|
| 237 | } |
---|
[7974] | 238 | |
---|
[8623] | 239 | bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
| 240 | { |
---|
| 241 | std::string newNick; |
---|
| 242 | int res = Converter::byteArrayToString( data, newNick, dataLength ); |
---|
[9406] | 243 | |
---|
[8623] | 244 | if ( res != dataLength ) |
---|
| 245 | { |
---|
| 246 | PRINTF(2)("invalid message size from user %d\n", userId); |
---|
| 247 | newNick = "invalid"; |
---|
| 248 | } |
---|
[9406] | 249 | |
---|
[8623] | 250 | if ( PlayerStats::getStats( userId ) ) |
---|
| 251 | PlayerStats::getStats( userId )->setNickName( newNick ); |
---|
[9406] | 252 | |
---|
[8623] | 253 | return true; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | void PlayerStats::shellNick( const std::string& newNick ) |
---|
| 257 | { |
---|
| 258 | if ( getStats( SharedNetworkData::getInstance()->getHostID() ) ) |
---|
| 259 | getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick ); |
---|
[9406] | 260 | |
---|
[9235] | 261 | Preferences::getInstance()->setString( "multiplayer", "nickname", newNick ); |
---|
[8623] | 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | void PlayerStats::deleteAllPlayerStats( ) |
---|
| 267 | { |
---|
| 268 | const std::list<BaseObject*> * list; |
---|
[9406] | 269 | |
---|
[8623] | 270 | while ( (list = ClassList::getList( CL_PLAYER_STATS )) != NULL && list->begin() != list->end() ) |
---|
| 271 | delete *list->begin(); |
---|
| 272 | } |
---|
| 273 | |
---|
[9110] | 274 | |
---|
| 275 | |
---|
| 276 | ScoreList PlayerStats::getScoreList( ) |
---|
| 277 | { |
---|
| 278 | ScoreList result; |
---|
[9406] | 279 | |
---|
[9110] | 280 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
[9406] | 281 | |
---|
[9110] | 282 | if ( !list ) |
---|
| 283 | { |
---|
| 284 | return result; |
---|
| 285 | } |
---|
[9406] | 286 | |
---|
[9110] | 287 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 288 | { |
---|
| 289 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
[9406] | 290 | |
---|
[9110] | 291 | TeamScoreList::iterator it = result[stats.getTeamId()].begin(); |
---|
[9406] | 292 | |
---|
[9110] | 293 | while ( it != result[stats.getTeamId()].end() && stats.score > it->score ) |
---|
| 294 | { |
---|
| 295 | it++; |
---|
| 296 | } |
---|
[9406] | 297 | |
---|
[9110] | 298 | PlayerScore score; |
---|
| 299 | score.name = stats.getNickName(); |
---|
| 300 | score.score = stats.getScore(); |
---|
[9406] | 301 | |
---|
[9110] | 302 | result[stats.getTeamId()].insert(it, score); |
---|
| 303 | } |
---|
[9406] | 304 | |
---|
[9110] | 305 | return result; |
---|
| 306 | } |
---|