| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | /* All the clock display stuff should be stuffed in here and taken out from |
| 24 | * all the redundant places elsewhere */ |
| 25 | /* It annoys me that clock can display 3:56 / 0 after a player has moved. It does |
| 26 | * this in observe because server hasn't sent the new time, so we don't display it. |
| 27 | * But that's a nonsencical reading and we should check it here and update it |
| 28 | * to the full time left. */ |
| 29 | |
| 30 | |
| 31 | #include "clockdisplay.h" |
| 32 | #include "boardwindow.h" |
| 33 | #include "audio.h" |
| 34 | #include "network/messages.h" //for the TimeRecord FIXME |
| 35 | |
| 36 | ClockDisplay::ClockDisplay(BoardWindow *bw, TimeSystem s, int _maintime, int _periods, int _periodtime) : QObject(bw) |
| 37 | { |
| 38 | boardwindow = bw; |
| 39 | setTimeSettings(s, _maintime, _periodtime, _periods); |
| 40 | |
| 41 | if(maintime == 0) |
| 42 | { |
| 43 | b_time = periodtime; |
| 44 | w_time = periodtime; |
| 45 | b_stones_periods = periods; |
| 46 | w_stones_periods = periods; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | b_time = maintime; |
| 51 | w_time = maintime; |
| 52 | b_stones_periods = -1; |
| 53 | w_stones_periods = -1; |
| 54 | } |
| 55 | QSettings settings; |
| 56 | playWarningSound = settings.value("BYO_SOUND_WARNING").toBool(); |
| 57 | warningSecs = settings.value("BYO_SEC_WARNING").toInt(); |
| 58 | |
| 59 | warningSound = new Sound("timer.wav"); |
| 60 | } |
| 61 | |
| 62 | void ClockDisplay::setTimeSettings(TimeSystem s, int m, int p, int o) |
| 63 | { |
| 64 | timeSystem = s; //defaults to canadian |
| 65 | maintime = m; |
| 66 | periodtime = p; |
| 67 | periods = o; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * This takes alls values coming from the time information and updates the clocks |
| 72 | */ |
| 73 | /* FIXME, change to take TimeRecords ?? or not*/ |
| 74 | void ClockDisplay::setTimeInfo(int btime, int bstones_periods, int wtime, int wstones_periods) |
| 75 | { |
| 76 | /* FIXME DOUBLECHECK if this is allowed. We want to be able |
| 77 | * to pass empty records but, this may not be the way to |
| 78 | * do it */ |
| 79 | if(wtime != 0 || wstones_periods != -1) |
| 80 | { |
| 81 | w_time = wtime; |
| 82 | w_stones_periods = wstones_periods; |
| 83 | } |
| 84 | if(btime != 0 || bstones_periods != -1) |
| 85 | { |
| 86 | b_time = btime; |
| 87 | b_stones_periods = bstones_periods; |
| 88 | } |
| 89 | //printf("wb %d %d %d %d\n", w_time, b_time, w_stones_periods, b_stones_periods); |
| 90 | updateTimers(); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * decrease the timer info according to the step, and updates the clocks |
| 95 | */ |
| 96 | void ClockDisplay::setTimeStep(bool black) |
| 97 | { |
| 98 | if (black) |
| 99 | { |
| 100 | b_time--; |
| 101 | if(b_time == 0 && b_stones_periods != 0) |
| 102 | { |
| 103 | /* FIXME, maybe we should have one clock and subclass |
| 104 | * the time system for different clocks??*/ |
| 105 | if(timeSystem == byoyomi || (timeSystem == tvasia && b_stones_periods != -1)) |
| 106 | { |
| 107 | //printf("b by: %d %d %d %d\n", b_time, b_stones_periods, periodtime, periods); |
| 108 | if(b_stones_periods > -1 || timeSystem == tvasia) |
| 109 | b_stones_periods--; |
| 110 | else |
| 111 | b_stones_periods = periods; |
| 112 | if(b_stones_periods != 0) |
| 113 | b_time = periodtime; |
| 114 | } |
| 115 | else if(b_stones_periods == -1) |
| 116 | { |
| 117 | if(timeSystem == canadian) |
| 118 | { |
| 119 | b_time = periodtime; |
| 120 | b_stones_periods = periods; |
| 121 | } |
| 122 | else if(timeSystem == tvasia) |
| 123 | { |
| 124 | b_time = periodtime; |
| 125 | b_stones_periods = periods - 1; |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | printf("other timesystem\n"); |
| 130 | /* IGS gets here a lot due to lag. I.e., canadian time steps down to 0 with stones |
| 131 | * still remainaining to be played, then opponent plays and turns out they have five |
| 132 | * seconds left or something. Would be neat to adjust for lag or something. FIXME */ |
| 133 | } |
| 134 | /* FIXME |
| 135 | * major thing, we need to get time to switch |
| 136 | * over into byoyomi time on IGS |
| 137 | * I think the issue was that IGS sends a byoyomi entering |
| 138 | * message... */ |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | w_time--; |
| 143 | if(w_time == 0 && w_stones_periods != 0) |
| 144 | { |
| 145 | if(timeSystem == byoyomi || (timeSystem == tvasia && w_stones_periods != -1)) |
| 146 | { |
| 147 | //printf("w by: %d %d %d %d\n", w_time, w_stones_periods, periodtime, periods); |
| 148 | if(w_stones_periods > -1 || timeSystem == tvasia) |
| 149 | w_stones_periods--; |
| 150 | else |
| 151 | w_stones_periods = periods; |
| 152 | if(w_stones_periods != 0) |
| 153 | w_time = periodtime; |
| 154 | } |
| 155 | else if(w_stones_periods == -1) |
| 156 | { |
| 157 | if(timeSystem == canadian) |
| 158 | { |
| 159 | w_time = periodtime; |
| 160 | w_stones_periods = periods; |
| 161 | } |
| 162 | else if(timeSystem == tvasia) |
| 163 | { |
| 164 | w_time = periodtime; |
| 165 | w_stones_periods = periods - 1; |
| 166 | } |
| 167 | } |
| 168 | else |
| 169 | printf("other timesystem\n"); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | updateTimers(); |
| 174 | } |
| 175 | |
| 176 | void ClockDisplay::makeMove(bool black) |
| 177 | { |
| 178 | if (black) |
| 179 | { |
| 180 | if(timeSystem == canadian && b_stones_periods != -1) |
| 181 | { |
| 182 | b_stones_periods--; |
| 183 | } |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | if(timeSystem == canadian && w_stones_periods != -1) |
| 188 | { |
| 189 | w_stones_periods--; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | updateTimers(); |
| 194 | } |
| 195 | |
| 196 | void ClockDisplay::rerackTime(bool black) |
| 197 | { |
| 198 | if (black) |
| 199 | { |
| 200 | if(timeSystem == canadian && b_stones_periods == 0) |
| 201 | { |
| 202 | b_time = periodtime; |
| 203 | b_stones_periods = periods; |
| 204 | } |
| 205 | else if(timeSystem == byoyomi && b_stones_periods != -1) |
| 206 | { |
| 207 | b_time = periodtime; |
| 208 | //b_stones_periods = periods; |
| 209 | } |
| 210 | else if(timeSystem == tvasia) |
| 211 | { |
| 212 | b_time = maintime; |
| 213 | b_stones_periods = -1; |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | if(timeSystem == canadian && w_stones_periods == 0) |
| 219 | { |
| 220 | w_time = periodtime; |
| 221 | w_stones_periods = periods; |
| 222 | } |
| 223 | else if(timeSystem == byoyomi && w_stones_periods != -1) |
| 224 | { |
| 225 | w_time = periodtime; |
| 226 | //w_stones_periods = periods; |
| 227 | } |
| 228 | else if(timeSystem == tvasia) |
| 229 | { |
| 230 | w_time = maintime; |
| 231 | w_stones_periods = -1; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | updateTimers(); |
| 236 | } |
| 237 | |
| 238 | TimeRecord ClockDisplay::getTimeRecord(bool black) |
| 239 | { |
| 240 | if(black) |
| 241 | return TimeRecord(b_time, b_stones_periods); |
| 242 | else |
| 243 | return TimeRecord(w_time, w_stones_periods); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * updates the clocks display |
| 248 | */ |
| 249 | void ClockDisplay::updateTimers() |
| 250 | { |
| 251 | QString bt, wt; |
| 252 | /* Negative stones are used to indicate main time period before byo yomi |
| 253 | * not sure about the other two, maybe just 0 minutes */ |
| 254 | /* FIXME periods appear to be unused... which is fine I suppose but |
| 255 | * its not clear */ |
| 256 | /* The below has to be first because otherwise the abs() make |
| 257 | * the time continually jump back up to one */ |
| 258 | if (b_time < 0) |
| 259 | b_time = 0; |
| 260 | //bt.prepend("-"); |
| 261 | if (w_time < 0) |
| 262 | w_time = 0; |
| 263 | //wt.prepend("-"); |
| 264 | |
| 265 | if(b_time > 3600 || w_time > 3600) |
| 266 | { |
| 267 | bt = QTime(0,0).addSecs(abs(b_time)).toString("h:mm:ss") ; |
| 268 | wt = QTime(0,0).addSecs(abs(w_time)).toString("h:mm:ss") ; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | bt = QTime(0,0).addSecs(abs(b_time)).toString("m:ss") ; |
| 273 | wt = QTime(0,0).addSecs(abs(w_time)).toString("m:ss") ; |
| 274 | } |
| 275 | |
| 276 | switch(timeSystem) |
| 277 | { |
| 278 | case canadian: |
| 279 | if (b_stones_periods > -1) //instead of != -1, safer for WING |
| 280 | bt.append(" / ").append(QString::number(b_stones_periods)); |
| 281 | |
| 282 | if (w_stones_periods > -1) |
| 283 | wt.append(" / ").append(QString::number(w_stones_periods)); |
| 284 | break; |
| 285 | default: |
| 286 | case byoyomi: |
| 287 | case tvasia: |
| 288 | if (b_stones_periods > -1) //instead of != -1, safer for WING |
| 289 | bt.append(" / ").append(QString::number(b_stones_periods)); |
| 290 | |
| 291 | if (w_stones_periods > -1) |
| 292 | wt.append(" / ").append(QString::number(w_stones_periods)); |
| 293 | break; |
| 294 | } |
| 295 | boardwindow->setTimeBlack(bt); |
| 296 | boardwindow->setTimeWhite(wt); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * activates the byo-time warnings |
| 301 | */ |
| 302 | bool ClockDisplay::warning(bool black) |
| 303 | { |
| 304 | if (black) |
| 305 | { |
| 306 | if ((b_time > 0) && (b_time < warningSecs)) |
| 307 | { |
| 308 | boardwindow->warnTimeBlack(TimeLow); |
| 309 | if (playWarningSound) |
| 310 | warningSound->play(); |
| 311 | } |
| 312 | else if(b_time == 0 && b_stones_periods == 0) //FIXME for tvasia |
| 313 | { |
| 314 | boardwindow->warnTimeBlack(TimeExpired); |
| 315 | return false; //out of time |
| 316 | } |
| 317 | else |
| 318 | boardwindow->warnTimeBlack(TimeOK); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | if ((w_time > 0) && (w_time < warningSecs)) |
| 323 | { |
| 324 | boardwindow->warnTimeWhite(TimeLow); |
| 325 | if (playWarningSound) |
| 326 | warningSound->play(); |
| 327 | } |
| 328 | else if(w_time == 0 && w_stones_periods == 0) |
| 329 | { |
| 330 | boardwindow->warnTimeWhite(TimeExpired); |
| 331 | return false; //out of time |
| 332 | } |
| 333 | else |
| 334 | boardwindow->warnTimeWhite(TimeOK); |
| 335 | } |
| 336 | return true; //still good |
| 337 | } |