Cmd_accounts Class Reference

#include <Cmd_accounts.h>

Inheritance diagram for Cmd_accounts:

ICommandServer List of all members.

Public Member Functions

 Cmd_accounts (std::string &params, Server *server)
void help ()
void execute ()

Private Member Functions

void showAccounts (Uint32 recordsPerPage=0)
void addAccount (const std::string &username="", const std::string &password="", const std::string &name="", const std::string &surname="", const std::string &email="")
void attachChar ()

Private Attributes

CppSQLite3DB myAccDb

Detailed Description

The account manager. For more help please take a look at developers documentation.

Definition at line 10 of file Cmd_accounts.h.


Member Function Documentation

void Cmd_accounts::addAccount const std::string &  username = "",
const std::string &  password = "",
const std::string &  name = "",
const std::string &  surname = "",
const std::string &  email = ""
[private]
 

Adds a new account to the database. If one or more of the parameters are omitted you will be asked to filla form with the informations needed.

Code based on the account-creator.

Author:
stonedz
Since:
pre-alpha
Parameters:
username Account's username.
password Account's password.
name Real name.
surname Real surname.
email Valid email address.
Todo:
Find a way to separate SQL code form C++.

Definition at line 56 of file Cmd_accounts.cpp.

References Logger::getInstance(), Logger::log(), and myAccDb.

00061 {
00062     std::string username_, password_, name_, surname_, email_;
00063     SHA1 pwdSHA1;
00064         Uint32 shaData[5];
00065 
00066         Logger* logger = Logger::getInstance();
00067 
00068     if (username == "" || password  == "" || name == "" || surname == "" || email == ""){
00069         std::cout << "ACCOUNTS CREATOR\n";
00070         std::cout << "================\n";
00071         std::cout << "Use this program to create simple user data into sqlite3 db.\n";
00072         std::cout << "User name : ";
00073         std::getline(std::cin, username_);
00074         std::cout << "Password: ";
00075         std::getline(std::cin, password_);
00076         std::cout << "Real name: ";
00077         std::getline(std::cin, name_);
00078         std::cout << "Real surname: ";
00079         std::getline(std::cin, surname_);
00080         std::cout << "Email address: ";
00081         std::getline(std::cin, email_);
00082     }
00083     else{
00084         username_ = username;
00085         password_= password;
00086         name_ = name;
00087         surname_ = surname;
00088         email_ = email;
00089     }
00090 
00091     std::string temp = username_+password_;
00092         pwdSHA1.Reset();
00093         for (unsigned int i = 0; i < temp.size(); i++){
00094                 pwdSHA1.Input(temp[i]);
00095         }
00096 
00097         if (!pwdSHA1.Result(shaData)){
00098                 std::cerr<<"Sha could not compute message digest for login informations." << std::endl;
00099                 logger->log("Error during SHA1 computation in addAccount() method.", LOGMODE_ERROR);
00100         }
00101         else{ // We have a valid SHA1 hash.
00102         char hash[40];
00103         for (int i = 1; i < 6; i++){ // Transforms the hash in a readable format.
00104             sprintf(hash + (8*(i-1)), "%08x", shaData[i-1]);
00105         }
00106 
00107         Sint32 nRows;
00108         #ifdef TESTPHASE
00109         nRows = myAccDb.execDML("CREATE TABLE IF NOT EXISTS registered_users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, index_hash TEXT, surname TEXT, name TEXT, email TEXT, pc_id INTEGER DEFAULT 0);");
00110         nRows = myAccDb.execDML("CREATE UNIQUE INDEX IF NOT EXISTS sha1_index ON registered_users (index_hash);");
00111         #endif
00112         std::string query = "insert into registered_users(username, index_hash, surname, name, email) values ( \""+username_+"\", \""+hash+"\", \""+name_+"\", \""+surname_+"\", \""+email_+"\");";
00113         nRows = myAccDb.execDML(query.data());
00114 
00115         std::cout << "OK account for username \"" << username_ << "\" created." << std::endl;
00116         }
00117 
00118 }

