| 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 | #include "qgoboard_net.h" |
| 24 | #include "tree.h" |
| 25 | #include "move.h" |
| 26 | #include "network/boarddispatch.h" |
| 27 | #include "network/messages.h" |
| 28 | #include "clockdisplay.h" |
| 29 | #include "boardwindow.h" |
| 30 | #include <QMessageBox> |
| 31 | #include "networkconnection.h" |
| 32 | |
| 33 | qGoBoardMatchInterface::qGoBoardMatchInterface(BoardWindow *bw, Tree * t, GameData *gd) : qGoBoardNetworkInterface(bw, t, gd) //, QObject(bw) |
| 34 | { |
| 35 | // warningSound = settings.value("BYO_SOUND_WARNING").toBool(); |
| 36 | // warningSecs = settings.value("BYO_SEC_WARNING").toInt(); |
| 37 | |
| 38 | if(connection->startTimerOnOpen() && |
| 39 | (connection->clientCountsTime() || connection->clientSendsTime())) |
| 40 | boardTimerId = startTimer(1000); |
| 41 | |
| 42 | tree->slotNavLast(); |
| 43 | } |
| 44 | |
| 45 | /* This timer stuff is awkward looking. FIXME */ |
| 46 | void qGoBoardMatchInterface::startGame(void) |
| 47 | { |
| 48 | if(!connection->startTimerOnOpen() && |
| 49 | (connection->clientCountsTime() || connection->clientSendsTime())) |
| 50 | boardTimerId = startTimer(1000); |
| 51 | } |
| 52 | |
| 53 | void qGoBoardMatchInterface::onFirstMove(void) |
| 54 | { |
| 55 | //we can now start the timer |
| 56 | if(!connection->startTimerOnOpen() && |
| 57 | (connection->clientCountsTime() || connection->clientSendsTime())) |
| 58 | boardTimerId = startTimer(1000); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * We subclass this function, because the server will send the move back |
| 63 | * so there is no need to have it displayed |
| 64 | */ |
| 65 | void qGoBoardMatchInterface::localMoveRequest(StoneColor c, int x, int y) |
| 66 | { |
| 67 | sendMoveToInterface(c,x,y); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * This functions gets the request (from a board click) |
| 72 | * to mark a stone as dead/undead (score mode). this sends the click coords to server |
| 73 | */ |
| 74 | void qGoBoardMatchInterface::localMarkDeadRequest(int x, int y) |
| 75 | { |
| 76 | //color is ignored as far as I know, still iffy FIXME |
| 77 | sendMoveToInterface(stoneBlack,x,y); |
| 78 | } |
| 79 | |
| 80 | void qGoBoardMatchInterface::timerEvent(QTimerEvent*) |
| 81 | { |
| 82 | if (boardwindow->getGamePhase() != phaseOngoing) |
| 83 | return; |
| 84 | BoardDispatch * boarddispatch = dispatch; |
| 85 | if(!boarddispatch) |
| 86 | { |
| 87 | qDebug("Match timer event but no board dispatch"); |
| 88 | return; |
| 89 | } |
| 90 | /* I added this clock stopping stuff but it seems unnecessary FIXME |
| 91 | * we could leave it in just in case, actually |
| 92 | * might still somehow be necessary for clientSendsTime()*/ |
| 93 | if(boarddispatch->isClockStopped()) |
| 94 | return; |
| 95 | if(connection->clientCountsTime()) |
| 96 | boardwindow->getClockDisplay()->setTimeStep(getBlackTurn()); |
| 97 | |
| 98 | |
| 99 | if ((getBlackTurn() && boardwindow->getMyColorIsBlack()) || |
| 100 | ((!getBlackTurn()) && boardwindow->getMyColorIsWhite())) |
| 101 | { |
| 102 | if(!connection->clientCountsTime() && connection->clientSendsTime()) |
| 103 | boardwindow->getClockDisplay()->setTimeStep(getBlackTurn()); |
| 104 | |
| 105 | /* FIXME, probably don't want to send time and time loss... even if |
| 106 | * network protocols are exclusive with these... */ |
| 107 | if(!boardwindow->getClockDisplay()->warning(getBlackTurn())) |
| 108 | boarddispatch->sendTimeLoss(); |
| 109 | |
| 110 | if(connection->clientSendsTime()) |
| 111 | boarddispatch->sendTime(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * time info has been send by parser |
| 117 | * TODO : make sure we won't need this in qgoboard (code duplicate) |
| 118 | */ |
| 119 | /* FIXME Looks like this function was used for more than it is now and the |
| 120 | * stuff with the interface handler, which I assume goes to the clockdisplay... |
| 121 | * its all a little awkward. */ |
| 122 | void qGoBoardMatchInterface::setTimerInfo(const QString &btime, const QString &bstones, const QString &wtime, const QString &wstones) |
| 123 | { |
| 124 | //int bt_i = btime.toInt(); |
| 125 | //int wt_i = wtime.toInt(); |
| 126 | // b_stones = bstones; |
| 127 | // w_stones = wstones; |
| 128 | /* |
| 129 | #ifdef SHOW_INTERNAL_TIME |
| 130 | if (chk_b < 0) |
| 131 | { |
| 132 | chk_b = bt_i; |
| 133 | chk_w = wt_i; |
| 134 | } |
| 135 | #endif |
| 136 | */ |
| 137 | // set string in any case |
| 138 | // bt = secToTime(bt_i); |
| 139 | // wt = secToTime(wt_i); |
| 140 | //QTime t0 = QTime::QTime(0,0); |
| 141 | // t0.addSecs(bt_i).toString("m:ss"); |
| 142 | //QTime t1 = t0; |
| 143 | |
| 144 | // set initial timer until game is initialized |
| 145 | // if (!have_gameData) |
| 146 | // win->getInterfaceHandler()->setTimes(bt, bstones, wt, wstones); |
| 147 | |
| 148 | //if (boardwindow->getGamePhase() != phaseInit) |
| 149 | // boardwindow->getInterfaceHandler()->setTimes(t0.addSecs(bt_i).toString("m:ss"), bstones, t0.addSecs(wt_i).toString("m:ss"), wstones); |
| 150 | if (boardwindow->getGamePhase() != phaseInit) |
| 151 | boardwindow->getClockDisplay()->setTimeInfo(btime.toInt(), bstones.toInt(), wtime.toInt(), wstones.toInt()); |
| 152 | // if time info available, sound can be played |
| 153 | // cause no moves cmd in execution |
| 154 | // sound = true; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * initialises the scoring phase in match mode |
| 159 | */ |
| 160 | void qGoBoardMatchInterface::enterScoreMode() |
| 161 | { |
| 162 | if(boardwindow->getGamePhase() == phaseScore) |
| 163 | return; |
| 164 | stopTime(); |
| 165 | qGoBoard::enterScoreMode(); |
| 166 | |
| 167 | //boardwindow->getUi().doneButton->setEnabled(true); |
| 168 | |
| 169 | kibitzReceived(tr("SCORE MODE: click on a stone to mark as dead...")); |
| 170 | } |
| 171 | |
| 172 | void qGoBoardMatchInterface::leaveScoreMode() |
| 173 | { |
| 174 | if(boardwindow->getGamePhase() != phaseScore) |
| 175 | return; |
| 176 | qGoBoard::leaveScoreMode(); |
| 177 | /* Make sure this doesn't conflict with game result stuff */ |
| 178 | if(!connection->startTimerOnOpen() && |
| 179 | (connection->clientCountsTime() || connection->clientSendsTime())) |
| 180 | boardTimerId = startTimer(1000); |
| 181 | |
| 182 | //boardwindow->getUi().doneButton->setEnabled(true); |
| 183 | |
| 184 | kibitzReceived(tr("LEAVING SCORE MODE")); //awkward text FIXME |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * 'resign button pressed |
| 189 | */ |
| 190 | void qGoBoardMatchInterface::slotReviewPressed() |
| 191 | { |
| 192 | #ifdef OLD |
| 193 | emit signal_sendCommandFromBoard("review create_prevgame", false); |
| 194 | #endif //OLD |
| 195 | } |
| 196 | |
| 197 | void qGoBoardMatchInterface::slotDrawPressed() |
| 198 | { |
| 199 | if (QMessageBox::question(boardwindow,tr("Request Draw?"),QString(tr("Ask %1 to end game in draw?\n")).arg(dispatch->getOpponentName())) |
| 200 | == QMessageBox::Yes) |
| 201 | { |
| 202 | if(connection) |
| 203 | { |
| 204 | stopTime(); //protocol specific or not? |
| 205 | boardwindow->setDrawEnabled(false); |
| 206 | connection->sendRequestDraw(gameData->number); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void qGoBoardMatchInterface::slotCountPressed() |
| 212 | { |
| 213 | if (QMessageBox::question(boardwindow, tr("Request Count?"), |
| 214 | QString(tr("Ask %1 to end game?\n")).arg(dispatch->getOpponentName())) == QMessageBox::Yes) |
| 215 | dispatch->sendRequestCount(); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * 'adjourn button pressed |
| 220 | */ |
| 221 | void qGoBoardMatchInterface::slotAdjournPressed() |
| 222 | { |
| 223 | qDebug("qGBMI::slotAdjournPressed()"); |
| 224 | /* I'm going to tie this to the destructor of the board instead */ |
| 225 | /* Scratch that... actually, its supposed to be something |
| 226 | * that can be denied although that's weird to me... since |
| 227 | * they can always close the window.*/ |
| 228 | /* But I have to figure out the IGS command first, at least...*/ |
| 229 | if (QMessageBox::question(boardwindow, tr("Adjourn?"), |
| 230 | QString(tr("Ask %1 to adjourn?\n")).arg(dispatch->getOpponentName())) == QMessageBox::Yes) |
| 231 | { |
| 232 | dispatch->sendAdjournRequest(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void qGoBoardMatchInterface::recvRefuseAdjourn(void) |
| 237 | { |
| 238 | QMessageBox::information(boardwindow, tr("Adjourn Declined"), tr("%1 has declined to adjourn the game.").arg(dispatch->getOpponentName())); |
| 239 | } |
| 240 | |
| 241 | void qGoBoardMatchInterface::requestAdjournDialog(void) |
| 242 | { |
| 243 | if (QMessageBox::question(boardwindow,tr("Adjourn?"), |
| 244 | QString(tr("%1 wants to adjourn\n\nDo you accept ? \n")).arg(dispatch->getOpponentName())) == QMessageBox::Yes) |
| 245 | { |
| 246 | dispatch->sendAdjourn(); |
| 247 | /* FIXME |
| 248 | * Once we hit adjourn, the window should close, or at the very least |
| 249 | * should change phase and the clocks should stop. */ |
| 250 | boardwindow->setGamePhase(phaseEnded); |
| 251 | // this should active Done button and disable other buttons (review okay?) |
| 252 | // also fix above instance !!! |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | dispatch->sendRefuseAdjourn(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void qGoBoardMatchInterface::requestCountDialog(void) |
| 261 | { |
| 262 | if (QMessageBox::question(boardwindow,tr("End game?"), |
| 263 | QString(tr("%1 requests count\n\nDo you accept ? \n")).arg(dispatch->getOpponentName())) |
| 264 | == QMessageBox::Yes) |
| 265 | { |
| 266 | dispatch->sendAcceptCountRequest(); |
| 267 | enterScoreMode(); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | dispatch->sendRefuseCountRequest(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void qGoBoardMatchInterface::recvRefuseCount(void) |
| 276 | { |
| 277 | QMessageBox::information(boardwindow, tr("Count Declined"), tr("%1 has declined to count and end the game.").arg(dispatch->getOpponentName())); |
| 278 | } |
| 279 | |
| 280 | void qGoBoardMatchInterface::requestMatchModeDialog(void) |
| 281 | { |
| 282 | if (QMessageBox::question(0,tr("Return to game?"), |
| 283 | QString(tr("%1 requests return to match mode\n\nDo you accept ? \n")).arg(dispatch->getOpponentName())) |
| 284 | == QMessageBox::Yes) |
| 285 | { |
| 286 | dispatch->sendAcceptMatchModeRequest(); |
| 287 | leaveScoreMode(); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | dispatch->sendRefuseMatchModeRequest(); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void qGoBoardMatchInterface::recvRefuseMatchMode(void) |
| 296 | { |
| 297 | QMessageBox::information(boardwindow, tr("Match mode declined"), tr("%1 has declined to return to the game.").arg(dispatch->getOpponentName())); |
| 298 | } |
| 299 | |
| 300 | void qGoBoardMatchInterface::requestDrawDialog(void) |
| 301 | { |
| 302 | if (QMessageBox::question(boardwindow,tr("End game?"), |
| 303 | QString(tr("%1 requests draw\n\nDo you accept ? \n")).arg(dispatch->getOpponentName())) |
| 304 | == QMessageBox::Yes) |
| 305 | { |
| 306 | dispatch->sendAcceptDrawRequest(); |
| 307 | //FIXME |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | dispatch->sendRefuseDrawRequest(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void qGoBoardMatchInterface::recvRefuseDraw(void) |
| 316 | { |
| 317 | QMessageBox::information(boardwindow, tr("Draw Declined"), tr("%1 has declined to draw the game.").arg(dispatch->getOpponentName())); |
| 318 | } |