[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 | |
---|
[8236] | 25 | #include "shell_command.h" |
---|
[7984] | 26 | |
---|
[8236] | 27 | |
---|
[7974] | 28 | CREATE_FACTORY(PlayerStats, CL_PLAYER_STATS); |
---|
[8236] | 29 | SHELL_COMMAND(nick, PlayerStats, shellNick); |
---|
[7974] | 30 | |
---|
| 31 | /** |
---|
| 32 | * constructor |
---|
| 33 | */ |
---|
| 34 | PlayerStats::PlayerStats( int userId ) |
---|
| 35 | { |
---|
| 36 | init(); |
---|
| 37 | |
---|
| 38 | this->userId = userId; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * constructor |
---|
| 43 | */ |
---|
| 44 | PlayerStats::PlayerStats( const TiXmlElement* root ) |
---|
| 45 | { |
---|
| 46 | init(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void PlayerStats::init( ) |
---|
| 50 | { |
---|
| 51 | this->setClassID( CL_PLAYER_STATS, "PlayerStats" ); |
---|
| 52 | |
---|
| 53 | this->userId = 0; |
---|
[8067] | 54 | this->teamId = TEAM_NOTEAM; |
---|
| 55 | this->preferedTeamId = TEAM_NOTEAM; |
---|
[7974] | 56 | this->score = 0; |
---|
| 57 | this->playableClassId = 0; |
---|
| 58 | this->modelFileName = ""; |
---|
[8237] | 59 | this->nickName = "Player"; |
---|
[7974] | 60 | |
---|
| 61 | userId_handle = registerVarId( new SynchronizeableInt( &userId, &userId, "userId" ) ); |
---|
[8067] | 62 | teamId_handle = registerVarId( new SynchronizeableInt( &teamId, &teamId, "teamId" ) ); |
---|
| 63 | preferedTeamId_handle = registerVarId( new SynchronizeableInt( &preferedTeamId, &preferedTeamId, "preferedUserId" ) ); |
---|
[7974] | 64 | score_handle = registerVarId( new SynchronizeableInt( &score, &score, "score" ) ); |
---|
| 65 | playableClassId_handle = registerVarId( new SynchronizeableInt( &playableClassId, &playableClassId, "playableClassId") ); |
---|
| 66 | playableUniqueId_handle = registerVarId( new SynchronizeableInt( &playableUniqueId, &playableUniqueId, "playableUniqueId" ) ); |
---|
| 67 | modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) ); |
---|
[8236] | 68 | nickName_handler = registerVarId( new SynchronizeableString( &nickName, &nickName, "nickName" ) ); |
---|
[8014] | 69 | |
---|
[8236] | 70 | MessageManager::getInstance()->registerMessageHandler( MSGID_CHANGENICKNAME, changeNickHandler, NULL ); |
---|
| 71 | |
---|
[8014] | 72 | PRINTF(0)("PlayerStats created\n"); |
---|
[7974] | 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * standard deconstructor |
---|
| 78 | */ |
---|
| 79 | PlayerStats::~PlayerStats() |
---|
| 80 | { |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * override this function to be notified on change |
---|
| 86 | * of your registred variables. |
---|
| 87 | * @param id id's which have changed |
---|
| 88 | */ |
---|
| 89 | void PlayerStats::varChangeHandler( std::list< int > & id ) |
---|
| 90 | { |
---|
| 91 | if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() ) |
---|
| 92 | { |
---|
| 93 | this->setPlayableUniqueId( this->playableUniqueId ); |
---|
[7984] | 94 | |
---|
[8228] | 95 | PRINTF(0)("uniqueID changed %d %d %d\n", userId, getHostID(), getUniqueID()); |
---|
[7974] | 96 | } |
---|
[8236] | 97 | |
---|
| 98 | if ( std::find( id.begin(), id.end(), nickName_handler ) != id.end() ) |
---|
| 99 | { |
---|
[8237] | 100 | PRINTF(0)("user %d is now known as %s\n", userId, nickName.c_str()); |
---|
[8236] | 101 | } |
---|
[7974] | 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * get stats for user userId |
---|
| 106 | * @param userId user's id |
---|
| 107 | * @return stats assigned to user |
---|
| 108 | */ |
---|
| 109 | PlayerStats * PlayerStats::getStats( int userId ) |
---|
| 110 | { |
---|
| 111 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
| 112 | |
---|
[8147] | 113 | if ( !list ) |
---|
[8228] | 114 | { |
---|
[8147] | 115 | return NULL; |
---|
[8228] | 116 | } |
---|
[8147] | 117 | |
---|
[7974] | 118 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 119 | { |
---|
| 120 | if ( dynamic_cast<PlayerStats*>(*it)->getUserId() == userId ) |
---|
| 121 | { |
---|
| 122 | return dynamic_cast<PlayerStats*>(*it); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return NULL; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * set playable class id and set playable |
---|
| 131 | */ |
---|
| 132 | void PlayerStats::setPlayableUniqueId( int uniqueId ) |
---|
| 133 | { |
---|
| 134 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE ); |
---|
| 135 | |
---|
[8147] | 136 | if ( !list ) |
---|
| 137 | { |
---|
| 138 | this->playableUniqueId = uniqueId; |
---|
| 139 | return; |
---|
| 140 | } |
---|
| 141 | |
---|
[7974] | 142 | this->playable = NULL; |
---|
| 143 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 144 | { |
---|
| 145 | if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId ) |
---|
| 146 | { |
---|
| 147 | this->playable = dynamic_cast<Playable*>(*it); |
---|
[8228] | 148 | //TODO when OM_PLAYERS is ticked add line: |
---|
| 149 | //this->playable->toList( OM_PLAYERS ); |
---|
[7974] | 150 | break; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
[8147] | 154 | if ( this->playable && userId == getHostID() ) |
---|
[8228] | 155 | { |
---|
[8147] | 156 | State::getPlayer()->setPlayable( this->playable ); |
---|
[8228] | 157 | } |
---|
[8147] | 158 | |
---|
[7974] | 159 | this->playableUniqueId = uniqueId; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * get playable associated to player |
---|
| 164 | * @return playable associated to player |
---|
| 165 | */ |
---|
| 166 | Playable * PlayerStats::getPlayable() |
---|
| 167 | { |
---|
| 168 | if ( playable ) |
---|
| 169 | return playable; |
---|
| 170 | |
---|
| 171 | assert( playableUniqueId > 0 ); |
---|
| 172 | |
---|
| 173 | setPlayableUniqueId( playableUniqueId ); |
---|
| 174 | |
---|
| 175 | assert( playable ); |
---|
| 176 | |
---|
| 177 | return playable; |
---|
| 178 | } |
---|
| 179 | |
---|
[8236] | 180 | /** |
---|
| 181 | * client sends server a message to change nick and server changes nick directly |
---|
| 182 | * @param nick new nickname |
---|
| 183 | */ |
---|
| 184 | void PlayerStats::setNickName( std::string nick ) |
---|
| 185 | { |
---|
| 186 | if ( isServer() ) |
---|
| 187 | { |
---|
| 188 | this->nickName = nick; |
---|
| 189 | return; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | byte * data = new byte[nick.length()+INTSIZE]; |
---|
| 194 | |
---|
| 195 | assert( Converter::stringToByteArray( nick, data, nick.length()+INTSIZE) == nick.length()+INTSIZE ); |
---|
| 196 | |
---|
| 197 | MessageManager::getInstance()->sendMessage( MSGID_CHANGENICKNAME, data, nick.length()+INTSIZE, RT_SERVER, 0, MP_HIGHBANDWIDTH ); |
---|
| 198 | return; |
---|
| 199 | } |
---|
| 200 | } |
---|
[7974] | 201 | |
---|
[8236] | 202 | bool PlayerStats::changeNickHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) |
---|
| 203 | { |
---|
| 204 | std::string newNick; |
---|
| 205 | int res = Converter::byteArrayToString( data, newNick, dataLength ); |
---|
| 206 | |
---|
| 207 | if ( res != dataLength ) |
---|
| 208 | { |
---|
| 209 | PRINTF(2)("invalid message size from user %d\n", userId); |
---|
| 210 | newNick = "invalid"; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if ( PlayerStats::getStats( userId ) ) |
---|
| 214 | PlayerStats::getStats( userId )->setNickName( newNick ); |
---|
[8238] | 215 | |
---|
| 216 | return true; |
---|
[8236] | 217 | } |
---|
| 218 | |
---|
| 219 | void PlayerStats::shellNick( const std::string& newNick ) |
---|
| 220 | { |
---|
| 221 | //if ( getStats( SharedNetworkData::getInstance()->getHostID() ) ) |
---|
| 222 | // getStats( SharedNetworkData::getInstance()->getHostID() )->setNickName( newNick ); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | |
---|