void Cmd_accounts::attachChar  )  [private]
 

Attaches a new basic character to an existing account.

Author:
stonedz
Since:
pre-alpha
Todo:
No erro handling, this may cause serious damage to the database...

Definition at line 120 of file Cmd_accounts.cpp.

References Logger::getInstance(), Logger::log(), and myAccDb.

00120                              {
00121     std::string charname, xPos, yPos, accNumber;
00122 
00123     std::cout << "Account ID :";
00124     std::getline(std::cin, accNumber);
00125     std::cout << "Initial X position :";
00126     std::getline(std::cin, xPos);   // Not really good...
00127     std::cout << "Initial Y position :";
00128     std::getline(std::cin, yPos);              //This neither
00129     std::cout << "Character name :";
00130     std::getline(std::cin, charname);
00131 
00132     Sint32 nRows;
00133     #ifdef TESTPHASE
00134     nRows = myAccDb.execDML("CREATE TABLE IF NOT EXISTS characters (id INTEGER PRIMARY KEY AUTOINCREMENT, account_id INTEGER, name TEXT UNIQUE, x_position INTEGER, y_position INTEGER);");
00135     #endif
00136     std::string query = "insert into characters(account_id, name, x_position, y_position) VALUES ( \""+accNumber+"\", \""+charname+"\", \""+xPos+"\", \""+yPos+"\");";
00137     nRows = myAccDb.execDML(query.data());
00138 
00139     if (nRows){
00140         query = "select id from characters WHERE name = \""+charname+"\";";
00141         CppSQLite3Query q = myAccDb.execQuery(query.data());
00142         if(!q.eof()){
00143             nRows = 0;
00144             std::ostringstream oss;
00145             oss << q.fieldValue(0);
00146             query = "UPDATE registered_users SET pc_id = \""+oss.str()+"\" WHERE id = \""+accNumber+"\";";
00147             nRows = myAccDb.execDML(query.data());
00148             if (nRows == 0){ //no tables updated, something went wrong, delete the char and print a warning.
00149                 query = "DELETE FROM characters WHERE id = \""+oss.str()+"\";";
00150                 nRows = myAccDb.execDML(query.data());
00151                 Logger::getInstance()->log("Failed to insert a new character due to an invalid account id.", LOGMODE_DB);
00152             }
00153         }
00154 
00155     }
00156 
00157 
00158 }

void Cmd_accounts::showAccounts Uint32  recordsPerPage = 0  )  [private]
 

Shows the registered accounts, if recordsPerPage is specified it will divide the results in pages with N recrods in it to ease record lecture.

Author:
stonedz
Since:
pre-alpha
Parameters:
recordsPerPage How many records we want on a page?
Todo:
recordsPerPage mechanism to be implemented!

Definition at line 42 of file Cmd_accounts.cpp.

References myAccDb.

00042                                                     {
00043     CppSQLite3Query q = myAccDb.execQuery("select * from registered_users order by id;");
00044 
00045     while (!q.eof()){
00046         std::cout << std::setw(3) << q.fieldValue(0) << " | ";
00047         std::cout << std::setw(10) << q.fieldValue(1) << " | ";
00048         std::cout << std::setw(10) << q.fieldValue(3) << " | ";
00049         std::cout << std::setw(10) << q.fieldValue(4) << " | ";
00050         std::cout << std::setw(3) << q.fieldValue(6) << " | ";
00051         std::cout << std::setw(20) <<q.fieldValue(5) << " | " << std::endl;
00052         q.nextRow();
00053     }
00054 }


Member Data Documentation

CppSQLite3DB Cmd_accounts::myAccDb [private]
 

Local DB reference to avoid multiple db accesses.

Definition at line 64 of file Cmd_accounts.h.

Referenced by addAccount(), attachChar(), and showAccounts().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1