ChessClock::addTime() for time controllers to work
[chessclock] / classes / chessclock.cpp
1  /**************************************************************************
2
3     Chess Clock
4
5     Copyright (c) Arto Hyvättinen 2010
6
7     This file is part of Chess Clock software.
8
9     Chess Clock is free software: you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation, either version 3 of the License, or
12     (at your option) any later version.
13
14     Chess Clock is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19
20 **************************************************************************/
21
22 #include "chessclock.h"
23
24 #include "turninformation.h"
25
26 const int ChessClock::UPDATEINTERVAL;
27
28 ChessClock::ChessClock(bool white, QWidget *parent) :
29     QWidget(parent)
30 {
31     isWhite_ = white;
32     loser_ = false;
33     turn_ = 0;
34     timePlayedBeforeTurn_ = 0;
35     status_ = NotRunning;
36     another_ = 0;
37
38     // Set clock timer calculating played time
39     clockTime_.start();
40
41     // Set updating timer
42     updateTimer_.setInterval( UPDATEINTERVAL );
43     connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
44 }
45
46 void ChessClock::startTurn()
47 {
48     turn_++;
49
50     // Turn information for this new turn
51     currentTurn_ = new TurnInformation(turn_, isWhite_);
52     clockTime_.restart();
53     updateTimer_.start();
54     status_=Running;
55
56     // Repaint clock
57     updateClock();
58 }
59
60 void ChessClock::pauseTurn()
61 {
62     updateTimer_.stop();
63     // Update turn time
64     currentTurn_->addTime( clockTime_.restart() );
65     status_ = Paused;
66     updateClock();
67 }
68
69 void ChessClock::continueTurn()
70 {
71     // Continue paused game
72     // Add pause duration to information object
73     currentTurn_->addPause( clockTime_.restart() );
74     status_ = Running;
75     updateTimer_.start();
76     updateClock();
77 }
78
79
80 TurnInformation* ChessClock::endTurn()
81 {
82     status_ = NotRunning;
83     updateTimer_.stop();
84     // Update turn time
85     currentTurn_->addTime( clockTime_.restart());
86     // Count time played
87     timePlayedBeforeTurn_ = getTimePlayed();
88     // Count time available
89     timeAvailableBeforeTurn_ = getTimeAvailable();
90     updateClock();
91
92     // Close and return turn information
93     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
94     TurnInformation* information = currentTurn_;
95     currentTurn_ = 0;
96     emit turnEnded();
97     return information;
98 }
99
100 void ChessClock::setAnother(ChessClock *another)
101 {
102     another_ = another;
103 }
104
105 int ChessClock::getTimeAvailable()
106 {
107     // Most simple - will be overwritten in more complex time controls:
108     // subtract duration time!
109     if( currentTurn_)
110     {
111         // Update turn time
112         currentTurn_->addTime( clockTime_.restart());
113         return timeAvailableBeforeTurn_-currentTurn_->getDuration();
114     }
115     else
116         return timeAvailableBeforeTurn_;
117 }
118
119
120 int ChessClock::getTimePlayed() const
121 {
122     // Count time played time
123     if( currentTurn_ )
124         return timePlayedBeforeTurn_ + currentTurn_->getDuration();
125     else
126         return timePlayedBeforeTurn_;
127 }
128
129
130 void ChessClock::setTimeAvailable(int msecs)
131 {
132     timeAvailableBeforeTurn_ = msecs;
133 }
134
135
136 void ChessClock::addTime(int msecs)
137 {
138    timeAvailableBeforeTurn_ += msecs;
139 }
140
141 void ChessClock::updateClock()
142 {
143     // Check loser
144     if( another_ && !another_->isLoser())
145     {
146         if( getTimeAvailable() < 0 && !loser_)
147         {
148             loser_ = true;
149             emit timeOutLoser();
150         }
151
152     }
153     repaintClock();
154
155 }
156