Public Member Functions | Private Attributes

Session Class Reference

#include <Session.h>

List of all members.

Public Member Functions

 Session ()
const Useruser ()
Dictionarydictionary ()
void setDictionary (const Dictionary &d)
bool login (std::string name, std::string password)
std::vector< UsertopUsers (int limit)
int findRanking (const User *user)
void addToScore (int s)

Private Attributes

Wt::Dbo::backend::Sqlite3 sqlite3_
Wt::Dbo::Session session_
Wt::Dbo::ptr< Useruser_
Dictionary dictionary_

Detailed Description

Definition at line 19 of file Session.h.


Constructor & Destructor Documentation

Session::Session (  )

Definition at line 14 of file Session.C.

  : sqlite3_(WApplication::instance()->appRoot() + "hangman.db")
{
  session_.setConnection(sqlite3_);
  sqlite3_.setProperty("show-queries", "true");

  session_.mapClass<User>("user");

  Dbo::Transaction transaction(session_);
  try {
    session_.createTables();
    session_.add(new User("guest", "guest"));    
    WApplication::instance()->log("info") << "Database created";
  } catch (...) {
    WApplication::instance()->log("info") << "Using existing database";
  }

  transaction.commit();
}

Member Function Documentation

void Session::addToScore ( int  s )

Definition at line 48 of file Session.C.

{
  user_.modify()->score += s;
  ++user_.modify()->gamesPlayed;
  user_.modify()->lastLogin = WDateTime::currentDateTime();
}
Dictionary& Session::dictionary (  ) [inline]

Definition at line 31 of file Session.h.

{ return dictionary_; } 
int Session::findRanking ( const User user )

Definition at line 69 of file Session.C.

{
  Dbo::Transaction transaction(session_);
  
  int ranking  = 
    session_.query<int>("select distinct count(score) from user")
    .where("score > ?")
    .bind(user->score);

  transaction.commit(); 
  
  return ranking + 1;
}
bool Session::login ( std::string  name,
std::string  password 
)

Definition at line 34 of file Session.C.

{
  Dbo::Transaction transaction(session_);
  user_ = session_.find<User>().where("name = ?").bind(name);
  if (!user_)
    user_ = session_.add(new User(name, password));

  bool ok = user_->authenticate(password);
  
  transaction.commit();
  
  return ok;
}
void Session::setDictionary ( const Dictionary d ) [inline]

Definition at line 32 of file Session.h.

{ dictionary_ = d;} 
std::vector< User > Session::topUsers ( int  limit )

Definition at line 55 of file Session.C.

{
  Dbo::Transaction transaction(session_);
  Users top = session_.find<User>().orderBy("score desc").limit(20);

  std::vector<User> result;
  for (Users::const_iterator i = top.begin(); i != top.end(); ++i)
    result.push_back(**i);

  transaction.commit();

  return result;
}
const User* Session::user (  ) [inline]

Definition at line 24 of file Session.h.

                     { 
    if (user_)
      return &(*user_);
    else 
      return 0;
  } 

Member Data Documentation

Definition at line 44 of file Session.h.

Definition at line 42 of file Session.h.

Wt::Dbo::backend::Sqlite3 Session::sqlite3_ [private]

Definition at line 40 of file Session.h.

Definition at line 43 of file Session.h.


The documentation for this class was generated from the following files:

Generated on Fri Sep 23 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.2