Debian Codegraph @

src/game_interfaces/qgoboard_network.cpp

raw symbols SCIP
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 "undoprompt.h"
29 #include "clockdisplay.h"
30 #include "gamedata.h"
31 #include "boardwindow.h"
32 #include "matrix.h"
33 #include <QMessageBox>
34 #include "networkconnection.h"
35 #include "audio.h"
36
37 qGoBoardNetworkInterface::qGoBoardNetworkInterface(BoardWindow *bw, Tree * t, GameData *gd) : qGoBoard(bw, t, gd)
38 {
39 game_Id = QString::number(gd->number);
40 bw->clearData();
41 dispatch = boardwindow->getBoardDispatch();
42 connection = dispatch->connection;
43
44 QSettings settings;
45 playSound = (settings.value("SOUND_MYGAMES").toBool() || settings.value("SOUND_OTHERGAMES").toBool());
46
47 if (gd->handicap)
48 {
49 setHandicap(gd->handicap);
50 }
51 dontsend = false;
52 boardTimerId = 0;
53 controlling_player = QString();
54 reviewCurrent = 0;
55 // what about review games? games without timers ??
56 }
57
58 /*
59 * This functions adds a move to a game. returns the new Move* if move was valid, 0 if not)
60 */
61 Move *qGoBoardNetworkInterface::doMove(StoneColor c, int x, int y)
62 {
63 bool validMove = (dontCheckValidity || tree->getCurrent()->checkMoveIsValid(c, x, y));
64 dontCheckValidity = false;
65 if (!validMove)
66 return NULL;
67
68 Move *result = tree->getCurrent()->makeMove(c,x,y,true);
69 tree->setCurrent(result);
70 tree->lastMoveInMainBranch = result;
71 setModified(true);
72
73
74 /* Not a great place for this, but maybe okay: */
75 TimeRecord t = boardwindow->getClockDisplay()->getTimeRecord(!getBlackTurn());
76 if(t.time != 0 || t.stones_periods != -1)
77 {
78 result->setTimeinfo(true);
79 result->setTimeLeft(t.time);
80 result->setOpenMoves(t.stones_periods);
81 }
82
83 /* Non trivial here. We don't want to play a sound as we get all
84 * the moves from an observed game. But there's no clean way
85 * to tell when the board has stopped loading, particularly for IGS.
86 * so we only play a sound every 250 msecs...
87 * Also, maybe it should play even if we aren't looking at last move, yeah not sure on that FIXME */
88 if(boardwindow->getGamePhase() == phaseOngoing && playSound)
89 {
90 clickSound->play();
91 }
92
93 return result;
94 }
95
96
97 void qGoBoardNetworkInterface::sendMoveToInterface(StoneColor c, int x, int y)
98 {
99 if(controlling_player != QString() && controlling_player != dispatch->getUsername())
100 return;
101 if(boardwindow->getGameData()->nigiriToBeSettled)
102 {
103 qDebug("Nigiri unsettled");
104 //return;
105 }
106 if(boardwindow->getGamePhase() == phaseScore && !dispatch->canMarkStonesDeadinScore())
107 return;
108 if(dontsend)
109 return;
110 // to prevent double clicking and upsetting servers...
111 dontsend = true;
112 if(boardwindow->getGamePhase() == phaseScore)
113 {
114 /* A little awkward... FIXME, why is there no getMyColor()
115 * function ?!?! */
116 //also "c" refers most likely to player color, not the stone clicked on
117 if(((tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneBlack && boardwindow->getMyColorIsWhite()) ||
118 (tree->getCurrent()->getMatrix()->getStoneAt(x, y) == stoneWhite && boardwindow->getMyColorIsBlack()))
119 && connection->cantMarkOppStonesDead())
120 {
121 dontsend = false; //ready to send again
122 return;
123 }
124 MoveRecord * m = new MoveRecord();
125 if(tree->getCurrent()->getMatrix()->isStoneDead(x, y))
126 {
127 if(connection->unmarkUnmarksAllDeadStones())
128 {
129 if (QMessageBox::question(boardwindow,tr("Unmark All?"),
130 QString(tr("Unmark all your dead stones?\n")))
131 == QMessageBox::No)
132 {
133 dontsend = false; //ready to send again
134 delete m;
135 return;
136 }
137 }
138 m->flags = MoveRecord::UNREMOVE;
139 }
140 else
141 m->flags = MoveRecord::REMOVE;
142 m->x = x;
143 m->y = y;
144 //move number shouldn't matter
145 m->color = c;
146 dispatch->sendMove(m);
147 delete m;
148 return;
149 }
150 else
151 {
152 /* Check validity of move before sending */
153 if(tree->getCurrent()->checkMoveIsValid(c, x, y))
154 dontCheckValidity = true;
155 else
156 {
157 QMessageBox::warning(boardwindow, tr("Invalid Move"), tr("Move %1 %2 is invalid").arg(QString::number(x), QString::number(y)));
158 dontsend = false; //okay to send again
159 return;
160 }
161 /* Rerack time before sending our move */
162 boardwindow->getClockDisplay()->makeMove(getBlackTurn());
163 dispatch->sendMove(new MoveRecord(
164 tree->getCurrent()->getMoveNumber(), x, y, c));
165 }
166 }
167
168 void qGoBoardNetworkInterface::handleMove(MoveRecord * m)
169 {
170 Move * const remember = tree->getCurrent();
171 Move * last = tree->findLastMoveInMainBranch();
172 // This handles territory marks provided by the server.
173 /* Separate handling is needed because we want to
174 * avoid GUI updates until the full information is available.
175 * The remaining part of the function should in principle also be rewritten in such a way that
176 * GUI updates are only made when needed. */
177 if (m->flags == MoveRecord::TERRITORY)
178 {
179 last->getMatrix()->insertMark(m->x,m->y,m->color == stoneBlack ? markTerrBlack : markTerrWhite);
180 return;
181 } else if (m->flags == MoveRecord::DONE_SCORING)
182 {
183 tree->countMarked(last);
184 boardwindow->updateMove(tree->getCurrent()); // Updates GUI
185 return;
186 }
187
188 int move_number, move_counter;
189 Move * goto_move;
190 //static bool offset_1 = false;
191 int i;
192
193 dontsend = false; //clear the dontsend bit
194
195 /* In case we join in score phase */
196 if(m->flags == MoveRecord::NONE && boardwindow->getGamePhase() == phaseScore)
197 m->flags = MoveRecord::REMOVE;
198
199 if(boardwindow->getGameData()->gameMode == modeReview)
200 {
201 if(!reviewCurrent)
202 reviewCurrent = last;
203 }
204 tree->setCurrent(last);
205
206 move_number = m->number;
207 //bool hcp_move = tree->getCurrent()->isHandicapMove();
208 move_counter = tree->getCurrent()->getMoveNumber();
209 if(move_number == NOMOVENUMBER) //not all services number
210 move_number = move_counter + 1; //since we add one to move counter later
211
212 //qDebug("MN: %d MC: %d", move_number, move_counter);
213
214 move_counter++;
215
216 switch(m->flags)
217 {
218 case MoveRecord::UNDO_TERRITORY:
219 {
220 int boardsize = last->getMatrix()->getSize();
221 for(int i = 1; i <= boardsize; i++)
222 {
223 for(int j = 1; j <= boardsize; j++)
224 {
225 if(last->getMatrix()->isStoneDead(i, j))
226 markLiveStone(i, j);
227 }
228 }
229 break;
230 }
231 case MoveRecord::REQUESTUNDO:
232 {
233 qDebug("Got undo message in network interface!!\n");
234 //move_number = mv_counter - 1;
235 QString opp = dispatch->getOpponentName();
236 UndoPrompt * up = new UndoPrompt(&opp,
237 connection->supportsMultipleUndo(),
238 m->number);
239
240 int up_return = up->exec();
241 if (up_return != -1)
242 {
243 dispatch->sendMove(new MoveRecord(m->number, MoveRecord::UNDO));
244 }
245 else
246 {
247 dispatch->sendMove(new MoveRecord(m->number, MoveRecord::REFUSEUNDO));
248 }
249 }
250 break;
251 case MoveRecord::UNDO:
252 {
253 /* Are we supposed to make a brother node or something ??? FIXME
254 * qgoboard.cpp also does this.*/
255 if(connection->supportsMultipleUndo())
256 {
257 while(move_counter > move_number + 1) //move_numbe r+ 1 if our turn?
258 {
259 tree->deleteNode();
260 move_counter--;
261 }
262 }
263 else
264 {
265 tree->deleteNode();
266 if ((getBlackTurn() && m->color == stoneBlack) ||
267 ((!getBlackTurn()) && m->color == stoneWhite))
268 {
269 tree->deleteNode();
270 }
271 }
272 /* I've turned off multiple undo for tygem, just for now...
273 * since NOMOVENUMBER FIXME, actually I'm not sure if tygem
274 * has a normal multiple undo, though it might for review games */
275 qDebug("Undoing move %d = %d - 1", move_number, move_counter);
276 /* FIXME This can get screwy especially around the scoreMode
277 * stuff.... apparently we can only undo our own passes
278 * unlike other WING moves and it only takes 2 undos, not
279 * 3... as if the first undo just gets you out of the
280 * score place. */
281 if(boardwindow->getGamePhase() == phaseScore)
282 {
283 /* Not sure if this is always true, but it appears
284 * that the last pass doesn't count as a move or
285 * something, meaning that we should delete twice
286 * for it. This is going to be a problem later
287 * FIXME FIXME FIXME*/
288 /* IGS does not leave score mode, ever !!!
289 * nor should anything else do to what scoremode
290 * does. Simply clears some of the dead marks*/
291 /* Actually, I'm not sure, its possible that the
292 * glGo client we've been using to test this
293 * doesn't handle undo from score very well. I've
294 * seen some bugs in it before. */
295 tree->deleteNode();
296 leaveScoreMode();
297 }
298 }
299 break;
300 case MoveRecord::PASS:
301 /* FIXME what about a kibitz here saying opponent name
302 * has passed. I think IGS needs it, not sure about
303 * other protocols which may have it, but if they do,
304 * it should be moved here. Otherwise easy to miss. */
305 /* If there's three passes, we should stop the clock,
306 * although some servers might have two pass models
307 * and we need a flag for that */
308 //if(!boardwindow->getMyColorIsBlack()) //if we're white
309 if(!connection->netWillEnterScoreMode()) //ugly for oro FIXME
310 {
311 if(connection->twoPassesEndsGame())
312 {
313 if(last->isPassMove())
314 enterScoreMode();
315 }
316 else
317 {
318 if(last->parent &&
319 last->isPassMove() &&
320 last->parent->isPassMove())
321 enterScoreMode();
322 //boardwindow->setGamePhase ( phaseScore ); //okay?
323 }
324 }
325 //FIXME potentially white/black here should come from color from network which is unreliable now
326 kibitzReceived(QString(getBlackTurn() ? "Black" : "White") + " passes.");
327 doPass();
328 break;
329 case MoveRecord::HANDICAP:
330 setHandicap(m->x);
331 break;
332 case MoveRecord::REMOVE:
333 //qDebug("md!! toggling life of %d %d", m->x, m->y);
334 markDeadStone(m->x, m->y);
335 //tree->getCurrent()->getMatrix()->toggleGroupAt(m->x, m->y);
336 //boardwindow->qgoboard->kibitzReceived("removing @ " + pt);
337 break;
338 case MoveRecord::REMOVE_AREA:
339 //FIXME
340 markDeadArea(m->x, m->y);
341 break;
342 case MoveRecord::UNREMOVE_AREA:
343 //FIXME
344 if(connection->unmarkUnmarksAllDeadStones())
345 {
346 /* Not sure where we get the dead groups from, FIXME
347 * okay, really, we should have a list of dead groups
348 * for each player that can be checked on here.
349 * The thing is, ORO also has such a list that it tracks
350 * and I don't want duplication of that code but right
351 * now I have other things on my mind and I just want to
352 * get this done, so I'm going to do something really
353 * quick, dirty, and awkward here and I'll fix it in
354 * a later version... ignoring the stitch in time
355 * thing. So cut this out soon. Note also that it
356 * might make sense to have an evaluation function
357 * in the board code, to do something to every
358 * stone of a type... maybe not.*/
359 int boardsize = last->getMatrix()->getSize();
360 for(int i = 1; i <= boardsize; i++)
361 {
362 for(int j = 1; j <= boardsize; j++)
363 {
364 if(last->getMatrix()->isStoneDead(i, j))
365 {
366 if(last->getMatrix()->getStoneAt(i, j) == m->color)
367 {
368 markLiveArea(i, j);
369 }
370 }
371 }
372 }
373
374 }
375 else
376 markLiveArea(m->x, m->y);
377 break;
378 case MoveRecord::REFUSEUNDO:
379 //handled by protocol as a recvKibitz for whatever reason
380 break;
381 case MoveRecord::FORWARD:
382 if(!dispatch->getReviewInVariation() && last->isInMainBranch())
383 {
384 /* In case it was in a variation previously to remove the marker.
385 * this should be elsewhere like on the setReviewInVariation(); FIXME */
386 last->marker = NULL;
387 }
388 for(i = 0; i < move_number; i++)
389 {
390 /*if(dispatch->getReviewInVariation() && tree->isInMainBranch(tree->getCurrent()))
391 {
392 boardwindow->getBoardHandler()->gotoMove(inVariationBranch);
393 }
394 else*/
395 tree->slotNavForward();
396 }
397 break;
398 case MoveRecord::BACKWARD:
399 for(i = 0; i < move_number; i++)
400 {
401 if(dispatch->getReviewInVariation() && last && last->isInMainBranch())
402 break;
403 tree->slotNavBackward();
404 }
405 break;
406 case MoveRecord::RESETBRANCH:
407 case MoveRecord::DELETEBRANCH:
408 goto_move = last;
409 if(goto_move->getMoveNumber() > lastMoveInGame->getMoveNumber())
410 goto_move = lastMoveInGame;
411 else
412 {
413 while(!(goto_move->isInMainBranch()))
414 goto_move = goto_move->parent;
415 }
416 tree->setCurrent(goto_move);
417 if(m->flags == MoveRecord::DELETEBRANCH) {}
418
419 break;
420 case MoveRecord::RESETGAME:
421 goto_move = tree->getRoot();
422 tree->setCurrent(goto_move);
423 break;
424 case MoveRecord::TOEND:
425 if(dispatch->getReviewInVariation())
426 goto_move = tree->findLastMoveInCurrentBranch();
427 else
428 goto_move = lastMoveInGame;
429 tree->setCurrent(goto_move);
430 break;
431 case MoveRecord::SETMOVE:
432 /* Might later want to record this
433 * also we should have already set the game to the last move and disabled nav*/
434 goto_move = tree->findNode(tree->getRoot(), m->number);
435
436 if (goto_move)
437 tree->setCurrent(goto_move);
438 else
439 {
440 QMessageBox::warning(boardwindow, tr("Invalid Move"), tr("Cannot set move to move number %1").arg(m->number));
441 }
442 break;
443 default:
444 case MoveRecord::NONE:
445
446 if(move_number == move_counter)
447 {
448 /* FIXME Can we guess at the color here ? */
449 if(m->color == stoneNone)
450 m->color = (getBlackTurn() ? stoneBlack : stoneWhite);
451 if (doMove(m->color, m->x, m->y) == NULL)
452 QMessageBox::warning(boardwindow, tr("Invalid Move"), tr("The incoming move %1 %2 seems to be invalid").arg(QString::number(m->x), QString::number(m->y)));
453 else if(m->color == stoneWhite && !boardTimerId) //awkward ? FIXME for always move 1?
454 {
455 onFirstMove();
456 }
457 }
458 else if(move_number < move_counter)
459 {
460 /* FIXME, this prevents the next if statement
461 * partly, this whole thing is screwy */
462 /* IGS, certain games have this remove a legit first
463 * move */
464 qDebug("Repeat move after undo?? %d %d", move_number, move_counter);
465 }
466 /* This is for resetting the stones for canadian
467 * timesystems, its awkward for it to be here... but
468 * I guess I'm getting lazy. FIXME */
469 /* I don't think this matters for ORO because time
470 * comes in right after move. It might matter for IGS */
471 /* For stones, this should be called before the move is sent or here
472 * but with getBlackTurn() negated. Otherwise, its like our move
473 * gets decremented possibly when they play. It looks weird... double
474 * check with other time style though. */
475 boardwindow->getClockDisplay()->rerackTime(getBlackTurn());
476 break;
477 }
478
479 if(boardwindow->getGameData()->gameMode == modeReview) //for now review mode means no navigation, see interfacehandler comment
480 reviewCurrent = tree->getCurrent();
481 else
482 {
483 if (remember != last)
484 {
485 tree->setCurrent(remember);
486 }
487 }
488 }
489
490 void qGoBoardNetworkInterface::passRequest()
491 {
492 /* doPass is called when we receive the move from the server,
493 * as with moves */
494 /* FIXME, we need to make sure the move is valid before we send it!!!!!
495 * but without playing it since we get that from server. */
496 if((getBlackTurn() && !(boardwindow->getMyColorIsBlack())) ||
497 (!getBlackTurn() && !(boardwindow->getMyColorIsWhite())))
498 {
499 return; // It is not our move
500 }
501
502 /* Rerack time before sending our move
503 * Is this okay here? Do we need to rerack for passes or what ? FIXME*/
504 boardwindow->getClockDisplay()->makeMove(getBlackTurn());
505 dispatch->sendMove(new MoveRecord(tree->getCurrent()->getMoveNumber(), MoveRecord::PASS));
506 }
507
508 void qGoBoardNetworkInterface::slotUndoPressed()
509 {
510 if(boardwindow->getGamePhase() == phaseScore)
511 {
512 if(connection->supportsRequestMatchMode())
513 {
514 if (QMessageBox::question(0,tr("Return to game?"),
515 QString(tr("Ask opponent to return to game?\n")))
516 == QMessageBox::Yes)
517 dispatch->sendRequestMatchMode();
518 return;
519 }
520 else if(dispatch->undoResetsScore())
521 {
522 dispatch->sendMove(new MoveRecord(-1, MoveRecord::UNDO));
523 return;
524 }
525 }
526 int moves = tree->getCurrent()->getMoveNumber();
527 //if its our turn - 2?
528 if ((getBlackTurn() && boardwindow->getMyColorIsBlack()) ||
529 ((!getBlackTurn()) && boardwindow->getMyColorIsWhite()))
530 moves -= 2;
531 else
532 moves--;
533 // might want to prompt anyway FIXME ?
534 if(connection->supportsMultipleUndo())
535 {
536 UndoPrompt * up = new UndoPrompt(0, true, moves);
537 int up_return = up->exec();
538 if(up_return == -1)
539 return;
540 else
541 moves = up_return;
542 }
543
544 dispatch->sendMove(new MoveRecord(moves, MoveRecord::REQUESTUNDO));
545 }
546 /* Note that these all crash if we disconnect from server first, but server should handle that anyway FIXME */
547
548 /* Really the ui button disables should be on some gamePhase code FIXME */
549 void qGoBoardNetworkInterface::slotDonePressed()
550 {
551 dispatch->sendMove(new MoveRecord(MoveRecord::DONE_SCORING));
552 boardwindow->setDoneEnabled(false); //FIXME okay? don't want to send done twice
553 }
554
555 void qGoBoardNetworkInterface::slotResignPressed()
556 {
557 if(dispatch->getOpponentName() == QString())
558 {
559 boardwindow->getGameData()->fullresult = new GameResult(); //temporary for bugs FIXME
560 return;
561 }
562 if (QMessageBox::question(boardwindow,tr("Resign?"),
563 QString(tr("Resign game with %1\n")).arg(dispatch->getOpponentName()))
564 == QMessageBox::Yes)
565 {
566 dispatch->sendMove(new MoveRecord(tree->getCurrent()->getMoveNumber(), MoveRecord::RESIGN));
567 boardwindow->setResignEnabled(false); //FIXME okay? don't want to send resign twice
568 }
569 }
570
571 void qGoBoardNetworkInterface::adjournGame(void)
572 {
573 QString opp_name = dispatch->getOpponentName();
574 boardwindow->setGamePhase(phaseEnded);
575 qDebug("qgBNI::adjournGame");
576 if(opp_name == QString())
577 {
578 GameData * r = dispatch->getGameData();
579 if(!r)
580 qDebug("No game record on adjourned game");
581 else
582 QMessageBox::information(boardwindow , tr("Game Adjourned"), tr("%1 vs. %2 has been adjourned.").arg(r->white_name).arg(r->black_name));
583
584 }
585 else
586 QMessageBox::information(boardwindow , tr("Game Adjourned"), tr("Game with %1 has been adjourned.").arg(opp_name));
587 boardwindow->getGameData()->fullresult = new GameResult(stoneNone, GameResult::ADJOURNED);
588 boardwindow->setAdjournEnabled(false); //FIXME okay? don't want to send adjourn after adjourn
589 }
590
591 /* Might look nicer if we just set the game phase to ended or
592 * something, I'll though we might just have lost connection so... */
593 void qGoBoardNetworkInterface::stopTime(void)
594 {
595 if(boardTimerId)
596 killTimer(boardTimerId);
597 }