| 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 <QtWidgets> |
| 24 | #include "../network/messages.h" |
| 25 | #include "countdialog.h" |
| 26 | #include "board/boardwindow.h" |
| 27 | #include "../network/boarddispatch.h" |
| 28 | #include "tree.h" |
| 29 | #include "qgoboard.h" |
| 30 | |
| 31 | CountDialog::CountDialog(BoardWindow * parent, BoardDispatch * dis, unsigned int game_id) |
| 32 | : QDialog((QDialog *)parent), dispatch(dis), board(parent) |
| 33 | { |
| 34 | oppAcceptsCount = 0; |
| 35 | oppRejectsCount = 0; |
| 36 | weAcceptCount = 0; |
| 37 | weRejectCount = 0; |
| 38 | |
| 39 | acceptButton = new QPushButton(tr("&Accept")); |
| 40 | //acceptButton->setDefault(true); |
| 41 | rejectButton = new QPushButton(tr("&Reject")); |
| 42 | |
| 43 | if(!dispatch) |
| 44 | { |
| 45 | qDebug("CountDialog called without board dispatch"); |
| 46 | deleteLater(); |
| 47 | return; |
| 48 | } |
| 49 | result = new GameResult(board->getTree()->retrieveScore()); |
| 50 | GameData * gamedata = board->getGameData(); |
| 51 | if(result->winner_color == stoneWhite) |
| 52 | { |
| 53 | result->winner_name = gamedata->white_name; |
| 54 | result->loser_name = gamedata->black_name; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | result->winner_name = gamedata->black_name; |
| 59 | result->loser_name = gamedata->white_name; |
| 60 | } |
| 61 | |
| 62 | mainlabel = new QLabel(result->longMessage() + |
| 63 | "\nWhite has " + QString::number(gamedata->white_prisoners) |
| 64 | + " captures\nBlack has " + |
| 65 | QString::number(gamedata->black_prisoners) + " captures"); |
| 66 | /* These needs to also post black and white captures and ultimate score |
| 67 | * FIXME */ |
| 68 | |
| 69 | buttonBox = new QDialogButtonBox(Qt::Horizontal); |
| 70 | buttonBox->addButton(acceptButton, QDialogButtonBox::ActionRole); |
| 71 | buttonBox->addButton(rejectButton, QDialogButtonBox::ActionRole); |
| 72 | |
| 73 | connect(acceptButton, &QPushButton::clicked, this, &CountDialog::slot_accept); |
| 74 | connect(rejectButton, &QPushButton::clicked, this, &CountDialog::slot_reject); |
| 75 | |
| 76 | QGridLayout * mainLayout = new QGridLayout; |
| 77 | /*mainLayout->setSizeConstraint(QLayout::SetFixedSize);*/ |
| 78 | mainLayout->addWidget(mainlabel, 0, 0); |
| 79 | mainLayout->addWidget(buttonBox, 1, 0); |
| 80 | setLayout(mainLayout); |
| 81 | |
| 82 | |
| 83 | setWindowTitle(QString::number(game_id) + tr(": Accept result?")); |
| 84 | //setSizePolicy(QSizePolicy::Minimum); |
| 85 | // FIXME size? |
| 86 | resize(200, 100); |
| 87 | //setMinimumSize(QSize(300, 150)); |
| 88 | show(); |
| 89 | } |
| 90 | |
| 91 | CountDialog::~CountDialog() |
| 92 | { |
| 93 | dispatch->clearCountDialog(); |
| 94 | delete acceptButton; |
| 95 | delete rejectButton; |
| 96 | delete mainlabel; |
| 97 | delete buttonBox; |
| 98 | delete result; |
| 99 | } |
| 100 | |
| 101 | void CountDialog::recvRejectCount() |
| 102 | { |
| 103 | oppRejectsCount = 1; |
| 104 | if(weRejectCount || weAcceptCount) |
| 105 | { |
| 106 | /* Regardless of what happens, I think |
| 107 | * we have to restart the game, get back into it |
| 108 | * this may require sending clear stone messages |
| 109 | * or some other messsage and it definitely |
| 110 | * requires leaving score mode and starting |
| 111 | * any timers that may have stopped in addition |
| 112 | * to possibly deleting "pass" moves from the tree |
| 113 | * FIXME */ |
| 114 | board->qgoboard->leaveScoreMode(); |
| 115 | deleteLater(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void CountDialog::recvAcceptCount() |
| 120 | { |
| 121 | oppAcceptsCount = 1; |
| 122 | if(weRejectCount) |
| 123 | { |
| 124 | /* See above comment FIXME */ |
| 125 | board->qgoboard->leaveScoreMode(); |
| 126 | deleteLater(); |
| 127 | } |
| 128 | else if(weAcceptCount) |
| 129 | { |
| 130 | /* This means we notify network connection |
| 131 | * to send any necessary game result messages FIXME */ |
| 132 | dispatch->recvResult(result); |
| 133 | dispatch->sendResult(result); |
| 134 | //board->qgoboard->setResult(*result); |
| 135 | deleteLater(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* We're not going to delete the countdialog immediately |
| 140 | * but instead let it keep track of things... I don't |
| 141 | * really know why... I guess because this is a dialog |
| 142 | * for counting and its light so why not have it do |
| 143 | * something besides displaying info and a couple |
| 144 | * buttons */ |
| 145 | void CountDialog::slot_accept(void) |
| 146 | { |
| 147 | weAcceptCount = 1; |
| 148 | dispatch->sendAcceptCount(); |
| 149 | hide(); |
| 150 | if(oppRejectsCount) |
| 151 | { |
| 152 | /* See above comment FIXME */ |
| 153 | board->qgoboard->leaveScoreMode(); |
| 154 | deleteLater(); |
| 155 | } |
| 156 | else if(oppAcceptsCount) |
| 157 | { |
| 158 | /* See above comment FIXME */ |
| 159 | dispatch->recvResult(result); |
| 160 | dispatch->sendResult(result); |
| 161 | //board->qgoboard->setResult(*result); |
| 162 | deleteLater(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void CountDialog::slot_reject(void) |
| 167 | { |
| 168 | weRejectCount = 1; |
| 169 | dispatch->sendRejectCount(); |
| 170 | hide(); |
| 171 | if(oppRejectsCount || oppAcceptsCount) |
| 172 | { |
| 173 | /* See above comment FIXME */ |
| 174 | board->qgoboard->leaveScoreMode(); |
| 175 | deleteLater(); |
| 176 | } |
| 177 | } |