First working gameclock!
[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     currentTurn_ = 0;
38
39     // Set clock timer calculating played time
40     clockTime_.start();
41
42     // Set updating timer
43     updateTimer_.setInterval( UPDATEINTERVAL );
44     connect( &updateTimer_, SIGNAL(timeout()),this,SLOT(updateClock()));
45 }
46
47 void ChessClock::startTurn()
48 {
49     turn_++;
50
51     // Turn information for this new turn
52     currentTurn_ = new TurnInformation(turn_, isWhite_);
53     clockTime_.restart();
54     updateTimer_.start();
55     status_=Running;
56
57     // Repaint clock
58     updateClock();
59 }
60
61 void ChessClock::pauseTurn()
62 {
63     updateTimer_.stop();
64     // Update turn time
65     currentTurn_->addTime( clockTime_.restart() );
66     status_ = Paused;
67     updateClock();
68 }
69
70 void ChessClock::continueTurn()
71 {
72     // Continue paused game
73     // Add pause duration to information object
74     currentTurn_->addPause( clockTime_.restart() );
75     status_ = Running;
76     updateTimer_.start();
77     updateClock();
78 }
79
80
81 TurnInformation* ChessClock::endTurn()
82 {
83     updateTimer_.stop();
84     // Count time played
85     timePlayedBeforeTurn_ = getTimePlayed();
86     // Count time available
87     // This update current turn
88     timeAvailableBeforeTurn_ = getTimeAvailable();
89
90     status_ = NotRunning;
91     updateClock();
92
93     // Close and return turn information
94     currentTurn_->turnReady(timeAvailableBeforeTurn_ );
95     TurnInformation* information = currentTurn_;
96     currentTurn_ = 0;
97     emit turnEnded();
98     return information;
99 }
100
101 void ChessClock::setAnother(ChessClock *another)
102 {
103     another_ = another;
104 }
105
106 int ChessClock::getTimeAvailable()
107 {
108     // Most simple - will be overwritten in more complex time controls:
109     // subtract duration time!
110     if( currentTurn_)
111     {
112         // Update turn time
113         return timeAvailableBeforeTurn_-currentTurnPlayed();
114     }
115     else
116         return timeAvailableBeforeTurn_;
117 }
118
119
120 int ChessClock::getTimePlayed()
121 {
122     // Count time played time
123     if( currentTurn_ )
124         return timePlayedBeforeTurn_ + currentTurnPlayed();
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 int ChessClock::currentTurnPlayed()
142 {
143     if( currentTurn_ )
144     {
145         // Update current time
146         if( status_ == Running )
147             currentTurn_->addTime( clockTime_.restart());
148
149         // Return current time
150         return currentTurn_->getDuration();
151      }
152     else
153         // No current turn!
154         return 0;
155 }
156
157 void ChessClock::updateClock()
158 {
159     // Check loser
160     if( another_ && !another_->isLoser())
161     {
162         if( getTimeAvailable() < 0 && !loser_)
163         {
164             loser_ = true;
165             emit timeOutLoser();
166         }
167
168     }
169     repaintClock();
170
171 }
172