Shadowrun: Awakened 29 September 2011 - Build 871
ClientRegister.cpp
Go to the documentation of this file.
00001 #include "ClientRegister.h"
00002 namespace SraNetwork {
00003 
00004     ClientRegister* SraNetwork::ClientRegister::instance = new ClientRegister();
00005 
00006 #pragma region Constructor/Destructor
00007     ClientRegister::ClientRegister(void)
00008     {
00009         //Initalize db connection
00010         // factory("localhost:3306", "root", "SRA_Dev", "sra_dev");
00011         //static SraData::DbConnectionFactory factory("localhost", "root", "SRA_Dev", "sra_dev");
00012         SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
00013         sql::Connection* connection = SraData::DbConnectionFactory::GetSingleton().getConnection();
00014         nextID = 42;
00015     }
00016 
00017     ClientRegister::~ClientRegister(void)
00018     {
00019     }
00020 #pragma endregion
00021 
00022 #pragma region Singleton Instance Getter
00023     ClientRegister* ClientRegister::getInstance()
00024     {
00025         return instance;
00026     }
00027 #pragma endregion
00028 
00029 #pragma region Add/Remove Client
00030     void ClientRegister::addClient( SystemAddress address, SraClientData client) 
00031     {
00032         SystemAddress s = address;
00033         tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::accessor acc;
00034         clientAddressRegister.insert(acc,s);
00035         acc->second = client;
00036     }
00037 
00038     void  ClientRegister::removeClient(const SystemAddress address)
00039     {
00040         tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::accessor acc;
00041         if (clientAddressRegister.find(acc, address) ) {
00042             clientAddressRegister.erase(acc);
00043         }
00044     }
00045 
00046     void  ClientRegister::removeClient( const SraClientData* data)
00047     {
00048         const SystemAddress* addr = getAddressFromClient(data);
00049         if (addr != NULL) 
00050         {
00051             removeClient(*addr);
00052         }
00053     }
00054 #pragma endregion
00055 
00056 #pragma region Client<->Address 
00057     const SystemAddress* ClientRegister::getAddressFromClientID( const int clientID)
00058     {
00059         tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_iterator it;
00060         for (  it = clientAddressRegister.begin(); it != clientAddressRegister.end(); ++it)
00061         {
00062             if ( it->second.clientID == clientID ) 
00063             {
00064                 return &it->first;
00065             }
00066         }
00067         return NULL;
00068     }
00069     const SraClientData* ClientRegister::getClientFromAddress( SystemAddress address)
00070     {
00071         tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_accessor acc;
00072         clientAddressRegister.find(acc,address);
00073         return acc.empty() ? NULL : &(acc->second);
00074     }
00075 
00076     const SystemAddress*  ClientRegister::getAddressFromClient( const SraClientData* client)
00077     {
00078         tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_iterator it;
00079         for (  it = clientAddressRegister.begin(); it != clientAddressRegister.end(); ++it)
00080         {
00081             if ( it->second == *client ) 
00082             {
00083                 return &it->first;
00084             }
00085         }
00086         return NULL;
00087     }
00088 #pragma endregion
00089 
00090     
00091     int ClientRegister::loginClient( const string& username, const string& password, SystemAddress address)
00092     {
00093         // Create db factory ... until its fixed
00094         SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
00095 
00096         int pID = -1;
00097 
00098         // Call database
00099         SraData::DbProcedures::createPlayer(username, password, &pID);
00100         
00101         // If we have found a player, process it and return the player ID
00102         if ( pID != -1 ) {
00103             //Create the struct
00104             SraClientData client = {0};
00105             client.clientID = pID;
00106             //client.lastKeepAlive = keepAlive.sec;
00107             //Read the current zone, we can only have one zone, so ignore the query id.
00108             int queryID;
00109             SraData::DbProcedures::getPcZone( pID, &client.zoneID, &queryID);
00110 
00111             addClient( address, client);
00112         }
00113         return pID;
00114     }
00115 
00116     int ClientRegister::createCharacterForPlayer(int raceId, SystemAddress address, int clientID )
00117     {
00118         if ( clientID == -1 )
00119         {
00120             return -1;
00121         }
00122 
00123         //int raceID = _wtoi(data);
00124         //if ( raceID == 0 )
00125         //{
00126         //  return;
00127         //}
00128         // Call database
00129         int charID, playID;
00130         
00131         // Create db factory ... until its fixed
00132         SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
00133         SraData::DbProcedures::createPlayerCharacter( raceId, clientID, &playID, &charID);
00134         return charID;
00135     }
00136 
00137     void ClientRegister::getPlayersForClient( const int clientID, const int* outPlayerIDs)
00138     {
00139         //TODO: query DB for players
00140         //int playerIndex = 0;
00141 
00142     }
00143 
00144     
00145     bool ClientRegister::checkForValidCharacterInfo( const BasicCharacterInfo* info, RakString* resultMessage)
00146     {
00147         // Create db factory ... until its fixed
00148         //SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
00149         //TODO: Add some checks to the DB 
00150         if (info->charName.IsEmpty())
00151         {
00152             *resultMessage = "Can not create a character without a name!";
00153             return false;
00154         } else if (info->raceId < 0)
00155         {
00156             *resultMessage = "Invalid race, please try again";
00157             return false;
00158         }
00159         return true;
00160     }
00161 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net