| 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 | #include "boarddispatch.h" |
| 23 | #include "boardwindow.h" |
| 24 | #include "messages.h" |
| 25 | #include "listviews.h" |
| 26 | #include "../game_interfaces/resultdialog.h" |
| 27 | #include "../game_interfaces/countdialog.h" |
| 28 | #include "gamedata.h" |
| 29 | #include "playergamelistings.h" |
| 30 | #include "networkconnection.h" |
| 31 | #include "qgoboard.h" |
| 32 | #include "clockdisplay.h" |
| 33 | #include "tree.h" |
| 34 | |
| 35 | /* It would be difficult to create a board without a connection, |
| 36 | * but we need to either be certain and not test at all, or assign |
| 37 | * it at creation. I'm just worried about the client code knowing |
| 38 | * too much about the connection.*/ |
| 39 | /* Also the !boardwindow things? I mean its pretty damn hard |
| 40 | * to get these messages without them, we just just need to be sure */ |
| 41 | |
| 42 | BoardDispatch::BoardDispatch(NetworkConnection * conn, GameListing * l) |
| 43 | { |
| 44 | connection = conn; |
| 45 | boardwindow = 0; |
| 46 | resultdialog = 0; |
| 47 | countdialog = 0; |
| 48 | observerListModel = 0; |
| 49 | |
| 50 | clockStopped = false; |
| 51 | reviewInVariation = false; |
| 52 | /* New rules. The boarddispatch creates the game data or |
| 53 | * whatever loads the game data before passing it to the |
| 54 | * boardwindow creates it. The boardwindow deletes it. |
| 55 | * there's only one gameData per board */ |
| 56 | if(l && l->gameData) |
| 57 | gameData = new GameData(l->gameData); //ORO uses this |
| 58 | else |
| 59 | gameData = new GameData(); |
| 60 | gameData->place = connection->getPlaceString(); |
| 61 | //maybe we could set number here? if we have it? |
| 62 | if(strlen(connection->getCodecString()) != 0) |
| 63 | gameData->codec = "UTF-8"; //all files in unicode where necessary |
| 64 | |
| 65 | /* Not sure we can do this. GameListings have to have a |
| 66 | * key. But to go without a gameListing in the BoardDispatch?*/ |
| 67 | if(l) |
| 68 | gameListing = l; |
| 69 | else |
| 70 | qDebug("BoardDispatch::BoardDispatch() : NULL GameListing * passed!"); |
| 71 | } |
| 72 | |
| 73 | BoardDispatch::~BoardDispatch() |
| 74 | { |
| 75 | qDebug("Destroying board dispatch\n"); |
| 76 | /* Clear dispatch so boardwindow doesn't try to close it */ |
| 77 | if(boardwindow) |
| 78 | { |
| 79 | //clearObservers(); |
| 80 | boardwindow->setObserverModel(NULL); //crashhere maybe |
| 81 | boardwindow->setGamePhase(phaseEnded); //disables all buttons |
| 82 | if(observerListModel) |
| 83 | delete observerListModel; |
| 84 | if(boardwindow->getGamePhase() != phaseEnded) |
| 85 | { |
| 86 | //got an oro crash here after return to match/rematch |
| 87 | //confusion, qgoboard was 0 FIXME |
| 88 | //and then after closing the window, apparently |
| 89 | //board dispatch was NOT cleared and got called after |
| 90 | //here on disconnect |
| 91 | boardwindow->qgoboard->stopTime(); |
| 92 | boardwindow->qgoboard->kibitzReceived("Disconnected"); |
| 93 | } |
| 94 | boardwindow->setBoardDispatch(0); |
| 95 | } |
| 96 | if(resultdialog) |
| 97 | delete resultdialog; |
| 98 | if(countdialog) |
| 99 | delete countdialog; |
| 100 | } |
| 101 | |
| 102 | bool BoardDispatch::canClose(void) |
| 103 | { |
| 104 | if(!gameData) |
| 105 | return true; |
| 106 | if(gameData->gameMode == modeMatch && gameData->fullresult == 0) |
| 107 | { |
| 108 | boardwindow->qgoboard->slotResignPressed(); |
| 109 | /* fullresult won't get set until we receive the server acknowledgement, so |
| 110 | * for now, we'll just stop the process each time. Its likely player is only |
| 111 | * playing one game so this is more of a warning */ |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /* FIXME, we need to make better use of this, |
| 118 | * or not at all */ |
| 119 | void BoardDispatch::closeBoard(void) |
| 120 | { |
| 121 | if(connection) |
| 122 | { |
| 123 | switch(boardwindow->getGameMode()) |
| 124 | { |
| 125 | case modeObserve: |
| 126 | if(boardwindow->getGamePhase() != phaseEnded) |
| 127 | connection->stopObserving(gameListing); //FIXME |
| 128 | break; |
| 129 | case modeReview: |
| 130 | connection->stopReviewing(gameListing); |
| 131 | break; |
| 132 | case modeMatch: |
| 133 | /* FIXME if game is over, we don't need to adjourn |
| 134 | * nor perhaps with the above modes either |
| 135 | * so we should check this out... maybe this |
| 136 | * function shouldn't be being called, |
| 137 | * at least not from here. Basically, |
| 138 | * responsibilities of board dispatch and |
| 139 | * qgoboard or boardwindow or whatever are |
| 140 | * overlapping here. Something needs to go */ |
| 141 | /* This shouldn't be here anymore since we do the canClose which |
| 142 | * prompts for resign. Or its an IGS only feature FIXME */ |
| 143 | if(boardwindow->getGamePhase() != phaseEnded) |
| 144 | connection->adjournGame(gameListing); //FIXME |
| 145 | if(resultdialog) |
| 146 | { |
| 147 | delete resultdialog; |
| 148 | resultdialog = 0; |
| 149 | } |
| 150 | break; |
| 151 | default: |
| 152 | qDebug("Unknown game Mode, board dispatch does nothing\n"); |
| 153 | break; |
| 154 | } |
| 155 | if(gameData) |
| 156 | { |
| 157 | qDebug("Closing board dispatch\n"); |
| 158 | connection->closeBoardDispatch(gameData->number); |
| 159 | gameData = 0; |
| 160 | } |
| 161 | } |
| 162 | //lets have closeBoardDispatch do this |
| 163 | //delete this; |
| 164 | } |
| 165 | |
| 166 | void BoardDispatch::setConnection(NetworkConnection *conn) |
| 167 | { |
| 168 | connection = conn; |
| 169 | /* This function is currently called if connection |
| 170 | *is closed when a board using that connection is open. |
| 171 | *Probably more has to be done here. */ |
| 172 | } |
| 173 | |
| 174 | void BoardDispatch::moveControl(QString & player) |
| 175 | { |
| 176 | boardwindow->qgoboard->moveControl(player); |
| 177 | } |
| 178 | |
| 179 | void BoardDispatch::recvMove(MoveRecord * m) |
| 180 | { |
| 181 | if(!boardwindow) |
| 182 | { |
| 183 | qDebug("Board dispatch has no board window\n"); |
| 184 | return; |
| 185 | } |
| 186 | if(m->flags == MoveRecord::SETMOVE || |
| 187 | m->flags == MoveRecord::BACKWARD || |
| 188 | m->flags == MoveRecord::FORWARD || |
| 189 | m->flags == MoveRecord::RESETBRANCH || |
| 190 | m->flags == MoveRecord::RESETGAME) |
| 191 | { |
| 192 | gameData->gameMode = modeReview; |
| 193 | } |
| 194 | boardwindow->qgoboard->handleMove(m); |
| 195 | } |
| 196 | |
| 197 | //FIXME should be another way to set the game moves |
| 198 | //related directly to the board... then again, I don't |
| 199 | //really want to access the tree from here |
| 200 | void BoardDispatch::sendMove(MoveRecord * m) |
| 201 | { |
| 202 | if(m->number && m->flags == MoveRecord::NONE) //i.e., a move |
| 203 | gameData->moves = m->number; |
| 204 | //qDebug("setting game moves: %d", gameData->moves); |
| 205 | if(connection) |
| 206 | connection->sendMove(gameData->number, m); |
| 207 | } |
| 208 | |
| 209 | /* FIXME |
| 210 | * note that after a request count is sent in tygem, |
| 211 | * NO other button can be hit until the response occurs, |
| 212 | * you can't even resign. |
| 213 | * Its also likely that you can only request one count |
| 214 | * per turn. |
| 215 | * But so this suggests maybe a lock buttons button. |
| 216 | * I don't like the placement of the request count |
| 217 | * button right now either. Also, done button means |
| 218 | * nothing. If it closes the window, that's one thing |
| 219 | * but otherwise, why have two buttons. done is for |
| 220 | * score, should be disabled afterwards. |
| 221 | * Also, closing windows is generally not allowed. It |
| 222 | * should always force a resign or prompt or maybe ask |
| 223 | * for an adjourn */ |
| 224 | void BoardDispatch::sendRequestCount(void) |
| 225 | { |
| 226 | if(connection) |
| 227 | { |
| 228 | stopTime(); //protocol specific or not? FIXME, huge issue if protocol does this to!!! see cyberoro killActiveMatchTimers, figure out who should do this when |
| 229 | boardwindow->setCountEnabled(false); |
| 230 | connection->sendRequestCount(gameData->number); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void BoardDispatch::sendRequestMatchMode(void) |
| 235 | { |
| 236 | if(connection) |
| 237 | { |
| 238 | stopTime(); //maybe?? |
| 239 | boardwindow->setUndoEnabled(false); |
| 240 | connection->sendRequestMatchMode(gameData->number); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void BoardDispatch::sendTimeLoss(void) |
| 245 | { |
| 246 | static bool already_sent = false; |
| 247 | if(connection && !already_sent) |
| 248 | connection->sendTimeLoss(gameData->number); |
| 249 | already_sent = true; |
| 250 | } |
| 251 | |
| 252 | void BoardDispatch::gameDataChanged(void) |
| 253 | { |
| 254 | if(!boardwindow) //this really shouldn't happen |
| 255 | return; |
| 256 | boardwindow->gameDataChanged(); |
| 257 | } |
| 258 | |
| 259 | void BoardDispatch::openBoard(void) |
| 260 | { |
| 261 | if(boardwindow) |
| 262 | { |
| 263 | qDebug("BoardDispatch::openBoard() : window already open\n"); |
| 264 | return; |
| 265 | } |
| 266 | if(!connection) |
| 267 | { |
| 268 | qDebug("BoardDispatch::openBoard() : connection not set\n"); |
| 269 | return; |
| 270 | } |
| 271 | mergeListingIntoRecord(gameData, gameListing); |
| 272 | |
| 273 | bool imWhite = false; |
| 274 | bool imBlack = false; |
| 275 | QString myName = connection->getUsername(); |
| 276 | if(gameData->gameMode == modeUndefined) |
| 277 | { |
| 278 | |
| 279 | imWhite = (gameData->white_name == myName); |
| 280 | imBlack = (gameData->black_name == myName); |
| 281 | if ( imWhite && imBlack ) |
| 282 | gameData->gameMode = modeTeach; |
| 283 | else if ( imWhite || imBlack) |
| 284 | gameData->gameMode = modeMatch; |
| 285 | else |
| 286 | gameData->gameMode = modeObserve; |
| 287 | } |
| 288 | //else, something else has set it ahead of time |
| 289 | |
| 290 | boardwindow = new BoardWindow(gameData, imBlack, imWhite, this); |
| 291 | if(!observerListModel) |
| 292 | { |
| 293 | observerListModel = new ObserverListModel(); |
| 294 | observerListModel->setAccountName(myName); |
| 295 | } |
| 296 | boardwindow->setObserverModel(observerListModel); |
| 297 | // do we need the below? |
| 298 | //boardwindow->qgoboard->set_statedMoveCount(gameData->moves); |
| 299 | boardwindow->gameDataChanged(); //necessary at least for cursor |
| 300 | if(gameData->record_sgf != QString()) //for ORO |
| 301 | boardwindow->getTree()->slotNavLast(); |
| 302 | } |
| 303 | |
| 304 | void BoardDispatch::recvTime(const TimeRecord & wt, const TimeRecord & bt) |
| 305 | { |
| 306 | /* I don't think I care if its not ongoing. That's a minor issue |
| 307 | * if we recvTime, we should set it */ |
| 308 | if(!boardwindow /*|| boardwindow->getGamePhase() != phaseOngoing*/) |
| 309 | return; |
| 310 | |
| 311 | boardwindow->getClockDisplay()->setTimeInfo(bt.time, |
| 312 | bt.stones_periods, |
| 313 | wt.time, |
| 314 | wt.stones_periods); |
| 315 | } |
| 316 | |
| 317 | void BoardDispatch::recvAddTime(int minutes, QString player_name) |
| 318 | { |
| 319 | if(gameData->white_name == player_name) |
| 320 | { |
| 321 | TimeRecord t = boardwindow->getClockDisplay()->getTimeRecord(false); |
| 322 | boardwindow->getClockDisplay()->setTimeInfo(0, -1, |
| 323 | t.time + (60 * minutes), |
| 324 | t.stones_periods); |
| 325 | } |
| 326 | else if(gameData->black_name == player_name) |
| 327 | { |
| 328 | TimeRecord t = boardwindow->getClockDisplay()->getTimeRecord(true); |
| 329 | boardwindow->getClockDisplay()->setTimeInfo(t.time + (60 * minutes), |
| 330 | t.stones_periods, 0, -1); |
| 331 | } |
| 332 | else |
| 333 | qDebug("Received add time for unknown player name %s", player_name.toLatin1().constData()); |
| 334 | } |
| 335 | |
| 336 | void BoardDispatch::recvResult(GameResult * r) |
| 337 | { |
| 338 | /* FIXME there's still some issues here with this being called from certain services |
| 339 | * at the wrong time, etc. */ |
| 340 | if(!boardwindow) |
| 341 | return; |
| 342 | if(!r) |
| 343 | { |
| 344 | GameResult res = boardwindow->getTree()->retrieveScore(); |
| 345 | r = &res; |
| 346 | } |
| 347 | // also set on GameData of boardwindow ??? |
| 348 | if(!gameData) |
| 349 | { |
| 350 | qDebug("No game record to check with result"); |
| 351 | return; |
| 352 | } |
| 353 | /* IGS passes sparse entries... bit ugly but: */ |
| 354 | if(r->winner_name == QString() && r->winner_color == stoneNone && r->result != GameResult::NOGAME) |
| 355 | { |
| 356 | if(r->loser_name == gameData->white_name) |
| 357 | { |
| 358 | r->winner_name = gameData->black_name; |
| 359 | r->winner_color = stoneBlack; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | r->winner_name = gameData->white_name; |
| 364 | r->winner_color = stoneWhite; |
| 365 | } |
| 366 | } |
| 367 | boardwindow->qgoboard->setResult(*r); |
| 368 | |
| 369 | //saveRecordToGameData(); |
| 370 | //testing |
| 371 | //connection->sendRematchRequest(gameData->number); |
| 372 | } |
| 373 | |
| 374 | /* Why doesn't this appear to save comments that we've seen in ORO games when rejoined?!? FIXME */ |
| 375 | void BoardDispatch::saveRecordToGameData(void) |
| 376 | { |
| 377 | if(gameData->record_sgf == QString()) |
| 378 | { |
| 379 | boardwindow->saveRecordToGameData(); //for ORO |
| 380 | connection->saveIfDoesntSave(gameData); //for ORO |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void BoardDispatch::sendResult(GameResult * r) |
| 385 | { |
| 386 | connection->sendResult(gameData, r); |
| 387 | } |
| 388 | |
| 389 | void BoardDispatch::recvObserver(PlayerListing * p, bool present) |
| 390 | { |
| 391 | if(!boardwindow) |
| 392 | return; |
| 393 | if(!gameData) |
| 394 | { |
| 395 | qDebug("received observer but no game data!!"); |
| 396 | return; |
| 397 | } |
| 398 | std::vector<unsigned short>::iterator i = std::find(p->room_list.begin(), p->room_list.end(), (unsigned short)gameData->number); |
| 399 | if(present) |
| 400 | { |
| 401 | if(p->room_list.end() == i) |
| 402 | p->room_list.push_back(gameData->number); |
| 403 | observerListModel->insertListing(p); |
| 404 | } |
| 405 | else if(i != p->room_list.end()) |
| 406 | { |
| 407 | p->room_list.erase(i); |
| 408 | observerListModel->removeListing(p); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void BoardDispatch::clearObservers(void) |
| 413 | { |
| 414 | if(!boardwindow) |
| 415 | return; |
| 416 | observerListModel->clearList(); |
| 417 | } |
| 418 | |
| 419 | void BoardDispatch::recvKibitz(QString name, QString text) |
| 420 | { |
| 421 | if(!boardwindow) |
| 422 | return; |
| 423 | if(name == QString()) |
| 424 | boardwindow->qgoboard->kibitzReceived(text); |
| 425 | else |
| 426 | boardwindow->qgoboard->kibitzReceived(name + ":" + text); |
| 427 | } |
| 428 | |
| 429 | void BoardDispatch::sendKibitz(QString text) |
| 430 | { |
| 431 | if(connection) |
| 432 | connection->sendMsg(gameData->number, text); |
| 433 | } |
| 434 | |
| 435 | /* FIXME: is this legitimate? doesn't qgoboard_network handle |
| 436 | * entering score mode within handleMove? why would we do it here? |
| 437 | * find out who calls this and why. Could be for special cases |
| 438 | * but... Okay... looks like everyone calls it... not sure |
| 439 | * what to say about that... might be redundant with set_move |
| 440 | * or perhaps we've been letting server tell us when to enter |
| 441 | * score mode and maybe set_move code should go*/ |
| 442 | void BoardDispatch::recvEnterScoreMode(void) |
| 443 | { |
| 444 | if(!boardwindow) |
| 445 | return; |
| 446 | boardwindow->qgoboard->enterScoreMode(); |
| 447 | } |
| 448 | |
| 449 | void BoardDispatch::recvLeaveScoreMode(void) |
| 450 | { |
| 451 | if(!boardwindow) |
| 452 | return; |
| 453 | startTime(); //protocol specific or not? |
| 454 | boardwindow->qgoboard->leaveScoreMode(); |
| 455 | } |
| 456 | |
| 457 | void BoardDispatch::recvRequestCount(void) |
| 458 | { |
| 459 | //this is those count messages before passes |
| 460 | //FIXME |
| 461 | //we need to copy the matchinvite file to a |
| 462 | //RequestCountDialog, etc. |
| 463 | if(boardwindow) |
| 464 | boardwindow->qgoboard->requestCountDialog(); |
| 465 | } |
| 466 | |
| 467 | void BoardDispatch::recvAcceptCountRequest(void) |
| 468 | { |
| 469 | recvEnterScoreMode(); |
| 470 | //is this all? |
| 471 | } |
| 472 | |
| 473 | void BoardDispatch::recvRejectCountRequest(void) |
| 474 | { |
| 475 | //so we can nag them endlessly: |
| 476 | //no, seriously, FIXME server protocol may have some limit |
| 477 | //on this |
| 478 | boardwindow->setCountEnabled(true); |
| 479 | if(boardwindow) |
| 480 | boardwindow->qgoboard->recvRefuseCount(); |
| 481 | } |
| 482 | |
| 483 | void BoardDispatch::recvRequestDraw(void) |
| 484 | { |
| 485 | if(boardwindow) |
| 486 | boardwindow->qgoboard->requestDrawDialog(); |
| 487 | } |
| 488 | |
| 489 | void BoardDispatch::recvAcceptDrawRequest(void) |
| 490 | { |
| 491 | //FIXME |
| 492 | //maybe we just draw? |
| 493 | } |
| 494 | |
| 495 | void BoardDispatch::recvRefuseDrawRequest(void) |
| 496 | { |
| 497 | //FIXME |
| 498 | startTime(); //protocol specific or not |
| 499 | if(boardwindow) |
| 500 | boardwindow->qgoboard->recvRefuseDraw(); |
| 501 | } |
| 502 | |
| 503 | void BoardDispatch::recvRequestMatchMode(void) |
| 504 | { |
| 505 | if(!boardwindow) |
| 506 | return; |
| 507 | boardwindow->qgoboard->requestMatchModeDialog(); |
| 508 | } |
| 509 | |
| 510 | void BoardDispatch::sendAcceptMatchModeRequest(void) |
| 511 | { |
| 512 | connection->sendAcceptRequestMatchMode(gameData->number); |
| 513 | } |
| 514 | |
| 515 | void BoardDispatch::sendRefuseMatchModeRequest(void) |
| 516 | { |
| 517 | startTime(); //protocol specific or not? |
| 518 | connection->sendDeclineRequestMatchMode(gameData->number); |
| 519 | } |
| 520 | |
| 521 | void BoardDispatch::recvRejectMatchModeRequest(void) |
| 522 | { |
| 523 | boardwindow->setUndoEnabled(true); |
| 524 | if(boardwindow) |
| 525 | boardwindow->qgoboard->recvRefuseMatchMode(); |
| 526 | } |
| 527 | |
| 528 | void BoardDispatch::createCountDialog(void) |
| 529 | { |
| 530 | // pretty sure we need to get the game result from the boardwindow |
| 531 | // without triggering an end game result dialog or something like |
| 532 | // that FIXME |
| 533 | countdialog = new CountDialog(boardwindow, this, gameData->number); |
| 534 | } |
| 535 | |
| 536 | void BoardDispatch::recvRejectCount(void) |
| 537 | { |
| 538 | if(countdialog) |
| 539 | countdialog->recvRejectCount(); |
| 540 | } |
| 541 | |
| 542 | void BoardDispatch::recvAcceptCount(void) |
| 543 | { |
| 544 | if(countdialog) |
| 545 | countdialog->recvAcceptCount(); |
| 546 | } |
| 547 | |
| 548 | void BoardDispatch::sendRejectCount(void) |
| 549 | { |
| 550 | connection->sendRejectCount(gameData); |
| 551 | } |
| 552 | |
| 553 | void BoardDispatch::sendAcceptCount(void) |
| 554 | { |
| 555 | connection->sendAcceptCount(gameData); |
| 556 | } |
| 557 | |
| 558 | void BoardDispatch::recvRequestAdjourn(void) |
| 559 | { |
| 560 | if(boardwindow) |
| 561 | boardwindow->qgoboard->requestAdjournDialog(); |
| 562 | } |
| 563 | |
| 564 | void BoardDispatch::sendAdjournRequest(void) |
| 565 | { |
| 566 | connection->sendAdjournRequest(); |
| 567 | } |
| 568 | |
| 569 | /* We either don't need this or it might overlap with some other message |
| 570 | * like an end game message */ |
| 571 | void BoardDispatch::sendAdjourn(void) |
| 572 | { |
| 573 | connection->sendAdjourn(); |
| 574 | } |
| 575 | |
| 576 | void BoardDispatch::sendRefuseAdjourn(void) |
| 577 | { |
| 578 | connection->sendRefuseAdjourn(); |
| 579 | } |
| 580 | |
| 581 | void BoardDispatch::sendAcceptCountRequest(void) |
| 582 | { |
| 583 | connection->sendAcceptCountRequest(gameData); |
| 584 | } |
| 585 | |
| 586 | void BoardDispatch::sendRefuseCountRequest(void) |
| 587 | { |
| 588 | startTime(); //protocol specific or not? |
| 589 | connection->sendRefuseCountRequest(gameData); |
| 590 | } |
| 591 | |
| 592 | void BoardDispatch::sendAcceptDrawRequest(void) |
| 593 | { |
| 594 | connection->sendAcceptDrawRequest(gameData); |
| 595 | } |
| 596 | |
| 597 | void BoardDispatch::sendRefuseDrawRequest(void) |
| 598 | { |
| 599 | connection->sendRefuseDrawRequest(gameData); |
| 600 | } |
| 601 | |
| 602 | void BoardDispatch::adjournGame(void) |
| 603 | { |
| 604 | // in case we get this after we've closed window |
| 605 | if(boardwindow) |
| 606 | boardwindow->qgoboard->adjournGame(); |
| 607 | //wrong, sometimes there is a dispatch if we want to talk, etc. |
| 608 | //let the close and the connection delete things |
| 609 | //delete this; //if the game is adjourned, there's no dispatch |
| 610 | } |
| 611 | |
| 612 | void BoardDispatch::setRematchDialog(ResultDialog * r) |
| 613 | { |
| 614 | resultdialog = r; |
| 615 | } |
| 616 | |
| 617 | void BoardDispatch::sendRematchRequest(void) |
| 618 | { |
| 619 | connection->sendRematchRequest(); |
| 620 | } |
| 621 | |
| 622 | void BoardDispatch::recvRematchRequest(void) //crash here somehow |
| 623 | { |
| 624 | if(resultdialog) |
| 625 | resultdialog->recvRematchRequest(); |
| 626 | else if(gameData) //incase we get this after recreating new dispatch somehow |
| 627 | resultdialog = new ResultDialog(boardwindow, this, gameData->number, 0); |
| 628 | else |
| 629 | qDebug("board dispatch received rematch but no gameData present"); |
| 630 | } |
| 631 | |
| 632 | void BoardDispatch::sendTime(void) |
| 633 | { |
| 634 | if(connection) |
| 635 | connection->sendTime(this); |
| 636 | } |
| 637 | |
| 638 | void BoardDispatch::sendAddTime(int minutes) |
| 639 | { |
| 640 | connection->sendAddTime(minutes); |
| 641 | } |
| 642 | |
| 643 | /* So far just to trigger timer */ |
| 644 | void BoardDispatch::startGame(void) |
| 645 | { |
| 646 | // Only used by Tygem connection, so assume that this is qGoBoardNetworkInterface::startGame(); |
| 647 | boardwindow->qgoboard->startGame(); |
| 648 | } |
| 649 | |
| 650 | void BoardDispatch::sendRematchAccept(void) |
| 651 | { |
| 652 | connection->sendRematchAccept(); |
| 653 | resultdialog = 0; |
| 654 | } |
| 655 | |
| 656 | void BoardDispatch::recvRefuseAdjourn(void) |
| 657 | { |
| 658 | // in case we get this after we've closed window |
| 659 | if(boardwindow) |
| 660 | boardwindow->qgoboard->recvRefuseAdjourn(); |
| 661 | } |
| 662 | |
| 663 | /* When we start observing a game, we get a little bit of information, |
| 664 | * the rest we have to get from a listing that the room dispatch passes |
| 665 | * us on creation. This could be trouble later because its specific |
| 666 | * to IGS, but maybe not */ |
| 667 | void BoardDispatch::mergeListingIntoRecord(GameData * r, GameListing * l) |
| 668 | { |
| 669 | /* IGS doesn't, for instance, serve up the board size very well, |
| 670 | * so we need to get that from somewhere else... the listing, |
| 671 | * but if its a new game... |
| 672 | * Basically only the game dialog knows the board size, so it |
| 673 | * has to supply it.*/ |
| 674 | /* I'm thinking that we should just have the gamedialog create |
| 675 | * the board. It should know the room, all the info, etc. */ |
| 676 | if(r->white_rank == QString()) |
| 677 | { |
| 678 | r->white_rank = l->white_rank(); |
| 679 | r->black_rank = l->black_rank(); |
| 680 | } |
| 681 | if(r->white_name == QString()) |
| 682 | { |
| 683 | r->white_name = l->white_name(); |
| 684 | r->black_name = l->black_name(); |
| 685 | } |
| 686 | /* FIXME Oro, second matches tend to lose the ranks. Add rematch |
| 687 | * and decline msgs and then fix this up. Also nigiri cursor */ |
| 688 | qDebug("bd::mlir %s %s vs %s %s", r->white_name.toLatin1().constData(), r->white_rank.toLatin1().constData(), |
| 689 | r->black_name.toLatin1().constData(), r->black_rank.toLatin1().constData()); |
| 690 | qDebug("komi: %f\n", r->komi); |
| 691 | /* FIXME, no komi in ORO listing... and |
| 692 | * what is this function for again? Maybe we shouldn't |
| 693 | * always do this?? FIXME FIXME */ |
| 694 | |
| 695 | /* FIXME we get here from IGS without white or black names somehow. |
| 696 | * also if game list has changed before refresh there's other issues */ |
| 697 | |
| 698 | /* FIXME if player observes IGS through console, the ranks can come up wrong |
| 699 | * possibly because the games list hasn't been refreshed and that thing above |
| 700 | * sets the ranks to what they are in the listing. They're not passed in the |
| 701 | * game info from the server. There's little that can be done about it, its |
| 702 | * more of a protocol bug. And I still want to get rid of this whole function */ |
| 703 | |
| 704 | //FIXME, trying not overwriting komi for now |
| 705 | //FIXME This is not okay, komi is apparently not passed all the time or with |
| 706 | //observed games on IGS, but we shouldn't have to do this: |
| 707 | if(connection->getPlaceString() == "IGS") |
| 708 | r->komi = l->komi; |
| 709 | //r->board_size = l->board_size; |
| 710 | r->moves = l->moves; |
| 711 | //r->handicap = l->handicap; |
| 712 | r->By = l->By; |
| 713 | r->FR = l->FR; |
| 714 | r->observers = l->observers; |
| 715 | |
| 716 | //GameData * gd = qgoif->makeGameData(gameData); //FIXME |
| 717 | if (gameData->FR.contains("F")) |
| 718 | gameData->free_rated = FREE; |
| 719 | else if (gameData->FR.contains("T")) |
| 720 | gameData->free_rated = TEACHING; |
| 721 | else |
| 722 | gameData->free_rated = RATED; |
| 723 | |
| 724 | gameData->date = QDate::currentDate().toString("dd MM yyyy") ; |
| 725 | } |
| 726 | |
| 727 | /* There's no "black komi" or I think we'd store it as a negative float. |
| 728 | * IGS lists them both the same though on the status messages which |
| 729 | * makes even less sense then the status message itself */ |
| 730 | bool BoardDispatch::isAttribBoard(QString black_player, unsigned int black_captures, float black_komi, QString white_player, unsigned int white_captures, float white_komi) |
| 731 | { |
| 732 | if(!gameData) |
| 733 | return 0; |
| 734 | qDebug("%s %s %s %s %d %d %d %d %f %f (b%f)\n", |
| 735 | black_player.toLatin1().constData(), |
| 736 | gameData->black_name.toLatin1().constData(), |
| 737 | white_player.toLatin1().constData(), |
| 738 | gameData->white_name.toLatin1().constData(), |
| 739 | black_captures, |
| 740 | gameData->black_prisoners, |
| 741 | white_captures, |
| 742 | gameData->white_prisoners, |
| 743 | white_komi, |
| 744 | gameData->komi, black_komi); |
| 745 | /* Dead stones are included in IGS message captures */ |
| 746 | if(black_player == gameData->black_name && |
| 747 | white_player == gameData->white_name && |
| 748 | /*black_captures == gameData->black_prisoners && |
| 749 | white_captures == gameData->white_prisoners &&*/ |
| 750 | white_komi == gameData->komi) |
| 751 | return 1; |
| 752 | else |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | bool BoardDispatch::isOpponentBoard(QString us, QString them) |
| 757 | { |
| 758 | if(!gameData) |
| 759 | return 0; |
| 760 | if((gameData->black_name == us && gameData->white_name == them) || (gameData->white_name == us && gameData->black_name == them)) |
| 761 | return 1; |
| 762 | else if(them == "" && (gameData->black_name == us || gameData->white_name == us)) |
| 763 | return 1; |
| 764 | else |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | void BoardDispatch::swapColors(bool noswap) |
| 769 | { |
| 770 | boardwindow->swapColors(noswap); |
| 771 | } |
| 772 | |
| 773 | void BoardDispatch::requestGameInfo(void) |
| 774 | { |
| 775 | connection->requestGameInfo(gameData->number); |
| 776 | } |
| 777 | |
| 778 | GameData * BoardDispatch::getGameData(void) |
| 779 | { |
| 780 | return gameData; |
| 781 | } |
| 782 | |
| 783 | /* We're getting this associated with sendKeepAlives and |
| 784 | * possibly rematches. We shouldn't need to check for |
| 785 | * boardwindow. FIXME */ |
| 786 | TimeRecord BoardDispatch::getOurTimeRecord(void) |
| 787 | { |
| 788 | if(boardwindow) |
| 789 | return boardwindow->qgoboard->getOurTimeRecord(); |
| 790 | else |
| 791 | return TimeRecord(); |
| 792 | } |
| 793 | |
| 794 | TimeRecord BoardDispatch::getTheirTimeRecord(void) |
| 795 | { |
| 796 | if(boardwindow) |
| 797 | return boardwindow->qgoboard->getTheirTimeRecord(); |
| 798 | else |
| 799 | return TimeRecord(); |
| 800 | } |
| 801 | |
| 802 | QString BoardDispatch::getOpponentName(void) |
| 803 | { |
| 804 | if(!gameData) |
| 805 | return QString(); |
| 806 | if(gameData->black_name == getUsername()) |
| 807 | return gameData->white_name; |
| 808 | else if(gameData->white_name == getUsername()) |
| 809 | return gameData->black_name; |
| 810 | else |
| 811 | return QString(); |
| 812 | } |
| 813 | |
| 814 | QString BoardDispatch::getUsername(void) { return connection->getUsername(); } |
| 815 | |
| 816 | QString BoardDispatch::getOurRank(void) |
| 817 | { |
| 818 | return connection->getOurListing()->rank; |
| 819 | } |
| 820 | |
| 821 | bool BoardDispatch::getBlackTurn(void) |
| 822 | { |
| 823 | if(boardwindow && boardwindow->qgoboard) |
| 824 | return boardwindow->qgoboard->getBlackTurn(); |
| 825 | return false; |
| 826 | } |
| 827 | |
| 828 | class ObserverListModel * BoardDispatch::getObserverListModelForRematch(void) |
| 829 | { |
| 830 | class ObserverListModel * olm = observerListModel; |
| 831 | observerListModel = 0; |
| 832 | return olm; |
| 833 | } |
| 834 | |
| 835 | void BoardDispatch::setObserverListModel(class ObserverListModel * olm) |
| 836 | { |
| 837 | if(observerListModel) |
| 838 | qDebug("warning: observer list model already set! overwriting"); |
| 839 | observerListModel = olm; |
| 840 | } |
| 841 | |
| 842 | bool BoardDispatch::supportsRematch(void) |
| 843 | { |
| 844 | /* A little tricky to have this check here but... */ |
| 845 | if(!gameData || gameData->gameMode != modeMatch) |
| 846 | return false; |
| 847 | if(connection) |
| 848 | return connection->supportsRematch(); |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | bool BoardDispatch::flipCoords(void) { return connection->flipCoords(); } |
| 853 | bool BoardDispatch::supportsRequestMatchMode(void) { return connection->supportsRequestMatchMode(); }; |
| 854 | bool BoardDispatch::supportsRequestCount(void) { return connection->supportsRequestCount(); }; |
| 855 | bool BoardDispatch::supportsRequestDraw(void) { return connection->supportsRequestDraw(); }; |
| 856 | bool BoardDispatch::supportsRequestAdjourn(void) { return connection->supportsRequestAdjourn(); }; |
| 857 | bool BoardDispatch::supportsAddTime(void) { return connection->supportsAddTime(); }; |
| 858 | bool BoardDispatch::undoResetsScore(void) { return connection->undoResetsScore(); } |
| 859 | bool BoardDispatch::canMarkStonesDeadinScore(void) { return connection->canMarkStonesDeadinScore(); } |