| 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 <QtCore> |
| 24 | #include "matchnegotiationstate.h" |
| 25 | #include "messages.h" |
| 26 | #include "gamedata.h" |
| 27 | |
| 28 | bool MatchNegotiationState::newMatchAllowed(void) |
| 29 | { |
| 30 | if(state == MSNONE) |
| 31 | return true; |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | //adjourned rematch |
| 36 | void MatchNegotiationState::setupRematchAdjourned(unsigned short id, QString opponent_name) |
| 37 | { |
| 38 | game_number = id; |
| 39 | opponent = opponent_name; |
| 40 | state = MSREMATCHADJOURNED; |
| 41 | } |
| 42 | |
| 43 | bool MatchNegotiationState::canEnterRematchAdjourned(void) |
| 44 | { |
| 45 | if(state == MSREMATCHADJOURNED) |
| 46 | return true; |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | bool MatchNegotiationState::inGame(void) |
| 51 | { |
| 52 | if(state == MSSTARTMATCH || state == MSONGOINGMATCH) |
| 53 | return true; |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | bool MatchNegotiationState::isOurGame(unsigned short id) |
| 58 | { |
| 59 | if(id == 0) |
| 60 | return false; |
| 61 | if(id == game_number) |
| 62 | return true; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | bool MatchNegotiationState::sentMatchInvite(void) |
| 67 | { |
| 68 | if(state == MSINVITE) |
| 69 | return true; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool MatchNegotiationState::sentMatchOfferPending(void) |
| 74 | { |
| 75 | if(state == MSMATCHOFFERPENDING) |
| 76 | return true; |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool MatchNegotiationState::justCreatedRoom(void) |
| 81 | { |
| 82 | if(state == MSCREATEDROOM) |
| 83 | return true; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | bool MatchNegotiationState::waitingForRoomNumber(void) |
| 88 | { |
| 89 | if(state == MSACCEPTINVITE) |
| 90 | return true; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | bool MatchNegotiationState::waitingForMatchOffer(void) |
| 95 | { |
| 96 | if(state == MSJOINEDROOM) |
| 97 | return true; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | bool MatchNegotiationState::sentMatchOffer(void) |
| 102 | { |
| 103 | if(state== MSMATCHOFFER || state == MSMATCHMODIFY) //doublecheck MODIFY fixme |
| 104 | return true; |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | bool MatchNegotiationState::startMatchAcceptable(void) |
| 109 | { |
| 110 | if(state == MSMATCHACCEPT || state == MSMATCHFINISHED || state == MSSENTADJOURNRESUME) |
| 111 | return true; |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool MatchNegotiationState::isOngoingMatch(unsigned short id) |
| 116 | { |
| 117 | if(!isOurGame(id)) |
| 118 | return false; |
| 119 | if(state != MSONGOINGMATCH) |
| 120 | return false; |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | bool MatchNegotiationState::isOngoingMatch(void) |
| 125 | { |
| 126 | if(state == MSONGOINGMATCH) |
| 127 | return true; |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool MatchNegotiationState::sentCreateRoom(void) |
| 132 | { |
| 133 | if(state == MSSENTCREATEROOM) |
| 134 | return true; |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | bool MatchNegotiationState::twoPasses(void) |
| 139 | { |
| 140 | if(state == MSTWOPASS) |
| 141 | return true; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | bool MatchNegotiationState::sentDoneCounting(void) |
| 146 | { |
| 147 | if(state == MSSENTDONECOUNTING) |
| 148 | return true; |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | bool MatchNegotiationState::receivedDoneCounting(void) |
| 153 | { |
| 154 | if(state == MSRECEIVEDDONECOUNTING) |
| 155 | return true; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | bool MatchNegotiationState::counting(void) |
| 160 | { |
| 161 | if(state == MSCOUNTING) |
| 162 | return true; |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool MatchNegotiationState::doneCounting(void) |
| 167 | { |
| 168 | if(state == MSDONECOUNTING) |
| 169 | return true; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | bool MatchNegotiationState::sentRematch(void) |
| 174 | { |
| 175 | if(state == MSSENTREMATCH) |
| 176 | return true; |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool MatchNegotiationState::sentRematchAccept(void) |
| 181 | { |
| 182 | if(state == MSSENTREMATCHACCEPT) |
| 183 | return true; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool MatchNegotiationState::sentAdjournResume(void) |
| 188 | { |
| 189 | if(state == MSSENTADJOURNRESUME) |
| 190 | return true; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | bool MatchNegotiationState::opponentDisconnected(void) |
| 195 | { |
| 196 | if(state == MSOPPONENTDISCONNECT) |
| 197 | return true; |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | bool MatchNegotiationState::opponentRejoining(void) |
| 202 | { |
| 203 | if(state == MSOPPONENTREJOINING) |
| 204 | return true; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | void MatchNegotiationState::reset(void) |
| 209 | { |
| 210 | player = 0; |
| 211 | game_number = 0; |
| 212 | opponent = QString(); |
| 213 | state = MSNONE; |
| 214 | } |
| 215 | |
| 216 | void MatchNegotiationState::sendMatchInvite(PlayerListing * p) |
| 217 | { |
| 218 | player = p; |
| 219 | state = MSINVITE; |
| 220 | } |
| 221 | |
| 222 | void MatchNegotiationState::sendMatchAccept(PlayerListing * p) |
| 223 | { |
| 224 | player = p; |
| 225 | state = MSACCEPTINVITE; |
| 226 | } |
| 227 | |
| 228 | void MatchNegotiationState::sendMatchOfferPending(void) |
| 229 | { |
| 230 | state = MSMATCHOFFERPENDING; |
| 231 | } |
| 232 | |
| 233 | void MatchNegotiationState::sendCreateRoom(void) |
| 234 | { |
| 235 | state = MSSENTCREATEROOM; |
| 236 | } |
| 237 | |
| 238 | void MatchNegotiationState::createdRoom(unsigned short id) |
| 239 | { |
| 240 | game_number = id; |
| 241 | state = MSCREATEDROOM; |
| 242 | } |
| 243 | |
| 244 | void MatchNegotiationState::sendJoinRoom(unsigned short id) |
| 245 | { |
| 246 | game_number = id; |
| 247 | state = MSJOINEDROOM; |
| 248 | } |
| 249 | |
| 250 | void MatchNegotiationState::offerMatchTerms(MatchRequest * mr) |
| 251 | { |
| 252 | state = MSMATCHOFFER; |
| 253 | match_request = new MatchRequest(*mr); |
| 254 | } |
| 255 | |
| 256 | void MatchNegotiationState::modifyMatchTerms(MatchRequest * mr) |
| 257 | { |
| 258 | state = MSMATCHMODIFY; |
| 259 | match_request = new MatchRequest(*mr); |
| 260 | } |
| 261 | |
| 262 | void MatchNegotiationState::acceptMatchTerms(MatchRequest * mr) |
| 263 | { |
| 264 | state = MSMATCHACCEPT; |
| 265 | match_request = new MatchRequest(*mr); |
| 266 | } |
| 267 | |
| 268 | void MatchNegotiationState::startMatch(void) |
| 269 | { |
| 270 | state = MSONGOINGMATCH; |
| 271 | } |
| 272 | |
| 273 | void MatchNegotiationState::incrementPasses(void) |
| 274 | { |
| 275 | if(state == MSONGOINGMATCH) |
| 276 | state = MSONEPASS; |
| 277 | else if(state == MSONEPASS) |
| 278 | state = MSTWOPASS; |
| 279 | else if(state == MSTWOPASS) |
| 280 | state = MSTHREEPASS; |
| 281 | //error |
| 282 | } |
| 283 | |
| 284 | void MatchNegotiationState::enterScoreMode(void) |
| 285 | { |
| 286 | state = MSCOUNTING; |
| 287 | } |
| 288 | |
| 289 | void MatchNegotiationState::sendDoneCounting(void) |
| 290 | { |
| 291 | state = MSSENTDONECOUNTING; |
| 292 | } |
| 293 | |
| 294 | void MatchNegotiationState::receiveDoneCounting(void) |
| 295 | { |
| 296 | state = MSRECEIVEDDONECOUNTING; |
| 297 | } |
| 298 | |
| 299 | void MatchNegotiationState::setDoneCounting(void) |
| 300 | { |
| 301 | state = MSDONECOUNTING; |
| 302 | } |
| 303 | |
| 304 | void MatchNegotiationState::sendMatchModeRequest(void) |
| 305 | { |
| 306 | state = MSMATCHMODEREQUEST; |
| 307 | } |
| 308 | |
| 309 | void MatchNegotiationState::sendRematch(PlayerListing * p) |
| 310 | { |
| 311 | state = MSSENTREMATCH; |
| 312 | player = p; |
| 313 | } |
| 314 | |
| 315 | void MatchNegotiationState::sendRematchAccept(void) |
| 316 | { |
| 317 | state = MSSENTREMATCHACCEPT; |
| 318 | } |
| 319 | |
| 320 | void MatchNegotiationState::opponentDisconnect(void) |
| 321 | { |
| 322 | state = MSOPPONENTDISCONNECT; |
| 323 | } |
| 324 | |
| 325 | void MatchNegotiationState::sendAdjournResume(void) |
| 326 | { |
| 327 | state = MSSENTADJOURNRESUME; |
| 328 | } |
| 329 | |
| 330 | void MatchNegotiationState::opponentRejoins(void) |
| 331 | { |
| 332 | state = MSOPPONENTREJOINING; |
| 333 | } |
| 334 | |
| 335 | void MatchNegotiationState::opponentReconnect(void) |
| 336 | { |
| 337 | state = MSONGOINGMATCH; //FIXME |
| 338 | } |
| 339 | |
| 340 | void MatchNegotiationState::swapColors(void) |
| 341 | { |
| 342 | if(match_request->color_request == MatchRequest::WHITE) |
| 343 | match_request->color_request = MatchRequest::BLACK; |
| 344 | else if(match_request->color_request == MatchRequest::BLACK) |
| 345 | match_request->color_request = MatchRequest::WHITE; |
| 346 | } |
| 347 | |
| 348 | bool MatchNegotiationState::verifyPlayer(PlayerListing * p) |
| 349 | { |
| 350 | if(p == player) |
| 351 | return true; |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | bool MatchNegotiationState::verifyMatchRequest(MatchRequest & mr) |
| 356 | { |
| 357 | if(!match_request) |
| 358 | return false; //shouldn't be called here |
| 359 | if(match_request->stones_periods != mr.stones_periods || |
| 360 | match_request->periodtime != mr.periodtime || |
| 361 | match_request->maintime != mr.maintime || |
| 362 | match_request->komi != mr.komi || |
| 363 | match_request->timeSystem != mr.timeSystem || |
| 364 | match_request->handicap != mr.handicap || |
| 365 | match_request->color_request != mr.color_request) |
| 366 | return false; |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | bool MatchNegotiationState::verifyGameData(GameData & g) |
| 371 | { |
| 372 | if(!match_request) |
| 373 | return false; //shouldn't be called here |
| 374 | if(match_request->stones_periods != g.stones_periods || |
| 375 | match_request->periodtime != g.periodtime || |
| 376 | match_request->maintime != g.maintime || |
| 377 | match_request->komi != g.komi || |
| 378 | match_request->timeSystem != g.timeSystem || |
| 379 | (match_request->handicap != g.handicap && (match_request->handicap != 1 || (g.handicap != 0 && g.handicap != 1))))/* || |
| 380 | match_request->color_request != g.color_request)*/ //FIXME |
| 381 | return false; |
| 382 | return true; |
| 383 | } |
| 384 | |
| 385 | bool MatchNegotiationState::verifyCountDoneMessage(unsigned short v) |
| 386 | { |
| 387 | if(v == counting_verification) |
| 388 | return true; |
| 389 | return false; |
| 390 | } |