Repository information in web pages
[chessclock] / classes / chessclock.cpp
index d3b169a..cc9269e 100644 (file)
@@ -34,6 +34,7 @@ ChessClock::ChessClock(bool white, QWidget *parent) :
     timePlayedBeforeTurn_ = 0;
     status_ = NotRunning;
     another_ = 0;
+    currentTurn_ = 0;
 
     // Set clock timer calculating played time
     clockTime_.start();
@@ -79,21 +80,24 @@ void ChessClock::continueTurn()
 
 TurnInformation* ChessClock::endTurn()
 {
-    status_ = NotRunning;
     updateTimer_.stop();
-    // Update turn time
-    currentTurn_->addTime( clockTime_.restart());
+    status_ = NotRunning;
+
+    updateClock();
     // Count time played
     timePlayedBeforeTurn_ = getTimePlayed();
+
     // Count time available
+    // This update current turn
     timeAvailableBeforeTurn_ = getTimeAvailable();
-    updateClock();
+
 
     // Close and return turn information
     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
     TurnInformation* information = currentTurn_;
     currentTurn_ = 0;
-    emit endTurn();
+
+    emit turnEnded();
     return information;
 }
 
@@ -109,21 +113,16 @@ int ChessClock::getTimeAvailable()
     if( currentTurn_)
     {
         // Update turn time
-        currentTurn_->addTime( clockTime_.restart());
-        return timeAvailableBeforeTurn_-currentTurn_->getDuration();
+        return timeAvailableBeforeTurn_-currentTurnPlayed();
     }
     else
         return timeAvailableBeforeTurn_;
 }
 
 
-int ChessClock::getTimePlayed() const
+int ChessClock::getTimePlayed()
 {
-    // Count time played time
-    if( currentTurn_ )
-        return timePlayedBeforeTurn_ + currentTurn_->getDuration();
-    else
-        return timePlayedBeforeTurn_;
+     return timePlayedBeforeTurn_ + currentTurnPlayed();
 }
 
 
@@ -132,6 +131,28 @@ void ChessClock::setTimeAvailable(int msecs)
     timeAvailableBeforeTurn_ = msecs;
 }
 
+
+void ChessClock::addTime(int msecs)
+{
+   timeAvailableBeforeTurn_ += msecs;
+}
+
+int ChessClock::currentTurnPlayed()
+{
+    if( currentTurn_ )
+    {
+        // Update current time
+        if( status_ == Running )
+            currentTurn_->addTime( clockTime_.restart());
+
+        // Return current time
+        return currentTurn_->getDuration();
+     }
+    else
+        // No current turn!
+        return 0;
+}
+
 void ChessClock::updateClock()
 {
     // Check loser