#include <HangmanDb.h>
Classes | |
| struct | Score |
Static Public Member Functions | |
| static bool | addUser (const std::wstring &user, const std::wstring &password) |
| static bool | validLogin (const std::wstring &user, const std::wstring &pass) |
| static void | addToScore (const std::wstring &user, int delta) |
| static std::vector< Score > | getHighScores (int top) |
| static Score | getUserPosition (const std::wstring &user) |
Static Private Member Functions | |
| static std::string | DbUser () |
| static std::string | DbPass () |
Definition at line 7 of file HangmanDb.h.
| void HangmanDb::addToScore | ( | const std::wstring & | user, |
| int | delta | ||
| ) | [static] |
Definition at line 70 of file HangmanDb.C.
{
try {
Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
Query q = con.query();
q << "update users set score=(score+" << delta << "), "
<< "numgames=(numgames+1), lastseen=now() "
<< "where user='" << Wt::toUTF8(user) << "'";
StoreQueryResult res = q.store();
} catch(Exception &e) {
std::cerr << "Database exception!\n";
std::cerr << e.what() << std::endl;
}
}
| bool HangmanDb::addUser | ( | const std::wstring & | user, |
| const std::wstring & | password | ||
| ) | [static] |
Definition at line 36 of file HangmanDb.C.
{
try {
Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
Query q = con.query();
q << "insert into users "
<< "set user='" << Wt::toUTF8(user) << "', pass=MD5('"
<< Wt::toUTF8(password) << "'), numgames=0, score=0, lastseen=now()";
q.store();
return true;
} catch(Exception &e) {
std::cerr << "Database exception!\n";
std::cerr << e.what() << std::endl;
return false;
}
}
| std::string HangmanDb::DbPass | ( | ) | [static, private] |
Definition at line 23 of file HangmanDb.C.
{
std::string retval;
std::ifstream dbconf((Wt::WApplication::appRoot()
+ "HangmanDb.info").c_str());
dbconf >> retval; // username
dbconf >> retval; // password
return retval;
}
| std::string HangmanDb::DbUser | ( | ) | [static, private] |
Definition at line 14 of file HangmanDb.C.
{
std::string retval;
std::ifstream dbconf((Wt::WApplication::appRoot()
+ "HangmanDb.info").c_str());
dbconf >> retval;
return retval;
}
| std::vector< HangmanDb::Score > HangmanDb::getHighScores | ( | int | top ) | [static] |
Definition at line 85 of file HangmanDb.C.
{
std::vector<HangmanDb::Score> retval;
try {
Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
Query q = con.query();
q << "select user, numgames, score, lastseen from users "
<< "order by score desc "
<< "limit " << top;
StoreQueryResult res = q.store();
for(unsigned int i = 0; i < res.size(); ++i) {
struct Score s;
s.number = i + 1;
s.user = Wt::fromUTF8((std::string)res.at(i)["user"]);
s.numgames = res.at(i)["numgames"];
s.score = res.at(i)["score"];
s.lastseen = Wt::fromUTF8((std::string)res.at(i)["lastseen"]);
retval.push_back(s);
}
} catch(Exception &e) {
std::cerr << "Database exception!\n";
std::cerr << e.what() << std::endl;
}
return retval;
}
| HangmanDb::Score HangmanDb::getUserPosition | ( | const std::wstring & | user ) | [static] |
Definition at line 112 of file HangmanDb.C.
{
try {
Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
Query q = con.query();
q << "select user, numgames, score, lastseen from users "
<< "order by score desc";
StoreQueryResult res = q.store();
// There MUST be a better way to do this...
for(unsigned int i = 0; i < res.size(); ++i) {
if(Wt::fromUTF8((std::string)res.at(i)["user"]) == user) {
struct Score s;
s.number = i + 1;
s.user = Wt::fromUTF8((std::string)res.at(i)["user"]);
s.numgames = res.at(i)["numgames"];
s.score = res.at(i)["score"];
s.lastseen = Wt::fromUTF8((std::string)res.at(i)["lastseen"]);
return s;
}
}
} catch(Exception &e) {
std::cerr << "Database exception!\n";
std::cerr << e.what() << std::endl;
}
Score s;
s.number=0;
s.user=L"DBase error";
s.numgames = s.score = 0;
return s;
}
| bool HangmanDb::validLogin | ( | const std::wstring & | user, |
| const std::wstring & | pass | ||
| ) | [static] |
Definition at line 53 of file HangmanDb.C.
{
try {
Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
Query q = con.query();
q << "select user,pass from users where "
<< "user='" << Wt::toUTF8(user)
<< "' and pass=MD5('" << Wt::toUTF8(pass) << "')";
StoreQueryResult res = q.store();
return res.size() > 0;
} catch(Exception &e) {
std::cerr << "Database exception!\n";
std::cerr << e.what() << std::endl;
return false;
}
}
1.7.2