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

Generated on Mon Apr 14 15:15:04 2008 for Wt by doxygen 1.5.3