TheChess

model/User.hpp

00001 /*
00002  * thechess, chess game web application written in C++ and based on Wt
00003  * Copyright (C) 2010 Boris Nagaev
00004  *
00005  * thechess is licensed under the GNU GPL Version 2.
00006  * Other versions of the GPL do not apply.
00007  * See the LICENSE file for terms of use.
00008  */
00009 
00010 #ifndef THECHESS_MODEL_USER_H_
00011 #define THECHESS_MODEL_USER_H_
00012 
00013 #include <vector>
00014 
00015 #include <Wt/Dbo/Dbo>
00016 #include <Wt/Dbo/ptr>
00017 #include <Wt/WString>
00018 #include <Wt/WDateTime>
00019 #include <Wt/Dbo/WtSqlTraits>
00020 #include <Wt/Dbo/collection>
00021 #include <Wt/Dbo/Query>
00022 #include <Wt/Dbo/Session>
00023 namespace dbo = Wt::Dbo;
00024 
00025 namespace thechess {
00026 namespace model {
00027 class User;
00028 typedef dbo::ptr<User> UserPtr;
00029 typedef dbo::collection<UserPtr> Users;
00030 typedef std::vector<UserPtr> UsersVector;
00031 }
00032 }
00033 
00034 #include "model/Game.hpp"
00035 #include "model/Competition.hpp"
00036 #include "model/EloPlayer.hpp"
00037 #include "model/CookieSession.hpp"
00038 #include "model/UserClassification.hpp"
00039 #include "time_intervals.hpp"
00040 
00041 namespace thechess {
00042 namespace model {
00043 
00044 class User : public dbo::Dbo<User> {
00045 public:
00046     enum Rights {
00047         admin = 5,
00048         moderator = 2,
00049         regular_user = 0
00050     };
00051 
00052     User();
00053     User(bool);
00054 
00055     template<class Action>
00056     void persist(Action& a) {
00057         dbo::field(a, username_, "username");
00058         dbo::field(a, password_, "password");
00059         dbo::field(a, rights_, "rights");
00060         dbo::field(a, sessions_, "sessions");
00061         dbo::field(a, last_enter_, "last_enter");
00062         dbo::field(a, online_time_, "online_time");
00063         dbo::field(a, classification_, "classification");
00064         dbo::field(a, classification_confirmer_, "classification_confirmer");
00065         dbo::hasMany(a, classification_confirmed_, dbo::ManyToOne, "classification_confirmer");
00066 
00067         dbo::hasMany(a, white_games_, dbo::ManyToOne, "white");
00068         dbo::hasMany(a, black_games_, dbo::ManyToOne, "black");
00069         dbo::hasMany(a, won_games_, dbo::ManyToOne, "winner_game");
00070         dbo::hasMany(a, init_games_, dbo::ManyToOne, "init_game");
00071 
00072         dbo::hasMany(a, competitions_, dbo::ManyToMany, "members_competitions");
00073         dbo::hasMany(a, init_competitions_, dbo::ManyToOne, "init_competitions");
00074         dbo::hasMany(a, virtual_allower_, dbo::ManyToOne, "virtual_allower");
00075         dbo::hasMany(a, won_competitions_, dbo::ManyToMany, "winners_competition");
00076 
00077         dbo::field(a, games_stat_, "games_stat");
00078     }
00079 
00080     void set_password(const std::string& password);
00081     bool test_password(const std::string& password) const;
00082     const Wt::WString& username() const {
00083         return username_;
00084     }
00085     void set_username(Wt::WString username) {
00086         username_ = username;
00087     }
00088     Rights rights() const {
00089         return rights_;
00090     }
00091     void set_rights(Rights rights) {
00092         rights_ = rights;
00093     }
00094     void login();
00095     void logout();
00096     bool online() const {
00097         return sessions_ != 0;
00098     }
00099     const Td& online_time() const {
00100         return online_time_;
00101     }
00102     dbo::Query<GamePtr> games() const;
00103 
00104     bool can_set_classification(UserPtr user) const;
00105     void set_classification(UserPtr user, Classification classification);
00106     Classification classification() const;
00107     Wt::WString classification_str() const;
00108 
00109     bool can_confirm_classification(UserPtr user) const;
00110     void confirm_classification(UserPtr user);
00111     bool classification_confirmed() const;
00112     UserPtr classification_confirmer() const;
00113 
00114     const EloPlayer& games_stat() const {
00115         return games_stat_;
00116     }
00117     EloPlayer& games_stat() {
00118         return games_stat_;
00119     }
00120 
00121 private:
00122     Wt::WString username_;
00123     std::string password_; // UTF8
00124     Rights rights_; // default constructor: 0
00125     int sessions_;
00126     Wt::WDateTime last_enter_;
00127     Td online_time_;
00128 
00129     Games white_games_;
00130     Games black_games_;
00131     Games won_games_;
00132     Games init_games_;
00133 
00134     Classification classification_;
00135     UserPtr classification_confirmer_;
00136     Users classification_confirmed_;
00137 
00138     Competitions competitions_;
00139     Competitions init_competitions_;
00140     Competitions virtual_allower_;
00141     Competitions won_competitions_;
00142 
00143     EloPlayer games_stat_;
00144 };
00145 
00146 }
00147 }
00148 
00149 #endif // THECHESS_MODEL_USER_H_
 All Classes Functions Enumerations Enumerator