Wt examples 3.1.10
/build/buildd/witty-3.1.10/examples/hangman/HangmanDb.C
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 #include <string>
00004 #include <fstream>
00005 
00006 #include <mysql++/mysql++.h>
00007 #include <Wt/WStringUtil>
00008 
00009 #include <Wt/WApplication>
00010 #include "HangmanDb.h"
00011 
00012 using namespace mysqlpp;
00013 
00014 std::string HangmanDb::DbUser()
00015 {
00016         std::string retval;
00017         std::ifstream dbconf((Wt::WApplication::appRoot()
00018                               + "HangmanDb.info").c_str());
00019         dbconf >> retval;
00020         return retval;
00021 }
00022 
00023 std::string HangmanDb::DbPass()
00024 {
00025         std::string retval;
00026         std::ifstream dbconf((Wt::WApplication::appRoot()
00027                               + "HangmanDb.info").c_str());
00028         dbconf >> retval; // username
00029         dbconf >> retval; // password
00030         return retval;
00031 }
00032 
00033 // this function returns false if user existed, true if user inserted
00034 // It guarantees atomic userExists() checking and adding it if the user
00035 // did not yet exits.
00036 bool HangmanDb::addUser(const std::wstring &user, const std::wstring &password)
00037 {
00038    try {
00039       Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
00040       Query q = con.query();
00041       q << "insert into users "
00042         << "set user='" << Wt::toUTF8(user) << "', pass=MD5('"
00043         << Wt::toUTF8(password) << "'), numgames=0, score=0, lastseen=now()";
00044       q.store();
00045       return true;
00046    } catch(Exception &e) {
00047       std::cerr << "Database exception!\n";
00048       std::cerr << e.what() << std::endl;
00049       return false;
00050    }
00051 }
00052 
00053 bool HangmanDb::validLogin(const std::wstring &user, const std::wstring &pass)
00054 {
00055    try {
00056       Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
00057       Query q = con.query();
00058       q << "select user,pass from users where "
00059         << "user='" << Wt::toUTF8(user)
00060         << "' and pass=MD5('" << Wt::toUTF8(pass) << "')";
00061       StoreQueryResult res = q.store();
00062       return res.size() > 0;
00063    } catch(Exception &e) {
00064       std::cerr << "Database exception!\n";
00065       std::cerr << e.what() << std::endl;
00066       return false;
00067    }
00068 }
00069 
00070 void HangmanDb::addToScore(const std::wstring &user, int delta)
00071 {
00072    try {
00073       Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
00074       Query q = con.query();
00075       q << "update users set score=(score+" << delta << "), "
00076         << "numgames=(numgames+1), lastseen=now() "
00077         << "where user='" << Wt::toUTF8(user) << "'";
00078       StoreQueryResult res = q.store();
00079    } catch(Exception &e) {
00080       std::cerr << "Database exception!\n";
00081       std::cerr << e.what() << std::endl;
00082    }
00083 }
00084 
00085 std::vector<HangmanDb::Score> HangmanDb::getHighScores(int top)
00086 {
00087    std::vector<HangmanDb::Score> retval;
00088    try {
00089       Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
00090       Query q = con.query();
00091       q << "select user, numgames, score, lastseen from users "
00092         << "order by score desc "
00093         << "limit " << top;
00094       StoreQueryResult res = q.store();
00095 
00096       for(unsigned int i = 0; i < res.size(); ++i) {
00097          struct Score s;
00098          s.number = i + 1;
00099          s.user = Wt::fromUTF8((std::string)res.at(i)["user"]);
00100          s.numgames = res.at(i)["numgames"];
00101          s.score = res.at(i)["score"];
00102          s.lastseen = Wt::fromUTF8((std::string)res.at(i)["lastseen"]);
00103          retval.push_back(s);
00104       }
00105    } catch(Exception &e) {
00106       std::cerr << "Database exception!\n";
00107       std::cerr << e.what() << std::endl;
00108    }
00109    return retval;
00110 }
00111 
00112 HangmanDb::Score HangmanDb::getUserPosition(const std::wstring &user)
00113 {
00114    try {
00115       Connection con("hangman", "localhost", DbUser().c_str(), DbPass().c_str());
00116       Query q = con.query();
00117       q << "select user, numgames, score, lastseen from users "
00118         << "order by score desc";
00119       StoreQueryResult res = q.store();
00120 
00121       // There MUST be a better way to do this...
00122       for(unsigned int i = 0; i < res.size(); ++i) {
00123          if(Wt::fromUTF8((std::string)res.at(i)["user"]) == user) {
00124             struct Score s;
00125             s.number = i + 1;
00126             s.user = Wt::fromUTF8((std::string)res.at(i)["user"]);
00127             s.numgames = res.at(i)["numgames"];
00128             s.score = res.at(i)["score"];
00129             s.lastseen = Wt::fromUTF8((std::string)res.at(i)["lastseen"]);
00130             return s;
00131          }
00132       }
00133    } catch(Exception &e) {
00134       std::cerr << "Database exception!\n";
00135       std::cerr << e.what() << std::endl;
00136    }
00137    Score s;
00138    s.number=0;
00139    s.user=L"DBase error";
00140    s.numgames = s.score = 0;
00141    return s;
00142 }

Generated on Mon Nov 14 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.4