TheChess
|
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_COMPETITION_HPP_ 00011 #define THECHESS_MODEL_COMPETITION_HPP_ 00012 00013 #include <string> 00014 #include <map> 00015 #include <vector> 00016 00017 #include <Wt/Dbo/Dbo> 00018 #include <Wt/Dbo/ptr> 00019 namespace dbo = Wt::Dbo; 00020 #include <Wt/WDateTime> 00021 #include <Wt/Dbo/WtSqlTraits> 00022 00023 namespace thechess { 00024 namespace model { 00025 class Competition; 00026 typedef dbo::ptr<Competition> CompetitionPtr; 00027 typedef dbo::collection<CompetitionPtr> Competitions; 00028 typedef std::vector<CompetitionPtr> CompetitionsVector; 00029 } 00030 } 00031 00032 #include "model/CompetitionParameters.hpp" 00033 #include "model/User.hpp" 00034 #include "model/Game.hpp" 00035 #include "model/td.hpp" 00036 #include "model/Object.hpp" 00037 00038 namespace thechess { 00039 namespace model { 00040 00041 typedef std::map<UserPtr, std::map<UserPtr, GamesVector> > GamesTable; 00042 00043 class Competition : public CompetitionParameters, public dbo::Dbo<Competition> { 00044 public: 00045 typedef CompetitionType Type; 00046 00047 enum State { 00048 RECRUITING = 10, 00049 ACTIVE = 30, 00050 ENDED = 50, 00051 CANCELLED = 70 00052 }; 00053 00054 Competition(); 00055 Competition(bool); 00056 00057 static Wt::WString type2str(Type type); 00058 static bool all_ended(const GamesVector& games); 00059 static void wins_number(const GamesVector& games, 00060 std::map<UserPtr, float>& wins); 00061 static UsersVector winners_of_games(const GamesVector& games); 00062 00063 template<class Action> 00064 void persist(Action& a) { 00065 CompetitionParameters::persist(a); 00066 dbo::field(a, state_, "state"); 00067 dbo::field(a, name_, "name"); 00068 dbo::field(a, description_, "description"); 00069 dbo::hasMany(a, members_, dbo::ManyToMany, "members_competitions"); 00070 dbo::belongsTo(a, init_, "init_competitions"); 00071 dbo::belongsTo(a, virtual_allower_, "virtual_allower"); 00072 dbo::hasMany(a, games_, dbo::ManyToOne, "competition"); 00073 dbo::field(a, created_, "created"); 00074 dbo::field(a, started_, "started"); 00075 dbo::field(a, ended_, "ended"); 00076 dbo::hasMany(a, winners_, dbo::ManyToMany, "winners_competition"); 00077 } 00078 00079 State state() const { 00080 return state_; 00081 } 00082 static const char* state2str(State state); 00083 UserPtr init() const { 00084 return init_; 00085 } 00086 00087 const Wt::WDateTime& created() const { 00088 return created_; 00089 } 00090 const Wt::WDateTime& started() const { 00091 return started_; 00092 } 00093 const Wt::WDateTime& ended() const { 00094 return ended_; 00095 } 00096 00097 const Wt::WString& name() const { 00098 return name_; 00099 } 00100 void set_name(const Wt::WString& v) { 00101 name_ = v; 00102 } 00103 const Wt::WString& description() const { 00104 return description_; 00105 } 00106 void set_description(const Wt::WString& v) { 00107 description_ = v; 00108 } 00109 00110 bool is_member(UserPtr user) const; 00111 bool is_winner(UserPtr user) const; 00112 00113 const Users& members() const { 00114 return members_; 00115 } 00116 const Games& games() const { 00117 return games_; 00118 } 00119 const Users& winners() const { 00120 return winners_; 00121 } 00122 00123 UsersVector members_vector() const; 00124 GamesVector games_vector() const; 00125 UsersVector winners_vector() const; 00126 00127 GamesTable games_table() const; 00128 Games games_with(UserPtr user) const; 00129 GamesVector games_with(UserPtr user, GamesTable& gt) const; 00130 00131 // auto-manage 00132 void check(Objects& objects); 00133 Wt::WDateTime next_check() const; 00134 00135 // methods of recruiting state 00136 static bool can_create_competition(UserPtr /*user*/) { 00137 return true; 00138 } 00139 void create_competition(UserPtr user); 00140 00141 bool can_join(UserPtr user) const; 00142 void join(UserPtr user); 00143 00144 bool can_leave(UserPtr user) const; 00145 void leave(UserPtr user); 00146 00147 bool can_kick(UserPtr kicker, UserPtr kicked) const; 00148 void kick(UserPtr kicker, UserPtr kicked); 00149 00150 bool can_change_parameters(UserPtr user) const; 00151 00152 bool can_cancel(UserPtr user) const; 00153 void cancel(UserPtr user); 00154 00155 // methods of active stage 00156 00157 private: 00158 // common attributes 00159 State state_; 00160 00161 Wt::WString name_; 00162 Wt::WString description_; 00163 00164 Users members_; 00165 UserPtr init_; 00166 00167 Users virtuals_; 00168 UserPtr virtual_allower_; 00169 00170 Games games_; 00171 00172 Wt::WDateTime created_; 00173 Wt::WDateTime started_; 00174 Wt::WDateTime ended_; 00175 00176 Users winners_; 00177 00178 bool can_start_() const; 00179 void start_(Objects& objects); 00180 void create_games_classical_(Objects& objects); 00181 void cancel_(); 00182 00183 void process_(Objects& objects); 00184 void process_classical_(Objects& objects); 00185 00186 void finish_(const UsersVector& winners, Objects& objects); 00187 00188 GamePtr create_game_(UserPtr white, UserPtr black, int stage=-1, 00189 bool no_draw=false); 00190 00191 friend class StagedCompetition; 00192 }; 00193 00194 } 00195 } 00196 00197 #endif 00198