/usr/lib/Wt/examples/hangman/HangmanDb.C is in witty-examples 3.1.10-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <mysql++/mysql++.h>
#include <Wt/WStringUtil>
#include <Wt/WApplication>
#include "HangmanDb.h"
using namespace mysqlpp;
std::string HangmanDb::DbUser()
{
std::string retval;
std::ifstream dbconf((Wt::WApplication::appRoot()
+ "HangmanDb.info").c_str());
dbconf >> retval;
return retval;
}
std::string HangmanDb::DbPass()
{
std::string retval;
std::ifstream dbconf((Wt::WApplication::appRoot()
+ "HangmanDb.info").c_str());
dbconf >> retval; // username
dbconf >> retval; // password
return retval;
}
// this function returns false if user existed, true if user inserted
// It guarantees atomic userExists() checking and adding it if the user
// did not yet exits.
bool HangmanDb::addUser(const std::wstring &user, const std::wstring &password)
{
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;
}
}
bool HangmanDb::validLogin(const std::wstring &user, const std::wstring &pass)
{
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;
}
}
void HangmanDb::addToScore(const std::wstring &user, int delta)
{
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;
}
}
std::vector<HangmanDb::Score> HangmanDb::getHighScores(int top)
{
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)
{
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;
}
|