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_STAGED_COMPETITION_HPP_ 00011 #define THECHESS_MODEL_STAGED_COMPETITION_HPP_ 00012 00013 #include <vector> 00014 #include <map> 00015 00016 #include "model/Competition.hpp" 00017 #include "model/User.hpp" 00018 #include "model/Game.hpp" 00019 00020 namespace thechess { 00021 namespace model { 00022 00023 class UserPair { 00024 public: 00025 UserPair(); 00026 UserPair(UserPtr first, UserPtr second); 00027 00028 bool operator<(const UserPair& other) const; 00029 bool operator==(const UserPair& other) const; 00030 bool operator!=(const UserPair& other) const; 00031 operator bool() const { 00032 return first_ && second_; 00033 } 00034 UserPtr first() const { 00035 return first_; 00036 } 00037 UserPtr second() const { 00038 return second_; 00039 } 00040 00041 private: 00042 UserPtr first_; 00043 UserPtr second_; 00044 }; 00045 00046 class StagedCompetition { 00047 public: 00048 enum State { 00049 LOSER, PAIRED, WINNER, UNPAIRED 00050 }; 00051 00052 typedef std::map<UserPtr, State> States; 00053 typedef std::map<UserPtr, int> Stages; 00054 typedef std::multimap<int, UserPair> Paires; 00055 typedef std::map<UserPair, GamesVector> Games; 00056 typedef std::map<UserPair, UserPtr> Winners; 00057 00058 StagedCompetition(const Competition* competition); 00059 void process(Competition* competition, Objects& objects); 00060 00061 const States& states() const { 00062 return states_; 00063 } 00064 const Stages& stages() const { 00065 return stages_; 00066 } 00067 const Paires& paires() const { 00068 return paires_; 00069 } 00070 const Games& games() const { 00071 return games_; 00072 } 00073 const Winners& winners() const { 00074 return winners_; 00075 } 00076 UserPtr winner() const { 00077 return winner_; 00078 } 00079 00080 private: 00081 const Competition* competition_; 00082 00083 States states_; 00084 Stages stages_; 00085 Paires paires_; 00086 Games games_; 00087 Winners winners_; 00088 00089 int winner_stage_; 00090 UserPtr winner_; 00091 00092 void read_games_(); 00093 void read_paires_(); 00094 void read_pair_(int stage, const UserPair& pair); 00095 00096 void start_competition_(); 00097 void join_users_(); 00098 void create_games_(Competition* competition, Objects& objects); 00099 }; 00100 00101 } 00102 } 00103 00104 #endif 00105