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