Started ChessClock::updateTimer_ in begin of turn
[chessclock] / classes / chessclock.cpp
index d7df9d3..d3b169a 100644 (file)
@@ -31,15 +31,16 @@ ChessClock::ChessClock(bool white, QWidget *parent) :
     isWhite_ = white;
     loser_ = false;
     turn_ = 0;
-    timePlayed_ = 0;
+    timePlayedBeforeTurn_ = 0;
     status_ = NotRunning;
+    another_ = 0;
 
     // Set clock timer calculating played time
     clockTime_.start();
 
     // Set updating timer
     updateTimer_.setInterval( UPDATEINTERVAL );
-    connect( &updateTimer_, SIGNAL(timeout),this,SLOT(updateClock()));
+    connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
 }
 
 void ChessClock::startTurn()
@@ -49,18 +50,20 @@ void ChessClock::startTurn()
     // Turn information for this new turn
     currentTurn_ = new TurnInformation(turn_, isWhite_);
     clockTime_.restart();
+    updateTimer_.start();
     status_=Running;
 
     // Repaint clock
-    repaintClock();
+    updateClock();
 }
 
 void ChessClock::pauseTurn()
 {
+    updateTimer_.stop();
     // Update turn time
     currentTurn_->addTime( clockTime_.restart() );
     status_ = Paused;
-    repaintClock();
+    updateClock();
 }
 
 void ChessClock::continueTurn()
@@ -69,23 +72,28 @@ void ChessClock::continueTurn()
     // Add pause duration to information object
     currentTurn_->addPause( clockTime_.restart() );
     status_ = Running;
-    repaintClock();
+    updateTimer_.start();
+    updateClock();
 }
 
 
 TurnInformation* ChessClock::endTurn()
 {
     status_ = NotRunning;
+    updateTimer_.stop();
     // Update turn time
     currentTurn_->addTime( clockTime_.restart());
+    // Count time played
+    timePlayedBeforeTurn_ = getTimePlayed();
     // Count time available
     timeAvailableBeforeTurn_ = getTimeAvailable();
-    repaintClock();
+    updateClock();
 
     // Close and return turn information
     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
     TurnInformation* information = currentTurn_;
     currentTurn_ = 0;
+    emit endTurn();
     return information;
 }
 
@@ -99,7 +107,44 @@ int ChessClock::getTimeAvailable()
     // Most simple - will be overwritten in more complex time controls:
     // subtract duration time!
     if( currentTurn_)
+    {
+        // Update turn time
+        currentTurn_->addTime( clockTime_.restart());
         return timeAvailableBeforeTurn_-currentTurn_->getDuration();
+    }
     else
         return timeAvailableBeforeTurn_;
 }
+
+
+int ChessClock::getTimePlayed() const
+{
+    // Count time played time
+    if( currentTurn_ )
+        return timePlayedBeforeTurn_ + currentTurn_->getDuration();
+    else
+        return timePlayedBeforeTurn_;
+}
+
+
+void ChessClock::setTimeAvailable(int msecs)
+{
+    timeAvailableBeforeTurn_ = msecs;
+}
+
+void ChessClock::updateClock()
+{
+    // Check loser
+    if( another_ && !another_->isLoser())
+    {
+        if( getTimeAvailable() < 0 && !loser_)
+        {
+            loser_ = true;
+            emit timeOutLoser();
+        }
+
+    }
+    repaintClock();
+
+}
+