Commits

Boris Nagaev committed de2efa6

menu: add anchor 'random competitor'

Comments (0)

Files changed (7)

locales/thechess.xml

     <message id='tc.menu.my_page'>my page</message>
     <message id='tc.menu.new_competition'>new competition</message>
     <message id='tc.menu.private_messages'>private messages</message>
+    <message id='tc.menu.random_competitor'>random competitor</message>
     <message id='tc.menu.settings'>settings</message>
     <message id='tc.menu.team'>team</message>
     <message id='tc.menu.teams_list'>list of teams</message>

locales/thechess_be.xml

     <message id="tc.menu.my_page">мая старонка</message>
     <message id="tc.menu.new_competition">новы турнір</message>
     <message id="tc.menu.private_messages">асабістыя паведамленні</message>
+    <message id='tc.menu.random_competitor'>случайный соперник</message>
     <message id="tc.menu.settings">налады</message>
     <message id='tc.menu.team'>команда</message>
     <message id='tc.menu.teams_list'>список команд</message>

locales/thechess_ru.xml

     <message id='tc.menu.my_page'>моя страница</message>
     <message id='tc.menu.new_competition'>новый турнир</message>
     <message id='tc.menu.private_messages'>личные сообщения</message>
+    <message id='tc.menu.random_competitor'>случайный соперник</message>
     <message id='tc.menu.settings'>настройки</message>
     <message id='tc.menu.team'>команда</message>
     <message id='tc.menu.teams_list'>список команд</message>

locales/thechess_uk.xml

     <message id='tc.menu.my_page'>моя сторінка</message>
     <message id='tc.menu.new_competition'>новий турнір</message>
     <message id='tc.menu.private_messages'>особисті повідомлення</message>
+    <message id='tc.menu.random_competitor'>случайный соперник</message>
     <message id='tc.menu.settings'>налаштування</message>
     <message id='tc.menu.team'>команда</message>
     <message id='tc.menu.teams_list'>список команд</message>
 Path::Path(Wt::WObject* parent):
     Parser(parent), main_widget_(0) {
     user_list_ = new PredefinedNode("user", this);
+    random_competitor_ = new PredefinedNode("random", this);
     all_sessions_ = new PredefinedNode("sessions", user_list_);
     user_view_ = new IntegerNode(user_list_);
     user_challenge_ = new PredefinedNode("challenge", user_view_);
     main_widget_ = mw;
     connect(this, boost::bind(&MainWidget::main_page, mw));
     connect(user_list_, boost::bind(&MainWidget::user_list, mw));
+    connect(random_competitor_,
+            boost::bind(&Path::open_random_competitor, this));
     connect(all_sessions_, boost::bind(&MainWidget::all_sessions, mw));
     connect(user_view_, boost::bind(&Path::open_user, this));
     connect(user_challenge_, boost::bind(&Path::open_user_challenge, this));
     { }
 }
 
+void Path::open_random_competitor() {
+    BOOST_ASSERT(main_widget_);
+    dbo::Transaction t(tApp->session());
+    if (tApp->user()) {
+        try {
+            UserPtr user = tApp->session().find<User>()
+                           .where("rights != ?").bind(NONE)
+                           .where("vacation_until is null")
+                           .where("last_online >= ?")
+                           .bind(now() - Options::instance()->away_timeout())
+                           .where("id <> ?").bind(tApp->user())
+                           .orderBy("random()").limit(1);
+            main_widget_->user_challenge(user);
+        } catch (...) {
+            main_widget_->game_new();
+        }
+    }
+}
+
 void Path::open_user_challenge() {
     BOOST_ASSERT(main_widget_);
     long long id = user_view_->integer();
         return user_list_;
     }
 
+    /** Random online user */
+    url::PredefinedNode* random_competitor() const {
+        return random_competitor_;
+    }
+
     /** List of sessions */
     url::PredefinedNode* all_sessions() const {
         return all_sessions_;
 private:
     MainWidget* main_widget_;
     url::PredefinedNode* user_list_;
+    url::PredefinedNode* random_competitor_;
     url::PredefinedNode* all_sessions_;
     url::IntegerNode* user_view_;
     url::PredefinedNode* virtuals_of_user_;
     url::IntegerNode* team_view_;
 
     void open_user();
+    void open_random_competitor();
     void open_user_challenge();
     void open_virtuals_of_user();
     void open_ip_of_user();

src/widgets/MainMenu.cpp

         add_button("tc.menu.games_list", path_->game_list());
         if (user_items_ && tApp->user()->has_permission(GAME_CREATOR)) {
             add_button("tc.menu.challenge", path_->game_new());
+            add_button("tc.menu.random_competitor", path_->random_competitor());
         }
         add_button("tc.menu.testing_board", path_->board_root());
         // FIXME list of GP and CP