| 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 <string.h> |
| 24 | #include <stdint.h> |
| 25 | #include <time.h> |
| 26 | #include <algorithm> |
| 27 | #include "tygemconnection.h" |
| 28 | #include "tygemprotocol.h" |
| 29 | #include "consoledispatch.h" |
| 30 | #include "room.h" |
| 31 | #include "boarddispatch.h" |
| 32 | #include "gamedialog.h" |
| 33 | #include "talk.h" |
| 34 | #include "serverlistdialog.h" |
| 35 | #include "codecwarndialog.h" |
| 36 | #include "matchinvitedialog.h" |
| 37 | #include "gamedialogflags.h" |
| 38 | //FIXME set phrases would be useful... |
| 39 | //#include "orosetphrasechat.h" |
| 40 | //#include "setphrasepalette.h" |
| 41 | #include "createroomdialog.h" |
| 42 | #include "gamedata.h" |
| 43 | #include "playergamelistings.h" |
| 44 | #include "matchnegotiationstate.h" |
| 45 | #include <QMessageBox> |
| 46 | |
| 47 | //#define RE_DEBUG |
| 48 | |
| 49 | unsigned char zodiac_byte; |
| 50 | /* I now think you have to buy avatar icons at the store, |
| 51 | * so we're not going to mess with this */ |
| 52 | /* 57 is our zodiac sign |
| 53 | 01 is rat, 02 is bull, 03 is tiger, 04 is rabbit |
| 54 | 05 is dragon, 06 is snake, 07 is horse |
| 55 | 08 is sheep (ram? goat?), 09 is monkey |
| 56 | 0a is rooster, 0b is dog, 0c is pig |
| 57 | |
| 58 | 0d is mercury like chibi figure with golden shield |
| 59 | 0e is red warrior palace chibi |
| 60 | 0f is wild west gunman on horse |
| 61 | 10 is chibi in snow sweeping |
| 62 | 11 red warrior bamboo forest |
| 63 | 12 calligrapher |
| 64 | 13 red haired bureaucrat, purple cap? |
| 65 | 14 red haired bureacrat, scroll, white cap |
| 66 | 15 guy with shield on battlefield? |
| 67 | 16 two blade zhong warrior night time balcony |
| 68 | 17 same with no zhong, bigger blades |
| 69 | 24 man running with bunny |
| 70 | 30 sports uniform kid with korean flag |
| 71 | 34 child spinning soccer ball on finger |
| 72 | 35 small girl playing go on computer |
| 73 | 40 soccer kid |
| 74 | 43 woman reading book with headphones |
| 75 | 50 purple mouse, bowtie, cheese on platter |
| 76 | 55 snake around yellow stone |
| 77 | 56 horse |
| 78 | 57 giant chicken |
| 79 | 58 strange blue purple dog with bone around neck |
| 80 | 59 childs drawing of pig in love |
| 81 | 5a woman walking dog |
| 82 | 5b ballet dancer stretching |
| 83 | 5c woman on balcony looking out over ocean |
| 84 | 5d girl blue flowers in hair, blue dress |
| 85 | 5e 31 gangster on dark blue background |
| 86 | 5f pig tailed beach girl |
| 87 | 60 blackhaired girl rainbow very blue |
| 88 | 61 ghetto guy against graffiti wall |
| 89 | 62 green haired boxing girl on sun like background |
| 90 | 63 frightened cloaked guy holding skull |
| 91 | 64 fierce blonde pigtailed girl on blue sky |
| 92 | 65 dominatrix infront of night time castle |
| 93 | 66 pigtailed brunette princess in red and gold background |
| 94 | 67 studying girl |
| 95 | 68 strong guy saying something, korean letters, blue star hat |
| 96 | 69 woman purple sheath pink background |
| 97 | 6a black haired girl black dress, roses |
| 98 | 6b pink girl holding valentine at night with moon |
| 99 | 7e ninja nighttime, gold crown |
| 100 | 7f japanese warlord |
| 101 | 80 new tygem congratulations, white smiling figure, gold badge |
| 102 | 81 same with black smirking figure |
| 103 | 82 same with out gold badge |
| 104 | 90 green haired happy girl under night sky smiling moon? |
| 105 | 91 grey pig eating or happy at table with food |
| 106 | 92 2 kids in love |
| 107 | 93 bowl of orange fruit |
| 108 | 94 night life guy with flowers |
| 109 | 95 night life business lady? |
| 110 | 96 blonde smiling guy showbiz stars |
| 111 | 97 korean infinity girl picture |
| 112 | 98 white creature with brown hat, fall leaves |
| 113 | 99 white creatures riding dolphins |
| 114 | 9a white and black creatures playing game |
| 115 | a0 thin santa claus, merry christmas |
| 116 | b0 animated bouncing girl on pink hearts |
| 117 | c0 korean letters, boiling chicken, and dancing chickens |
| 118 | d0 scarecrow golden field |
| 119 | e0 no picture |
| 120 | FIXME */ |
| 121 | |
| 122 | TygemConnection::TygemConnection(const ConnectionCredentials credentials) |
| 123 | : NetworkConnection(credentials) |
| 124 | { |
| 125 | textCodec = QTextCodec::codecForLocale(); |
| 126 | serverCodec = QTextCodec::codecForLocale(); |
| 127 | //FIXME check for validity of codecs ??? |
| 128 | //ORO_setup_setphrases(); |
| 129 | |
| 130 | http_connect_content_length = 0; |
| 131 | current_server_index = -1; |
| 132 | encode_offset = 0; |
| 133 | |
| 134 | matchKeepAliveTimerID = 0; |
| 135 | matchRequestKeepAliveTimerID = 0; |
| 136 | retryLoginTimerID = 0; |
| 137 | opponentDisconnectTimerID = 0; |
| 138 | |
| 139 | /* We should either create the palette on creation of |
| 140 | * the connection, or have a button to activate it that |
| 141 | * appears when the room is connected */ |
| 142 | //setphrasepalette = new SetPhrasePalette(this); |
| 143 | //setphrasepalette->show(); |
| 144 | |
| 145 | //for server list |
| 146 | //error handling FIXME |
| 147 | if(connectionType == TypeTYGEM) |
| 148 | { |
| 149 | serverCodec = QTextCodec::codecForName(getCodecString()); |
| 150 | if(!serverCodec) |
| 151 | { |
| 152 | new CodecWarnDialog(getCodecString()); |
| 153 | serverCodec = QTextCodec::codecForLocale(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | requestServerInfo(); |
| 158 | } |
| 159 | |
| 160 | /* I think we must not request this too often which is why hitting cancel and |
| 161 | * trying to reconnect screws things up. So I'm thinking we need to store |
| 162 | * this some where for at least the three tygem protocols and possibly oro |
| 163 | * as well. retrieve on application start, gray out the tabs if we can't |
| 164 | * get the info. */ |
| 165 | int TygemConnection::requestServerInfo(void) |
| 166 | { |
| 167 | qDebug("Requesting Tygem Server Info"); |
| 168 | if(!openConnection(hostname, port, NOT_MAIN_CONNECTION)) |
| 169 | { |
| 170 | qDebug("Can't get server info"); |
| 171 | return -1; |
| 172 | } |
| 173 | unsigned int length = 0x96; |
| 174 | unsigned char * packet = new unsigned char[length]; //term 0x00 |
| 175 | snprintf((char *)packet, length, |
| 176 | "GET /service/down_korea/livebaduk3.cfg HTTP/1.1\r\n" \ |
| 177 | "User-Agent: Tygem HTTP\r\n" \ |
| 178 | "Host: service.tygem.com\r\n" \ |
| 179 | "Connection: Keep-Alive\r\n" \ |
| 180 | "Cache-Control: no-cache\r\n\r\n"); |
| 181 | #ifdef RE_DEBUG |
| 182 | for(unsigned int i = 0; i < length; i++) |
| 183 | printf("%02x", packet[i]); |
| 184 | printf("\n"); |
| 185 | #endif //RE_DEBUG |
| 186 | if(write((const char *)packet, length) < 0) |
| 187 | { |
| 188 | qWarning("*** failed sending server info request"); |
| 189 | return -1; |
| 190 | } |
| 191 | delete[] packet; |
| 192 | |
| 193 | setState(Info); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | TygemConnection::~TygemConnection() |
| 198 | { |
| 199 | //if(setphrasepalette) |
| 200 | // delete setphrasepalette; |
| 201 | // FIXME for non tygem subclasses! |
| 202 | qDebug("Destroying Tygem connection"); |
| 203 | closeConnection(); |
| 204 | //maybe backup serverList at this point |
| 205 | } |
| 206 | |
| 207 | void TygemConnection::OnConnected() |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | void TygemConnection::sendDisconnect(void) |
| 212 | { |
| 213 | /* Yeah, FIXME, we don't want to send this |
| 214 | * when we're reconnecting... which means we |
| 215 | * need a special connection state ... yada yada */ |
| 216 | if(connectionState == Connected) |
| 217 | sendDisconnectMsg(); |
| 218 | } |
| 219 | |
| 220 | void TygemConnection::sendText(const char * text) |
| 221 | { |
| 222 | qDebug("Wanted to send %s", text); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | void TygemConnection::sendText(QString text) |
| 227 | { |
| 228 | sendServerChat(text); |
| 229 | } |
| 230 | |
| 231 | void TygemConnection::sendMsg(PlayerListing * player, QString text) |
| 232 | { |
| 233 | Talk * t = getDefaultRoom()->getIfTalk(player); |
| 234 | if(!t) |
| 235 | { |
| 236 | qDebug("sendMsg called with no talk dialog"); |
| 237 | return; |
| 238 | } |
| 239 | if(!t->getConversationOpened()) |
| 240 | { |
| 241 | sendOpenConversation(player); |
| 242 | pendingConversationMsg[player] = text; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | sendConversationMsg(player, text.toLatin1().constData()); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void TygemConnection::sendToggle(const QString & param, bool val) |
| 251 | { |
| 252 | /* FIXME, more to do here probably */ |
| 253 | qDebug("Setting open %d", val); |
| 254 | if(param == "open") |
| 255 | sendInvitationSettings(val); |
| 256 | } |
| 257 | |
| 258 | /* If we can observe something besides games, |
| 259 | * like rooms or people, then this name is a bit |
| 260 | * iffy too... */ |
| 261 | void TygemConnection::sendObserve(const GameListing *game) |
| 262 | { |
| 263 | sendJoin(game->number); |
| 264 | } |
| 265 | |
| 266 | void TygemConnection::sendJoin(unsigned short game_number) |
| 267 | { |
| 268 | unsigned int length = 24; |
| 269 | char *packet = new char[length]; |
| 270 | int i; |
| 271 | if(getBoardDispatches() == 3) |
| 272 | { |
| 273 | //legit client won't let you watch more than 3 games |
| 274 | QMessageBox::information(0, tr("3 Boards Open"), tr("You must close a board before you can open another one")); |
| 275 | //message box, FIXME |
| 276 | //it is allowed but probably taxes server, as server sets |
| 277 | //time, etc... and who watches more than three games at a time... |
| 278 | //besides people testing hacked together clients? |
| 279 | return; |
| 280 | } |
| 281 | /* We need to turn this to a join if its a room, not a game |
| 282 | * otherwise bad things happen FIXME */ |
| 283 | |
| 284 | packet[0] = 0x00; |
| 285 | packet[1] = 0x18; |
| 286 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 287 | packet[3] = 0x38; |
| 288 | packet[4] = (game_number >> 8); |
| 289 | packet[5] = game_number & 0x00ff; |
| 290 | packet[6] = 0x00; |
| 291 | packet[7] = 0x00; |
| 292 | for(i = 8; i < 0x18; i++) |
| 293 | packet[i] = 0x00; |
| 294 | #ifdef RE_DEBUG |
| 295 | //printf("Sending observe to %s vs %s: %d", game.white_name().toLatin1().constData(), game.black_name().toLatin1().constData(), game.game_code); |
| 296 | printf("Sending observe to %d\n", game_number); |
| 297 | for(i = 0; i < (int)length; i++) |
| 298 | printf("%02x ", (unsigned char)packet[i]); |
| 299 | printf("\n"); |
| 300 | #endif //RE_DEBUG |
| 301 | encode((unsigned char *)packet, (length / 4) - 2); |
| 302 | #ifdef RE_DEBUG |
| 303 | printf("After encode\n"); |
| 304 | for(i = 0; i < (int)length; i++) |
| 305 | printf("%02x ", (unsigned char)packet[i]); |
| 306 | printf("\n"); |
| 307 | #endif //RE_DEBUG |
| 308 | if(write((const char *)packet, length) < 0) |
| 309 | qWarning("*** failed sending observe outside"); |
| 310 | delete[] packet; |
| 311 | } |
| 312 | |
| 313 | /* This is sent after sendJoin when we've been disconnected. |
| 314 | * Presumably its to resume the game */ |
| 315 | void TygemConnection::sendResume(unsigned short game_number) |
| 316 | { |
| 317 | unsigned int length = 12; |
| 318 | char * packet = new char[length]; |
| 319 | int i; |
| 320 | |
| 321 | packet[0] = 0x00; |
| 322 | packet[1] = 0x0c; |
| 323 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 324 | packet[3] = 0x86; |
| 325 | packet[4] = (game_number >> 8); |
| 326 | packet[5] = game_number & 0x00ff; |
| 327 | for(i = 6; i < 12; i++) |
| 328 | packet[i] = 0x00; |
| 329 | #ifdef RE_DEBUG |
| 330 | printf("Sending resume %d", game_number); |
| 331 | for(i = 0; i < (int)length; i++) |
| 332 | printf("%02x ", (unsigned char)packet[i]); |
| 333 | printf("\n"); |
| 334 | #endif //RE_DEBUG |
| 335 | encode((unsigned char *)packet, (length / 4) - 2); |
| 336 | #ifdef RE_DEBUG |
| 337 | printf("After encode\n"); |
| 338 | for(i = 0; i < (int)length; i++) |
| 339 | printf("%02x ", (unsigned char)packet[i]); |
| 340 | printf("\n"); |
| 341 | #endif //RE_DEBUG |
| 342 | if(write((const char *)packet, length) < 0) |
| 343 | qWarning("*** failed sending resume"); |
| 344 | delete[] packet; |
| 345 | match_negotiation_state->sendAdjournResume(); |
| 346 | } |
| 347 | |
| 348 | void TygemConnection::closeTalk(PlayerListing * opponent) |
| 349 | { |
| 350 | Talk * t = getDefaultRoom()->getIfTalk(opponent); |
| 351 | if(!t) |
| 352 | { |
| 353 | qDebug("closing talk but no talk registered!!"); |
| 354 | return; |
| 355 | } |
| 356 | if(t->getConversationOpened()) |
| 357 | sendCloseConversation(opponent); |
| 358 | getDefaultRoom()->closeTalk(opponent); |
| 359 | } |
| 360 | |
| 361 | void TygemConnection::closeBoardDispatch(unsigned int game_id) |
| 362 | { |
| 363 | sendFinishObserving((unsigned short)game_id); |
| 364 | NetworkConnection::closeBoardDispatch(game_id); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* I'd like to just send the match invite, but only if we're not |
| 369 | * in the same room... actually, maybe its almost like a room invite */ |
| 370 | void TygemConnection::sendMatchRequest(MatchRequest * mr) |
| 371 | { |
| 372 | #ifdef RE_DEBUG |
| 373 | qDebug("send match offer"); |
| 374 | #endif //RE_DEBUG |
| 375 | if(mr->first_offer) |
| 376 | { |
| 377 | sendMatchOffer(*mr, offer); |
| 378 | mr->first_offer = false; |
| 379 | } |
| 380 | else |
| 381 | sendMatchOffer(*mr, modify); |
| 382 | } |
| 383 | |
| 384 | /* FIXME gamedialog maintime supports m:ss, not h:mm:ss, |
| 385 | * for some reason 90 minutes is getting set as 50 minutes |
| 386 | * certainly not an hour and a half, but its sent okay. |
| 387 | * but this is a BIG ISSUE for later, okay 5 hours screws |
| 388 | * things up even more.*/ |
| 389 | unsigned long TygemConnection::getGameDialogFlags(void) |
| 390 | { |
| 391 | /* I think there is CANADIAN, but haven't seen it or implemented it yet */ |
| 392 | return (/*GDF_CANADIAN | */GDF_BYOYOMI | /*GDF_TVASIA |*/ GDF_FREE_RATED_CHOICE |
| 393 | | GDF_RATED_SIZE_FIXED | GDF_RATED_HANDICAP_FIXED |
| 394 | | GDF_RATED_NO_HANDICAP |
| 395 | | GDF_HANDICAP1 |
| 396 | | GDF_NIGIRI_EVEN /*| GDF_ONLY_DISPUTE_TIME*/ |
| 397 | | GDF_BY_CAN_MAIN_MIN); |
| 398 | } |
| 399 | |
| 400 | const char * TygemConnection::getCodecString(void) |
| 401 | { |
| 402 | return "eucKR"; |
| 403 | } |
| 404 | |
| 405 | QString TygemConnection::getPlaceString(void) |
| 406 | { |
| 407 | return "Tygem: " + serverList[current_server_index]->name; |
| 408 | } |
| 409 | |
| 410 | int TygemConnection::gd_verifyBoardSize(int v) |
| 411 | { |
| 412 | return 19; |
| 413 | /* If game is ranked, has to be 19x19 but right now |
| 414 | * game dialog has an old flag that sets that up */ |
| 415 | if(v < 5) |
| 416 | return 5; |
| 417 | if(v > 13) |
| 418 | return 19; |
| 419 | if(v > 9) |
| 420 | return 13; |
| 421 | if(v > 7) |
| 422 | return 9; |
| 423 | if(v > 5) |
| 424 | return 7; |
| 425 | return v; |
| 426 | } |
| 427 | |
| 428 | QTime TygemConnection::gd_checkMainTime(TimeSystem s, const QTime & t) |
| 429 | { |
| 430 | int seconds_options[] = {0, 60, 300, 600, 900, 1200, 1800, 2400, 3000, 3600, 5400, 7200, |
| 431 | 10800, 18000, 28800, 54600 }; |
| 432 | int seconds = (t.hour() * 3600) + (t.minute() * 60) + t.second(); |
| 433 | bool increase; |
| 434 | int i, hours, minutes; |
| 435 | printf("maintime: %d %d", seconds, lastMainTimeChecked); |
| 436 | if(lastMainTimeChecked == seconds) |
| 437 | return t; |
| 438 | if(seconds < lastMainTimeChecked) |
| 439 | increase = false; |
| 440 | else |
| 441 | increase = true; |
| 442 | switch(s) |
| 443 | { |
| 444 | default: |
| 445 | case byoyomi: |
| 446 | //it looks like there's an unlimited time |
| 447 | //option, its a dropdown menu... |
| 448 | if(increase) |
| 449 | { |
| 450 | i = 15; |
| 451 | while(seconds < seconds_options[i]) |
| 452 | i--; |
| 453 | if(i != 15) |
| 454 | i++; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | i = 1; |
| 459 | while(seconds >= seconds_options[i]) |
| 460 | { |
| 461 | i++; |
| 462 | if(i == 16) |
| 463 | break; |
| 464 | } |
| 465 | i--; |
| 466 | } |
| 467 | seconds = seconds_options[i]; |
| 468 | lastMainTimeChecked = seconds; |
| 469 | minutes = seconds % 3600; |
| 470 | seconds -= minutes; |
| 471 | hours = seconds / 3600; |
| 472 | seconds = minutes % 60; |
| 473 | minutes -= seconds; |
| 474 | minutes /= 60; |
| 475 | return QTime(hours, minutes, seconds); |
| 476 | break; |
| 477 | case canadian: |
| 478 | //haven't seen yet FIXME |
| 479 | break; |
| 480 | } |
| 481 | return t; |
| 482 | } |
| 483 | |
| 484 | QTime TygemConnection::gd_checkPeriodTime(TimeSystem s, const QTime & t) |
| 485 | { |
| 486 | int seconds_options[] = {20, 30, 40, 60}; |
| 487 | int seconds = (t.minute() * 60) + t.second(); |
| 488 | bool increase; |
| 489 | int i, minutes; |
| 490 | printf("periodtime: %d %d", seconds, lastPeriodTimeChecked); |
| 491 | if(lastPeriodTimeChecked == seconds) |
| 492 | return t; |
| 493 | if(seconds < lastPeriodTimeChecked) |
| 494 | increase = false; |
| 495 | else |
| 496 | increase = true; |
| 497 | if(lastPeriodTimeChecked == 60 && seconds == 0) //awkward |
| 498 | { |
| 499 | lastPeriodTimeChecked = 40; |
| 500 | return QTime(0, 0, 40); |
| 501 | } |
| 502 | switch(s) |
| 503 | { |
| 504 | default: |
| 505 | case byoyomi: |
| 506 | if(increase) |
| 507 | { |
| 508 | i = 3; |
| 509 | while(seconds < seconds_options[i]) |
| 510 | { |
| 511 | i--; |
| 512 | if(i == -1) |
| 513 | break; |
| 514 | } |
| 515 | if(i != 3) |
| 516 | i++; |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | i = 1; |
| 521 | while(seconds >= seconds_options[i]) |
| 522 | { |
| 523 | i++; |
| 524 | if(i == 4) |
| 525 | break; |
| 526 | } |
| 527 | i--; |
| 528 | } |
| 529 | seconds = seconds_options[i]; //what this isn't called?!?! |
| 530 | lastPeriodTimeChecked = seconds; |
| 531 | minutes = seconds; |
| 532 | seconds = minutes % 60; |
| 533 | minutes -= seconds; |
| 534 | minutes /= 60; |
| 535 | return QTime(0, minutes, seconds); |
| 536 | break; |
| 537 | case canadian: |
| 538 | //haven't seen FIXME |
| 539 | break; |
| 540 | } |
| 541 | return t; |
| 542 | } |
| 543 | |
| 544 | unsigned int TygemConnection::gd_checkPeriods(TimeSystem s, unsigned int p) |
| 545 | { |
| 546 | unsigned int newp; |
| 547 | if(lastPeriodsChecked == (signed)p) |
| 548 | return p; |
| 549 | switch(s) |
| 550 | { |
| 551 | default: |
| 552 | case byoyomi: |
| 553 | if(p == 0) |
| 554 | newp = 1; |
| 555 | else if(lastPeriodsChecked > 2 && p < 3) |
| 556 | newp = 1; |
| 557 | else if((lastPeriodsChecked < 3 && p > 1) || (lastPeriodsChecked > 4 && p < 5)) |
| 558 | newp = 3; |
| 559 | else |
| 560 | newp = 5; |
| 561 | break; |
| 562 | case canadian: |
| 563 | //haven't seen yet FIXME |
| 564 | break; |
| 565 | } |
| 566 | lastPeriodsChecked = newp; |
| 567 | return newp; |
| 568 | } |
| 569 | |
| 570 | void TygemConnection::declineMatchOffer(const PlayerListing * opponent) |
| 571 | { |
| 572 | qDebug("Declining match offer"); |
| 573 | unsigned short playing_game_number = match_negotiation_state->getGameId(); |
| 574 | if(!playing_game_number) |
| 575 | return; |
| 576 | GameDialog * gameDialogDispatch = getIfGameDialog(opponent); |
| 577 | if(!gameDialogDispatch) |
| 578 | { |
| 579 | qDebug("No game dialog but just got decline!"); |
| 580 | return; |
| 581 | } |
| 582 | MatchRequest * mr = gameDialogDispatch->getMatchRequest(); |
| 583 | BoardDispatch * boarddispatch = getIfBoardDispatch(playing_game_number); |
| 584 | if(boarddispatch) |
| 585 | { |
| 586 | /* Awkward but to prevent issues on board close */ |
| 587 | GameResult g; |
| 588 | g.result = GameResult::NOGAME; |
| 589 | boarddispatch->recvResult(&g); |
| 590 | } |
| 591 | sendMatchOffer(*mr, decline); |
| 592 | } |
| 593 | |
| 594 | void TygemConnection::cancelMatchOffer(const PlayerListing * opponent) |
| 595 | { |
| 596 | qDebug("Canceling match offer"); |
| 597 | declineMatchOffer(opponent); |
| 598 | } |
| 599 | |
| 600 | void TygemConnection::acceptMatchOffer(const PlayerListing * opponent, MatchRequest * mr) |
| 601 | { |
| 602 | qDebug("accept match offer"); |
| 603 | if(!mr->opponent_is_challenger) //should be our_invitation var name maybe |
| 604 | { |
| 605 | getAndCloseGameDialog(opponent); |
| 606 | sendStartGame(*mr); |
| 607 | } |
| 608 | else |
| 609 | sendMatchOffer(*mr, accept); |
| 610 | } |
| 611 | |
| 612 | void TygemConnection::handlePendingData() |
| 613 | { |
| 614 | unsigned char * c; |
| 615 | unsigned char header[4]; |
| 616 | unsigned int packet_size; |
| 617 | int bytes; |
| 618 | |
| 619 | /* We need better connection states, but we'll use the old |
| 620 | * for now */ |
| 621 | |
| 622 | switch(connectionState) |
| 623 | { |
| 624 | case Info: |
| 625 | bytes = qsocket->bytesAvailable(); |
| 626 | if(http_connect_content_length > 0) |
| 627 | { |
| 628 | if(bytes >= (int)http_connect_content_length) // There might be some unwanted trailing characters |
| 629 | { |
| 630 | c = new unsigned char[http_connect_content_length + 1]; |
| 631 | qsocket->read((char *)c, http_connect_content_length); |
| 632 | handleServerInfo(c, http_connect_content_length); |
| 633 | delete[] c; |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | if(qsocket->canReadLine()) |
| 638 | { |
| 639 | QByteArray data = qsocket->readLine(); |
| 640 | sscanf(data.constData(), "Content-Length: %d", &http_connect_content_length); |
| 641 | } |
| 642 | break; |
| 643 | case Login: |
| 644 | bytes = qsocket->bytesAvailable(); |
| 645 | if(bytes) |
| 646 | { |
| 647 | c = new unsigned char[bytes]; |
| 648 | qsocket->read((char *)c, bytes); |
| 649 | #ifdef RE_DEBUG |
| 650 | for(int i = 0; i < bytes; i++) |
| 651 | printf("%02x", c[i]); |
| 652 | printf("\n"); |
| 653 | #endif //RE_DEBUG |
| 654 | /* First packet is list of servers and ips */ |
| 655 | //handleServerList(c); |
| 656 | |
| 657 | //00080698ff040068 //password incorrect |
| 658 | if(c[1] == 0x08) |
| 659 | { |
| 660 | //00080698ff010065 //maybe because too recent? |
| 661 | //could also be already |
| 662 | //logged in |
| 663 | //00080698ff040013 //error on reconnect... |
| 664 | //00080698ff060065 //room full |
| 665 | //0008069801000000 //everything okay? |
| 666 | if(c[5] == 0x04/*c[7] == 0x68*/ || c[5] == 0x03) //00080698ff040065 //(03 is Tom) |
| 667 | { |
| 668 | //0008 1398 ff03 0000 tom bad password |
| 669 | if(c[7] == 0x00 && connectionType != TypeTOM) |
| 670 | { |
| 671 | //this might be okay for eweiqi and tygem |
| 672 | //on change server, probably not okay for tom |
| 673 | // Totally wrong FIXME |
| 674 | qDebug("Trying again..."); |
| 675 | retryLoginTimerID = startTimer(2000); |
| 676 | delete[] c; |
| 677 | return; |
| 678 | } |
| 679 | //00080698ff040000 tom returns from already logged in |
| 680 | qDebug("Bad password or login"); |
| 681 | setState(PassFailed); |
| 682 | delete[] c; |
| 683 | //closeConnection(); |
| 684 | return; |
| 685 | } |
| 686 | else if(c[5] == 0x06) |
| 687 | { |
| 688 | qDebug("Server full"); |
| 689 | QMessageBox::information(0, tr("Server full"), tr("Server full, try another")); |
| 690 | if(reconnectToServer() < 0) |
| 691 | { |
| 692 | qDebug("User canceled"); |
| 693 | setState(Canceled); |
| 694 | delete[] c; |
| 695 | return; |
| 696 | } |
| 697 | delete[] c; |
| 698 | return; |
| 699 | } |
| 700 | else if(c[5] == 0x01) //c5 = 4? |
| 701 | { |
| 702 | if(c[7] == 0x72 || c[7] == 0x00) //00 is tom |
| 703 | { |
| 704 | //for eweiqi we get this on server change... |
| 705 | //so this seems to work better without |
| 706 | //connectionState = ALREADY_LOGGED_IN; |
| 707 | //delete[] c; |
| 708 | //return; |
| 709 | } |
| 710 | //we seem to get 01 65 every time |
| 711 | //we change servers which makes me |
| 712 | //think it might be like an already |
| 713 | //logged in message... then again, |
| 714 | //if we don't get the encode offset, |
| 715 | //we can't continue |
| 716 | //special response |
| 717 | qDebug("Already logged in? Too recent?"); |
| 718 | fflush(stdout); |
| 719 | //00080698ff010072 |
| 720 | retryLoginTimerID = startTimer(2000); |
| 721 | /* FIXME its real ugly if this happens |
| 722 | * more than once and we hammer the |
| 723 | * server until it lets us in. We need |
| 724 | * to sleep or put out a message or |
| 725 | * something. */ |
| 726 | /* There's a chance that 0x38 vs 0x08 |
| 727 | * isn't the only distinction here in |
| 728 | * which case, maybe we ought to |
| 729 | * try "handleLogin"? even from 0x08? |
| 730 | * requires testing to see what gets |
| 731 | * us cut off */ |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | qDebug("Received %02x %02x from login", c[5], c[7]); |
| 736 | } |
| 737 | //00080698ff010000, this is after changing servers on TOM |
| 738 | //00080698ff010072, changing ervers tygem |
| 739 | |
| 740 | } |
| 741 | else if(c[1] == 0x38) |
| 742 | handleLogin(c, bytes); |
| 743 | |
| 744 | delete[] c; |
| 745 | } |
| 746 | break; |
| 747 | case Setup: |
| 748 | case Connected: |
| 749 | while((bytes = qsocket->bytesAvailable())) |
| 750 | { |
| 751 | qsocket->peek((char *)header, 4); |
| 752 | #ifdef RE_DEBUG |
| 753 | #ifdef NOISY_DEBUG |
| 754 | printf("%02x%02x%02x%02x\n", header[0], header[1], header[2], header[3]); |
| 755 | #endif //NOISY_DEBUG |
| 756 | #endif //RE_DEBUG |
| 757 | packet_size = header[1] + (header[0] << 8); |
| 758 | if((unsigned)bytes < packet_size) |
| 759 | break; |
| 760 | c = new unsigned char[packet_size + 1]; |
| 761 | qsocket->read((char *)c, packet_size); |
| 762 | handleMessage(c, packet_size); |
| 763 | delete[] c; |
| 764 | } |
| 765 | break; |
| 766 | case Canceled: |
| 767 | case Reconnecting: |
| 768 | break; |
| 769 | /*case AUTH_FAILED: |
| 770 | qDebug("Auth failed\n"); |
| 771 | break;*/ |
| 772 | default: |
| 773 | qDebug("connection state not handled by Tygem type protocol"); |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* Really we should have a legitimate parse function, but I feel |
| 779 | * like this is the only time we're going to do this. |
| 780 | * Yeah, FIXME, I've decided we should write a parser just because... |
| 781 | * handleServerInfo?? not handleServerList?? anyway, we don't need both*/ |
| 782 | void TygemConnection::handleServerInfo(unsigned char * msg, unsigned int length) |
| 783 | { |
| 784 | char * p = (char *)msg; |
| 785 | int i, j; |
| 786 | ServerItem * si; |
| 787 | |
| 788 | #ifdef RE_DEBUG |
| 789 | /*for(i = 0; i < length; i++) |
| 790 | printf("%c", p[i]); |
| 791 | printf("\n");*/ |
| 792 | #endif //RE_DEBUG |
| 793 | |
| 794 | /* We're having the connection handle the http header, nevertheless |
| 795 | * we should check something for it */ |
| 796 | /*if(strncmp(p, "HTTP/1.1 200 OK\r\n", 17) != 0) |
| 797 | { |
| 798 | qDebug("Server info response not OK!"); |
| 799 | closeConnection(); |
| 800 | return; |
| 801 | }*/ |
| 802 | while(p < ((char *)msg + length)) |
| 803 | { |
| 804 | while(p < ((char *)msg + length - 10) && (p[0] != '\r' || p[1] != '\n')) |
| 805 | p++; |
| 806 | p += 2; |
| 807 | //FIXME, check for size |
| 808 | if(strncmp(p, "/LIVE ", 6) == 0) |
| 809 | { |
| 810 | //same as for /SERVER but with only 2 [], name and ip |
| 811 | } |
| 812 | else if(strncmp(p, "/LECTURE ", 9) == 0) |
| 813 | { |
| 814 | //same as for /SERVER but with only 2 [], name and ip |
| 815 | } |
| 816 | else if(strncmp(p, "/SERVER ", 8) == 0 || |
| 817 | strncmp(p, "/_SERVER ", 9) == 0) //whats the "_" FIXME |
| 818 | { |
| 819 | if(p[1] == '_') |
| 820 | p++; |
| 821 | p += 8; |
| 822 | unsigned char ipaddr[16]; |
| 823 | unsigned char name[20]; |
| 824 | i = 0; |
| 825 | while(i < 7 && p < ((char *)msg + length)) |
| 826 | { |
| 827 | if(*p != '[') |
| 828 | { |
| 829 | qDebug("Server info parse error"); |
| 830 | setState(ProtocolError); |
| 831 | closeConnection(); |
| 832 | return; |
| 833 | } |
| 834 | p++; |
| 835 | si = new ServerItem(); |
| 836 | unsigned char * dest; |
| 837 | if(i == 1) |
| 838 | dest = ipaddr; |
| 839 | else if(i == 0) |
| 840 | dest = name; |
| 841 | j = 0; |
| 842 | while(*p != ']' && p < ((char *)msg + length)) |
| 843 | { |
| 844 | if((i == 1 && j < 15) || (i == 0 && j < 19)) |
| 845 | dest[j++] = *p; |
| 846 | p++; |
| 847 | } |
| 848 | if(i == 0 || i == 1) |
| 849 | { |
| 850 | dest[j++] = 0x00; |
| 851 | } |
| 852 | p += 2; |
| 853 | if(i == 1) |
| 854 | { |
| 855 | strncpy(si->ipaddress, (const char *)ipaddr, 16); |
| 856 | si->name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 857 | qDebug("Server: %s", ipaddr); |
| 858 | serverList.push_back(si); |
| 859 | } |
| 860 | i++; |
| 861 | if(i == 7) |
| 862 | p--; //no space |
| 863 | } |
| 864 | } |
| 865 | else if(strncmp(p, "/ENCRYPT ", 9) == 0) |
| 866 | { |
| 867 | //FIXME |
| 868 | //[encode] [on] |
| 869 | } |
| 870 | } |
| 871 | /* We close here because this first time, its going to close |
| 872 | * anyway, and if we don't close here, we'll get an error |
| 873 | * and lose the object */ |
| 874 | setState(Reconnecting); |
| 875 | closeConnection(false); |
| 876 | |
| 877 | if(reconnectToServer() < 0) |
| 878 | { |
| 879 | qDebug("User canceled"); |
| 880 | setState(Canceled); |
| 881 | return; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void TygemConnection::sendLogin(bool response_bit, bool change_server) |
| 886 | { |
| 887 | unsigned int length = 0x2c; |
| 888 | unsigned char * packet = new unsigned char[length]; |
| 889 | unsigned int i; |
| 890 | packet[0] = (length >> 8); |
| 891 | packet[1] = length & 0x00ff; |
| 892 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 893 | packet[3] = 0xa5; |
| 894 | for(i = 0; i < (unsigned)username.length(); i++) |
| 895 | packet[i + 4] = username.toLatin1().data()[i]; |
| 896 | for(i = username.length() + 4; i < 16; i++) |
| 897 | packet[i] = 0x00; |
| 898 | for(i = 16; i < 20; i++) |
| 899 | packet[i] = 0x00; |
| 900 | packet[19] = response_bit; |
| 901 | packet[20] = change_server; |
| 902 | for(i = 21; i < 24; i++) |
| 903 | packet[i] = 0x00; |
| 904 | for(i = 0; i < (unsigned)password.length(); i++) |
| 905 | packet[24 + i] = password.toLatin1().data()[i]; |
| 906 | for(i = 24 + password.length(); i < length; i++) |
| 907 | packet[i] = 0x00; |
| 908 | //before encode |
| 909 | #ifdef RE_DEBUG |
| 910 | printf("Sending login: "); |
| 911 | for(i = 0; i < length; i++) |
| 912 | printf("%02x", packet[i]); |
| 913 | printf("\n"); |
| 914 | #endif //RE_DEBUG |
| 915 | encode_offset = 0; //clear for new server |
| 916 | encode(packet, (length / 4) - 2); |
| 917 | //after encode |
| 918 | #ifdef RE_DEBUG |
| 919 | for(i = 0; i < length; i++) |
| 920 | printf("%02x", packet[i]); |
| 921 | printf("\n"); |
| 922 | #endif //RE_DEBUG |
| 923 | |
| 924 | if(write((const char *)packet, length) < 0) |
| 925 | qWarning("*** failed sending login"); |
| 926 | delete[] packet; |
| 927 | } |
| 928 | |
| 929 | void TygemConnection::handleLogin(unsigned char * msg, unsigned int length) |
| 930 | { |
| 931 | if(length != 0x38) |
| 932 | { |
| 933 | qDebug("Login of strange size: %d", length); |
| 934 | } |
| 935 | unsigned char * p = msg; |
| 936 | |
| 937 | //0038 0698 0100 0200 228b ae64 0000 0000 |
| 938 | //0000 0000 0000 0000 0000 0000 0000 0000 |
| 939 | //0000 0000 0000 0000 7065 7465 7269 7573 |
| 940 | //0000 0002 0001 0000 |
| 941 | /* Note that that 02 is likely our country code!! we should store that |
| 942 | * perhaps? (or maybe just get it from player lists*/ |
| 943 | /* One of these is also the zodiac byte, but I don't know which one |
| 944 | * here, I'm thinking its probably not that first 1, so I'll use the |
| 945 | * second one, I think we get the country_id from the player lists*/ |
| 946 | p += 8; |
| 947 | encode_offset = *(unsigned long *)p; |
| 948 | p += 4; |
| 949 | p += 0x1c; |
| 950 | // this could be the nickname if we passed the username |
| 951 | //pretty certain this is the nickname, so that kind of |
| 952 | //casts some of this into question. FIXME |
| 953 | //we either have to change the username or... I just... |
| 954 | //we could check that its not the same |
| 955 | p += 11; |
| 956 | //country_id |
| 957 | p++; |
| 958 | p++; |
| 959 | zodiac_byte = p[0]; |
| 960 | #ifdef RE_DEBUG |
| 961 | for(unsigned int i = 0; i < length; i++) |
| 962 | printf("%02x ", msg[i]); |
| 963 | printf("\n"); |
| 964 | //printf("encode offset: %08x\n", encode_offset); |
| 965 | #endif //RE_DEBUG |
| 966 | |
| 967 | received_games = false; |
| 968 | received_players = false; |
| 969 | |
| 970 | /* What happens if both players get disconnected FIXME */ |
| 971 | |
| 972 | initial_connect = true; |
| 973 | |
| 974 | sendRequestGames(); |
| 975 | sendRequest(); |
| 976 | setState(Setup); |
| 977 | sendName(); |
| 978 | /* FIXME currently we have handleFriends doing setConnected which closes the please wait |
| 979 | * getting us out of SETUP. This means that the players haven't come in yet. |
| 980 | * the problem is that handleFriends puts the friends on a list that's then checked when |
| 981 | * we receive players. But there's some other issues with this as well and we should get |
| 982 | * CyberORO working better with this, as well as IGS Before we worry about this |
| 983 | * relatively minor problem */ |
| 984 | sendFriendsBlocksRequest(); |
| 985 | sendRequest2(); //the 53 |
| 986 | sendRequestPlayers(); |
| 987 | //53 |
| 988 | //11 ae 14 name(91) 95 53 |
| 989 | onReady(); //sends invite settings as well |
| 990 | } |
| 991 | |
| 992 | void TygemConnection::sendRequestGames(void) |
| 993 | { |
| 994 | unsigned int length = 8; |
| 995 | unsigned char * packet = new unsigned char[length]; |
| 996 | |
| 997 | packet[0] = 0x00; |
| 998 | packet[1] = 0x08; |
| 999 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1000 | packet[3] = 0x11; |
| 1001 | packet[4] = 0x00; |
| 1002 | packet[5] = 0x00; |
| 1003 | packet[6] = 0x00; |
| 1004 | packet[7] = 0x00; |
| 1005 | |
| 1006 | //before encode |
| 1007 | #ifdef RE_DEBUG |
| 1008 | for(unsigned int i = 0; i < length; i++) |
| 1009 | printf("%02x", packet[i]); |
| 1010 | printf("\n"); |
| 1011 | #endif //RE_DEBUG |
| 1012 | /*encode(packet, 0x8); |
| 1013 | //after encode |
| 1014 | for(i = 0; i < length; i++) |
| 1015 | printf("%02x", packet[i]); |
| 1016 | printf("\n"); |
| 1017 | */ |
| 1018 | if(write((const char *)packet, length) < 0) |
| 1019 | qWarning("*** failed sending login confirm"); |
| 1020 | delete[] packet; |
| 1021 | } |
| 1022 | |
| 1023 | //we send |
| 1024 | //00 0C 06 AE 5C C8 E9 D4 00 E8 03 00 |
| 1025 | void TygemConnection::sendRequest(void) |
| 1026 | { |
| 1027 | unsigned int length = 12; |
| 1028 | unsigned char * packet = new unsigned char[length]; |
| 1029 | |
| 1030 | packet[0] = 0x00; |
| 1031 | packet[1] = 0x0c; |
| 1032 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1033 | packet[3] = 0xae; |
| 1034 | packet[4] = 0x0d; |
| 1035 | packet[5] = 0xf0; |
| 1036 | packet[6] = 0xad; |
| 1037 | packet[7] = 0xba; |
| 1038 | packet[8] = 0x0d; |
| 1039 | packet[9] = 0xf0; |
| 1040 | packet[10] = 0xad; |
| 1041 | packet[11] = 0xba; |
| 1042 | //a8a88cce |
| 1043 | //before encode |
| 1044 | #ifdef RE_DEBUG |
| 1045 | for(unsigned int i = 0; i < length; i++) |
| 1046 | printf("%02x", packet[i]); |
| 1047 | printf("\n"); |
| 1048 | #endif //RE_DEBUG |
| 1049 | |
| 1050 | encode(packet, (length / 4) - 2); |
| 1051 | |
| 1052 | if(write((const char *)packet, length) < 0) |
| 1053 | qWarning("*** failed sending request"); |
| 1054 | delete[] packet; |
| 1055 | } |
| 1056 | |
| 1057 | void TygemConnection::sendRequest2(void) |
| 1058 | { |
| 1059 | unsigned int length = 8; |
| 1060 | unsigned char * packet = new unsigned char[length]; |
| 1061 | |
| 1062 | packet[0] = (length >> 8); |
| 1063 | packet[1] = length & 0x00ff; |
| 1064 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1065 | packet[3] = 0x53; |
| 1066 | packet[4] = 0x00; |
| 1067 | packet[5] = 0x00; |
| 1068 | packet[6] = 0x00; |
| 1069 | packet[7] = 0x00; |
| 1070 | |
| 1071 | #ifdef RE_DEBUG |
| 1072 | for(unsigned int i = 0; i < length; i++) |
| 1073 | printf("%02x", packet[i]); |
| 1074 | printf("\n"); |
| 1075 | #endif //RE_DEBUG |
| 1076 | |
| 1077 | if(write((const char *)packet, length) < 0) |
| 1078 | qWarning("*** failed sending request 2"); |
| 1079 | delete[] packet; |
| 1080 | } |
| 1081 | |
| 1082 | /* Its possible we're supposed to send only ascii or only encode here */ |
| 1083 | /* Note that this is also a request for some 0692 information */ |
| 1084 | void TygemConnection::sendName(void) |
| 1085 | { |
| 1086 | unsigned int length = 24; |
| 1087 | unsigned char * packet = new unsigned char[length]; |
| 1088 | |
| 1089 | packet[0] = 0x00; |
| 1090 | packet[1] = 0x18; |
| 1091 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1092 | packet[3] = 0x91; |
| 1093 | writeZeroPaddedString((char *)&(packet[4]), getUsername(), 20); |
| 1094 | //before encode |
| 1095 | #ifdef RE_DEBUG |
| 1096 | printf("Sending name"); |
| 1097 | for(unsigned int i = 0; i < length; i++) |
| 1098 | printf("%02x", packet[i]); |
| 1099 | printf("\n"); |
| 1100 | #endif //RE_DEBUG |
| 1101 | encode(packet, (length / 4) - 2); |
| 1102 | |
| 1103 | if(write((const char *)packet, length) < 0) |
| 1104 | qWarning("*** failed sending name"); |
| 1105 | delete[] packet; |
| 1106 | } |
| 1107 | |
| 1108 | void TygemConnection::sendRequestPlayers(void) |
| 1109 | { |
| 1110 | unsigned int length = 8; |
| 1111 | unsigned char * packet = new unsigned char[length]; |
| 1112 | |
| 1113 | packet[0] = 0x00; |
| 1114 | packet[1] = 0x08; |
| 1115 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1116 | packet[3] = 0x14; |
| 1117 | packet[4] = 0x00; |
| 1118 | packet[5] = 0x00; |
| 1119 | packet[6] = 0x00; |
| 1120 | packet[7] = 0x00; |
| 1121 | |
| 1122 | //before encode |
| 1123 | #ifdef RE_DEBUG |
| 1124 | for(unsigned int i = 0; i < length; i++) |
| 1125 | printf("%02x", packet[i]); |
| 1126 | printf("\n"); |
| 1127 | #endif //RE_DEBUG |
| 1128 | |
| 1129 | /*encode(packet, 0x8); |
| 1130 | //after encode |
| 1131 | for(i = 0; i < length; i++) |
| 1132 | printf("%02x", packet[i]); |
| 1133 | printf("\n"); |
| 1134 | */ |
| 1135 | if(write((const char *)packet, length) < 0) |
| 1136 | qWarning("*** failed sending players list request"); |
| 1137 | delete[] packet; |
| 1138 | } |
| 1139 | |
| 1140 | /* Note that as currently set up, this MUST be sent |
| 1141 | * before the player list request since we only set |
| 1142 | * the friends/blocks flags when the player is received |
| 1143 | * and so the friends list must be filled out before |
| 1144 | * we receive any players on it */ |
| 1145 | void TygemConnection::sendFriendsBlocksRequest(void) |
| 1146 | { |
| 1147 | unsigned int length = 8; |
| 1148 | unsigned char * packet = new unsigned char[length]; |
| 1149 | |
| 1150 | packet[0] = 0x00; |
| 1151 | packet[1] = 0x08; |
| 1152 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1153 | packet[3] = 0x95; |
| 1154 | packet[4] = 0x00; |
| 1155 | packet[5] = 0x00; |
| 1156 | packet[6] = 0x00; |
| 1157 | packet[7] = 0x00; |
| 1158 | |
| 1159 | //before encode |
| 1160 | #ifdef RE_DEBUG |
| 1161 | for(unsigned int i = 0; i < length; i++) |
| 1162 | printf("%02x", packet[i]); |
| 1163 | printf("\n"); |
| 1164 | #endif //RE_DEBUG |
| 1165 | |
| 1166 | /*encode(packet, 0x8); |
| 1167 | //after encode |
| 1168 | for(i = 0; i < length; i++) |
| 1169 | printf("%02x", packet[i]); |
| 1170 | printf("\n"); |
| 1171 | */ |
| 1172 | if(write((const char *)packet, length) < 0) |
| 1173 | qWarning("*** failed sending friends/blocks list request"); |
| 1174 | delete[] packet; |
| 1175 | } |
| 1176 | |
| 1177 | void TygemConnection::sendObserversRequest(unsigned short game_number) |
| 1178 | { |
| 1179 | unsigned int length = 12; |
| 1180 | unsigned char * packet = new unsigned char[length]; |
| 1181 | |
| 1182 | packet[0] = 0x00; |
| 1183 | packet[1] = 0x0c; |
| 1184 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1185 | packet[3] = 0x65; |
| 1186 | packet[4] = (game_number >> 8); |
| 1187 | packet[5] = game_number & 0x00ff; |
| 1188 | packet[6] = 0x00; |
| 1189 | packet[7] = 0x00; |
| 1190 | packet[8] = 0x00; |
| 1191 | packet[9] = 0x00; |
| 1192 | packet[10] = 0x00; |
| 1193 | packet[11] = 0x00; |
| 1194 | |
| 1195 | #ifdef RE_DEBUG |
| 1196 | printf("observer list request:\n"); |
| 1197 | //before encode |
| 1198 | for(unsigned int i = 0; i < length; i++) |
| 1199 | printf("%02x", packet[i]); |
| 1200 | printf("\n"); |
| 1201 | #endif //RE_DEBUG |
| 1202 | encode(packet, (length / 4) - 2); |
| 1203 | |
| 1204 | if(write((const char *)packet, length) < 0) |
| 1205 | qWarning("*** failed sending observers request"); |
| 1206 | delete[] packet; |
| 1207 | } |
| 1208 | |
| 1209 | void TygemConnection::changeServer(void) |
| 1210 | { |
| 1211 | if(connectionState != Connected) |
| 1212 | return; |
| 1213 | if(reconnectToServer() < 0) |
| 1214 | { |
| 1215 | qDebug("User canceled"); |
| 1216 | return; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | void TygemConnection::createRoom(void) |
| 1221 | { |
| 1222 | #ifdef FIXME |
| 1223 | CreateRoomDialog * createroomdialog = new CreateRoomDialog(this); |
| 1224 | createroomdialog->exec(); |
| 1225 | #endif //FIXME |
| 1226 | } |
| 1227 | |
| 1228 | /* FIXME we need error handling here, a return value, something |
| 1229 | * or at least we need to call the right destructors throughout, |
| 1230 | * and safely. */ |
| 1231 | int TygemConnection::reconnectToServer(void) |
| 1232 | { |
| 1233 | bool reconnecting; |
| 1234 | /* FIXME, why does the serverReconnectDialog disappear the ui |
| 1235 | * elements in the server window ?!?!? */ |
| 1236 | ServerListDialog * serverReconnectDialog = new ServerListDialog(serverList, current_server_index); |
| 1237 | int server_i = serverReconnectDialog->exec(); |
| 1238 | if(server_i < 0 || server_i == QDialog::Rejected) |
| 1239 | return -1; |
| 1240 | server_i--; |
| 1241 | if(!this) |
| 1242 | { |
| 1243 | /* FIXME, this needs to be much better. It should be, |
| 1244 | * to start with, irrelevant. */ |
| 1245 | qDebug("Waited too long to connect"); |
| 1246 | return -1; |
| 1247 | } |
| 1248 | current_server_index = server_i; |
| 1249 | /* FIXME |
| 1250 | * Might be neat if we listed the server that one was currently on |
| 1251 | * somewhere, like on the main window. */ |
| 1252 | |
| 1253 | qDebug("Reconnecting to %s: %s...", serverList[server_i]->name.toLatin1().constData(), serverList[server_i]->ipaddress); |
| 1254 | |
| 1255 | if(connectionState == Connected) |
| 1256 | { |
| 1257 | sendDisconnectMsg(); |
| 1258 | closeConnection(false); |
| 1259 | reconnecting = true; |
| 1260 | } |
| 1261 | else if(connectionState == Login) |
| 1262 | { |
| 1263 | closeConnection(false); |
| 1264 | reconnecting = false; |
| 1265 | } |
| 1266 | else |
| 1267 | reconnecting = false; |
| 1268 | if(openConnection(serverList[server_i]->ipaddress, 12320)) |
| 1269 | { |
| 1270 | qDebug("Reconnected %d", reconnecting); |
| 1271 | setState(Login); |
| 1272 | sendLogin(false, false); |
| 1273 | //sendLogin(reconnecting, reconnecting); |
| 1274 | fflush(stdout); |
| 1275 | fflush(stderr); |
| 1276 | onAuthenticationNegotiated(); |
| 1277 | } |
| 1278 | else |
| 1279 | qDebug("Can't open Connection!!"); |
| 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | unsigned short TygemConnection::getRoomNumber(void) |
| 1285 | { |
| 1286 | return match_negotiation_state->getGameId(); |
| 1287 | } |
| 1288 | |
| 1289 | //change text to QString |
| 1290 | //FIXME why is this commented out?!? |
| 1291 | /* I'm not certain why this is commented out, |
| 1292 | * but there's an email type message and then |
| 1293 | * there's a conversation message. You have to |
| 1294 | * request the conversation message and then |
| 1295 | * there's a message sent when you leave. I have it |
| 1296 | * in tygem_endgame4.txt on spacecoyote I think FIXME */ |
| 1297 | /* I think we can use existing messages in UI but that should be fixed up |
| 1298 | * anyway */ |
| 1299 | /* Okay, there's a simple reason this is commented out. There's no UI element |
| 1300 | * to handle it. We'd need to set something up to send messages and I know there's |
| 1301 | * some facility for that, as in IGS, its like an on-server email service, but we |
| 1302 | * haven't set that feature up in general and that would need to be done first. |
| 1303 | * also note that that 0617 conflicts with the server announcement code as |
| 1304 | * far as I can tell. See handlePersonalChat with 069f I think. Good chance |
| 1305 | * this stuff is copy and pasted. FIXME */ |
| 1306 | void TygemConnection::sendPersonalChat(const PlayerListing * , const char * ) |
| 1307 | { |
| 1308 | /*unsigned int length = 42 + text.size(); |
| 1309 | while(length % 4 != 0) |
| 1310 | length++; |
| 1311 | length += 4; |
| 1312 | unsigned char * packet = new unsigned char[length]; |
| 1313 | char * our_name, * msg; |
| 1314 | int i; |
| 1315 | int ordinal; |
| 1316 | unsigned char qualifier; |
| 1317 | Room * room = getDefaultRoom(); |
| 1318 | PlayerListing * ourPlayer; |
| 1319 | ourPlayer = room->getPlayerListing(getUsername()); |
| 1320 | if(!ourPlayer) |
| 1321 | { |
| 1322 | qDebug("Can't get our player listing"); |
| 1323 | return; |
| 1324 | } |
| 1325 | |
| 1326 | packet[0] = (length >> 8); |
| 1327 | packet[1] = length & 0x00ff; |
| 1328 | packet[2] = 0x06; |
| 1329 | packet[3] = 0x17; |
| 1330 | msg = (char *)text.toLatin1().constData(); |
| 1331 | writeZeroPaddedString((char *)&(packet[4]), getUsername(), 14); |
| 1332 | packet[18] = 0x00; |
| 1333 | sscanf(ourPlayer->rank.toLatin1().constData(), "%d%c", &ordinal, &qualifier); |
| 1334 | if(qualifier == 'k') |
| 1335 | packet[19] = 0x12 - ordinal; |
| 1336 | else if(qualifier == 'p') |
| 1337 | packet[19] = ordinal + 0x1a; |
| 1338 | else |
| 1339 | packet[19] = ordinal + 0x11; |
| 1340 | //this would actually be our ascii name, with the earlier |
| 1341 | //being the encoded version |
| 1342 | for(i = 0; i < strlen(our_name); i++) |
| 1343 | packet[i + 20] = our_name[i]; |
| 1344 | for(i = strlen(our_name) + 20; i < 30; i++) |
| 1345 | packet[i] = 0x00; |
| 1346 | packet[30] = 0x00; |
| 1347 | packet[31] = 0x02; //what is this? |
| 1348 | packet[32] = 0x00; |
| 1349 | packet[33] = 0x00; |
| 1350 | packet[34] = 0x00; |
| 1351 | packet[35] = ((strlen(msg) + 1) >> 8); |
| 1352 | packet[36] = (strlen(msg) + 1) & 0x00ff; |
| 1353 | packet[37] = 0x00; |
| 1354 | packet[38] = 0xff; |
| 1355 | packet[39] = 0xff; //possibly implied destination username? |
| 1356 | packet[40] = 0x33; //don't know, sometimes 0x32, something to do with encoding? |
| 1357 | for(i = 0; i < strlen(msg); i++) |
| 1358 | packet[41 + i] = msg[i]; |
| 1359 | packet[41 + strlen(msg)] = 0x00; |
| 1360 | i = 42 + strlen(msg); |
| 1361 | while(i % 4 != 0) |
| 1362 | { |
| 1363 | packet[i] = 0x00; |
| 1364 | i++; |
| 1365 | } |
| 1366 | packet[i] = 0x00; |
| 1367 | packet[i + 1] = 0x00; |
| 1368 | packet[i + 2] = 0x00; |
| 1369 | packet[i + 3] = 0x00; |
| 1370 | |
| 1371 | printf("personal chat before encode\n"); |
| 1372 | for(i = 0; i < length; i++) |
| 1373 | printf("%02x", packet[i]); |
| 1374 | printf("\n"); |
| 1375 | |
| 1376 | encode(packet, (length / 4) - 2); |
| 1377 | |
| 1378 | printf("personal chat after encode\n"); |
| 1379 | for(i = 0; i < length; i++) |
| 1380 | printf("%02x", packet[i]); |
| 1381 | printf("\n"); |
| 1382 | if(write((const char *)packet, length) < 0) |
| 1383 | qWarning("*** failed sending personal chat"); |
| 1384 | delete[] packet;*/ |
| 1385 | } |
| 1386 | |
| 1387 | void TygemConnection::sendOpenConversation(PlayerListing * player) |
| 1388 | { |
| 1389 | unsigned int length = 0x4c; |
| 1390 | char * packet = new char[length]; |
| 1391 | unsigned int i; |
| 1392 | |
| 1393 | packet[0] = (length >> 8); |
| 1394 | packet[1] = (length & 0xff); |
| 1395 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1396 | packet[3] = 0x33; |
| 1397 | |
| 1398 | const PlayerListing * ourPlayer = getOurListing(); |
| 1399 | |
| 1400 | writeNotNicknameAndRank((char *)&(packet[4]), player); |
| 1401 | writeNotNicknameAndRank((char *)&(packet[20]), ourPlayer); |
| 1402 | writeNicknameAndCID((char *)&(packet[36]), player); |
| 1403 | writeNicknameAndCID((char *)&(packet[48]), ourPlayer); |
| 1404 | for(i = 60; i < 66; i++) |
| 1405 | packet[i] = 0x00; |
| 1406 | packet[66] = 0x02; |
| 1407 | |
| 1408 | for(i = 67; i < length; i++) |
| 1409 | packet[i] = 0x00; |
| 1410 | |
| 1411 | #ifdef RE_DEBUG |
| 1412 | qDebug("Sending open conversation"); |
| 1413 | for(i = 0; i < length; i++) |
| 1414 | printf("%02x ", packet[i]); |
| 1415 | #endif //RE_DEBUG |
| 1416 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1417 | if(write((const char *)packet, length) < 0) |
| 1418 | qWarning("*** failed sending close conversation"); |
| 1419 | delete[] packet; |
| 1420 | } |
| 1421 | |
| 1422 | void TygemConnection::sendConversationReply(PlayerListing * player, enum MIVersion version) |
| 1423 | { |
| 1424 | if(version != accept && version != decline) |
| 1425 | { |
| 1426 | qDebug("sendConversationReply passed unhandled version"); |
| 1427 | return; |
| 1428 | } |
| 1429 | unsigned int length = 0x60; |
| 1430 | char * packet = new char[length]; |
| 1431 | unsigned int i; |
| 1432 | |
| 1433 | packet[0] = (length >> 8); |
| 1434 | packet[1] = (length & 0xff); |
| 1435 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1436 | packet[3] = 0x34; |
| 1437 | |
| 1438 | const PlayerListing * ourPlayer = getOurListing(); |
| 1439 | |
| 1440 | writeNotNicknameAndRank((char *)&(packet[4]), player); |
| 1441 | writeNotNicknameAndRank((char *)&(packet[20]), ourPlayer); |
| 1442 | writeNicknameAndCID((char *)&(packet[36]), player); |
| 1443 | writeNicknameAndCID((char *)&(packet[48]), ourPlayer); |
| 1444 | for(i = 60; i < 66; i++) |
| 1445 | packet[i] = 0x00; |
| 1446 | if(version == accept) |
| 1447 | packet[66] = 0x01; |
| 1448 | else |
| 1449 | packet[66] = 0x00; |
| 1450 | |
| 1451 | for(i = 67; i < length; i++) |
| 1452 | packet[i] = 0x00; |
| 1453 | |
| 1454 | #ifdef RE_DEBUG |
| 1455 | qDebug("Sending open conversation reply"); |
| 1456 | for(i = 0; i < length; i++) |
| 1457 | printf("%02x ", packet[i]); |
| 1458 | #endif //RE_DEBUG |
| 1459 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1460 | if(write((const char *)packet, length) < 0) |
| 1461 | qWarning("*** failed sending open converation reply"); |
| 1462 | delete[] packet; |
| 1463 | } |
| 1464 | |
| 1465 | void TygemConnection::sendConversationMsg(PlayerListing * player, const char * text) |
| 1466 | { |
| 1467 | unsigned int text_length = strlen(text); |
| 1468 | unsigned int length = 0x44 + text_length; |
| 1469 | if(length % 4 == 0) |
| 1470 | length += 4; |
| 1471 | else |
| 1472 | { |
| 1473 | while(length % 4 != 0) |
| 1474 | length++; |
| 1475 | } |
| 1476 | length += 4; |
| 1477 | char * packet = new char[length]; |
| 1478 | unsigned int i; |
| 1479 | |
| 1480 | packet[0] = (length >> 8); |
| 1481 | packet[1] = (length & 0xff); |
| 1482 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1483 | packet[3] = 0x35; |
| 1484 | |
| 1485 | const PlayerListing * ourPlayer = getOurListing(); |
| 1486 | |
| 1487 | writeNotNicknameAndRank((char *)&(packet[4]), player); |
| 1488 | writeNotNicknameAndRank((char *)&(packet[20]), ourPlayer); |
| 1489 | writeNicknameAndCID((char *)&(packet[36]), player); |
| 1490 | writeNicknameAndCID((char *)&(packet[48]), ourPlayer); |
| 1491 | for(i = 60; i < 64; i++) |
| 1492 | packet[i] = 0x00; |
| 1493 | //these four zeroes have to do with text color I think |
| 1494 | //32 cd 32 00 //green |
| 1495 | //ff 00 00 00 //after a background change? also red |
| 1496 | //actually 63 maybe be the high byte of the text size |
| 1497 | //assuming text can be more than 255? double check FIXME |
| 1498 | packet[64] = text_length; |
| 1499 | packet[65] = 0x00; |
| 1500 | packet[66] = 0x05; |
| 1501 | packet[67] = 0x00; |
| 1502 | for(i = 0; i < text_length; i++) |
| 1503 | packet[68 + i] = text[i]; |
| 1504 | packet[68 + text_length] = 0x00; |
| 1505 | |
| 1506 | for(i = 69 + text_length; i < length; i++) |
| 1507 | packet[i] = 0x00; |
| 1508 | |
| 1509 | #ifdef RE_DEBUG |
| 1510 | qDebug("Sending conversation msg"); |
| 1511 | for(i = 0; i < length; i++) |
| 1512 | printf("%02x ", packet[i]); |
| 1513 | #endif //RE_DEBUG |
| 1514 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1515 | if(write((const char *)packet, length) < 0) |
| 1516 | qWarning("*** failed sending conversation msg"); |
| 1517 | delete[] packet; |
| 1518 | } |
| 1519 | |
| 1520 | void TygemConnection::sendCloseConversation(PlayerListing * player) |
| 1521 | { |
| 1522 | QString closeMsg = '\'' + getUsername() + '\''; |
| 1523 | //FIXME pretty sure close Msg says closeMsg in korean, only |
| 1524 | //our hacked client sends a bunch of ???????????? |
| 1525 | const char * text = closeMsg.toLatin1().constData(); |
| 1526 | unsigned int text_length = strlen(text); |
| 1527 | unsigned int length = 0x44 + text_length; |
| 1528 | while(length % 4 != 0) |
| 1529 | length++; |
| 1530 | length += 4; |
| 1531 | char * packet = new char[length]; |
| 1532 | unsigned int i; |
| 1533 | |
| 1534 | packet[0] = (length >> 8); |
| 1535 | packet[1] = (length & 0xff); |
| 1536 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1537 | packet[3] = 0x35; |
| 1538 | |
| 1539 | const PlayerListing * ourPlayer = getOurListing(); |
| 1540 | |
| 1541 | writeNotNicknameAndRank((char *)&(packet[4]), player); |
| 1542 | writeNotNicknameAndRank((char *)&(packet[20]), ourPlayer); |
| 1543 | writeNicknameAndCID((char *)&(packet[36]), player); |
| 1544 | writeNicknameAndCID((char *)&(packet[48]), ourPlayer); |
| 1545 | for(i = 60; i < 64; i++) |
| 1546 | packet[i] = 0x00; |
| 1547 | //these four zeroes have to do with text color I think |
| 1548 | //32 cd 32 00 //green |
| 1549 | //ff 00 00 00 //after a background change? also red |
| 1550 | //actually 63 maybe be the high byte of the text size |
| 1551 | //assuming text can be more than 255? double check FIXME |
| 1552 | packet[64] = text_length; |
| 1553 | packet[65] = 0x00; |
| 1554 | packet[66] = 0x04; //this would be the close bit, rather than 5 |
| 1555 | packet[67] = 0x00; |
| 1556 | for(i = 0; i < text_length; i++) |
| 1557 | packet[68 + i] = text[i]; |
| 1558 | packet[68 + text_length] = 0x00; |
| 1559 | |
| 1560 | for(i = 69 + text_length; i < length; i++) |
| 1561 | packet[i] = 0x00; |
| 1562 | |
| 1563 | #ifdef RE_DEBUG |
| 1564 | qDebug("Sending close conversation"); |
| 1565 | for(i = 0; i < length; i++) |
| 1566 | printf("%02x ", packet[i]); |
| 1567 | #endif //RE_DEBUG |
| 1568 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1569 | if(write((const char *)packet, length) < 0) |
| 1570 | qWarning("*** failed sending close conversation"); |
| 1571 | delete[] packet; |
| 1572 | } |
| 1573 | |
| 1574 | /* FIXME I think both IGS and maybe even oro have a chat echo. |
| 1575 | * but with tygem its particularly ugly */ |
| 1576 | /* This is in game, name should be changed */ |
| 1577 | void TygemConnection::sendMsg(unsigned int room_number, QString text) |
| 1578 | { |
| 1579 | if(room_number == 0) |
| 1580 | { |
| 1581 | sendServerChat(text); |
| 1582 | return; |
| 1583 | } |
| 1584 | if(text.size() > 0x7e) |
| 1585 | { |
| 1586 | qDebug("send Message too large according to tygem standard"); |
| 1587 | return; |
| 1588 | } |
| 1589 | unsigned int length = 74 + text.size(); |
| 1590 | if(length % 4 == 0) |
| 1591 | length += 4; |
| 1592 | else |
| 1593 | { |
| 1594 | while(length % 4 != 0) |
| 1595 | length++; |
| 1596 | } |
| 1597 | length += 4; |
| 1598 | unsigned char * packet = new unsigned char[length]; |
| 1599 | unsigned int i; |
| 1600 | const PlayerListing * ourPlayer = getOurListing(); |
| 1601 | |
| 1602 | packet[0] = (length >> 8); |
| 1603 | packet[1] = length & 0x00ff; |
| 1604 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1605 | packet[3] = 0x61; |
| 1606 | packet[4] = (room_number >> 8); |
| 1607 | packet[5] = room_number & 0x00ff; |
| 1608 | packet[6] = 0x00; |
| 1609 | packet[7] = 0x01; //our player? |
| 1610 | for(i = 8; i < 24; i++) |
| 1611 | packet[i] = 0x00; |
| 1612 | writeNotNicknameAndRank((char *)&(packet[24]), ourPlayer); |
| 1613 | writeNicknameAndCID((char *)&(packet[40]), ourPlayer); |
| 1614 | for(i = 53; i < 68; i++) |
| 1615 | packet[i] = 0x00; |
| 1616 | packet[68] = (text.size() + 1) & 0x00ff; |
| 1617 | packet[69] = 0x00; //size cannot apparently be larger than 0x7f |
| 1618 | packet[70] = 0x00; |
| 1619 | packet[71] = 0x00; |
| 1620 | packet[72] = 0x33; //don't know, sometimes 0x32, something to do with encoding? |
| 1621 | for(i = 0; i < (unsigned)text.size(); i++) |
| 1622 | packet[73 + i] = text.toLatin1().constData()[i]; |
| 1623 | packet[73 + text.size()] = 0x20; //append space, don't know why |
| 1624 | |
| 1625 | for(i = 74 + text.size(); i < length; i++) |
| 1626 | packet[i] = 0x00; |
| 1627 | |
| 1628 | |
| 1629 | #ifdef RE_DEBUG |
| 1630 | printf("room chat before encode\n"); |
| 1631 | for(i = 0; i < length; i++) |
| 1632 | printf("%02x", packet[i]); |
| 1633 | printf("\n"); |
| 1634 | #endif //RE_DEBUG |
| 1635 | encode(packet, (length / 4) - 2); |
| 1636 | #ifdef RE_DEBUG |
| 1637 | printf("room chat after encode\n"); |
| 1638 | for(i = 0; i < length; i++) |
| 1639 | printf("%02x", packet[i]); |
| 1640 | printf("\n"); |
| 1641 | #endif //RE_DEBUG |
| 1642 | if(write((const char *)packet, length) < 0) |
| 1643 | qWarning("*** failed sending room chat"); |
| 1644 | delete[] packet; |
| 1645 | } |
| 1646 | |
| 1647 | void TygemConnection::sendServerChat(QString text) |
| 1648 | { |
| 1649 | unsigned int length = 42 + text.size(); |
| 1650 | while(length % 4 != 0) |
| 1651 | length++; |
| 1652 | length += 4; |
| 1653 | unsigned char * packet = new unsigned char[length]; |
| 1654 | char * msg; |
| 1655 | unsigned int i; |
| 1656 | const PlayerListing * ourPlayer = getOurListing(); |
| 1657 | |
| 1658 | packet[0] = (length >> 8); |
| 1659 | packet[1] = length & 0x00ff; |
| 1660 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1661 | packet[3] = 0x17; |
| 1662 | |
| 1663 | writeNotNicknameAndRank((char *)&(packet[4]), ourPlayer); |
| 1664 | writeNicknameAndCID((char *)&(packet[20]), ourPlayer); |
| 1665 | |
| 1666 | msg = (char *)text.toLatin1().constData(); |
| 1667 | packet[32] = 0x00; |
| 1668 | packet[33] = 0x00; |
| 1669 | packet[34] = 0x00; |
| 1670 | packet[35] = ((strlen(msg) + 1) >> 8); |
| 1671 | packet[36] = (strlen(msg) + 1) & 0x00ff; |
| 1672 | packet[37] = 0x00; |
| 1673 | packet[38] = 0xff; |
| 1674 | packet[39] = 0xff; //possibly implied destination username? |
| 1675 | packet[40] = 0x33; //don't know, sometimes 0x32, something to do with encoding? |
| 1676 | for(i = 0; i < strlen(msg); i++) |
| 1677 | packet[41 + i] = msg[i]; |
| 1678 | packet[41 + strlen(msg)] = 0x00; |
| 1679 | i = 42 + strlen(msg); |
| 1680 | while(i % 4 != 0) |
| 1681 | { |
| 1682 | packet[i] = 0x00; |
| 1683 | i++; |
| 1684 | } |
| 1685 | packet[i] = 0x00; |
| 1686 | packet[i + 1] = 0x00; |
| 1687 | packet[i + 2] = 0x00; |
| 1688 | packet[i + 3] = 0x00; |
| 1689 | |
| 1690 | #ifdef RE_DEBUG |
| 1691 | printf("Server chat before encode\n"); |
| 1692 | for(i = 0; i < length; i++) |
| 1693 | printf("%02x", packet[i]); |
| 1694 | printf("\n"); |
| 1695 | #endif //RE_DEBUG |
| 1696 | |
| 1697 | encode(packet, (length / 4) - 2); |
| 1698 | |
| 1699 | #ifdef RE_DEBUG |
| 1700 | printf("Server chat after encode\n"); |
| 1701 | for(i = 0; i < length; i++) |
| 1702 | printf("%02x", packet[i]); |
| 1703 | printf("\n"); |
| 1704 | #endif //RE_DEBUG |
| 1705 | if(write((const char *)packet, length) < 0) |
| 1706 | qWarning("*** failed sending server chat"); |
| 1707 | delete[] packet; |
| 1708 | } |
| 1709 | |
| 1710 | //FIXME fix this when we do create rooms |
| 1711 | void TygemConnection::sendJoinRoom(const RoomListing & /*room*/, const char * /*password*/) |
| 1712 | { |
| 1713 | //FIXME |
| 1714 | } |
| 1715 | |
| 1716 | void TygemConnection::addFriend(PlayerListing * player) |
| 1717 | { |
| 1718 | unsigned int length = 0x18; |
| 1719 | char * packet = new char[length]; |
| 1720 | unsigned int i; |
| 1721 | |
| 1722 | packet[0] = (length >> 8); |
| 1723 | packet[1] = (length & 0xff); |
| 1724 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1725 | packet[3] = 0x93; |
| 1726 | writeZeroPaddedString((char *)&(packet[4]), player->notnickname, 14); |
| 1727 | //there's some bytes here that the official client doesn't even zero I don't |
| 1728 | //think |
| 1729 | packet[19] = 0x00; //this is 0x00 if they are friend |
| 1730 | for(i = 20; i < length; i++) |
| 1731 | packet[i] = 0x00; |
| 1732 | |
| 1733 | #ifdef RE_DEBUG |
| 1734 | qDebug("Sending add friend"); |
| 1735 | #endif //RE_DEBUG |
| 1736 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1737 | if(write((const char *)packet, length) < 0) |
| 1738 | qWarning("*** failed sending add friend"); |
| 1739 | delete[] packet; |
| 1740 | |
| 1741 | NetworkConnection::addFriend(player); |
| 1742 | } |
| 1743 | |
| 1744 | void TygemConnection::removeFriend(PlayerListing * player) |
| 1745 | { |
| 1746 | unsigned int length = 0x18; |
| 1747 | char * packet = new char[length]; |
| 1748 | unsigned int i; |
| 1749 | |
| 1750 | packet[0] = (length >> 8); |
| 1751 | packet[1] = (length & 0xff); |
| 1752 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1753 | packet[3] = 0x94; |
| 1754 | writeZeroPaddedString((char *)&(packet[4]), player->notnickname, 14); |
| 1755 | //there's some bytes here that the official client doesn't even zero I don't |
| 1756 | //think |
| 1757 | packet[19] = 0x01; //this is weird because 93 to 94 distinguishes removal |
| 1758 | //why not 0 here for friend as with addFriend?!?? |
| 1759 | for(i = 20; i < length; i++) |
| 1760 | packet[i] = 0x00; |
| 1761 | |
| 1762 | #ifdef RE_DEBUG |
| 1763 | qDebug("Sending remove friend"); |
| 1764 | #endif //RE_DEBUG |
| 1765 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1766 | if(write((const char *)packet, length) < 0) |
| 1767 | qWarning("*** failed sending remove friend"); |
| 1768 | delete[] packet; |
| 1769 | |
| 1770 | NetworkConnection::removeFriend(player); |
| 1771 | } |
| 1772 | |
| 1773 | /* This could be fan instead of block, need to double check, likely block though */ |
| 1774 | void TygemConnection::addBlock(PlayerListing * player) |
| 1775 | { |
| 1776 | unsigned int length = 0x18; |
| 1777 | char * packet = new char[length]; |
| 1778 | unsigned int i; |
| 1779 | |
| 1780 | packet[0] = (length >> 8); |
| 1781 | packet[1] = (length & 0xff); |
| 1782 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1783 | packet[3] = 0x93; |
| 1784 | writeZeroPaddedString((char *)&(packet[4]), player->notnickname, 14); |
| 1785 | |
| 1786 | //there's some bytes here that the official client doesn't even zero I don't |
| 1787 | //think |
| 1788 | packet[19] = 0xff; //this is 0x00 if they are friend |
| 1789 | for(i = 20; i < length; i++) |
| 1790 | packet[i] = 0x00; |
| 1791 | |
| 1792 | #ifdef RE_DEBUG |
| 1793 | qDebug("Sending add block"); |
| 1794 | #endif //RE_DEBUG |
| 1795 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1796 | if(write((const char *)packet, length) < 0) |
| 1797 | qWarning("*** failed sending add block"); |
| 1798 | delete[] packet; |
| 1799 | |
| 1800 | NetworkConnection::addBlock(player); |
| 1801 | } |
| 1802 | |
| 1803 | void TygemConnection::removeBlock(PlayerListing * player) |
| 1804 | { |
| 1805 | unsigned int length = 0x18; |
| 1806 | char * packet = new char[length]; |
| 1807 | unsigned int i; |
| 1808 | |
| 1809 | packet[0] = (length >> 8); |
| 1810 | packet[1] = (length & 0xff); |
| 1811 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1812 | packet[3] = 0x94; |
| 1813 | writeZeroPaddedString((char *)&(packet[4]), player->notnickname, 14); |
| 1814 | |
| 1815 | //there's some bytes here that the official client doesn't even zero I don't |
| 1816 | //think |
| 1817 | packet[19] = 0xff; //this is 0x00 if they are friend |
| 1818 | for(i = 20; i < length; i++) |
| 1819 | packet[i] = 0x00; |
| 1820 | |
| 1821 | #ifdef RE_DEBUG |
| 1822 | qDebug("Sending remove block"); |
| 1823 | #endif //RE_DEBUG |
| 1824 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1825 | if(write((const char *)packet, length) < 0) |
| 1826 | qWarning("*** failed sending remove block"); |
| 1827 | delete[] packet; |
| 1828 | |
| 1829 | NetworkConnection::removeBlock(player); |
| 1830 | } |
| 1831 | |
| 1832 | /* Not sure what to do with this yet */ |
| 1833 | void TygemConnection::requestLongInfo(PlayerListing * player) |
| 1834 | { |
| 1835 | unsigned int length = 0x18; |
| 1836 | char * packet = new char[length]; |
| 1837 | unsigned int i; |
| 1838 | |
| 1839 | packet[0] = (length >> 8); |
| 1840 | packet[1] = (length & 0xff); |
| 1841 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1842 | packet[3] = 0x91; |
| 1843 | writeZeroPaddedString((char *)&(packet[4]), player->notnickname, 14); |
| 1844 | |
| 1845 | //there's some bytes here that the official client doesn't even zero I don't |
| 1846 | //think |
| 1847 | packet[19] = 0xff; //this is 0x00 if they are friend |
| 1848 | for(i = 20; i < length; i++) |
| 1849 | packet[i] = 0x00; |
| 1850 | |
| 1851 | #ifdef RE_DEBUG |
| 1852 | qDebug("Sending request long info"); |
| 1853 | #endif //RE_DEBUG |
| 1854 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1855 | if(write((const char *)packet, length) < 0) |
| 1856 | qWarning("*** failed sending request long info"); |
| 1857 | delete[] packet; |
| 1858 | } |
| 1859 | |
| 1860 | /* Not sure what to do with this yet: this may not even be |
| 1861 | * an info thing, its like a list of options, I don't know |
| 1862 | * why it sends anything but it might request some small |
| 1863 | * info */ |
| 1864 | void TygemConnection::requestShortInfo(PlayerListing * player) |
| 1865 | { |
| 1866 | unsigned int length = 0x18; |
| 1867 | char * packet = new char[length]; |
| 1868 | unsigned int i; |
| 1869 | |
| 1870 | packet[0] = (length >> 8); |
| 1871 | packet[1] = (length & 0xff); |
| 1872 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1873 | packet[3] = 0xa3; |
| 1874 | writeZeroPaddedString((char *)&(packet[4]), player->notnickname, 14); |
| 1875 | |
| 1876 | //there's some bytes here that the official client doesn't even zero I don't |
| 1877 | //think |
| 1878 | packet[19] = 0xff; //this is 0x00 if they are friend |
| 1879 | for(i = 20; i < length; i++) |
| 1880 | packet[i] = 0x00; |
| 1881 | |
| 1882 | #ifdef RE_DEBUG |
| 1883 | qDebug("Sending request short info"); |
| 1884 | #endif //RE_DEBUG |
| 1885 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1886 | if(write((const char *)packet, length) < 0) |
| 1887 | qWarning("*** failed sending request short info"); |
| 1888 | delete[] packet; |
| 1889 | } |
| 1890 | |
| 1891 | |
| 1892 | /* This looks like its also for leaving games were playing */ |
| 1893 | void TygemConnection::sendFinishObserving(unsigned short game_number) |
| 1894 | { |
| 1895 | unsigned int length = 12; |
| 1896 | char *packet = new char[length]; |
| 1897 | |
| 1898 | /* We need to turn this to a join if its a room, not a game |
| 1899 | * otherwise bad things happen FIXME */ |
| 1900 | |
| 1901 | packet[0] = 0x00; |
| 1902 | packet[1] = 0x0c; |
| 1903 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1904 | packet[3] = 0x3d; |
| 1905 | packet[4] = (game_number >> 8); |
| 1906 | packet[5] = game_number & 0x00ff; |
| 1907 | /* I'm pretty certain that 6 and 7 are just garbage |
| 1908 | * remember that bytes aren't zeroed */ |
| 1909 | packet[6] = 0xad; |
| 1910 | packet[7] = 0xba; |
| 1911 | packet[8] = 0x00; |
| 1912 | packet[9] = 0x00; |
| 1913 | packet[10] = 0x00; |
| 1914 | packet[11] = 0x00; |
| 1915 | #ifdef RE_DEBUG |
| 1916 | printf("Sending finishing observe to %d ", game_number); |
| 1917 | for(unsigned int i = 0; i < length; i++) |
| 1918 | printf("%02x ", (unsigned char)packet[i]); |
| 1919 | printf("\n"); |
| 1920 | #endif //RE_DEBUG |
| 1921 | encode((unsigned char *)packet, (length / 4) - 2); |
| 1922 | |
| 1923 | if(write((const char *)packet, length) < 0) |
| 1924 | qWarning("*** failed sending finish observing"); |
| 1925 | delete[] packet; |
| 1926 | } |
| 1927 | |
| 1928 | /* I get the feeling that this requests the room, |
| 1929 | * maybe we can specify password or name if we were creating |
| 1930 | * it from button rather than auto on match accept. And |
| 1931 | * then maybe a number comes back |
| 1932 | * Incidentally, if we break on this, take too long, we can get |
| 1933 | * a bad answer. Or maybe our invite response is bad and there's |
| 1934 | * decent checking on it now.*/ |
| 1935 | void TygemConnection::sendCreateRoom(void) |
| 1936 | { |
| 1937 | unsigned int length = 0x28; |
| 1938 | if(connectionType == TypeTOM) |
| 1939 | length = 0x30; //at least for actual button |
| 1940 | char *packet = new char[length]; |
| 1941 | unsigned int i; |
| 1942 | |
| 1943 | packet[0] = (length >> 8); |
| 1944 | packet[1] = (length & 0xff); |
| 1945 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 1946 | packet[3] = 0x36; |
| 1947 | for(i = 4; i < length; i++) |
| 1948 | packet[i] = 0x00; |
| 1949 | #ifdef FIXME |
| 1950 | //*** create room first option |
| 1951 | for(i = 4; i < 26; i++) |
| 1952 | packet[i] = 0x00; |
| 1953 | |
| 1954 | packet[26] = 0xff; |
| 1955 | packet[27] = 0xff; |
| 1956 | packet[28] = 0xff; |
| 1957 | packet[29] = 0xff; |
| 1958 | for(i = 30; i < length; i++) |
| 1959 | packet[i] = 0x00; |
| 1960 | //followed by a 0665 sendObserverRequest ?? weird |
| 1961 | |
| 1962 | //*** create room second option with text "hello" |
| 1963 | packet[4] = 0x02; |
| 1964 | packet[5] = 0x00; |
| 1965 | packet[6] = 0x00; |
| 1966 | packet[7] = 0x00; |
| 1967 | for(i = 8; i < 26; i++) |
| 1968 | packet[i] = 0x00; |
| 1969 | packet[26] = 0xff; |
| 1970 | packet[27] = 0xff; |
| 1971 | packet[28] = 0xff; |
| 1972 | packet[29] = 0xff; |
| 1973 | packet[30] = 0x02; |
| 1974 | packet[31] = 0x00; |
| 1975 | packet[32] = 0x00; |
| 1976 | packet[33] = 0x00; |
| 1977 | |
| 1978 | packet[34] = (text.size() >> 8); |
| 1979 | packet[35] = text.size & 0xff; |
| 1980 | packet[36] = //null terminated text string |
| 1981 | //6 more zeroes |
| 1982 | |
| 1983 | //*** create room third option with 2 checkboxes below checked, same checkboxes on second option |
| 1984 | //there was a dropdown to select a room as I recall |
| 1985 | //4 and 5 are likely a room number to create a review room off of |
| 1986 | //6 and 7 are 0 |
| 1987 | //up to 26 is 0 |
| 1988 | //26 is 0, 27 is 1, 28 is 1, 29 is 0 |
| 1989 | //then there's the 4 and 5 room number again, what I think is a room number |
| 1990 | //rest is 0 to 2c/40 |
| 1991 | |
| 1992 | //then there's weird packets |
| 1993 | //one I missed because I thought it was an 0665 |
| 1994 | //after is 0018063b 00 01 00 01 with the rest 0s |
| 1995 | //then 00140683 stop time which I won't bother to copy right now |
| 1996 | //then the 0665 sendObserversRequest |
| 1997 | //I'm not going to screw with this one right now, but if I did, I'd have to get that one I missed. |
| 1998 | #endif //FIXME |
| 1999 | #ifdef RE_DEBUG |
| 2000 | printf("Sending create room"); |
| 2001 | for(i = 0; i < length; i++) |
| 2002 | printf("%02x ", (unsigned char)packet[i]); |
| 2003 | printf("\n"); |
| 2004 | #endif //RE_DEBUG |
| 2005 | encode((unsigned char *)packet, (length / 4) - 2); |
| 2006 | #ifdef RE_DEBUG |
| 2007 | printf("After encode\n"); |
| 2008 | for(i = 0; i < length; i++) |
| 2009 | printf("%02x ", (unsigned char)packet[i]); |
| 2010 | printf("\n"); |
| 2011 | |
| 2012 | qDebug("Sending create room"); |
| 2013 | #endif //RE_DEBUG |
| 2014 | if(write((const char *)packet, length) < 0) |
| 2015 | qWarning("*** failed sending create room"); |
| 2016 | delete[] packet; |
| 2017 | match_negotiation_state->sendCreateRoom(); |
| 2018 | } |
| 2019 | |
| 2020 | //FIXME |
| 2021 | void TygemConnection::sendCreateRoom(RoomCreate * /*room*/) |
| 2022 | { |
| 2023 | qDebug("create room unimplemented on tygem"); |
| 2024 | return; |
| 2025 | } |
| 2026 | |
| 2027 | /* Tygem has a different system. We send a basic invite and if |
| 2028 | * its accepted we enter a board room where there is then the debate |
| 2029 | * on match specifics. */ |
| 2030 | void TygemConnection::sendMatchInvite(const PlayerListing * player) |
| 2031 | { |
| 2032 | if(getBoardDispatches() == 3) |
| 2033 | QMessageBox::information(0, tr("3 Boards Open"), tr("You must close a board before you can start a game")); |
| 2034 | else if(player->info == QString('X')) |
| 2035 | { |
| 2036 | QMessageBox::information(0, tr("Not open"), tr("%1 is not accepting invitations").arg(player->name)); |
| 2037 | } |
| 2038 | else |
| 2039 | sendMatchInvite(player, offer); |
| 2040 | } |
| 2041 | |
| 2042 | /* note that this only enters the match offer dialog |
| 2043 | * it doesn't accept the game. Haven't decided whether |
| 2044 | * to auto send this or to have a special popup for it.*/ |
| 2045 | void TygemConnection::sendMatchInvite(const PlayerListing * player, enum MIVersion version, unsigned short room_number) |
| 2046 | { |
| 2047 | unsigned int length = 0x44; |
| 2048 | unsigned char * packet = new unsigned char[length]; |
| 2049 | int i; |
| 2050 | if(version == offer) |
| 2051 | opponent_is_challenger = false; |
| 2052 | //else we actually might want to set it here instead of in handleMatchInvite |
| 2053 | packet[0] = (length >> 8); |
| 2054 | packet[1] = length & 0x00ff; |
| 2055 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2056 | if(version == accept || version == decline || version == decline_all || version == alreadyingame) |
| 2057 | packet[3] = 0x44; |
| 2058 | else //offer or create |
| 2059 | packet[3] = 0x43; |
| 2060 | |
| 2061 | const PlayerListing * ourPlayer = getOurListing(); |
| 2062 | |
| 2063 | /* Careful, they could have logged off in the mean time possibly? */ |
| 2064 | writeNotNicknameAndRank((char *)&(packet[4]), player); |
| 2065 | |
| 2066 | if(version == create) |
| 2067 | { |
| 2068 | /* This is after the room has been created */ |
| 2069 | /* really erase rank, double double check?? FIXME */ |
| 2070 | for(i = 19; i < 32; i++) |
| 2071 | packet[i] = 0x00; |
| 2072 | } |
| 2073 | else |
| 2074 | { |
| 2075 | writeNicknameAndCID((char *)&(packet[20]), player); |
| 2076 | } |
| 2077 | writeNotNicknameAndRank((char *)&(packet[32]), ourPlayer); |
| 2078 | writeNicknameAndCID((char *)&(packet[48]), ourPlayer); |
| 2079 | |
| 2080 | if(version == accept) |
| 2081 | { |
| 2082 | packet[60] = 0x01; |
| 2083 | packet[61] = 0x01; |
| 2084 | //packet[61] = FOURTH_BYTE(mi_flags); |
| 2085 | packet[62] = 0x00; |
| 2086 | packet[63] = 0x00; |
| 2087 | //below three had no effect |
| 2088 | //packet[61] = 0x13; //?? |
| 2089 | //packet[62] = 0xff; |
| 2090 | //packet[63] = 0xff; |
| 2091 | match_negotiation_state->sendMatchAccept((PlayerListing *)&player); |
| 2092 | } |
| 2093 | else if(version == offer || version == decline) |
| 2094 | { |
| 2095 | packet[60] = 0x01; |
| 2096 | if(getBoardDispatches() == 3) //implied: version = decline |
| 2097 | packet[61] = 0x0d; |
| 2098 | else |
| 2099 | packet[61] = 0x00; |
| 2100 | packet[62] = 0xff; |
| 2101 | packet[63] = 0xff; //not implied username... see server chat comment |
| 2102 | if(version == offer) |
| 2103 | match_negotiation_state->sendMatchInvite((PlayerListing *)&player); |
| 2104 | } |
| 2105 | else if(version == alreadyingame) |
| 2106 | { |
| 2107 | //0x000b is sent out on TOM to decline when we have match invite |
| 2108 | //settings off |
| 2109 | packet[60] = 0x01; |
| 2110 | packet[61] = 0x0b; //doublecheck FIXME |
| 2111 | packet[62] = 0xff; |
| 2112 | packet[63] = 0xff; |
| 2113 | } |
| 2114 | else if(version == decline_all) |
| 2115 | { |
| 2116 | packet[60] = 0x01; |
| 2117 | //packet[61] = 0x0c; //doublecheck FIXME |
| 2118 | packet[61] = 0x0e; |
| 2119 | packet[62] = 0xff; |
| 2120 | packet[63] = 0xff; |
| 2121 | } |
| 2122 | else if(version == create) |
| 2123 | { |
| 2124 | packet[60] = 0x02; |
| 2125 | packet[61] = 0x00; |
| 2126 | /* Mr Wang to Niu Yen: |
| 2127 | * I know, let's randomly change the byte order just on this one |
| 2128 | * piece of information, just to screw up people trying to reverse |
| 2129 | * the protocol: */ |
| 2130 | packet[62] = room_number & 0x00ff; |
| 2131 | packet[63] = (room_number >> 8); |
| 2132 | /* No no, they're probably just inconsistent with their ntohs calls... */ |
| 2133 | match_negotiation_state->createdRoom(room_number); |
| 2134 | } |
| 2135 | for(i = 64; i < 68; i++) |
| 2136 | packet[i] = 0x00; |
| 2137 | |
| 2138 | #ifdef RE_DEBUG |
| 2139 | printf("match %s packet before encoding: ", (version == offer ? "offer" : |
| 2140 | (version == accept ? "accept" : |
| 2141 | (version == decline || version == decline_all |
| 2142 | || version == alreadyingame ? "decline" : "create")))); |
| 2143 | for(int i = 0; i < (int)length; i++) |
| 2144 | printf("%02x ", packet[i]); |
| 2145 | printf("\n"); |
| 2146 | #endif //RE_DEBUG |
| 2147 | encode(packet, (length / 4) - 2); |
| 2148 | #ifdef RE_DEBUG |
| 2149 | qDebug("Sending accept match request or invite"); |
| 2150 | #endif //RE_DEBUG |
| 2151 | if(write((const char *)packet, length) < 0) |
| 2152 | qWarning("*** failed sending accept match request or invite"); |
| 2153 | delete[] packet; |
| 2154 | } |
| 2155 | |
| 2156 | /* This looks like its a declaration of the match for the game listings */ |
| 2157 | void TygemConnection::sendMatchMsg1(const PlayerListing * player, unsigned short game_number) |
| 2158 | { |
| 2159 | unsigned int length = 0x2c; |
| 2160 | unsigned char * packet = new unsigned char[length]; |
| 2161 | packet[0] = (length >> 8); |
| 2162 | packet[1] = length & 0x00ff; |
| 2163 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2164 | packet[3] = 0xc6; |
| 2165 | packet[4] = (game_number >> 8); |
| 2166 | packet[5] = game_number & 0x00ff; |
| 2167 | packet[6] = 0x00; |
| 2168 | packet[7] = 0x00; |
| 2169 | |
| 2170 | const PlayerListing * ourPlayer = getOurListing(); |
| 2171 | |
| 2172 | writeNotNicknameAndRank((char *)&(packet[8]), ourPlayer); |
| 2173 | writeNotNicknameAndRank((char *)&(packet[24]), player); |
| 2174 | packet[40] = 0x00; |
| 2175 | packet[41] = 0x00; |
| 2176 | packet[42] = 0x00; |
| 2177 | packet[43] = 0x00; |
| 2178 | |
| 2179 | #ifdef RE_DEBUG |
| 2180 | printf("Match msg 1: \n"); |
| 2181 | for(int i = 0; i < (int)length; i++) |
| 2182 | printf("%02x ", packet[i]); |
| 2183 | printf("\n"); |
| 2184 | #endif //RE_DEBUG |
| 2185 | encode(packet, (length / 4) - 2); |
| 2186 | #ifdef RE_DEBUG |
| 2187 | qDebug("Sending match msg 1"); |
| 2188 | #endif //RE_DEBUG |
| 2189 | if(write((const char *)packet, length) < 0) |
| 2190 | qWarning("*** failed sending match msg 1"); |
| 2191 | delete[] packet; |
| 2192 | } |
| 2193 | |
| 2194 | void TygemConnection::sendMatchOffer(const MatchRequest & mr, enum MIVersion version) |
| 2195 | { |
| 2196 | unsigned int length = 0x5c; |
| 2197 | if(connectionType == TypeTOM) |
| 2198 | length = 0x60; |
| 2199 | unsigned char * packet = new unsigned char[length]; |
| 2200 | int i; |
| 2201 | Room * room = getDefaultRoom(); |
| 2202 | PlayerListing * opponent = room->getPlayerListing(mr.opponent); |
| 2203 | const PlayerListing * ourPlayer = getOurListing(); |
| 2204 | |
| 2205 | packet[0] = (length >> 8); |
| 2206 | packet[1] = length & 0x00ff; |
| 2207 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2208 | if(version == offer) |
| 2209 | packet[3] = 0x45; |
| 2210 | else //if(version == accept || version == modify) |
| 2211 | packet[3] = 0x46; |
| 2212 | |
| 2213 | writeNotNicknameAndRank((char *)&(packet[4]), opponent); |
| 2214 | writeNotNicknameAndRank((char *)&(packet[20]), ourPlayer); |
| 2215 | |
| 2216 | packet[36] = (mr.number >> 8); |
| 2217 | packet[37] = mr.number & 0x00ff; |
| 2218 | //012c 2801 0100 0100 0002 //offer? |
| 2219 | //012c 2801 0100 0100 0001 //accept? |
| 2220 | //0bb8 2803 0100 0100 0002 //offer |
| 2221 | //time settings |
| 2222 | //0007 012c 1e03 0100 0100 |
| 2223 | /* Its not clear that declines have to have the time settings, but they do seem to */ |
| 2224 | /*if(version == decline) |
| 2225 | { |
| 2226 | for(i = 38; i < 47; i++) |
| 2227 | packet[i] = 0x00; |
| 2228 | } |
| 2229 | else |
| 2230 | {*/ |
| 2231 | /*I've seen an accept sent with these as 0s |
| 2232 | * and yet that literally sets time to 0 so... */ |
| 2233 | //if(version == offer) |
| 2234 | //{ |
| 2235 | packet[38] = (mr.maintime >> 8); |
| 2236 | packet[39] = mr.maintime & 0x00ff; |
| 2237 | packet[40] = mr.periodtime; |
| 2238 | packet[41] = mr.stones_periods; |
| 2239 | if(version != decline) |
| 2240 | { |
| 2241 | packet[42] = mr.handicap; //this is handicap bit!!! |
| 2242 | /* FIXME it sets the handicap but doesn't know |
| 2243 | * whos turn it is or something !?!?! */ |
| 2244 | qDebug("Handicap %d\n", mr.handicap); |
| 2245 | /* Not sure if tygem has negative komis, it might, but for right now |
| 2246 | * we're getting -1 here, so FIXME */ |
| 2247 | if(mr.komi > 0.0) |
| 2248 | packet[43] = (unsigned char)(mr.komi - 0.5); |
| 2249 | else |
| 2250 | packet[43] = 0x00; |
| 2251 | |
| 2252 | /*} |
| 2253 | else if(version == accept) |
| 2254 | { |
| 2255 | for(i = 38; i < 44; i++) |
| 2256 | packet[i] = 0x00; |
| 2257 | }*/ |
| 2258 | switch(mr.color_request) |
| 2259 | { |
| 2260 | case MatchRequest::WHITE: |
| 2261 | if(version == offer) |
| 2262 | packet[44] = 0x01; |
| 2263 | else |
| 2264 | packet[44] = 0x00; |
| 2265 | break; |
| 2266 | case MatchRequest::BLACK: |
| 2267 | if(version == offer) //their color when we offer? |
| 2268 | packet[44] = 0x00; |
| 2269 | else |
| 2270 | packet[44] = 0x01;//our color when we accept |
| 2271 | break; |
| 2272 | case MatchRequest::NIGIRI: |
| 2273 | packet[44] = 0x02; |
| 2274 | break; |
| 2275 | default: |
| 2276 | break; |
| 2277 | |
| 2278 | } |
| 2279 | } |
| 2280 | /* This bit, if we're offering is basically ignored since |
| 2281 | * sent 0671 sendStartGame overrides colors, etc. */ |
| 2282 | //if(version == accept) |
| 2283 | // packet[44] = 0x00; //definitely we're black in tests right now |
| 2284 | /* Changing the accept above from 01 to 00 fixed the names but broke our move |
| 2285 | * ability as white */ |
| 2286 | /* On the other hand, changing the offer from 01 to 00 screwed the names |
| 2287 | * but allows us to play white */ |
| 2288 | /* Trying both as 01 with 01 below instead of 02 after our name */ |
| 2289 | /* That last 02 to 01 broke our move ability, so thinking, that's |
| 2290 | * offerer's color? */ |
| 2291 | //02 seemed to do something strange |
| 2292 | //maybe swapped the pictures but not the names |
| 2293 | /* For 44, 0x00 means that, for this as an accept send, |
| 2294 | * the challenger, that first name, is black, and we're white |
| 2295 | * 0x01 probably means the opposite, 02 might be nigiri or something |
| 2296 | * but there's flags later, that also need to be set I think */ |
| 2297 | /* Actually 02 instantly makes challenger black and allow them to play... |
| 2298 | * but then looks like inconsistent, so yeah, nigiri */ |
| 2299 | //00 here is, as accept send, we play white, they play black, |
| 2300 | //according to bits below and name orders as they are, this seems |
| 2301 | // to work except we can't play |
| 2302 | /* Okay. These bits, 45 and 46 determine bits 47 and 49 in |
| 2303 | * 0671 sendStartGame, whose name should be changed to sendMatchOpen |
| 2304 | * the also seem to be carried through from here in 0645 to 0646 the |
| 2305 | * accept, to 0671 to open the game */ |
| 2306 | if(mr.free_rated == FREE) |
| 2307 | { |
| 2308 | /* FIXME doublecheck */ |
| 2309 | packet[45] = 0x01; |
| 2310 | packet[46] = 0x01; |
| 2311 | } |
| 2312 | else //RATED |
| 2313 | { |
| 2314 | packet[45] = 0x00; |
| 2315 | packet[46] = 0x00; |
| 2316 | } |
| 2317 | //on tom sending accept I've seen 45 and 46 as 10 00 |
| 2318 | //might even be critical |
| 2319 | //} |
| 2320 | if(version == offer) |
| 2321 | { |
| 2322 | packet[47] = 0x02; |
| 2323 | packet[48] = 0x00; |
| 2324 | packet[49] = 0x00; |
| 2325 | packet[50] = 0x00; |
| 2326 | packet[51] = 0x00; |
| 2327 | match_negotiation_state->offerMatchTerms((MatchRequest *)&mr); |
| 2328 | } |
| 2329 | else if(version == accept) |
| 2330 | { |
| 2331 | packet[47] = 0x01; |
| 2332 | /* Could also be challenger bit, but if we change 47 to 02 |
| 2333 | * it definitely rechallenges or disputes or something */ |
| 2334 | packet[48] = 0xff; |
| 2335 | packet[49] = 0xff; |
| 2336 | packet[50] = 0xff; |
| 2337 | packet[51] = 0xff; |
| 2338 | match_negotiation_state->acceptMatchTerms((MatchRequest *)&mr); |
| 2339 | } |
| 2340 | else if(version == modify) |
| 2341 | { |
| 2342 | packet[47] = 0x02; |
| 2343 | packet[48] = 0xff; |
| 2344 | packet[49] = 0xff; |
| 2345 | packet[50] = 0xff; |
| 2346 | packet[51] = 0xff; |
| 2347 | match_negotiation_state->modifyMatchTerms((MatchRequest *)&mr); |
| 2348 | } |
| 2349 | else if(version == decline) |
| 2350 | { |
| 2351 | /*packet[47] = 0x03; |
| 2352 | packet[48] = 0xff; |
| 2353 | packet[49] = 0xff; |
| 2354 | packet[50] = 0xff; |
| 2355 | packet[51] = 0xff;*/ |
| 2356 | /* zeroes seem to work better here although there's still an autodecline if we try to modify |
| 2357 | * their modify */ |
| 2358 | for(i = 47; i < 52; i++) |
| 2359 | packet[i] = 0x00; |
| 2360 | match_negotiation_state->reset(); |
| 2361 | } |
| 2362 | |
| 2363 | if(version == offer) |
| 2364 | { |
| 2365 | /* Apparently we don't have opponent's nickname before |
| 2366 | * they've responded to the offer ? */ |
| 2367 | for(i = 52; i < 76; i++) |
| 2368 | packet[i] = 0x00; |
| 2369 | } |
| 2370 | else //if(version == accept || version == modify || version == decline) |
| 2371 | { |
| 2372 | for(i = 52; i < 56; i++) |
| 2373 | packet[i] = 0x00; |
| 2374 | packet[56] = 0x00; |
| 2375 | packet[57] = zodiac_byte; |
| 2376 | packet[58] = 0x00; |
| 2377 | packet[59] = 0x00; //their pic? |
| 2378 | for(i = 60; i < 64; i++) |
| 2379 | packet[i] = 0x00; |
| 2380 | |
| 2381 | /* Below doesn't seem to matter, if its this. The major thing was the 03ffffff */ |
| 2382 | /*if(mr.opponent_is_challenger) |
| 2383 | packet[63] = 0x01; */ //seen on modify and decline |
| 2384 | |
| 2385 | writeNicknameAndCID((char *)&(packet[64]), opponent); |
| 2386 | } |
| 2387 | |
| 2388 | writeNicknameAndCID((char *)&(packet[76]), ourPlayer); |
| 2389 | |
| 2390 | for(i = 88; i < 92; i++) |
| 2391 | packet[i] = 0x00; |
| 2392 | //tom has 91 = 0x61 92 = 0xea and then 4 more bytes with a size of 60? |
| 2393 | |
| 2394 | #ifdef RE_DEBUG |
| 2395 | printf("match packet we are sending now: "); |
| 2396 | for(i = 0; i < (int)length; i++) |
| 2397 | printf("%02x", packet[i]); |
| 2398 | printf("\n"); |
| 2399 | #endif //RE_DEBUG |
| 2400 | |
| 2401 | encode(packet, (length / 4) - 2); |
| 2402 | #ifdef RE_DEBUG |
| 2403 | qDebug("Sending match offer"); |
| 2404 | #endif //RE_DEBUG |
| 2405 | if(write((const char *)packet, length) < 0) |
| 2406 | qWarning("*** failed sending match offer"); |
| 2407 | delete[] packet; |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | /* Somewhat inconsistent name... */ |
| 2412 | void TygemConnection::sendStartGame(const MatchRequest & mr) |
| 2413 | { |
| 2414 | unsigned int length = 0x60; |
| 2415 | if(connectionType == TypeTOM) |
| 2416 | length = 0x64; |
| 2417 | unsigned char * packet = new unsigned char[length]; |
| 2418 | int i; |
| 2419 | Room * room = getDefaultRoom(); |
| 2420 | const PlayerListing * ourPlayer = getOurListing(); |
| 2421 | PlayerListing * opponent = room->getPlayerListing(mr.opponent); |
| 2422 | |
| 2423 | packet[0] = (length >> 8); |
| 2424 | packet[1] = length & 0x00ff; |
| 2425 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2426 | packet[3] = 0x71; |
| 2427 | packet[4] = (mr.number >> 8); |
| 2428 | packet[5] = mr.number & 0x00ff; |
| 2429 | packet[6] = 0x00; |
| 2430 | packet[7] = 0x01; //probably color |
| 2431 | |
| 2432 | writeNotNicknameAndRank((char *)&(packet[8]), opponent); |
| 2433 | writeNotNicknameAndRank((char *)&(packet[24]), ourPlayer); |
| 2434 | |
| 2435 | //012c 2801 0100 0100 0002 |
| 2436 | //time settings |
| 2437 | packet[40] = (mr.maintime >> 8); |
| 2438 | packet[41] = mr.maintime & 0x00ff; |
| 2439 | packet[42] = mr.periodtime; |
| 2440 | packet[43] = mr.stones_periods; |
| 2441 | packet[44] = mr.handicap; |
| 2442 | #ifdef OLD |
| 2443 | /* Just make sure this works okay */ |
| 2444 | //for now since time isn't really set right |
| 2445 | packet[40] = 0x01; //04 //time setting type? |
| 2446 | packet[41] = 0x2c; //b0 //minutes? |
| 2447 | packet[42] = 0x28; //1e //seconds? |
| 2448 | packet[43] = 0x01; //03 //periods? |
| 2449 | //packet[44] = 0x01; |
| 2450 | #endif //OLD |
| 2451 | |
| 2452 | packet[45] = 0x00; |
| 2453 | //this is our color |
| 2454 | switch(mr.color_request) |
| 2455 | { |
| 2456 | case MatchRequest::WHITE: |
| 2457 | packet[46] = 0x00; |
| 2458 | break; |
| 2459 | case MatchRequest::BLACK: |
| 2460 | packet[46] = 0x01; |
| 2461 | break; |
| 2462 | case MatchRequest::NIGIRI: |
| 2463 | packet[46] = 0x02; |
| 2464 | break; |
| 2465 | |
| 2466 | } |
| 2467 | |
| 2468 | packet[47] = 0x01; //was 0 |
| 2469 | packet[48] = 0x01; |
| 2470 | packet[49] = 0x01; //was 0 |
| 2471 | packet[50] = 0x00; |
| 2472 | packet[51] = 0x12; |
| 2473 | |
| 2474 | packet[52] = 0xff; |
| 2475 | packet[53] = 0xff; |
| 2476 | packet[54] = 0xff; |
| 2477 | packet[55] = 0xff; |
| 2478 | |
| 2479 | packet[56] = 0xff; |
| 2480 | packet[57] = 0xff; |
| 2481 | packet[58] = 0xff; |
| 2482 | packet[59] = 0xff; |
| 2483 | |
| 2484 | packet[60] = 0x00; |
| 2485 | /* This is likely their zodiac_byte but I think its ignored */ |
| 2486 | packet[61] = 0x01; //pics? |
| 2487 | packet[62] = 0x00; |
| 2488 | packet[63] = zodiac_byte; |
| 2489 | #ifdef FIXME |
| 2490 | //proper order here seems to be their name then our name |
| 2491 | //careful might change if we play black |
| 2492 | writeZeroPaddedString((char *)&(packet[64]), opponent->notnickname, 10); |
| 2493 | packet[74] = 0x00; |
| 2494 | /* The 0x02 bytes (75 and 87) are most likely the country and these second |
| 2495 | * names are refered to by tygem as "WHITENICK" as opposed to |
| 2496 | * "WHITENAME" which is the first name. So as far as I know, |
| 2497 | * nothing to do with ascii, versus encoding which makes sense */ |
| 2498 | //packet[75] = 0x02; //changed from 2 in the hope of making opp black |
| 2499 | packet[75] = opponent->country_id; |
| 2500 | writeZeroPaddedString((char *)&(packet[76]), getUsername(), 10); |
| 2501 | packet[86] = 0x00; |
| 2502 | packet[87] = ourPlayer->country_id; |
| 2503 | #endif //FIXME |
| 2504 | /* This could be a problem, typically, the below is right, but I have "notnickname" |
| 2505 | * used above. Often it doesn't matter, but I'm assuming this packet conforms |
| 2506 | * to the general format and maybe it doesn't. Doublecheck FIXME */ |
| 2507 | writeNicknameAndCID((char *)&(packet[64]), opponent); |
| 2508 | writeNicknameAndCID((char *)&(packet[76]), ourPlayer); |
| 2509 | |
| 2510 | for(i = 88; i < 96; i++) |
| 2511 | packet[i] = 0x00; |
| 2512 | |
| 2513 | #ifdef RE_DEBUG |
| 2514 | printf("start match we are sending now: "); |
| 2515 | for(i = 0; i < (int)length; i++) |
| 2516 | printf("%02x", packet[i]); |
| 2517 | printf("\n"); |
| 2518 | #endif //RE_DEBUG |
| 2519 | |
| 2520 | encode(packet, (length / 4) - 2); |
| 2521 | #ifdef RE_DEBUG |
| 2522 | qDebug("Sending start match"); |
| 2523 | #endif //RE_DEBUG |
| 2524 | if(write((const char *)packet, length) < 0) |
| 2525 | qWarning("*** failed sending start match"); |
| 2526 | delete[] packet; |
| 2527 | } |
| 2528 | |
| 2529 | void TygemConnection::sendServerKeepAlive(uint32_t keepaliveIV) |
| 2530 | { |
| 2531 | if(!keepaliveIV) |
| 2532 | { |
| 2533 | qDebug("No server keep alive IV"); |
| 2534 | return; |
| 2535 | } |
| 2536 | unsigned int length = 0xc; |
| 2537 | unsigned char * packet = new unsigned char[length]; |
| 2538 | |
| 2539 | packet[0] = (length >> 8); |
| 2540 | packet[1] = length & 0x00ff; |
| 2541 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2542 | packet[3] = 0x4e; |
| 2543 | if(connectionType == TypeTOM) |
| 2544 | { |
| 2545 | packet[4] = 0xc4;//?? |
| 2546 | packet[5] = 0x9b;//?? |
| 2547 | packet[6] = 0xba;//?? |
| 2548 | packet[7] = 0x11;//?? |
| 2549 | } |
| 2550 | else |
| 2551 | { |
| 2552 | packet[7] = (keepaliveIV >> 24); |
| 2553 | packet[6] = (keepaliveIV >> 16) & 0x000000ff; |
| 2554 | packet[5] = (keepaliveIV >> 8) & 0x000000ff; |
| 2555 | packet[4] = keepaliveIV & 0x000000ff; |
| 2556 | } |
| 2557 | packet[8] = 0x00; |
| 2558 | packet[9] = 0x00; |
| 2559 | packet[10] = 0x00; |
| 2560 | packet[11] = 0x00; |
| 2561 | |
| 2562 | #ifdef RE_DEBUG |
| 2563 | /*printf("server keep alive: "); |
| 2564 | for(int i = 0; i < (int)length; i++) |
| 2565 | printf("%02x", packet[i]); |
| 2566 | printf("\n");*/ |
| 2567 | #endif //RE_DEBUG |
| 2568 | |
| 2569 | encode(packet, (length / 4) - 2); |
| 2570 | #ifdef RE_DEBUG |
| 2571 | /*printf("encoded server keep alive: "); |
| 2572 | for(int i = 0; i < (int)length; i++) |
| 2573 | printf("%02x", packet[i]); |
| 2574 | printf("\n"); |
| 2575 | |
| 2576 | qDebug("Sending server keep alive");*/ |
| 2577 | #endif //RE_DEBUG |
| 2578 | if(write((const char *)packet, length) < 0) |
| 2579 | qWarning("*** failed sending server keep alive"); |
| 2580 | delete[] packet; |
| 2581 | } |
| 2582 | |
| 2583 | uint32_t TygemConnection::encodeKeepAliveIV(uint32_t server) |
| 2584 | { |
| 2585 | uint32_t magic1 = 0x54268553; |
| 2586 | uint32_t magic2 = 0x347f1064; |
| 2587 | uint32_t magic3 = 0xb92af7fe; |
| 2588 | uint32_t magic4 = 0x1301a49a; |
| 2589 | |
| 2590 | uint32_t temp1, temp2; |
| 2591 | int i; |
| 2592 | for(i = 0; i < 4; i++) |
| 2593 | ((unsigned char *)&temp1)[i] = ((unsigned char *)&server)[i] - ((unsigned char *)&magic1)[i]; |
| 2594 | ((unsigned char *)&temp2)[0] = ((unsigned char *)&temp1)[2]; |
| 2595 | ((unsigned char *)&temp2)[1] = ((unsigned char *)&temp1)[0]; |
| 2596 | ((unsigned char *)&temp2)[2] = ((unsigned char *)&temp1)[3]; |
| 2597 | ((unsigned char *)&temp2)[3] = ((unsigned char *)&temp1)[1]; |
| 2598 | for(i = 0; i < 4; i++) |
| 2599 | ((unsigned char *)&temp1)[i] = ((unsigned char *)&temp2)[i] ^ ((unsigned char *)&magic2)[i]; |
| 2600 | for(i = 0; i < 4; i++) |
| 2601 | ((unsigned char *)&temp2)[i] = ((unsigned char *)&temp1)[i] ^ ((unsigned char *)&magic3)[i]; |
| 2602 | ((unsigned char *)&temp1)[0] = ((unsigned char *)&temp2)[2]; |
| 2603 | ((unsigned char *)&temp1)[1] = ((unsigned char *)&temp2)[1]; |
| 2604 | ((unsigned char *)&temp1)[2] = ((unsigned char *)&temp2)[3]; |
| 2605 | ((unsigned char *)&temp1)[3] = ((unsigned char *)&temp2)[0]; |
| 2606 | for(i = 0; i < 4; i++) |
| 2607 | ((unsigned char *)&temp2)[i] = ((unsigned char *)&temp1)[i] + ((unsigned char *)&magic4)[i]; |
| 2608 | |
| 2609 | return temp2; |
| 2610 | } |
| 2611 | |
| 2612 | void TygemConnection::timerEvent(QTimerEvent * event) |
| 2613 | { |
| 2614 | if(event->timerId() == retryLoginTimerID) |
| 2615 | { |
| 2616 | //response bit seems necessary here for tygem |
| 2617 | //at least, double check for tom |
| 2618 | //still screwy FIXME |
| 2619 | sendLogin(true); //was true (nothing seems to work for tom) |
| 2620 | killTimer(retryLoginTimerID); |
| 2621 | retryLoginTimerID = 0; |
| 2622 | } |
| 2623 | else if(event->timerId() == opponentDisconnectTimerID) |
| 2624 | { |
| 2625 | unsigned short playing_game_number = match_negotiation_state->getGameId(); |
| 2626 | if(!playing_game_number) |
| 2627 | return; |
| 2628 | sendOpponentDisconnectTimer(playing_game_number); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | /* FIXME This may be sent even if clock is stopped (with stopped time)*/ |
| 2633 | void TygemConnection::sendTime(BoardDispatch * boarddispatch) |
| 2634 | { |
| 2635 | unsigned int length = 0x14; |
| 2636 | unsigned char * packet = new unsigned char[length]; |
| 2637 | GameData * gd = boarddispatch->getGameData(); |
| 2638 | |
| 2639 | packet[0] = (length >> 8); |
| 2640 | packet[1] = length & 0x00ff; |
| 2641 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2642 | packet[3] = 0x7d; |
| 2643 | packet[4] = (gd->number >> 8); |
| 2644 | packet[5] = gd->number & 0x00ff; |
| 2645 | //we're black sending this |
| 2646 | // f p s m |
| 2647 | //0101 013b 0440 0100 0580 0000 0000 |
| 2648 | packet[6] = (gd->black_name == getUsername()) ^ gd->white_first_flag; |
| 2649 | //packet[6] = (gd->black_name == getUsername()); |
| 2650 | packet[7] = 0x01; |
| 2651 | |
| 2652 | fillTimeChunk(boarddispatch, &(packet[8])); |
| 2653 | |
| 2654 | packet[16] = 0x00; |
| 2655 | packet[17] = 0x00; |
| 2656 | packet[18] = 0x00; |
| 2657 | packet[19] = 0x00; |
| 2658 | #ifdef RE_DEBUG |
| 2659 | printf("TIME SENT: "); |
| 2660 | for(int i = 0; i < 20; i++) |
| 2661 | printf("%02x ", packet[i]); |
| 2662 | printf("\n"); |
| 2663 | #endif //RE_DEBUG |
| 2664 | encode(packet, (length / 4) - 2); |
| 2665 | |
| 2666 | if(write((const char *)packet, length) < 0) |
| 2667 | qWarning("*** failed sending time"); |
| 2668 | delete[] packet; |
| 2669 | return; |
| 2670 | } |
| 2671 | |
| 2672 | void TygemConnection::fillTimeChunk(BoardDispatch * boarddispatch, unsigned char * packet) |
| 2673 | { |
| 2674 | GameData * game = boarddispatch->getGameData(); |
| 2675 | TimeRecord us = boarddispatch->getOurTimeRecord(); |
| 2676 | TimeRecord them = boarddispatch->getTheirTimeRecord(); |
| 2677 | |
| 2678 | if(us.stones_periods == -1) |
| 2679 | packet[0] = game->stones_periods; |
| 2680 | else |
| 2681 | packet[0] = us.stones_periods; |
| 2682 | packet[1] = us.time % 60; |
| 2683 | packet[2] = (us.time - packet[1]) / 60; |
| 2684 | //0x40 and 0x80 are also used for something as |
| 2685 | //well as a time loss flag as well |
| 2686 | if(game->black_name == getUsername()) |
| 2687 | packet[3] = 0x40; |
| 2688 | else |
| 2689 | packet[3] = 0x80; |
| 2690 | if(us.stones_periods != -1) |
| 2691 | packet[3] |= 0x20 |
| 2692 | ; |
| 2693 | if(them.stones_periods == -1) |
| 2694 | packet[4] = game->stones_periods; |
| 2695 | else |
| 2696 | packet[4] = them.stones_periods; |
| 2697 | packet[5] = them.time % 60; |
| 2698 | packet[6] = (them.time - packet[5]) / 60; |
| 2699 | if(game->black_name == getUsername()) |
| 2700 | packet[7] = 0x80; |
| 2701 | else |
| 2702 | packet[7] = 0x40; |
| 2703 | if(them.stones_periods != -1) |
| 2704 | packet[7] |= 0x20; |
| 2705 | } |
| 2706 | |
| 2707 | void TygemConnection::sendMove(unsigned int game_id, MoveRecord * move) |
| 2708 | { |
| 2709 | BoardDispatch * boarddispatch = getIfBoardDispatch(game_id); |
| 2710 | if(!boarddispatch) |
| 2711 | { |
| 2712 | qDebug("Can't get board for send move: %d", game_id); |
| 2713 | return; |
| 2714 | } |
| 2715 | GameData * r = boarddispatch->getGameData(); |
| 2716 | |
| 2717 | char move_str[32]; |
| 2718 | int player_number; |
| 2719 | unsigned int i; |
| 2720 | /*if(move->number + 2 > move_message_number) |
| 2721 | move_message_number = move->number + 2; |
| 2722 | else*/ |
| 2723 | if(move->flags != MoveRecord::UNREMOVE && move->flags != MoveRecord::REQUESTUNDO && move->flags != MoveRecord::REFUSEUNDO) |
| 2724 | move_message_number++; |
| 2725 | //0001 //color |
| 2726 | //0000 0000 0000 ADBA //probably like 8 zeroes |
| 2727 | //text string ending in 200a and 0 padded out to the full 32 |
| 2728 | //like: |
| 2729 | //SUR 0 2 2 white surrenders |
| 2730 | //STO 0 move_number player_number x y 0a |
| 2731 | //SUR 0 move_number player_number 0a |
| 2732 | //REM 0 312 -1 -1 0 1 |
| 2733 | //REM 0 307 5 6 1 0 |
| 2734 | //REM 0 306 10 14 2 0 un remove? |
| 2735 | //REM 0 number x y player_number 0 |
| 2736 | //DSC 1 0 1 //dead stone clear, I think, maybe not |
| 2737 | //DSC 1 0 2 |
| 2738 | |
| 2739 | //BAC 0 246 1 //part of review |
| 2740 | //CSP 0 233 0 |
| 2741 | //CST 0 234 |
| 2742 | //FOR 0 186 1 |
| 2743 | //BEG 0 47 |
| 2744 | //INI is in gibo files also as move number 1 |
| 2745 | /* Might also just be stone color here without the flag */ |
| 2746 | //player_number = ((r->black_name == getUsername()) ^ r->white_first_flag) + 1; |
| 2747 | //1 is likely black, 2, white |
| 2748 | player_number = (r->black_name != getUsername()) + 1; |
| 2749 | //player_number = SECOND_BYTE(send_flags); |
| 2750 | switch(move->flags) |
| 2751 | { |
| 2752 | case MoveRecord::RESIGN: |
| 2753 | //if this is resign, we also have to |
| 2754 | //send the 0670 or 0672 or whatever if its our offer/game |
| 2755 | sprintf(move_str, "SUR %d %d %d \n", 0, move_message_number, player_number); |
| 2756 | break; |
| 2757 | case MoveRecord::DONE_SCORING: |
| 2758 | case MoveRecord::REMOVE_AREA: //very awkward, this is really return to match mode, but don't want to trigger done_scoring endgame msg below |
| 2759 | /* Looks like this is a little different at the end... |
| 2760 | * doesn't seem to be a player number*/ |
| 2761 | sprintf(move_str, "REM %d %d -1 -1 %d %d \n", 0, move_message_number, 0, 1 ); |
| 2762 | break; |
| 2763 | case MoveRecord::REMOVE: |
| 2764 | //move number continues here ! FIXME |
| 2765 | /* I sent a move_message_number here of 33763 by accident and |
| 2766 | * it crashed the official eWeiQi client. There's no error checking |
| 2767 | * there, its potentially an exploit even, not that our client |
| 2768 | * doesn't have those as well but... our client's not done yet... */ |
| 2769 | sprintf(move_str, "REM %d %d %d %d %d %d \n", 0, move_message_number, move->x - 1, move->y - 1, player_number, 0 ); |
| 2770 | break; |
| 2771 | case MoveRecord::UNREMOVE: |
| 2772 | //we can only unremove all stones!, dead stone clear |
| 2773 | if(move->color == stoneErase) |
| 2774 | { |
| 2775 | if(player_number == 1) |
| 2776 | player_number = 2; |
| 2777 | else |
| 2778 | player_number = 1; |
| 2779 | } |
| 2780 | sprintf(move_str, "DSC %d %d %d \n", 1, 0, player_number); |
| 2781 | break; |
| 2782 | case MoveRecord::PASS: |
| 2783 | //we might always send a 0669 as well before the SKI |
| 2784 | //0669 might be the real pass and SKI just the board skip |
| 2785 | sendEndgameMsg(r, pass); |
| 2786 | sprintf(move_str, "SKI %d %d \n", 0, move_message_number); //is that space newline okay? FIXME |
| 2787 | break; |
| 2788 | case MoveRecord::REQUESTUNDO: |
| 2789 | //shouldn't be sent during rated games I don't think |
| 2790 | sendEndgameMsg(r, request_undo); |
| 2791 | return; |
| 2792 | break; |
| 2793 | case MoveRecord::REFUSEUNDO: |
| 2794 | sendEndgameMsg(r, refuse_undo_request); |
| 2795 | return; |
| 2796 | break; |
| 2797 | case MoveRecord::UNDO: |
| 2798 | sendEndgameMsg(r, accept_undo_request); |
| 2799 | int undo_player_number; |
| 2800 | // double check FIXME |
| 2801 | if(r->black_name == getUsername()) |
| 2802 | undo_player_number = 2;//(player_number == 1 ? 2 : 1); |
| 2803 | else |
| 2804 | undo_player_number = 1;//player_number; |
| 2805 | sprintf(move_str, "WIT %d %d %d\n", 0, move_message_number, undo_player_number); |
| 2806 | break; |
| 2807 | case MoveRecord::NONE: |
| 2808 | default: |
| 2809 | sprintf(move_str, "STO %d %d %d %d %d \n", 0, move_message_number, player_number, move->x - 1, move->y - 1); |
| 2810 | break; |
| 2811 | } |
| 2812 | unsigned int length = 16 + strlen(move_str); |
| 2813 | while(length % 4 != 0) |
| 2814 | length++; |
| 2815 | length += 4; |
| 2816 | unsigned char * packet = new unsigned char[length]; |
| 2817 | |
| 2818 | packet[0] = (length >> 8); |
| 2819 | packet[1] = length & 0x00ff; |
| 2820 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2821 | packet[3] = 0x68; |
| 2822 | packet[4] = (r->number >> 8); |
| 2823 | packet[5] = r->number & 0x00ff; |
| 2824 | //definitely seen black here with 0, and our invite |
| 2825 | //also seen black here with 1 and not our invite |
| 2826 | //FIXME we could really try our_invitation here |
| 2827 | //but remember there's that 8th byte that gets |
| 2828 | //wrapped to the next row in the packet dumps |
| 2829 | /*if(!r->our_invitation) |
| 2830 | packet[6] = (r->black_name != getUsername()); |
| 2831 | else |
| 2832 | packet[6] = (r->black_name == getUsername());*/ |
| 2833 | packet[6] = !r->our_invitation; |
| 2834 | //packet[6] = 0x00; |
| 2835 | //packet[6] = (r->black_name != getUsername()); |
| 2836 | //packet[6] = r->white_first_flag; |
| 2837 | //packet[6] = FIRST_BYTE(send_flags); |
| 2838 | packet[7] = 0x01; //this as 0 didn't come back |
| 2839 | //packet[8] is definitely whether we accepted game |
| 2840 | packet[8] = !r->our_invitation; |
| 2841 | for(i = 9; i < 16; i++) |
| 2842 | packet[i] = 0x00; |
| 2843 | |
| 2844 | //int m = move_message_number; |
| 2845 | //if(move->flags == MoveRecord::UNREMOVE) |
| 2846 | // m++; //expects next message? |
| 2847 | packet[12] = (move_message_number_ack >> 8); //this seems like its okay as 0, at least as far as I've seen |
| 2848 | packet[13] = move_message_number_ack & 0x00ff; |
| 2849 | |
| 2850 | strncpy((char *)&(packet[16]), move_str, strlen(move_str)); |
| 2851 | for(i = strlen(move_str) + 16; i < length; i++) |
| 2852 | packet[i] = 0x00; |
| 2853 | |
| 2854 | #ifdef RE_DEBUG |
| 2855 | printf("movepacket: "); |
| 2856 | for(i = 0; i < length; i++) |
| 2857 | printf("%02x ", packet[i]); |
| 2858 | printf("\n"); |
| 2859 | for(i = 0; i < length; i++) |
| 2860 | printf("%c", packet[i]); |
| 2861 | printf("\n"); |
| 2862 | #endif //RE_DEBUG |
| 2863 | encode(packet, (length / 4) - 2); |
| 2864 | #ifdef RE_DEBUG |
| 2865 | qDebug("Sending move"); |
| 2866 | #endif //RE_DEBUG |
| 2867 | if(write((const char *)packet, length) < 0) |
| 2868 | qWarning("*** failed sending move packet"); |
| 2869 | delete[] packet; |
| 2870 | |
| 2871 | if(move->flags == MoveRecord::RESIGN) |
| 2872 | { |
| 2873 | //FIXME, pretty sure this is just if its our game |
| 2874 | //actually maybe loser does always send this |
| 2875 | // !!person who accepted offer sends 0672!! FIXME |
| 2876 | // or maybe not on the above? |
| 2877 | sendMatchResult(r->number); |
| 2878 | if(r->moves < 14) |
| 2879 | { |
| 2880 | GameResult aGameResult; |
| 2881 | aGameResult.result = GameResult::RESIGN; |
| 2882 | if(r->white_name == getUsername()) |
| 2883 | { |
| 2884 | aGameResult.winner_name = r->black_name; |
| 2885 | aGameResult.winner_color = stoneBlack; |
| 2886 | aGameResult.loser_name = r->white_name; |
| 2887 | } |
| 2888 | else |
| 2889 | { |
| 2890 | aGameResult.winner_name = r->white_name; |
| 2891 | aGameResult.winner_color = stoneWhite; |
| 2892 | aGameResult.loser_name = r->black_name; |
| 2893 | } |
| 2894 | boarddispatch->recvResult(&aGameResult); |
| 2895 | } |
| 2896 | } |
| 2897 | else if(move->flags == MoveRecord::DONE_SCORING) |
| 2898 | { |
| 2899 | sendEndgameMsg(r, done_scoring); |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | /* Move this elsewhere in file to be appropriately ordered */ |
| 2904 | void TygemConnection::writeNotNicknameAndRank(char * p, const PlayerListing *player) |
| 2905 | { |
| 2906 | unsigned int ordinal; |
| 2907 | char qualifier; |
| 2908 | |
| 2909 | writeZeroPaddedString(p, player->notnickname, 14); |
| 2910 | p[14] = 0x00; //correct? doublecheck FIXME |
| 2911 | sscanf(player->rank.toLatin1().constData(), "%d%c", &ordinal, &qualifier); |
| 2912 | if(qualifier == 'k') |
| 2913 | p[15] = 0x12 - ordinal; |
| 2914 | else if(qualifier == 'p') |
| 2915 | p[15] = ordinal + 0x1a; |
| 2916 | else |
| 2917 | p[15] = ordinal + 0x11; |
| 2918 | } |
| 2919 | |
| 2920 | void TygemConnection::writeNicknameAndCID(char * p, const PlayerListing * player) |
| 2921 | { |
| 2922 | writeZeroPaddedString(p, player->name, 11); |
| 2923 | p[11] = player->country_id; |
| 2924 | } |
| 2925 | |
| 2926 | /* We could use this in handlePlayerListing except that that would double the calls |
| 2927 | * to new since player listings are copied by room in entering them into the registry. |
| 2928 | * FIXME however, I can't off hand remember why it does that, so I should consider |
| 2929 | * fixing that to not copy construct the registry entries if that's okay |
| 2930 | * with all the registries. */ |
| 2931 | PlayerListing * TygemConnection::getOrCreatePlayerListingFromRecord(char * r) |
| 2932 | { |
| 2933 | Room * room = getDefaultRoom(); |
| 2934 | unsigned char name[15]; |
| 2935 | name[14] = 0x00; |
| 2936 | |
| 2937 | strncpy((char *)name, r, 14); |
| 2938 | //QString encoded_name = QString((char *)name); |
| 2939 | QString rank; |
| 2940 | unsigned char country_id; |
| 2941 | QString encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 2942 | if(r[15] < 0x12) |
| 2943 | rank = QString::number(0x12 - r[15]) + 'k'; |
| 2944 | else if(r[15] > 0x1a) |
| 2945 | rank = QString::number(r[15] - 0x1a) + 'p'; |
| 2946 | else |
| 2947 | rank = QString::number(r[15] - 0x11) + 'd'; |
| 2948 | strncpy((char *)name, &(r[16]), 11); |
| 2949 | //QString encoded_name2 = QString((char *)name); |
| 2950 | QString encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 2951 | country_id = r[27]; |
| 2952 | PlayerListing * player = room->getPlayerListing(encoded_name2); |
| 2953 | return player; |
| 2954 | } |
| 2955 | /* This is in addition to and before the board SKI msg from sendMove() |
| 2956 | * and since its the same form, we're adding the counting messages to |
| 2957 | * it as well. Remember to delete sendCountMsg() FIXME*/ |
| 2958 | void TygemConnection::sendEndgameMsg(const GameData * game, enum EGVersion version) |
| 2959 | { |
| 2960 | unsigned int length; |
| 2961 | if(version == opponent_disconnect_timer) |
| 2962 | length = 0x38; |
| 2963 | else if(version == opponent_reconnects) |
| 2964 | length = 0x34; |
| 2965 | else |
| 2966 | length = 0x30; |
| 2967 | unsigned char * packet = new unsigned char[length]; |
| 2968 | unsigned int i; |
| 2969 | PlayerListing * opponent; |
| 2970 | |
| 2971 | packet[0] = (length >> 8); |
| 2972 | packet[1] = length & 0x00ff; |
| 2973 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 2974 | if(version == pass) |
| 2975 | packet[3] = 0x69; |
| 2976 | else if(version == request_count) |
| 2977 | packet[3] = 0x6a; |
| 2978 | else if(version == accept_count_request || |
| 2979 | version == refuse_count_request) |
| 2980 | packet[3] = 0x6b; |
| 2981 | else |
| 2982 | packet[3] = 0x7b; |
| 2983 | packet[4] = (game->number >> 8); |
| 2984 | packet[5] = game->number & 0x00ff; |
| 2985 | /*if(!game->our_invitation) |
| 2986 | packet[6] = (game->black_name != getUsername()); |
| 2987 | else |
| 2988 | packet[6] = (game->black_name == getUsername());*/ |
| 2989 | packet[6] = !game->our_invitation; |
| 2990 | //packet[6] = (game->black_name == getUsername()); |
| 2991 | if(version == opponent_reconnects) //try this FIXME? |
| 2992 | packet[7] = 0x00; |
| 2993 | else |
| 2994 | packet[7] = 0x01; //see discussions in sendMove about bytes 6, 7 and 8 |
| 2995 | |
| 2996 | //is opp_name encoded or normal? we need to look up so, this matters FIXME |
| 2997 | // Also, there's a boarddispatch->getOpponentName() function, clean this stuff up FIXME |
| 2998 | opponent = getDefaultRoom()->getPlayerListing((game->black_name != getUsername() ? game->black_name : game->white_name)); |
| 2999 | |
| 3000 | if(version == opponent_disconnect_timer) |
| 3001 | { |
| 3002 | writeZeroPaddedString((char *)&(packet[8]), getUsername(), 14); |
| 3003 | for(i = 22; i < 36; i++) |
| 3004 | packet[i] = 0x00; |
| 3005 | } |
| 3006 | else if(version == opponent_reconnects) |
| 3007 | { |
| 3008 | writeZeroPaddedString((char *)&(packet[8]), opponent->notnickname, 14); |
| 3009 | for(i = 22; i < 36; i++) |
| 3010 | packet[i] = 0x00; |
| 3011 | } |
| 3012 | else |
| 3013 | { |
| 3014 | writeNotNicknameAndRank((char *)&(packet[8]), opponent); |
| 3015 | writeNicknameAndCID((char *)&(packet[24]), opponent); |
| 3016 | } |
| 3017 | |
| 3018 | for(i = 36; i < length; i++) |
| 3019 | packet[i] = 0x00; |
| 3020 | |
| 3021 | switch(version) |
| 3022 | { |
| 3023 | case done_scoring: |
| 3024 | packet[37] = 0xc5; |
| 3025 | break; |
| 3026 | case reject_count: |
| 3027 | { |
| 3028 | MoveRecord aMove; |
| 3029 | aMove.flags = MoveRecord::REMOVE_AREA; //really return to match mode |
| 3030 | sendMove(game->number, &aMove); |
| 3031 | packet[37] = 0xc6; |
| 3032 | } |
| 3033 | break; |
| 3034 | case accept_count: |
| 3035 | packet[36] = 0x01; |
| 3036 | packet[37] = 0xc6; |
| 3037 | break; |
| 3038 | // sure this is draw and not adjourn ?!?! |
| 3039 | // looks like it but translate the chinese |
| 3040 | // FIXME |
| 3041 | case request_draw: |
| 3042 | packet[36] = 0x01; |
| 3043 | packet[37] = 0xc3; |
| 3044 | break; |
| 3045 | case accept_draw_request: |
| 3046 | packet[36] = 0x01; |
| 3047 | packet[37] = 0xc4; |
| 3048 | break; |
| 3049 | case refuse_draw_request: |
| 3050 | //packet[36] = 0x00; //implied, same below |
| 3051 | packet[37] = 0xc4; |
| 3052 | break; |
| 3053 | case accept_count_request: |
| 3054 | packet[36] = 0x01; |
| 3055 | break; |
| 3056 | case opponent_disconnect_timer: |
| 3057 | { |
| 3058 | packet[36] = 0x01; |
| 3059 | packet[37] = 0xf1; |
| 3060 | packet[38] = 0x0c; |
| 3061 | //packet[39] = 0x00; |
| 3062 | BoardDispatch * boarddispatch = getIfBoardDispatch(game->number); |
| 3063 | if(!boarddispatch) |
| 3064 | { |
| 3065 | qDebug("Opponent disconnect but no board dispatch"); |
| 3066 | return; |
| 3067 | } |
| 3068 | fillTimeChunk(boarddispatch, &(packet[40])); |
| 3069 | packet[48] = seconds_until_opp_forfeits & 0x00ff; |
| 3070 | packet[49] = (seconds_until_opp_forfeits >> 8); |
| 3071 | } |
| 3072 | break; |
| 3073 | case we_resume: |
| 3074 | packet[36] = 0x01; |
| 3075 | packet[37] = 0xf0; |
| 3076 | //packet[38] = 0x00; |
| 3077 | //packet[39] = 0x00; |
| 3078 | break; |
| 3079 | case opponent_reconnects: |
| 3080 | { |
| 3081 | //packet[36] = 0x00; |
| 3082 | packet[37] = 0xf1; |
| 3083 | packet[38] = 0x08; |
| 3084 | //packet[39] = 0x00; |
| 3085 | BoardDispatch * boarddispatch = getIfBoardDispatch(game->number); |
| 3086 | if(!boarddispatch) |
| 3087 | { |
| 3088 | qDebug("Opponent disconect but no board dispatch"); |
| 3089 | return; |
| 3090 | } |
| 3091 | fillTimeChunk(boarddispatch, &(packet[40])); |
| 3092 | } |
| 3093 | break; |
| 3094 | case request_undo: |
| 3095 | packet[37] = 0xc1; |
| 3096 | break; |
| 3097 | case refuse_undo_request: |
| 3098 | packet[37] = 0xc2; |
| 3099 | break; |
| 3100 | case accept_undo_request: |
| 3101 | packet[36] = 0x01; |
| 3102 | packet[37] = 0xc2; |
| 3103 | break; |
| 3104 | default: |
| 3105 | break; |
| 3106 | } |
| 3107 | |
| 3108 | #ifdef RE_DEBUG |
| 3109 | printf("Endgame packet:\n"); |
| 3110 | for(i = 0; i < length; i++) |
| 3111 | printf("%02x ", packet[i]); |
| 3112 | printf("\n"); |
| 3113 | #endif //RE_DEBUG |
| 3114 | |
| 3115 | /* FIXME, we should somehow have this standard thing attached to all send |
| 3116 | * messages instead of duplicating the code everywhere */ |
| 3117 | encode(packet, (length / 4) - 2); |
| 3118 | qDebug("Sending endgame msg"); |
| 3119 | if(write((const char *)packet, length) < 0) |
| 3120 | qWarning("*** failed sending endgame msg packet"); |
| 3121 | delete[] packet; |
| 3122 | |
| 3123 | BoardDispatch * boarddispatch = getBoardDispatch(game->number); |
| 3124 | if(!boarddispatch) |
| 3125 | { |
| 3126 | qDebug("No board dispatch for our game: %d", game->number); |
| 3127 | return; |
| 3128 | } |
| 3129 | |
| 3130 | if(version != pass && version != opponent_disconnect_timer && |
| 3131 | version != we_resume && version != opponent_reconnects && |
| 3132 | version != request_undo && version != refuse_undo_request && |
| 3133 | version != accept_undo_request) //double check undo and stop time 0683 FIXME |
| 3134 | sendStopTime(boarddispatch, version); |
| 3135 | } |
| 3136 | |
| 3137 | /* These seem like they're sent really liberally: |
| 3138 | - after match offerer hits done_scoring first |
| 3139 | - after match offerer accepts count |
| 3140 | - after match accepter hits done_scoring first |
| 3141 | - after match accepter rejects count |
| 3142 | - after match accepter hits done_scoring second |
| 3143 | - after match acccepter accepts count |
| 3144 | And they're all basically the same... I imagine they're some kind of |
| 3145 | server update but clients could use them for info too, they do get |
| 3146 | them as far as I know so the server does pass them on. |
| 3147 | |
| 3148 | There also might be a stop/start flag... maybe... FIXME |
| 3149 | */ |
| 3150 | void TygemConnection::sendStopTime(BoardDispatch * boarddispatch, enum EGVersion version) |
| 3151 | { |
| 3152 | unsigned int length = 0x18; |
| 3153 | unsigned char * packet = new unsigned char[length]; |
| 3154 | unsigned int i; |
| 3155 | GameData * game = boarddispatch->getGameData(); |
| 3156 | |
| 3157 | packet[0] = (length >> 8); |
| 3158 | packet[1] = length & 0x00ff; |
| 3159 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3160 | packet[3] = 0x83; |
| 3161 | packet[4] = (game->number >> 8); |
| 3162 | packet[5] = game->number & 0x00ff; |
| 3163 | |
| 3164 | packet[6] = 0xaa; //no idea |
| 3165 | packet[7] = 0x01; //color == black ? |
| 3166 | packet[8] = (version == done_scoring ? 0x03 : 0x05); |
| 3167 | packet[9] = 0x00; |
| 3168 | packet[10] = 0x00; //this might be 0x01 if after accept count |
| 3169 | packet[11] = 0x00; |
| 3170 | |
| 3171 | /* Below is taken from sendTime(): (and shifted by 4 bytes, gd => game)*/ |
| 3172 | //03 0a 12 80 03 19 12 40 00 00 00 00 |
| 3173 | //03 07 0f 80 03 30 11 40 00 00 00 00 |
| 3174 | //03 33 13 80 03 23 13 40 00 00 00 00 |
| 3175 | |
| 3176 | fillTimeChunk(boarddispatch, &(packet[12])); |
| 3177 | for(i = 20; i < length; i++) |
| 3178 | packet[i] = 0x00; |
| 3179 | |
| 3180 | #ifdef RE_DEBUG |
| 3181 | printf("stop time packet:\n"); |
| 3182 | for(i = 0; i < length; i++) |
| 3183 | printf("%02x ", packet[i]); |
| 3184 | printf("\n"); |
| 3185 | #endif //RE_DEBUG |
| 3186 | |
| 3187 | encode(packet, (length / 4) - 2); |
| 3188 | qDebug("Sending stop time"); |
| 3189 | if(write((const char *)packet, length) < 0) |
| 3190 | qWarning("*** failed sending stop time packet"); |
| 3191 | delete[] packet; |
| 3192 | //boarddispatch->stopTime(); |
| 3193 | } |
| 3194 | |
| 3195 | /* This appears to be sent by both players as part of |
| 3196 | * resuming a game after a disconnect, could restart time |
| 3197 | * more likely alters game listing in lobby */ |
| 3198 | void TygemConnection::sendResumeFromDisconnect(unsigned int game_number) |
| 3199 | { |
| 3200 | unsigned int length = 0x0c; |
| 3201 | unsigned char * packet = new unsigned char[length]; |
| 3202 | unsigned int i; |
| 3203 | |
| 3204 | packet[0] = (length >> 8); |
| 3205 | packet[1] = length & 0x00ff; |
| 3206 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3207 | packet[3] = 0x8d; |
| 3208 | packet[4] = (game_number >> 8); |
| 3209 | packet[5] = game_number & 0x00ff; |
| 3210 | for(i = 6; i < length; i++) |
| 3211 | packet[i] = 0x00; |
| 3212 | |
| 3213 | encode(packet, (length / 4) - 2); |
| 3214 | qDebug("Sending resume from disconnect"); |
| 3215 | if(write((const char *)packet, length) < 0) |
| 3216 | qWarning("*** failed sending resume from disconnect"); |
| 3217 | delete[] packet; |
| 3218 | } |
| 3219 | |
| 3220 | void TygemConnection::sendOpponentDisconnect(unsigned int game_number, enum OpponentDisconnectUpdate opp) |
| 3221 | { |
| 3222 | unsigned int length = 0x18; |
| 3223 | unsigned char * packet = new unsigned char[length]; |
| 3224 | unsigned int i; |
| 3225 | BoardDispatch * boarddispatch; |
| 3226 | |
| 3227 | boarddispatch = getIfBoardDispatch(game_number); |
| 3228 | if(!boarddispatch) |
| 3229 | { |
| 3230 | qDebug("Can't send opponent disconnect, no board for %d", game_number); |
| 3231 | return; |
| 3232 | } |
| 3233 | GameData * game = boarddispatch->getGameData(); |
| 3234 | packet[0] = (length >> 8); |
| 3235 | packet[1] = length & 0x00ff; |
| 3236 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3237 | packet[3] = 0x81; |
| 3238 | packet[4] = (game->number >> 8); |
| 3239 | packet[5] = game->number & 0x00ff; |
| 3240 | |
| 3241 | for(i = 8; i < length; i++) |
| 3242 | packet[i] = 0x00; |
| 3243 | |
| 3244 | if(opp == opponent_disconnect) |
| 3245 | { |
| 3246 | packet[6] = 0x06; |
| 3247 | //packet[6] = !game->our_invitation; |
| 3248 | packet[7] = 0x01; //not color |
| 3249 | } |
| 3250 | else if(opp == opponent_reconnect) |
| 3251 | { |
| 3252 | packet[6] = 0x01; |
| 3253 | //packet[6] = !game->our_invitation; |
| 3254 | packet[7] = 0x01; //could this be the not invitation of the person to drop? |
| 3255 | fillTimeChunk(boarddispatch, &(packet[8])); |
| 3256 | } |
| 3257 | |
| 3258 | encode(packet, (length / 4) - 2); |
| 3259 | qDebug("Sending opp disconnect"); |
| 3260 | if(write((const char *)packet, length) < 0) |
| 3261 | qWarning("*** failed sending opp disconnect packet"); |
| 3262 | delete[] packet; |
| 3263 | } |
| 3264 | |
| 3265 | /* Sent following the normal one */ |
| 3266 | void TygemConnection::sendLongOpponentDisconnect(unsigned int game_number) |
| 3267 | { |
| 3268 | unsigned int length = 0x40; |
| 3269 | unsigned char * packet = new unsigned char[length]; |
| 3270 | unsigned int i; |
| 3271 | BoardDispatch * boarddispatch; |
| 3272 | |
| 3273 | boarddispatch = getIfBoardDispatch(game_number); |
| 3274 | if(!boarddispatch) |
| 3275 | { |
| 3276 | qDebug("Can't send long opponent disconnect, no board for %d", game_number); |
| 3277 | return; |
| 3278 | } |
| 3279 | GameData * game = boarddispatch->getGameData(); |
| 3280 | packet[0] = (length >> 8); |
| 3281 | packet[1] = length & 0x00ff; |
| 3282 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3283 | packet[3] = 0x52; |
| 3284 | |
| 3285 | game->opponentdropcount++; |
| 3286 | QString disconnect_info = "**Halt Daekuk GAME_BLOCK_IND! - room:" + |
| 3287 | QString::number(game_number) + " <" + |
| 3288 | //be careful here because there's usernames and nicknames FIXME |
| 3289 | (game->white_name == getUsername() ? game->black_name : game->white_name) + |
| 3290 | "> cnt:" + QString::number(game->opponentdropcount); |
| 3291 | unsigned int str_len = strlen(disconnect_info.toLatin1().constData()); |
| 3292 | for(i = 4; i < str_len; i++) |
| 3293 | packet[i] = disconnect_info.toLatin1().constData()[i - 4]; |
| 3294 | for(i = 4 + str_len; i < length; i++) |
| 3295 | packet[i] = 0x00; |
| 3296 | |
| 3297 | encode(packet, (length / 4) - 2); |
| 3298 | qDebug("Sending long opp disconnect"); |
| 3299 | if(write((const char *)packet, length) < 0) |
| 3300 | qWarning("*** failed sending long opp disconnect packet"); |
| 3301 | delete[] packet; |
| 3302 | |
| 3303 | seconds_until_opp_forfeits = 300; |
| 3304 | } |
| 3305 | |
| 3306 | /* FIXME header uses "game_code" everywhere, but that's for oro, |
| 3307 | * tygem uses game_numbers */ |
| 3308 | void TygemConnection::sendOpponentDisconnectTimer(unsigned int game_number) |
| 3309 | { |
| 3310 | BoardDispatch * boarddispatch; |
| 3311 | |
| 3312 | boarddispatch = getIfBoardDispatch(game_number); |
| 3313 | if(!boarddispatch) |
| 3314 | { |
| 3315 | qDebug("Can't send opponent disconnect timer, no board for %d", game_number); |
| 3316 | return; |
| 3317 | } |
| 3318 | GameData * game = boarddispatch->getGameData(); |
| 3319 | seconds_until_opp_forfeits--; |
| 3320 | if(seconds_until_opp_forfeits == -1) |
| 3321 | { |
| 3322 | //just send a straight 0x0672 |
| 3323 | GameResult aGameResult; |
| 3324 | aGameResult.result = GameResult::FORFEIT; |
| 3325 | aGameResult.winner_name = getUsername(); |
| 3326 | if(game->white_name == getUsername()) |
| 3327 | { |
| 3328 | aGameResult.winner_color = stoneWhite; |
| 3329 | aGameResult.loser_name = game->black_name; |
| 3330 | } |
| 3331 | else |
| 3332 | { |
| 3333 | aGameResult.winner_color = stoneBlack; |
| 3334 | aGameResult.loser_name = game->white_name; |
| 3335 | } |
| 3336 | boarddispatch->recvResult(&aGameResult); |
| 3337 | sendLongMatchResult(game_number); |
| 3338 | killTimer(opponentDisconnectTimerID); |
| 3339 | opponentDisconnectTimerID = 0; |
| 3340 | /* FIXME we probably shouldn't have to look this up */ |
| 3341 | PlayerListing * opponent = getDefaultRoom()->getPlayerListing(game->black_name == getUsername() ? game->white_name : game->black_name); |
| 3342 | opponent->online = false; |
| 3343 | emit playerListingReceived(opponent); |
| 3344 | /* Note that we also need to do this if we get tired of waiting and resign the game FIXME FIXME */ |
| 3345 | return; |
| 3346 | } |
| 3347 | /* Next, we need to figure when all these things get sent |
| 3348 | * as well as their being some other messages and custom 0672s |
| 3349 | * both for forfeits as well as DRAW messages |
| 3350 | * but the three bytes in the move and other headers are still |
| 3351 | * wrong, so we should fix that so we can, for instance, send resigns |
| 3352 | * again at least. |
| 3353 | * And then I think that the accepted_match_player needs to be cleared |
| 3354 | * or something and anyway, its an ugly way of doing it, but that |
| 3355 | * affects all the protocols so its a big deal */ |
| 3356 | if((seconds_until_opp_forfeits + 1) % 60 == 0) |
| 3357 | { |
| 3358 | boarddispatch->recvKibitz(0, QString("Opponent forfeits game in %1 minutes.").arg((seconds_until_opp_forfeits + 1) / 60)); |
| 3359 | } |
| 3360 | sendEndgameMsg(game, opponent_disconnect_timer); |
| 3361 | } |
| 3362 | |
| 3363 | /* As far as I can tell, we send the match result if we win... */ |
| 3364 | void TygemConnection::sendResult(GameData * game, GameResult * result) |
| 3365 | { |
| 3366 | //if(!game->our_invitation) |
| 3367 | if(result->winner_name == getUsername()) |
| 3368 | sendLongMatchResult(game->number); |
| 3369 | } |
| 3370 | |
| 3371 | void TygemConnection::sendRequestCount(unsigned int game_id) |
| 3372 | { |
| 3373 | BoardDispatch * bd = getIfBoardDispatch(game_id); |
| 3374 | if(!bd) |
| 3375 | { |
| 3376 | qDebug("No board dispatch for %d", game_id); |
| 3377 | } |
| 3378 | GameData * gd = bd->getGameData(); |
| 3379 | sendEndgameMsg(gd, request_count); |
| 3380 | } |
| 3381 | |
| 3382 | void TygemConnection::sendAcceptCountRequest(GameData * data) |
| 3383 | { |
| 3384 | sendEndgameMsg(data, accept_count_request); |
| 3385 | } |
| 3386 | |
| 3387 | void TygemConnection::sendRefuseCountRequest(GameData * data) |
| 3388 | { |
| 3389 | sendEndgameMsg(data, refuse_count_request); |
| 3390 | } |
| 3391 | |
| 3392 | void TygemConnection::sendRequestDraw(unsigned int game_id) |
| 3393 | { |
| 3394 | BoardDispatch * bd = getIfBoardDispatch(game_id); |
| 3395 | if(!bd) |
| 3396 | { |
| 3397 | qDebug("No board dispatch for %d", game_id); |
| 3398 | } |
| 3399 | GameData * gd = bd->getGameData(); |
| 3400 | sendEndgameMsg(gd, request_draw); |
| 3401 | } |
| 3402 | |
| 3403 | void TygemConnection::sendAcceptDrawRequest(GameData * data) |
| 3404 | { |
| 3405 | sendEndgameMsg(data, accept_draw_request); |
| 3406 | } |
| 3407 | |
| 3408 | void TygemConnection::sendRefuseDrawRequest(GameData * data) |
| 3409 | { |
| 3410 | sendEndgameMsg(data, refuse_draw_request); |
| 3411 | } |
| 3412 | |
| 3413 | void TygemConnection::sendRejectCount(GameData * data) |
| 3414 | { |
| 3415 | sendEndgameMsg(data, reject_count); |
| 3416 | MoveRecord * aMove = new MoveRecord(); //here?? FIXME |
| 3417 | //aMove->color = stoneErase; |
| 3418 | aMove->flags = MoveRecord::UNREMOVE; |
| 3419 | sendMove(data->number, aMove); |
| 3420 | delete aMove; |
| 3421 | match_negotiation_state->startMatch(); //reset passes |
| 3422 | } |
| 3423 | |
| 3424 | void TygemConnection::sendAcceptCount(GameData * data) |
| 3425 | { |
| 3426 | sendEndgameMsg(data, accept_count); |
| 3427 | } |
| 3428 | /* FIXME pretty much all messages that I can think of off the top of my head |
| 3429 | * come back from the server meaning that perhaps we should not act on any |
| 3430 | * messages until they come back? Much like placing stones? */ |
| 3431 | |
| 3432 | |
| 3433 | /* Loser sends this after resign, but I think only if they offered |
| 3434 | * the game |
| 3435 | * Not sure here, looks like winner sends 0672, but again, I've so |
| 3436 | * far only seen that when it was the person offering... |
| 3437 | * offer loser does not send, non-offer-loser does send, I think (0672) |
| 3438 | * also, FIXME, we use "match" and "game" inconsistently */ |
| 3439 | /* I think loser has to send this upon accepting a lost match... ? |
| 3440 | * or maybe I just screwed up the 7b */ |
| 3441 | void TygemConnection::sendMatchResult(unsigned short game_code) |
| 3442 | { |
| 3443 | unsigned int length = 0x30; |
| 3444 | unsigned char * packet = new unsigned char[length]; |
| 3445 | unsigned int i; |
| 3446 | BoardDispatch * boarddispatch = getIfBoardDispatch(game_code); |
| 3447 | if(!boarddispatch) |
| 3448 | { |
| 3449 | qDebug("Can't get board for %d for match result send", game_code); |
| 3450 | return; |
| 3451 | } |
| 3452 | /* We need to notify boarddispatch of finished game so we can |
| 3453 | * get the full result */ |
| 3454 | //boarddispatch->recvResult(0); //doublecheck FIXME don't think we need to do this anymore |
| 3455 | /* Unless we ever send it with the score margin from somewhere else */ |
| 3456 | GameData *aGameData = boarddispatch->getGameData(); |
| 3457 | packet[0] = (length >> 8); |
| 3458 | packet[1] = length & 0x00ff; |
| 3459 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3460 | packet[3] = 0x70; |
| 3461 | packet[4] = (game_code >> 8); |
| 3462 | packet[5] = game_code & 0x00ff; |
| 3463 | |
| 3464 | //packet[6] = 0x00; //winner color |
| 3465 | //packet[6] = (aGameData->black_name == getUsername()) ^ aGameData->white_first_flag; |
| 3466 | //definitely who offered |
| 3467 | packet[6] = !aGameData->our_invitation; |
| 3468 | packet[7] = 0x01; |
| 3469 | |
| 3470 | PlayerListing * opponent = getDefaultRoom()->getPlayerListing(aGameData->black_name == getUsername() ? aGameData->white_name : aGameData->black_name); |
| 3471 | |
| 3472 | writeNotNicknameAndRank((char *)&(packet[8]), opponent); |
| 3473 | writeNicknameAndCID((char *)&(packet[24]), opponent); |
| 3474 | |
| 3475 | //certainly all zeroes if resign |
| 3476 | /* |
| 3477 | if(!aGameData->fullresult) |
| 3478 | { |
| 3479 | qDebug("No result on game data to send result"); |
| 3480 | return; |
| 3481 | } |
| 3482 | float margin = aGameData->fullresult->winner_score - aGameData->fullresult->loser_score; |
| 3483 | packet[10] = (int)margin & 0x00ff; |
| 3484 | packet[11] = ((int)margin >> 8); |
| 3485 | */ |
| 3486 | for(i = 36; i < length; i++) |
| 3487 | packet[i] = 0x00; |
| 3488 | #ifdef RE_DEBUG |
| 3489 | printf("Sending match result:\n"); |
| 3490 | for(i = 0; i < length; i++) |
| 3491 | printf("%02x", packet[i]); |
| 3492 | printf("\n"); |
| 3493 | #endif //RE_DEBUG |
| 3494 | encode(packet, (length / 4) - 2); |
| 3495 | #ifdef RE_DEBUG |
| 3496 | qDebug("Sending match result"); |
| 3497 | #endif //RE_DEBUG |
| 3498 | if(write((const char *)packet, length) < 0) |
| 3499 | qWarning("*** failed sending match result"); |
| 3500 | delete[] packet; |
| 3501 | } |
| 3502 | |
| 3503 | /* FIXME we also use game_code and number inconsistently as holdover from |
| 3504 | * oro code. */ |
| 3505 | void TygemConnection::sendLongMatchResult(unsigned short game_code) |
| 3506 | { |
| 3507 | unsigned int tygem_game_record_str_length; |
| 3508 | unsigned int i; |
| 3509 | |
| 3510 | BoardDispatch * boarddispatch = getIfBoardDispatch(game_code); |
| 3511 | if(!boarddispatch) |
| 3512 | { |
| 3513 | qDebug("Can't get board for %d for match result send", game_code); |
| 3514 | return; |
| 3515 | } |
| 3516 | /* We need to notify boarddispatch of finished game so we can |
| 3517 | * get the full result */ |
| 3518 | //boarddispatch->recvResult(0); |
| 3519 | GameData * game = boarddispatch->getGameData(); |
| 3520 | |
| 3521 | |
| 3522 | QByteArray tygem_game_record = getTygemGameRecordQByteArray(game); |
| 3523 | if(tygem_game_record == QByteArray()) |
| 3524 | return; //error already reported by function |
| 3525 | tygem_game_record_str_length = tygem_game_record.size(); |
| 3526 | #ifdef RE_DEBUG |
| 3527 | printf("Tygem game record length: %d\n", tygem_game_record_str_length); |
| 3528 | #endif //RE_DEBUG |
| 3529 | unsigned int length = 0x28 + tygem_game_record_str_length; |
| 3530 | while(length % 0x10 != 0) |
| 3531 | length++; |
| 3532 | unsigned char * packet = new unsigned char[length]; |
| 3533 | packet[0] = (length >> 8); |
| 3534 | packet[1] = length & 0x00ff; |
| 3535 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3536 | packet[3] = 0x72; |
| 3537 | packet[4] = (game_code >> 8); |
| 3538 | packet[5] = game_code & 0x00ff; |
| 3539 | //6 may not matter? |
| 3540 | //FIXME need to consolidate these headers |
| 3541 | //packet[6] = (game->black_name == getUsername()); |
| 3542 | /*if(!game->our_invitation) |
| 3543 | packet[6] = (game->black_name != getUsername()); |
| 3544 | else |
| 3545 | packet[6] = (game->black_name == getUsername());*/ |
| 3546 | packet[6] = !game->our_invitation; |
| 3547 | packet[7] = 0x01; //see discussions in sendMove about bytes 6, 7 and 8 |
| 3548 | |
| 3549 | PlayerListing * opponent = getDefaultRoom()->getPlayerListing(game->black_name == getUsername() ? game->white_name : game->black_name); |
| 3550 | writeNotNicknameAndRank((char *)&(packet[8]), opponent); |
| 3551 | |
| 3552 | packet[24] = getVictoryCode(*(game->fullresult)); |
| 3553 | packet[25] = 0x00; |
| 3554 | packet[26] = 0x00; |
| 3555 | //packet[27] = 0x3c; //what is this FIXME (score?) |
| 3556 | packet[27] = 0x00; |
| 3557 | packet[28] = (game->moves >> 8); |
| 3558 | packet[29] = game->moves & 0xff; |
| 3559 | packet[30] = 0x00; |
| 3560 | packet[31] = 0x00; |
| 3561 | //03 00 00 00 00 01 00 00 //opp white resigns |
| 3562 | //04 00 00 00 00 0e 00 00 //opp black resigns |
| 3563 | // 0b //draw |
| 3564 | //01 00 05 73 00 6c 00 00 //likely scores |
| 3565 | // |
| 3566 | fillTimeChunk(boarddispatch, &(packet[32])); |
| 3567 | |
| 3568 | for(i = 40; i < tygem_game_record_str_length + 40; i++) |
| 3569 | packet[i] = tygem_game_record.constData()[i - 40]; |
| 3570 | for(i = 40 + tygem_game_record_str_length; i < length; i++) |
| 3571 | packet[i] = 0x00; |
| 3572 | |
| 3573 | #ifdef RE_DEBUG |
| 3574 | printf("long match packet:\n"); |
| 3575 | for(i = 0; i < length; i++) |
| 3576 | printf("%c ", packet[i]); |
| 3577 | printf("\nandinhex:\n"); |
| 3578 | for(i = 0; i < length; i++) |
| 3579 | printf("%02x ", packet[i]); |
| 3580 | printf("\n"); |
| 3581 | fflush(stdout); |
| 3582 | |
| 3583 | //printf("And we're not sending this yet\n"); |
| 3584 | //delete[] packet; |
| 3585 | //return; |
| 3586 | #endif //RE_DEBUG |
| 3587 | |
| 3588 | encode(packet, (length / 4) - 2); |
| 3589 | #ifdef RE_DEBUG |
| 3590 | qDebug("Sending long match result"); |
| 3591 | #endif //RE_DEBUG |
| 3592 | if(write((const char *)packet, length) < 0) |
| 3593 | qWarning("*** failed sending long match result"); |
| 3594 | delete[] packet; |
| 3595 | } |
| 3596 | |
| 3597 | /* FIXME convert from the tygem packets */ |
| 3598 | /* Okay, I think this also happens to be the ".gib" gibo game file format |
| 3599 | * and further, I think you can request listings of players games. Now while |
| 3600 | * that latter requires some other windows to display and track games which |
| 3601 | * might even warrant a thumbnail explorer type window, etc., etc., which |
| 3602 | * basically puts it behind the "watch/fans/friends" list windows and |
| 3603 | * features for me, although I still might want to get the protocol |
| 3604 | * part down... but anyway, the point is, we might eventually change these |
| 3605 | * to a more generic function where the virtuals would just return the |
| 3606 | * specific labels somehow, or fill those labels in, for the different |
| 3607 | * servers and places |
| 3608 | * Actually, I think we should investigate the gamerecord retrieve protocols |
| 3609 | * because that might hint at what sort of info is used where and also |
| 3610 | * allow us to more precisely pin down the differences between the three |
| 3611 | * servers.*/ |
| 3612 | /* Another note is that, I almost feel like putting english words in here |
| 3613 | * or something. For the most part only that last tag is I think read. |
| 3614 | * They also seem to change it sometimes. Like they change which words |
| 3615 | * are used, etc.. Anything mildly appropriate is probably fine. The worst |
| 3616 | * that could happen is someone would have an illegible game record, maybe |
| 3617 | * and even that's assuming the tag isn't read */ |
| 3618 | QByteArray TygemConnection::getTygemGameRecordQByteArray(GameData * game) |
| 3619 | { |
| 3620 | Room * room = getDefaultRoom(); |
| 3621 | const PlayerListing * black, * white; |
| 3622 | int black_ordinal, white_ordinal; |
| 3623 | char black_qualifier, white_qualifier; |
| 3624 | QByteArray white_qualifier_string, black_qualifier_string; |
| 3625 | unsigned char black_level, white_level; |
| 3626 | unsigned short year; |
| 3627 | unsigned char month, day, hour, minute, second; |
| 3628 | secondsToDate(year, month, day, hour, minute, second); |
| 3629 | |
| 3630 | int margin = 0; |
| 3631 | |
| 3632 | black = room->getPlayerListing(game->black_name); |
| 3633 | white = room->getPlayerListing(game->white_name); |
| 3634 | /* FIXME we need the korean dan qualifier character */ |
| 3635 | sscanf(black->rank.toLatin1().constData(), "%d%c", &black_ordinal, &black_qualifier); |
| 3636 | if(black_qualifier == 'k') |
| 3637 | { |
| 3638 | black_level = 0x12 - black_ordinal; |
| 3639 | black_qualifier_string.append("\xb1\xde"); |
| 3640 | } |
| 3641 | else if(black_qualifier == 'p') |
| 3642 | black_level = black_ordinal + 0x1a; |
| 3643 | else |
| 3644 | { |
| 3645 | black_level = black_ordinal + 0x11; |
| 3646 | black_qualifier_string.append("\xb4\xdc"); |
| 3647 | } |
| 3648 | sscanf(white->rank.toLatin1().constData(), "%d%c", &white_ordinal, &white_qualifier); |
| 3649 | if(white_qualifier == 'k') |
| 3650 | { |
| 3651 | white_level = 0x12 - white_ordinal; |
| 3652 | white_qualifier_string.append("\xb1\xde"); |
| 3653 | } |
| 3654 | else if(white_qualifier == 'p') |
| 3655 | white_level = white_ordinal + 0x1a; |
| 3656 | else |
| 3657 | { |
| 3658 | white_level = white_ordinal + 0x11; |
| 3659 | white_qualifier_string.append("\xb4\xdc"); |
| 3660 | } |
| 3661 | |
| 3662 | QByteArray string; |
| 3663 | string += "\\[GIBOKIND=China\\]\r\n"; |
| 3664 | string += "\\[TYPE=0\\]\r\n"; |
| 3665 | string += "\\[GAMECONDITION="; |
| 3666 | |
| 3667 | //there's also: |
| 3668 | //bbe7c8b02031203a20c8a3bcb1 |
| 3669 | //which might be not enough moves |
| 3670 | //or no actually that was when I played a friendly |
| 3671 | //game with a huge handicap |
| 3672 | //c8a3bcb1203a20 |
| 3673 | //room number?? |
| 3674 | //c1fdb9dd20 |
| 3675 | //b0f8c1a6 |
| 3676 | //white player forfeits |
| 3677 | //c8a3bcb1203a20 |
| 3678 | //room number (I think) |
| 3679 | //c1fdb9dd20 |
| 3680 | //b0f8c1a6 |
| 3681 | string.append("\xc1\xa4\xbc\xb1"); |
| 3682 | //c1a4bcb1 seems sufficient for a finished |
| 3683 | //game |
| 3684 | string += "\\]\r\n"; |
| 3685 | string += "\\[GAMETIME="; |
| 3686 | //FIXME doublecheck |
| 3687 | //c1a6 c7d1 bdc3 b0a3 20 minutes ba d0 0d 0a number c3 ca 20 c3 ca c0 |
| 3688 | //d0 b1e2 20 periods b9f8 |
| 3689 | string.append("\xc1\xa6\xc6\xd1\xbd\xc3\xb0\xa3"); |
| 3690 | string += " " + QByteArray::number(game->maintime); |
| 3691 | string.append("\xba\xd0\x0d\x0a"); |
| 3692 | string += QByteArray::number(game->periodtime); |
| 3693 | string.append("\xc3\xca \xc3\xca\xc0\xd0\xb1\xe2"); |
| 3694 | string += " " + QByteArray::number(game->stones_periods); |
| 3695 | string.append("\xb9\xf8"); |
| 3696 | string += "\\]\r\n"; |
| 3697 | string += "\\[GAMERESULT="; //tygem is 3f203332393f203f with the 2 spaces in there |
| 3698 | //3f3332353f3f //w + 325 margin ONLY, no color |
| 3699 | //badac6e5cab1bce4caa4 B + T |
| 3700 | |
| 3701 | switch(game->fullresult->result) |
| 3702 | { |
| 3703 | case GameResult::DRAW: |
| 3704 | string.append("\xb9\xab\xbd\xc2\xba\xce"); |
| 3705 | break; |
| 3706 | case GameResult::FORFEIT: |
| 3707 | if(game->fullresult->winner_color == stoneWhite) |
| 3708 | string.append("\xb9\xe9"); |
| 3709 | else |
| 3710 | string.append("\xc8\xe6"); |
| 3711 | //c8e6 20 bdc3 b0a3 20 c3ca b0fa bdc2 //white forfeits |
| 3712 | string.append(" \xbd\xc3\xb0\xa3 \xc3\xca\xb0\xfa\xbd\xc2"); |
| 3713 | break; |
| 3714 | case GameResult::TIME: |
| 3715 | if(game->fullresult->winner_color == stoneWhite) |
| 3716 | string.append("\xb9\xe9"); |
| 3717 | else |
| 3718 | string.append("\xc8\xe6"); |
| 3719 | //c8e6 20 bdc3 b0a3 bdc2 //white loses on time |
| 3720 | string.append(" \xbd\xc3\xb0\xa3\xbd\xc2"); |
| 3721 | break; |
| 3722 | case GameResult::RESIGN: |
| 3723 | if(game->fullresult->winner_color == stoneWhite) |
| 3724 | { |
| 3725 | //b9e9 20 bad2 b0e8 bdc2 |
| 3726 | //b0d7 c6e5 d6d0 c5cc caa4 W + R |
| 3727 | string.append("\xb9\xe9"); |
| 3728 | } |
| 3729 | else |
| 3730 | { |
| 3731 | //c8e6 20 bad2 b0e8 bdc2 |
| 3732 | string.append("\xc8\xe6"); |
| 3733 | } |
| 3734 | string.append(" \xba\xd2\xb0\xe8\xbd\xc2"); |
| 3735 | break; |
| 3736 | case GameResult::SCORE: |
| 3737 | { |
| 3738 | float fmargin = game->fullresult->winner_score = game->fullresult->loser_score; |
| 3739 | margin = (int)fmargin; |
| 3740 | if(fmargin > (float)margin) |
| 3741 | margin = (margin * 10) + 5; |
| 3742 | else |
| 3743 | margin *= 10; |
| 3744 | qDebug("FIXME margin: %d\n", margin); //margin 200? instead of 303? bug somewhere, somehow? |
| 3745 | //3f20 margin 3f3f203f //black wins |
| 3746 | //3f20 margin 3f203f //white wins? |
| 3747 | //string += QChar(0x3f); |
| 3748 | string += " " + QByteArray::number(margin); |
| 3749 | // QChar(0x3f3f) + " " + QChar(0x3f); |
| 3750 | //pretty sure these ??? are a mistake |
| 3751 | } |
| 3752 | break; |
| 3753 | default: |
| 3754 | break; |
| 3755 | } |
| 3756 | string += "\\]\r\n"; |
| 3757 | string += "\\[GAMEZIPSU=" + QByteArray::number(margin) + "\\]\r\n"; |
| 3758 | string += "\\[GAMEDUM=0\\]\r\n"; |
| 3759 | string += "\\[GAMEGONGJE=0\\]\r\n"; |
| 3760 | //often 65 |
| 3761 | string += "\\[GAMETOTALNUM="; //0xd7dc: 3335cafd" 35? |
| 3762 | //c3d13a20 moves bcf6 |
| 3763 | //this has the number of moves in it I think |
| 3764 | string.append("\xc3\xd1\x3a"); |
| 3765 | string += " " + QByteArray::number(game->moves); |
| 3766 | string.append("\xbc\xf6"); |
| 3767 | string += "\\]\r\n"; |
| 3768 | string += "\\[SZAUDIO=0\\]\r\n"; |
| 3769 | string += "\\[GAMENAME="; |
| 3770 | string.append("\xd3\xd1\xd2\xea\xb6\xd4\xbe\xd6"); |
| 3771 | string += "\\]\r\n"; //c8b3bcb1 |
| 3772 | /* Be careful here also, no idea what this says, we should |
| 3773 | * probably leave it blank */ |
| 3774 | string += "\\[GAMEDATE=" + QByteArray::number(year) + "-"; |
| 3775 | string.append("\xc4\xea"); |
| 3776 | string += QByteArray::number(month); |
| 3777 | string.append("\xd4\xc2"); |
| 3778 | string += QByteArray::number(day); |
| 3779 | string.append("\xc8\xd5 \xcf\xc2\xce\xe7"); |
| 3780 | string += " " + QByteArray::number(hour % 12) + |
| 3781 | (minute < 10 ? ":0" : ":") + QByteArray::number(minute) + "\\]\r\n"; |
| 3782 | /* Note that its very likely that one of the above strings contains a PM versus |
| 3783 | * AM */ |
| 3784 | const char tygem_game_place_array[] = {static_cast<const char>(0xc5),static_cast<const char>(0xb8), |
| 3785 | static_cast<const char>(0xc0),static_cast<const char>(0xcc), |
| 3786 | static_cast<const char>(0xc1),static_cast<const char>(0xaa), |
| 3787 | static_cast<const char>(0x4c),static_cast<const char>(0x69), |
| 3788 | static_cast<const char>(0x76),static_cast<const char>(0x65), |
| 3789 | static_cast<const char>(0xb9),static_cast<const char>(0xd9), |
| 3790 | static_cast<const char>(0xb5),static_cast<const char>(0xcf)}; |
| 3791 | QByteArray tygem_game_place(tygem_game_place_array, 14); |
| 3792 | string += "\\[GAMEPLACE="; |
| 3793 | string += tygem_game_place; |
| 3794 | string += "\\]\r\n"; |
| 3795 | string += "\\[GAMELECNAME=\\]\r\n"; |
| 3796 | string += "\\[GAMEWHITENAME=" + serverCodec->fromUnicode(white->notnickname) + "(" |
| 3797 | + QByteArray::number(white_ordinal) |
| 3798 | + white_qualifier_string + ")\\]\r\n"; |
| 3799 | string += "\\[GAMEWHITELEVEL=" + QByteArray::number(white_level) |
| 3800 | + white_qualifier_string + "\\]\r\n"; |
| 3801 | string += "\\[GAMEWHITENICK=" + serverCodec->fromUnicode(white->name) + "\\]\r\n"; |
| 3802 | string += "\\[GAMEWHITECOUNTRY=" + QByteArray::number(white->country_id) + "\\]\r\n"; |
| 3803 | string += "\\[GAMEWAVATA=1\\]\r\n"; |
| 3804 | string += "\\[GAMEWIMAGE=\\]\r\n"; |
| 3805 | string += "\\[GAMEBLACKNAME=" + serverCodec->fromUnicode(black->notnickname) + "(" |
| 3806 | + QByteArray::number(black_ordinal) |
| 3807 | + black_qualifier_string + ")\\]\r\n"; |
| 3808 | string += "\\[GAMEBLACKLEVEL=" + QByteArray::number(black_level) |
| 3809 | + black_qualifier_string + "\\]\r\n"; |
| 3810 | string += "\\[GAMEBLACKNICK=" + serverCodec->fromUnicode(black->name) + "\\]\r\n"; |
| 3811 | string += "\\[GAMEBLACKCOUNTRY=" + QByteArray::number(black->country_id) + "\\]\r\n"; |
| 3812 | string += "\\[GAMEBAVATA=1\\]\r\n"; |
| 3813 | string += "\\[GAMEBIMAGE=\\]\r\n"; |
| 3814 | string += "\\[GAMECOMMENT=\\]\r\n"; |
| 3815 | //country = 2 = gbkind = gibokind |
| 3816 | string += "\\[GAMEINFOMAIN=GBKIND:2,GTYPE:0,GCDT:1,GTIME:" + |
| 3817 | QByteArray::number(game->maintime) + "-" + |
| 3818 | QByteArray::number(game->periodtime) + "-" + |
| 3819 | QByteArray::number(game->stones_periods) + ",GRLT:0"; |
| 3820 | string += ",ZIPSU:" + QByteArray::number(margin) + |
| 3821 | ",DUM:0,GONGJE:0,TCNT:" + |
| 3822 | QByteArray::number(game->moves); |
| 3823 | //FIXME doublecheck game->moves is accurate |
| 3824 | qDebug("FIXME Game moves: %d\n", game->moves); |
| 3825 | string += ",AUSZ:0\\]\r\n"; |
| 3826 | string += "\\[GAMEINFOSUB=GNAMEF:0,GPLCF:0,GNAME:"; //name is c8a3bcb1 just like name of game |
| 3827 | string += "GDATE:" + QByteArray::number(year) + "- " + QByteArray::number(month) |
| 3828 | + "- " + QByteArray::number(day) + "-" + QByteArray::number(hour) |
| 3829 | + "-" + QByteArray::number(minute) + "-" + QByteArray::number(second); |
| 3830 | string += ",GPLC:" + tygem_game_place; |
| 3831 | string += ",GCMT:\\]\r\n"; |
| 3832 | string += "\\[WUSERINFO=WID:" + serverCodec->fromUnicode(white->notnickname) + ",WLV:" + QByteArray::number(white_level) + |
| 3833 | ",WNICK:" + serverCodec->fromUnicode(white->name) + ",WNCD:" + QByteArray::number(white->country_id) + |
| 3834 | ",WAID:60001,WIMG:\\]\r\n"; |
| 3835 | string += "\\[BUSERINFO=BID:" + serverCodec->fromUnicode(black->notnickname) + ",BLV:" + QByteArray::number(black_level) + |
| 3836 | ",BNICK:" + serverCodec->fromUnicode(black->name) + ",BNCD:" + QByteArray::number(black->country_id) + |
| 3837 | ",BAID:60001,BIMG:\\]\r\n"; |
| 3838 | /* I've seen 60001 for the BAID and WAID, |
| 3839 | * could be the image they use... maybe, ... */ |
| 3840 | /* Here, I'm thinking S0 is black wins, S1 is white wins |
| 3841 | * then again, there's also W1 indicating white win with W0 as white loss */ |
| 3842 | /* I've also seen W4 as indicating W + R */ |
| 3843 | /* I've also seen S1 W7 as B + T also with Z0 indicating that's time remaining |
| 3844 | * or something */ |
| 3845 | /* T is likely time settings, 30 seconds, 3 periods, 1200 total time maybe (20min)*/ |
| 3846 | /* Also, I'm thinking Z and ZIPSU are the score, the margin */ |
| 3847 | |
| 3848 | /* From tygem: W0 B wins by score |
| 3849 | W3 B + R */ |
| 3850 | /* if 3 and 4 are resigns, we might assume 6 and 7 are time, but then there's |
| 3851 | * the question of what 2 and 5 are... we need to see W win by score and by time */ |
| 3852 | /* I suspect this is the stuff that really matters */ |
| 3853 | string += getTygemGameTagQByteArray(game, white_level, black_level, margin); |
| 3854 | |
| 3855 | //tag is simple of earlier info |
| 3856 | return string; |
| 3857 | } |
| 3858 | |
| 3859 | /* Arguably, this is the only part that matters */ |
| 3860 | QByteArray TygemConnection::getTygemGameTagQByteArray(GameData * game, unsigned char white_level, unsigned char black_level, int margin) |
| 3861 | { |
| 3862 | /* This first bit is redundant with the only caller, but |
| 3863 | * I'm starting to feel lazier tonight and I don't want to pass |
| 3864 | * all those arguments or find some more elegant way of doing |
| 3865 | * this nonsense. FIXME */ |
| 3866 | unsigned short year; |
| 3867 | unsigned char month, day, hour, minute, second; |
| 3868 | secondsToDate(year, month, day, hour, minute, second); |
| 3869 | |
| 3870 | const PlayerListing * white, * black; |
| 3871 | |
| 3872 | black = getDefaultRoom()->getPlayerListing(game->black_name); |
| 3873 | white = getDefaultRoom()->getPlayerListing(game->white_name); |
| 3874 | |
| 3875 | QByteArray string; |
| 3876 | string = "\\[GAMETAG=S0,R1,D0,GO"; |
| 3877 | //I see a lot of R0 and G65 |
| 3878 | //R could be rated... |
| 3879 | //S1 or maybe R3 could refer to a rated game |
| 3880 | //R0 means something too though |
| 3881 | string += ",W" + QByteArray::number(getVictoryCode(*(game->fullresult))); |
| 3882 | string += ",Z" + QByteArray::number(margin); |
| 3883 | string += ",T" + |
| 3884 | QByteArray::number(game->periodtime) + "-" + |
| 3885 | QByteArray::number(game->stones_periods) + "-" + |
| 3886 | QByteArray::number(game->maintime) + ",C" + |
| 3887 | QByteArray::number(year) + (month < 10 ? ":0" : ":") + |
| 3888 | QByteArray::number(month) + (day < 10 ? ":0" : ":") + |
| 3889 | QByteArray::number(day) + (hour < 10 ? ":0" : ":") + |
| 3890 | QByteArray::number(hour) + (minute < 10 ? ":0" : ":") + |
| 3891 | QByteArray::number(minute);/* + (second < 10 ? ":0" : ":") + |
| 3892 | QByteArray::number(second);*/ |
| 3893 | string += ",I:" + serverCodec->fromUnicode(white->notnickname) + ",L:" + QByteArray::number(white_level) + |
| 3894 | ",M:" + serverCodec->fromUnicode(black->notnickname) + ",N:" + QByteArray::number(black_level) + |
| 3895 | ",A:" + serverCodec->fromUnicode(white->name) + ",B:" + serverCodec->fromUnicode(black->name) + |
| 3896 | ",J:" + QByteArray::number(white->country_id) + |
| 3897 | ",K:" + QByteArray::number(black->country_id) + "\\]\r\n"; |
| 3898 | qDebug("GAMETAG: %s", string.constData()); |
| 3899 | return string; |
| 3900 | } |
| 3901 | |
| 3902 | unsigned char TygemConnection::getVictoryCode(GameResult & aGameResult) |
| 3903 | { |
| 3904 | switch(aGameResult.result) |
| 3905 | { |
| 3906 | case GameResult::TIME: |
| 3907 | if(aGameResult.winner_color == stoneWhite) |
| 3908 | return 8; |
| 3909 | else |
| 3910 | return 7; |
| 3911 | break; |
| 3912 | case GameResult::RESIGN: |
| 3913 | if(aGameResult.winner_color == stoneWhite) |
| 3914 | return 4; |
| 3915 | else |
| 3916 | return 3; |
| 3917 | break; |
| 3918 | case GameResult::SCORE: |
| 3919 | if(aGameResult.winner_color == stoneWhite) |
| 3920 | return 1; |
| 3921 | else |
| 3922 | return 0; |
| 3923 | break; |
| 3924 | case GameResult::FORFEIT: |
| 3925 | if(aGameResult.winner_color == stoneWhite) |
| 3926 | return 6; |
| 3927 | else |
| 3928 | return 5; |
| 3929 | break; |
| 3930 | case GameResult::DRAW: |
| 3931 | return 2; |
| 3932 | break; |
| 3933 | default: |
| 3934 | break; |
| 3935 | } |
| 3936 | qDebug("Unhandled GameResult code!!"); |
| 3937 | return 0; |
| 3938 | } |
| 3939 | |
| 3940 | /* It seems like this appears like a match invite if opponent closes |
| 3941 | * the window FIXME */ |
| 3942 | void TygemConnection::sendRematchRequest(void) |
| 3943 | { |
| 3944 | } |
| 3945 | |
| 3946 | void TygemConnection::sendRematchAccept(void) |
| 3947 | { |
| 3948 | } |
| 3949 | |
| 3950 | /* This really needs to be a whole packet of stuff but for now, |
| 3951 | * since the full thing requires some chinese translation: */ |
| 3952 | void TygemConnection::sendInvitationSettings(bool invite) |
| 3953 | { |
| 3954 | unsigned int length = 0x0c; |
| 3955 | unsigned char * packet = new unsigned char[length]; |
| 3956 | |
| 3957 | packet[0] = 0x00; |
| 3958 | packet[1] = 0x0c; |
| 3959 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3960 | packet[3] = 0xa2; |
| 3961 | packet[4] = (invite ? 0 : 2); |
| 3962 | packet[5] = 0xf0; |
| 3963 | packet[6] = 0xad; |
| 3964 | packet[7] = 0xba; |
| 3965 | packet[8] = 0x00; |
| 3966 | packet[9] = 0x00; |
| 3967 | packet[10] = 0x00; |
| 3968 | packet[11] = 0x00; //weird byte??? |
| 3969 | |
| 3970 | #ifdef RE_DEBUG |
| 3971 | printf("Sending invite settings: "); |
| 3972 | for(int i = 0; i < (int)length; i++) |
| 3973 | printf("%02x ", packet[i]); |
| 3974 | printf("\n"); |
| 3975 | #endif //RE_DEBUG |
| 3976 | encode(packet, (length / 4) - 2); |
| 3977 | #ifdef RE_DEBUG |
| 3978 | printf("encoded: "); |
| 3979 | for(int i = 0; i < (int)length; i++) |
| 3980 | printf("%02x ", packet[i]); |
| 3981 | printf("\n"); |
| 3982 | #endif //RE_DEBUG |
| 3983 | qDebug("Sending invitation settings\n"); |
| 3984 | if(write((const char *)packet, length) < 0) |
| 3985 | qWarning("*** failed sending invitation settings"); |
| 3986 | delete[] packet; |
| 3987 | } |
| 3988 | |
| 3989 | void TygemConnection::sendDisconnectMsg(void) |
| 3990 | { |
| 3991 | unsigned int length = 8; |
| 3992 | unsigned char * packet = new unsigned char[length]; |
| 3993 | |
| 3994 | packet[0] = 0x00; |
| 3995 | packet[1] = 0x08; |
| 3996 | packet[2] = TYGEM_PROTOCOL_VERSION; |
| 3997 | packet[3] = TYGEM_SERVERDISCONNECT; |
| 3998 | packet[4] = 0x00; |
| 3999 | packet[5] = 0x00; |
| 4000 | packet[6] = 0x00; |
| 4001 | packet[7] = 0x00; |
| 4002 | |
| 4003 | qDebug("Sending server disconnect"); |
| 4004 | if(write((const char *)packet, length) < 0) |
| 4005 | qWarning("*** failed sending server disconnect"); |
| 4006 | delete[] packet; |
| 4007 | } |
| 4008 | |
| 4009 | void TygemConnection::encode(unsigned char * p, unsigned int cycles) |
| 4010 | { |
| 4011 | uint32_t b, c = 0; |
| 4012 | uint32_t header = *(uint32_t *)p; |
| 4013 | header ^= encode_offset; |
| 4014 | p += 4; |
| 4015 | while(cycles--) |
| 4016 | { |
| 4017 | b = *(uint32_t *)p; |
| 4018 | c ^= b; |
| 4019 | b ^= header; |
| 4020 | *(uint32_t *)p = b; |
| 4021 | p += 4; |
| 4022 | } |
| 4023 | *(uint32_t *)p = c; |
| 4024 | } |
| 4025 | |
| 4026 | void TygemConnection::handleMessage(QString) |
| 4027 | { |
| 4028 | } |
| 4029 | |
| 4030 | /* We may convert everything here to unsigned char *s, delete the old... |
| 4031 | * we're still sort of hung up on all the things we were doing |
| 4032 | * to work with the IGS code. */ |
| 4033 | void TygemConnection::handleMessage(unsigned char * msg, unsigned int size) |
| 4034 | { |
| 4035 | unsigned short message_type = msg[3] + (msg[2] << 8); |
| 4036 | int i; |
| 4037 | //message_type = msg[0]; |
| 4038 | //message_type >>= 8; |
| 4039 | //message_type += msg[1]; |
| 4040 | /* Third byte is likely the size of the message meaning multiple |
| 4041 | * messages per packet, etc. */ |
| 4042 | /*if((message_type & 0x00ff) == 0x00b3) |
| 4043 | {*/ |
| 4044 | /*printf("\n****: \n"); |
| 4045 | for(i = 0; i < (int)size; i++) |
| 4046 | printf("%02x", msg[i]); |
| 4047 | printf("\n\n");*/ |
| 4048 | /*}*/ |
| 4049 | |
| 4050 | msg += 4; |
| 4051 | size -=4; |
| 4052 | switch(message_type) |
| 4053 | { |
| 4054 | case TPC(TYGEM_GAMESINIT): |
| 4055 | case TPC(TYGEM_GAMESUPDATE): |
| 4056 | handleGamesList(msg, size); |
| 4057 | break; |
| 4058 | // possibly update after game won, first 00df |
| 4059 | //009806130002000000df0000000100020000ff00706c6d64000000000000000000000011706c6d64003200390000000294ca |
| 4060 | //8ee1944c000000000000000000116c6c36366c6c006e673200010004000000110000010000020000ff00d0c4ccacc8e7c7ef |
| 4061 | //31000000000000006c6f6f706f706f7000390002ccecc9bdc0cf4b0000000000000000086c616f6b31320068657200020001 |
| 4062 | //0000 |
| 4063 | |
| 4064 | |
| 4065 | case TPC(TYGEM_PLAYERSINIT): |
| 4066 | handlePlayerList(msg, size); |
| 4067 | //0653 request should also be sent here FIXME |
| 4068 | break; |
| 4069 | case TPC(TYGEM_PLAYERSUPDATE): |
| 4070 | handlePlayerList(msg, size); |
| 4071 | break; |
| 4072 | case TPC(0x17): |
| 4073 | handleServerRoomChat(msg, size); |
| 4074 | break; |
| 4075 | case TPC(0x18): |
| 4076 | //baec cec0 b1f8 0000 0000 0000 0000 0012 |
| 4077 | //6877 6200 0000 0000 0000 0002 |
| 4078 | //3030ff003d007a00 |
| 4079 | //323fc7ebc7f3b0efd6faa3ac5b3132325db7bfbce4baeccec0b1f8c7ebc7f3b0efd6faa1a |
| 4080 | //328b6d4b2bbcec4c3f7b5c4c6e5d3d1cdb6cbdfa1a3290000000000 |
| 4081 | //some kind of game announcement likely |
| 4082 | |
| 4083 | //obviously announcements |
| 4084 | /* I got one of these on windows shortly before a server disconnect! weird FIXME */ |
| 4085 | #ifdef RE_DEBUG |
| 4086 | printf("0x0618: "); |
| 4087 | for(i = 0; i < (int)size; i++) |
| 4088 | printf("%02x", msg[i]); |
| 4089 | printf("\n"); |
| 4090 | #endif //RE_DEBUG |
| 4091 | break; |
| 4092 | case TPC(TYGEM_OPENCONVERSATION): |
| 4093 | handleOpenConversation(msg, size); |
| 4094 | break; |
| 4095 | case TPC(TYGEM_CONVERSATIONREPLY): |
| 4096 | handleConversationReply(msg, size); |
| 4097 | break; |
| 4098 | case TPC(TYGEM_CONVERSATIONMSG): |
| 4099 | handleConversationMsg(msg, size); |
| 4100 | break; |
| 4101 | case TPC(TYGEM_ROOMCREATED): |
| 4102 | handleCreateRoomResponse(msg, size); |
| 4103 | break; |
| 4104 | case TPC(0x38): |
| 4105 | #ifdef RE_DEBUG |
| 4106 | printf("0x0638: "); |
| 4107 | for(i = 0; i < (int)size; i++) |
| 4108 | printf("%02x", msg[i]); |
| 4109 | printf("\n"); |
| 4110 | #endif //RE_DEBUG |
| 4111 | //break; |
| 4112 | case TPC(TYGEM_BOARDOPENED): //in response to 0638 sendJoin |
| 4113 | handleBoardOpened(msg, size); |
| 4114 | break; |
| 4115 | case TPC(TYGEM_MATCHINVITE): |
| 4116 | //0x0643: 70657465726975730000000000000001706574657269757300000000696e74727573696f6e00000000000 |
| 4117 | // 009696e74727573696f6e0000020100ffff |
| 4118 | handleMatchInvite(msg, size); |
| 4119 | break; |
| 4120 | case TPC(TYGEM_MATCHINVITERESPONSE): |
| 4121 | handleMatchInviteResponse(msg, size); |
| 4122 | break; |
| 4123 | case TPC(TYGEM_MATCHOFFER): |
| 4124 | #ifdef RE_DEBUG |
| 4125 | printf("0645: "); |
| 4126 | for(i = 0; i < (int)size; i++) |
| 4127 | printf("%02x", msg[i]); |
| 4128 | printf("\n"); |
| 4129 | #endif //RE_DEBUG |
| 4130 | if(msg[43] == 0x02) |
| 4131 | handleMatchOffer(msg, size, offer); |
| 4132 | else if(msg[43] == 0x03) //double check |
| 4133 | handleMatchOffer(msg, size, decline); |
| 4134 | else |
| 4135 | printf("*** 0645 match offer has strange type: %02x!!!\n", msg[43]); |
| 4136 | //type 0x1e could be rematch, or something else |
| 4137 | break; |
| 4138 | case TPC(TYGEM_MATCHOFFERRESPONSE): |
| 4139 | if(msg[43] == 0x01) |
| 4140 | handleMatchOffer(msg, size, accept); |
| 4141 | else if(msg[43] == 0x02) |
| 4142 | handleMatchOffer(msg, size, modify); |
| 4143 | //match offer accept |
| 4144 | //00580646 |
| 4145 | //0x0646: 70657465726975730000000000000008696e74727573696f6e000000000000090032012c2801010001 |
| 4146 | //000001ffffffff000000000001000000000000706574657269757300000002696e74727573696f6e000002 |
| 4147 | //00a00616 |
| 4148 | |
| 4149 | |
| 4150 | //rematch related? |
| 4151 | //0646 |
| 4152 | //c4ba d3ea b3bf d4c6 0000 0000 0000 0014 |
| 4153 | //6269 7473 0000 0000 0000 0000 0000 0014 |
| 4154 | //0003 012c 1e03 0000 0000 001e ffff ffff |
| 4155 | //ffff ffff 0001 0001 0000 0100 6a65 7070 |
| 4156 | //3100 0000 0000 0002 6269 7473 0000 0000 |
| 4157 | //0000 0002 |
| 4158 | break; |
| 4159 | case TPC(TYGEM_SERVERPING): |
| 4160 | #ifdef RE_DEBUG |
| 4161 | /*printf("0x064d: "); |
| 4162 | for(i = 0; i < (int)size; i++) |
| 4163 | printf("%02x", msg[i]); |
| 4164 | printf("\n");*/ |
| 4165 | #endif //RE_DEBUG |
| 4166 | handleRequestKeepAlive(msg, size); |
| 4167 | break; |
| 4168 | case TPC(TYGEM_GAMECHAT): //in game chat |
| 4169 | handleGameChat(msg, size); |
| 4170 | break; |
| 4171 | //0633: |
| 4172 | //063370657465726975730000000000000008696e74727573696f6e00000000000009706574657269757300000002696e7 |
| 4173 | //4727573696f6e000002000000000000020000000000 |
| 4174 | |
| 4175 | case TPC(TYGEM_OBSERVERSINIT): |
| 4176 | case TPC(TYGEM_OBSERVERSUPDATE): |
| 4177 | //maybe we do have to request with oro... |
| 4178 | handleObserverList(msg, size); |
| 4179 | break; |
| 4180 | case TPC(TYGEM_MOVE): |
| 4181 | handleMove(msg, size); |
| 4182 | break; |
| 4183 | case TPC(0x69): //first pass |
| 4184 | handlePass(msg, size, 1); |
| 4185 | break; |
| 4186 | case TPC(0x6a): |
| 4187 | handleCountRequest(msg, size); |
| 4188 | break; |
| 4189 | case TPC(0x6b): |
| 4190 | handleCountRequestResponse(msg, size); |
| 4191 | break; |
| 4192 | case TPC(0x70): |
| 4193 | handleGameResult2(msg, size); |
| 4194 | break; |
| 4195 | case TPC(TYGEM_MATCH): |
| 4196 | handleMatchOpened(msg, size); |
| 4197 | break; |
| 4198 | case TPC(TYGEM_MATCHRESULT): |
| 4199 | handleGameResult(msg, size); |
| 4200 | break; |
| 4201 | case TPC(0x74): |
| 4202 | //067401f90001 |
| 4203 | //0x0674: 00910101 |
| 4204 | //I think this is the enter score message... |
| 4205 | //seems to happen after counts... but I don't get |
| 4206 | //who sends it... we get it if we request the count |
| 4207 | //from them and they agree FIXME |
| 4208 | //game number two bytes for colors sender something, also seen CSP nearby |
| 4209 | printf("0x0674: "); |
| 4210 | for(i = 0; i < (int)size; i++) |
| 4211 | printf("%02x", msg[i]); |
| 4212 | printf("\n"); |
| 4213 | //I think this starts the review, maybe not |
| 4214 | break; |
| 4215 | //an REM -1 -1 likely proceeds these as meaning done? |
| 4216 | case TPC(TYGEM_ENDGAMEMSG): |
| 4217 | handleEndgameMsg(msg, size); |
| 4218 | break; |
| 4219 | case TPC(TYGEM_TIME): |
| 4220 | handleTime(msg, size); |
| 4221 | break; |
| 4222 | case TPC(0x81): |
| 4223 | handleGameStateChange(msg, size); |
| 4224 | break; |
| 4225 | case TPC(0x83): //clock stop? //enter score? |
| 4226 | /* This comes a lot during broadcasted games, not |
| 4227 | * certain but could be time update, like stopped time |
| 4228 | * at the time listed */ |
| 4229 | handleScoreMsg1(msg, size); |
| 4230 | break; |
| 4231 | |
| 4232 | // probably 068d requests |
| 4233 | //068e 004d 0000 7065 7465 7269 7573 0000 |
| 4234 | //0000 0000 0000 696e 7472 7573 696f 6e00 |
| 4235 | //0000 0000 0001 |
| 4236 | |
| 4237 | case TPC(TYGEM_SERVERPLAYERCOUNTS): //0653 requests |
| 4238 | //0651 might request this |
| 4239 | //in which case 0651 might not be |
| 4240 | //a disconnect |
| 4241 | handleServerPlayerCounts(msg, size); |
| 4242 | break; |
| 4243 | case TPC(TYGEM_RESUMEBROKENMATCH): |
| 4244 | handleResumeMatch(msg, size); |
| 4245 | break; |
| 4246 | case TPC(0xc7): |
| 4247 | //this is related to either a failed match create or a bad |
| 4248 | //sent message or maybe something legit |
| 4249 | //00280000696e74727573696f6e00000000000009 396b 0000000000000 |
| 4250 | // I think we get these after we send 06c6, so that might |
| 4251 | // be like the creation confirm when its added to the list? |
| 4252 | // anyway, seems legit, I mean just names and ranks here: |
| 4253 | //000c000070657465726975730000000000000008696e74727573696f6e0000000000000900000000 |
| 4254 | #ifdef RE_DEBUG |
| 4255 | printf("***0x06c7: "); |
| 4256 | for(i = 0; i < (int)size; i++) |
| 4257 | printf("%02x", msg[i]); |
| 4258 | printf("\n"); |
| 4259 | #endif //RE_DEBUG |
| 4260 | break; |
| 4261 | //Two bytestrange: 0692: |
| 4262 | //0692696e74727573696f6e00000000000009696e74727573696f6e0049020000000000000000000000000000000000000 |
| 4263 | //0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
| 4264 | //0000000000000000000000000000002710006f756e742c2073746f74616c5f636f756e742c20737769000000000000000 |
| 4265 | //10000000300000004000000056368696e61005f636f756e742c206c746f74616c5f636f0000000000004218b34d6f7573 |
| 4266 | //652e676966006f73735f636f756e742c206c7400000000000000000000000000000000000000000000000000000000004 |
| 4267 | //8f80619000000000000000000000000000000000000000000000000 |
| 4268 | |
| 4269 | //when we're trying to offer |
| 4270 | //706574657269757300000000000000087065746572697573002f490200000000000000000000000000 |
| 4271 | //000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
| 4272 | //0000000000000000000000000000000000000000000000002328006f756e742c2073746f74616c5f636f756e74 |
| 4273 | //2c2073776900000000000000010000000300000004000000056368696e61005f636f756e742c206c746f74616c |
| 4274 | //5f636f000000000000406c8b4d6f7573652e676966006f73735f636f756e742c206c7400000000000000000000 |
| 4275 | //0000000000000000000000000000000000000048fa788200000000000000000000000000000000000000000000 |
| 4276 | //0000 |
| 4277 | |
| 4278 | //70657465726975730000000000000008 |
| 4279 | //706574657269757300c7920200000015 |
| 4280 | //00000011000000040000000000000009 |
| 4281 | //0000000800000001000000000000000c |
| 4282 | //00000009000000030000000000000000 |
| 4283 | //0000000000000000000000000000000c |
| 4284 | //00000009000000030000000000002457584f4f58584f4f4f4f4f4f4f005f636f756e742c2073776900000000000000010000000300000004000000056368696e61005f636f756e742c206c746f74616c5f636f00031b000000406c8b4d6f7573652e676966006f73735f636f756e742c206c74000000000000004e200000000000004e2000000000000000000000000049f2e7010000000000000000000000000000000000000000000061ea |
| 4285 | |
| 4286 | |
| 4287 | case TPC(0x92): |
| 4288 | //possibly related to match opening or negotiation |
| 4289 | //actually, unlikely, we send an 0691 at open |
| 4290 | #ifdef RE_DEBUG |
| 4291 | printf("0x0692: "); |
| 4292 | for(i = 0; i < (int)size; i++) |
| 4293 | printf("%02x", msg[i]); |
| 4294 | printf("\n"); |
| 4295 | #endif //RE_DEBUG |
| 4296 | break; |
| 4297 | case TPC(TYGEM_FRIENDSBLOCKSLIST): |
| 4298 | handleFriendsBlocksList(msg, size); |
| 4299 | break; |
| 4300 | case TPC(0x98): |
| 4301 | //maybe number of games/players? |
| 4302 | //definitely not, this is part of the login or something |
| 4303 | #ifdef RE_DEBUG |
| 4304 | printf("0x0698: "); |
| 4305 | for(i = 0; i < (int)size; i++) |
| 4306 | printf("%02x", msg[i]); |
| 4307 | printf("\n"); |
| 4308 | #endif //RE_DEBUG |
| 4309 | break; |
| 4310 | case TPC(0xca): |
| 4311 | #ifdef RE_DEBUG |
| 4312 | printf("unknown new on login 13ca: "); |
| 4313 | for(i = 0; i < (int)size; i++) |
| 4314 | printf("%02x", msg[i]); |
| 4315 | printf("\n"); |
| 4316 | #endif //RE_DEBUG |
| 4317 | break; |
| 4318 | case TPC(TYGEM_PERSONALCHAT): |
| 4319 | handlePersonalChat(msg, size); |
| 4320 | break; |
| 4321 | case TPC(0xb1): //a name during game? //maybe bets? probably bets FIXME |
| 4322 | //0001 0700 b0fc |
| 4323 | //c3b6 b5bf bdc3 b4eb 0000 0000 0000 0000 01 |
| 4324 | //0000 0000 0000 0003 |
| 4325 | //e86f 6c64 6572 6100 0000 0000 00 |
| 4326 | #ifdef RE_DEBUG |
| 4327 | printf("0x06b1: "); |
| 4328 | for(i = 0; i < (int)size; i++) |
| 4329 | printf("%02x", msg[i]); |
| 4330 | printf("\n"); |
| 4331 | #endif //RE_DEBUG |
| 4332 | break; |
| 4333 | case TPC(0xaf): //login request response? |
| 4334 | #ifdef RE_DEBUG |
| 4335 | printf("0x06af: "); |
| 4336 | for(i = 0; i < (int)size; i++) |
| 4337 | printf("%02x", msg[i]); |
| 4338 | printf("\n"); |
| 4339 | #endif //RE_DEBUG |
| 4340 | //is this right here?!?!? commenting out for now |
| 4341 | //sendInvitationSettings(false); |
| 4342 | break; |
| 4343 | default: |
| 4344 | #ifdef RE_DEBUG |
| 4345 | msg -= 4; //FIXME |
| 4346 | printf("Two bytestrange: %02x%02x: \n", msg[2], msg[3]); |
| 4347 | //#ifdef RE_DEBUG |
| 4348 | for(i = 2; i < (int)size + 4; i++) |
| 4349 | printf("%02x", msg[i]); |
| 4350 | printf("\n"); |
| 4351 | #endif //RE_DEBUG |
| 4352 | break; |
| 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | /* FIXME shouldn't this do something ?? */ |
| 4357 | void TygemConnection::handleConnected(unsigned char * , unsigned int ) |
| 4358 | { |
| 4359 | } |
| 4360 | |
| 4361 | #ifdef RE_DEBUG |
| 4362 | //#define PLAYERLIST_DEBUG |
| 4363 | #endif //RE_DEBUG |
| 4364 | //0613 |
| 4365 | void TygemConnection::handlePlayerList(unsigned char * msg, unsigned int size) |
| 4366 | { |
| 4367 | unsigned char * p = msg; |
| 4368 | unsigned char name[14]; |
| 4369 | QString encoded_name, encoded_name2, rank; |
| 4370 | int players; |
| 4371 | bool no_rank = false; |
| 4372 | bool special_account; |
| 4373 | Room * room = getDefaultRoom(); |
| 4374 | PlayerListing * aPlayer; |
| 4375 | |
| 4376 | players = (p[0] << 8) + p[1]; |
| 4377 | p += 4; |
| 4378 | //printf("Players %d\n", players); |
| 4379 | if(!received_players && players < 512) |
| 4380 | { |
| 4381 | setState(Connected); |
| 4382 | received_players = true; |
| 4383 | } |
| 4384 | //make sure these aren't one off, FIXME, |
| 4385 | //i.e., cutting off last record, also check on ORO |
| 4386 | while(p < (msg + size - 0x37) || (p < (msg + size - 19) && p[0] == 0x01)) |
| 4387 | { |
| 4388 | #ifdef PLAYERLIST_DEBUG |
| 4389 | for(int j = 0; j < 0x38; j++) |
| 4390 | printf("%c", p[j]); |
| 4391 | printf("\n"); |
| 4392 | for(int j = 0; j < 0x38; j++) |
| 4393 | printf("%02x", p[j]); |
| 4394 | printf("\n"); |
| 4395 | #endif //PLAYERLIST_DEBUG |
| 4396 | if(p[0] == 0x01) |
| 4397 | { |
| 4398 | p += 4; |
| 4399 | strncpy((char *)name, (char *)p, 14); |
| 4400 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4401 | aPlayer = room->getPlayerListingByNotNickname(encoded_name); |
| 4402 | /* This "isOngoingMatch" is called too often. FIXME */ |
| 4403 | if((match_negotiation_state->isOngoingMatch() || match_negotiation_state->opponentDisconnected()) && |
| 4404 | match_negotiation_state->verifyPlayer(aPlayer)) |
| 4405 | { |
| 4406 | /* basically, we can't disconnect this player because we need their info to |
| 4407 | * send stuff */ |
| 4408 | |
| 4409 | //FIXME nick or username? probably nick but is that ascii or normal? |
| 4410 | //don't delete the playerlisting if we're still in a game with them |
| 4411 | p += 16; |
| 4412 | continue; |
| 4413 | } |
| 4414 | aPlayer->online = false; |
| 4415 | emit playerListingReceived(aPlayer); |
| 4416 | |
| 4417 | p += 14; |
| 4418 | #ifdef PLAYERLIST_DEBUG |
| 4419 | printf("Player %s disconnected, last bytes: %02x%02x\n", encoded_name.toLatin1().constData(), p[0], p[1]); |
| 4420 | #endif //PLAYERLIST_DEBUG |
| 4421 | p += 2; |
| 4422 | continue; |
| 4423 | } |
| 4424 | p += 2; |
| 4425 | p++; |
| 4426 | #ifdef FIXME |
| 4427 | /* We get disconnects for at least some of these so this isn't ignorable like this. */ |
| 4428 | if(!p[0]) |
| 4429 | { |
| 4430 | //often [Gxxxx] player, I assume guest? |
| 4431 | //0002 0000 5b47 3236 3734 5d32 3000 0000 |
| 4432 | //0000 0000 5b47 3236 3734 5d32 3000 0000 |
| 4433 | //0000 0100 0000 0000 0000 0000 0000 0000 |
| 4434 | //0000 45ab 0001 004b |
| 4435 | #ifdef PLAYERLIST_DEBUG |
| 4436 | printf("Guest\n"); |
| 4437 | #endif //PLAYERLIST_DEBUG |
| 4438 | p += 0x35; |
| 4439 | continue; |
| 4440 | } |
| 4441 | /* I think p[0] is the flag icon, but it can be different |
| 4442 | * pictures too so... */ |
| 4443 | #endif //FIXME |
| 4444 | p++; |
| 4445 | if(p[0] == '[' && p[1] == 'G' && (p[2] == 'S' || p[2] == 'M')) |
| 4446 | special_account = true; |
| 4447 | else |
| 4448 | special_account = false; |
| 4449 | strncpy((char *)name, (char *)p, 14); |
| 4450 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4451 | if(p[14]) |
| 4452 | printf("FIXME player 14 char:\n\n"); |
| 4453 | p += 15; |
| 4454 | //rank byte |
| 4455 | if(p[0] < 0x12) |
| 4456 | rank = QString::number(0x12 - p[0]) + 'k'; |
| 4457 | else if(p[0] > 0x1a) |
| 4458 | rank = QString::number(p[0] - 0x1a) + 'p'; |
| 4459 | else |
| 4460 | rank = QString::number(p[0] - 0x11) + 'd'; |
| 4461 | p++; |
| 4462 | strncpy((char *)name, (char *)p, 11); |
| 4463 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4464 | //another name |
| 4465 | aPlayer = room->getPlayerListing(encoded_name2); |
| 4466 | aPlayer->notnickname = encoded_name; |
| 4467 | aPlayer->name = encoded_name2; |
| 4468 | aPlayer->rank = rank; |
| 4469 | aPlayer->hidden = special_account; |
| 4470 | |
| 4471 | p += 11; |
| 4472 | aPlayer->country_id = p[0]; |
| 4473 | p++; |
| 4474 | p += 4; |
| 4475 | // are these unsigned longs or shorts ?!?!? |
| 4476 | p += 2; |
| 4477 | aPlayer->wins = (p[0] << 8) + p[1]; |
| 4478 | p += 2; |
| 4479 | p += 2; |
| 4480 | aPlayer->losses = (p[0] << 8) + p[1]; |
| 4481 | p += 2; //w/l |
| 4482 | p++; |
| 4483 | if(p[0]) |
| 4484 | { |
| 4485 | //FIXME |
| 4486 | printf("player uses username, not nickname?!?\n"); |
| 4487 | /* If this occurs often, ... well its one thing to store |
| 4488 | * the player on the username, but if it affects the game |
| 4489 | * listing displays, we have to check the byte on the player... |
| 4490 | * meaning a separate variable. I'll see if it happens |
| 4491 | * a lot or if its an issue */ |
| 4492 | } |
| 4493 | p++; |
| 4494 | p += 2; |
| 4495 | if(p[0] == 0xff && p[1] == 0xff) //normally 0000 ----, then ffff ff40 |
| 4496 | no_rank = true; |
| 4497 | p += 2; |
| 4498 | |
| 4499 | if(no_rank) |
| 4500 | { |
| 4501 | aPlayer->rank_score = 0; |
| 4502 | no_rank = false; |
| 4503 | } |
| 4504 | else |
| 4505 | aPlayer->rank_score = (p[0] << 8) + p[1]; |
| 4506 | /* player rank scores MUST Be normalized to be filtered in view properly !! */ |
| 4507 | //aPlayer->rank_score = rankToScore(aPlayer->rank); |
| 4508 | p += 2; |
| 4509 | //p[0] probably means keyi, can contact with game |
| 4510 | if(p[0]) |
| 4511 | aPlayer->info = "X"; |
| 4512 | else |
| 4513 | aPlayer->info = "O"; |
| 4514 | //p[1] |
| 4515 | //00 |
| 4516 | //01 might mean observing |
| 4517 | //02 probably means in a game |
| 4518 | /* I've seen 10, then 11 when we enter room for game and then 12 when we've accepted and game is in progress */ |
| 4519 | //aPlayer->country = QString::number(p[0], 16) + QString::number(p[1], 16); |
| 4520 | p += 2; |
| 4521 | p += 2; |
| 4522 | #ifdef PLAYERLIST_DEBUG |
| 4523 | /* FIXME there's some object code playerlist structure dependency |
| 4524 | * such that if we alter the playerlisting structure, it can |
| 4525 | * screw up the lists or something until we make clean and make again */ |
| 4526 | printf("%s %s %d/%d\n", name, rank.toLatin1().constData(), aPlayer->wins, aPlayer->losses); |
| 4527 | #endif //PLAYERLIST_DEBUG |
| 4528 | emit playerListingReceived(aPlayer); |
| 4529 | } |
| 4530 | |
| 4531 | if(p != (msg + size)) |
| 4532 | { |
| 4533 | qDebug("handlePlayerListing packet strange size %d", (msg + size) - p); |
| 4534 | } |
| 4535 | |
| 4536 | if(received_games && match_negotiation_state->canEnterRematchAdjourned()) |
| 4537 | promptResumeMatch(); |
| 4538 | } |
| 4539 | |
| 4540 | QString TygemConnection::rating_pointsToRank(unsigned int rp) |
| 4541 | { |
| 4542 | int rem = rp % 1000; |
| 4543 | rp -= rem; |
| 4544 | rp /= 1000; |
| 4545 | if(rp < 26) |
| 4546 | { |
| 4547 | rp = 26 - rp; |
| 4548 | return QString::number(rp) + QString("k"); |
| 4549 | } |
| 4550 | else |
| 4551 | { |
| 4552 | rp -= 25; |
| 4553 | return QString::number(rp) + QString("d"); |
| 4554 | } |
| 4555 | return QString("?"); |
| 4556 | } |
| 4557 | |
| 4558 | /* FIXME, I really hate using regular expressions in code that's |
| 4559 | * called this often, but okay for now. Also, its basically the |
| 4560 | * same everywhere, don't know why I thought it would be different |
| 4561 | * per protocol. |
| 4562 | |
| 4563 | * No, there is a problem, the rating points are maybe different and also |
| 4564 | * listed in view on tygem, so they have to be sorted differently. i.e. |
| 4565 | * this should actually return the real tygem rank scores instead of |
| 4566 | * changing the rating_points or rank score, above to this normalized |
| 4567 | * thing. FIXME*/ |
| 4568 | unsigned int TygemConnection::rankToScore(QString rank) |
| 4569 | { |
| 4570 | QString buffer = rank; |
| 4571 | //buffer.replace(QRegExp("[pdk+?\\*\\s]"), ""); |
| 4572 | buffer.replace(QRegExp("[pdk]"), ""); |
| 4573 | int ordinal = buffer.toInt(); |
| 4574 | unsigned int rp; |
| 4575 | |
| 4576 | /* This is for ranking purposes, these are all kind of off and |
| 4577 | * tygem doesn't equate rank to points directly I don't think. |
| 4578 | * might be weighted by win/loss ratio or something. |
| 4579 | * at the very least, its logarithmic */ |
| 4580 | /* right now, the score filter goes up to 5d, no more FIXME */ |
| 4581 | if(rank.contains("k")) |
| 4582 | rp = (19 - ordinal) * 1000; |
| 4583 | else if(rank.contains("d")) |
| 4584 | rp = 18000 + (ordinal * 1000); |
| 4585 | else if(rank.contains("p")) |
| 4586 | rp = 28000 + (ordinal * 1000); |
| 4587 | else |
| 4588 | return 0; |
| 4589 | |
| 4590 | return rp; |
| 4591 | } |
| 4592 | |
| 4593 | //FIXME this was oro code, but tygem has numbers for like 3 countries at least |
| 4594 | QString TygemConnection::getCountryFromCode(unsigned char code) |
| 4595 | { |
| 4596 | code &= 0x0f; |
| 4597 | switch(code) |
| 4598 | { |
| 4599 | case 1: |
| 4600 | return "KOREA"; |
| 4601 | break; |
| 4602 | case 2: |
| 4603 | return "CHINA"; |
| 4604 | break; |
| 4605 | case 3: |
| 4606 | return "JAPAN"; |
| 4607 | break; |
| 4608 | case 4: |
| 4609 | return "INTL"; |
| 4610 | break; |
| 4611 | case 5: |
| 4612 | return "THAILAND"; |
| 4613 | break; |
| 4614 | case 6: |
| 4615 | //that blue flag... |
| 4616 | return "UN?"; |
| 4617 | break; |
| 4618 | case 7: |
| 4619 | return "USA"; |
| 4620 | break; |
| 4621 | case 8: |
| 4622 | return "HONGKONG"; |
| 4623 | break; |
| 4624 | case 9: |
| 4625 | return "SINGAPORE"; |
| 4626 | break; |
| 4627 | case 0xa: |
| 4628 | return "CANADA"; |
| 4629 | break; |
| 4630 | case 0xb: |
| 4631 | return "GERMANY"; |
| 4632 | break; |
| 4633 | case 0xc: |
| 4634 | return "FRANCE"; |
| 4635 | break; |
| 4636 | case 0xd: |
| 4637 | return "AUSTRAILIA"; |
| 4638 | break; |
| 4639 | case 0xe: |
| 4640 | return "RUSSIA"; |
| 4641 | break; |
| 4642 | case 0xf: |
| 4643 | return "UKRAINE"; |
| 4644 | break; |
| 4645 | } |
| 4646 | return QString("-"); |
| 4647 | } |
| 4648 | |
| 4649 | /* FIXME I think this p++, p += n, stuff is a really |
| 4650 | * bad idea now. I need to get that other protocol |
| 4651 | * idea working or just use the numbers somehow. It would |
| 4652 | * be better to have msg[n + offset + offset] then what we're |
| 4653 | * doing now */ |
| 4654 | /* FIXME, we may be getting this twice, meaning we may not have to request it |
| 4655 | * or possibly we may not have to request it for eweiqi or something. */ |
| 4656 | void TygemConnection::handleFriendsBlocksList(unsigned char * msg, unsigned int size) |
| 4657 | { |
| 4658 | //0018 0696 |
| 4659 | //0001 2066 696e 7472 7573 696f 6e00 6672 6965 6e00 |
| 4660 | unsigned char name[15]; |
| 4661 | unsigned int i; |
| 4662 | |
| 4663 | QString encoded_name; |
| 4664 | name[14] = 0x00; |
| 4665 | int records = (msg[0] << 8) + msg[1]; |
| 4666 | if((records * 16) + 4 != (int)size) |
| 4667 | { |
| 4668 | qDebug("FriendsBlock message of strange size: %d", size); |
| 4669 | //return; |
| 4670 | } |
| 4671 | #ifdef RE_DEBUG |
| 4672 | printf("FriendsBlocksList: %d\n", records); |
| 4673 | for(i = 0; i < size; i++) |
| 4674 | printf("%02x", msg[i]); |
| 4675 | printf("\n"); |
| 4676 | #endif //RE_DEBUG |
| 4677 | //i = 2; //2066 |
| 4678 | i = 4; |
| 4679 | while(records--) |
| 4680 | { |
| 4681 | strncpy((char *)name, (char *)&(msg[i]), 14); |
| 4682 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4683 | i += 15; |
| 4684 | /* FIXME somehow, its possible for the same name to be on the official |
| 4685 | * server list more than once */ |
| 4686 | if(msg[i] == 0x00) |
| 4687 | friendedList.push_back(new FriendWatchListing(encoded_name, friendwatch_notify_default)); |
| 4688 | else if(msg[i] == 0xff) |
| 4689 | blockedList.push_back(new FriendWatchListing(encoded_name, friendwatch_notify_default)); |
| 4690 | else |
| 4691 | printf("unknown indicator: %02x\n", msg[i]); |
| 4692 | #ifdef RE_DEBUG |
| 4693 | printf("%02x %s %s\n", msg[i], encoded_name.toLatin1().constData(), name); |
| 4694 | #endif //RE_DEBUG |
| 4695 | i++; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | /* These are the number of players on the other servers |
| 4700 | * FIXME |
| 4701 | * seem they're sent out maybe every 20 seconds*/ |
| 4702 | //0x0654: |
| 4703 | void TygemConnection::handleServerPlayerCounts(unsigned char * msg, unsigned int size) |
| 4704 | { |
| 4705 | unsigned char * p = msg; |
| 4706 | p += 2; |
| 4707 | //int records = (p[0] << 8) + p[1]; |
| 4708 | p += 2; |
| 4709 | #ifdef RE_DEBUG |
| 4710 | //printf("0654 %d unknown records: \n", records); |
| 4711 | #endif //RE_DEBUG |
| 4712 | while(p < (msg + size - 3)) |
| 4713 | { |
| 4714 | #ifdef RE_DEBUG |
| 4715 | // printf("%02x%02x for %d(%02x%02x) %d\n", |
| 4716 | // p[0], p[1], (p[2] << 8) + p[3], p[2], p[3], |
| 4717 | // (p[6] << 8) + p[7]); |
| 4718 | #endif //RE_DEBUG |
| 4719 | p += 8; |
| 4720 | } |
| 4721 | //0100 000d |
| 4722 | //79bd 0913 0000 0b3f |
| 4723 | //79bd 092d 0000 0092 |
| 4724 | //79bd 0914 0000 0909 |
| 4725 | //79bd 0912 0000 001a |
| 4726 | //79bd 094b 0000 04b5 |
| 4727 | //79bd 092c 0000 01ee |
| 4728 | //79bd 097d 0000 00c9 |
| 4729 | //79bd 092f 0000 031a |
| 4730 | //79bd 094c 0000 0302 |
| 4731 | //79bd 0915 0000 0000 |
| 4732 | //79bd 0911 0000 0729 |
| 4733 | //79bd 092e 0000 005c |
| 4734 | //79bd 0915 0000 0004 |
| 4735 | } |
| 4736 | |
| 4737 | /* We might FIXME replace the return on this with a QString |
| 4738 | * when we alter the list view to handle different columns. |
| 4739 | * But for now, moves is pretty accurate for the field */ |
| 4740 | int TygemConnection::getPhase(unsigned char byte) |
| 4741 | { |
| 4742 | /* Note that this is unused right now */ |
| 4743 | switch(byte) |
| 4744 | { |
| 4745 | case 0: |
| 4746 | return 0; |
| 4747 | //open |
| 4748 | break; |
| 4749 | case 1: |
| 4750 | return 30; |
| 4751 | //early |
| 4752 | break; |
| 4753 | case 2: |
| 4754 | return 50; |
| 4755 | //middle |
| 4756 | break; |
| 4757 | case 3: |
| 4758 | return 130; |
| 4759 | //end |
| 4760 | break; |
| 4761 | case 6: //adjourned due to drop, I think |
| 4762 | case 11: |
| 4763 | //looks like wished opponent |
| 4764 | default: |
| 4765 | return -1; |
| 4766 | break; |
| 4767 | } |
| 4768 | } |
| 4769 | |
| 4770 | //this was oro but certainly there's tygem flags we don't get |
| 4771 | QString TygemConnection::getRoomTag(unsigned char byte) |
| 4772 | { |
| 4773 | switch(byte) |
| 4774 | { |
| 4775 | case 0x1f: |
| 4776 | case 0x3d: |
| 4777 | //normal and broadcast in progress |
| 4778 | return QString(); |
| 4779 | break; |
| 4780 | case 0x04: |
| 4781 | return QString("** chat 04"); |
| 4782 | break; |
| 4783 | case 0x1a: |
| 4784 | return QString("** chat 1a"); |
| 4785 | break; |
| 4786 | case 0x70: |
| 4787 | //likely chat 0 locked? |
| 4788 | return QString("** chat 70"); |
| 4789 | break; |
| 4790 | case 0x3c: |
| 4791 | return QString("** review 3c"); |
| 4792 | break; |
| 4793 | case 0x19: |
| 4794 | return QString("** chat 19"); |
| 4795 | break; |
| 4796 | case 0x1c: |
| 4797 | return QString("** quit? 19"); |
| 4798 | break; |
| 4799 | case 0x0b: |
| 4800 | return QString("** wished 0b"); |
| 4801 | break; |
| 4802 | case 0x0f: |
| 4803 | return QString("** one color 0f"); |
| 4804 | break; |
| 4805 | default: |
| 4806 | return (QString("** ") + QString::number(byte, 16)); |
| 4807 | break; |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | #ifdef RE_DEBUG |
| 4812 | //#define GAMESLIST_DEBUG |
| 4813 | #endif //RE_DEBUG |
| 4814 | //0612 |
| 4815 | void TygemConnection::handleGamesList(unsigned char * msg, unsigned int size) |
| 4816 | { |
| 4817 | unsigned char * p = msg; |
| 4818 | unsigned char * game_record; |
| 4819 | unsigned int id; |
| 4820 | unsigned int name_length; |
| 4821 | unsigned char name[15]; |
| 4822 | unsigned char flags; |
| 4823 | Room * room = getDefaultRoom(); |
| 4824 | QString encoded_nameA, encoded_nameB, rankA, rankB; |
| 4825 | QString encoded_nameA2, encoded_nameB2; |
| 4826 | GameListing * aGameListing; |
| 4827 | |
| 4828 | name[14] = 0x00; |
| 4829 | /*printf("Gamelist message:\n"); |
| 4830 | for(i = 0; i < size; i++) |
| 4831 | printf("%02x", msg[i]); |
| 4832 | printf("\n");*/ |
| 4833 | //unsigned int number_of_games = (p[0] << 8) + p[1]; |
| 4834 | p += 4; |
| 4835 | while(p < (msg + size - 0x48) || (p < (msg + size - 3) && p[2] == 0x01)) |
| 4836 | { |
| 4837 | game_record = p; |
| 4838 | id = (game_record[0] << 8) + game_record[1]; |
| 4839 | #ifdef GAMESLIST_DEBUG |
| 4840 | printf("Id: %d:\n", id); |
| 4841 | #endif //GAMESLIST_DEBUG |
| 4842 | aGameListing = room->getGameListing(id); |
| 4843 | aGameListing->running = true; |
| 4844 | aGameListing->FR = ""; |
| 4845 | if(game_record[2] == 0x01) |
| 4846 | { |
| 4847 | |
| 4848 | #ifdef GAMESLIST_DEBUG |
| 4849 | printf("Id: %d:\n", id); |
| 4850 | printf("is ended!!!\n"); |
| 4851 | for(unsigned int i = 0; i < 4; i++) |
| 4852 | printf("%c", p[i]); |
| 4853 | printf("\n"); |
| 4854 | #endif //GAMESLIST_DEBUG |
| 4855 | |
| 4856 | p += 4; |
| 4857 | aGameListing->running = false; |
| 4858 | room->recvGameListing(aGameListing); |
| 4859 | continue; |
| 4860 | } |
| 4861 | if(game_record[3] & 0x01) |
| 4862 | { |
| 4863 | aGameListing->FR += "L"; |
| 4864 | aGameListing->isLocked = true; |
| 4865 | } |
| 4866 | //p[4] == 0x02 is some other kind of record |
| 4867 | #ifdef GAMESLIST_DEBUG |
| 4868 | for(unsigned int i = 0; i < 0x4c; i++) |
| 4869 | { |
| 4870 | printf("%c", p[i]); |
| 4871 | } |
| 4872 | printf("\n"); |
| 4873 | for(unsigned int i = 0; i < 0x4c; i++) |
| 4874 | { |
| 4875 | printf("%02x", p[i]); |
| 4876 | } |
| 4877 | printf("\n"); |
| 4878 | #endif //GAMESLIST_DEBUG |
| 4879 | p += 2; //past id |
| 4880 | //0000 0001 0002 0000 ff00 7978 7973 7a00 |
| 4881 | p++; |
| 4882 | //if(p[0] == 0x00) |
| 4883 | //p += 2; |
| 4884 | p++; |
| 4885 | |
| 4886 | p++; |
| 4887 | //impacts time |
| 4888 | //there's also 0x30, I think 0x50, etc.. issues here |
| 4889 | aGameListing->white_first_flag = p[0] & 0x01; //game_record[5] |
| 4890 | //aGameListing->FR += QString::number(p[0], 16) + " "; |
| 4891 | /* 40 looks like a lit 2 computer screens, 50 like 2 dark computers */ |
| 4892 | /* 30 is just betting and over*/ |
| 4893 | /* Thinking 40 bit is broadcast and 20 bit is betting */ |
| 4894 | p++; |
| 4895 | aGameListing->observers = (game_record[6] << 8) + game_record[7]; |
| 4896 | //We have an issue here I think FIXME with observer numbers |
| 4897 | p += 2; |
| 4898 | //int some_number = (p[0] << 8) + p[1]; // This number seems to be either 0 or -1 |
| 4899 | p += 2; |
| 4900 | p++; |
| 4901 | //aGameListing->FR = ""; |
| 4902 | //p += 2; |
| 4903 | flags = p[0]; |
| 4904 | |
| 4905 | //FIXME we need a flag parse function |
| 4906 | //but note that we might want a variable return value in the sense |
| 4907 | //that maybe we want to get the flags, maybe we want to set the name of the black |
| 4908 | //player to ** review ** or something... |
| 4909 | if(flags & 0x80) |
| 4910 | { |
| 4911 | // Betting |
| 4912 | aGameListing->FR += "B"; |
| 4913 | } |
| 4914 | if(flags & 0x20) |
| 4915 | { |
| 4916 | //probably broadcast, maybe as well as betting |
| 4917 | aGameListing->FR += "R"; |
| 4918 | } |
| 4919 | //ff00 |
| 4920 | p++; |
| 4921 | |
| 4922 | //name 1 |
| 4923 | strncpy((char *)name, (char *)p, 14); |
| 4924 | encoded_nameA = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4925 | p += 15; |
| 4926 | //rank byte |
| 4927 | if(p[0] < 0x12) |
| 4928 | rankA = QString::number(0x12 - p[0]) + 'k'; |
| 4929 | else if(p[0] > 0x1a) |
| 4930 | rankA = QString::number(p[0] - 0x1a) + 'p'; |
| 4931 | else |
| 4932 | rankA = QString::number(p[0] - 0x11) + 'd'; |
| 4933 | p++; |
| 4934 | strncpy((char *)name, (char *)p, 11); |
| 4935 | #ifdef GAMESLIST_DEBUG |
| 4936 | printf("%s vs.", name); |
| 4937 | #endif //GAMESLIST_DEBUG |
| 4938 | encoded_nameA2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4939 | p += 12; |
| 4940 | if(p[0] == 0x00) |
| 4941 | { |
| 4942 | p += 16; |
| 4943 | encoded_nameB = QString(); |
| 4944 | encoded_nameB2 = QString(); |
| 4945 | rankB = QString(); |
| 4946 | } |
| 4947 | else |
| 4948 | { |
| 4949 | strncpy((char *)name, (char *)p, 14); |
| 4950 | encoded_nameB = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4951 | p += 15; |
| 4952 | if(p[0] < 0x12) |
| 4953 | rankB = QString::number(0x12 - p[0]) + 'k'; |
| 4954 | else if(p[0] > 0x1a) |
| 4955 | rankB = QString::number(p[0] - 0x1a) + 'p'; |
| 4956 | else |
| 4957 | rankB = QString::number(p[0] - 0x11) + 'd'; |
| 4958 | p++; |
| 4959 | strncpy((char *)name, (char *)p, 11); //chinese |
| 4960 | #ifdef GAMESLIST_DEBUG |
| 4961 | printf(" %s\n", name); |
| 4962 | #endif //GAMESLIST_DEBUG |
| 4963 | encoded_nameB2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 4964 | } |
| 4965 | p += 12; |
| 4966 | |
| 4967 | p++; |
| 4968 | //this p[0] should be game status |
| 4969 | //aGameListing->FR += QString::number(p[0], 16); |
| 4970 | switch(game_record[73]) |
| 4971 | { |
| 4972 | case 0: |
| 4973 | aGameListing->FR += "?"; //looking |
| 4974 | break; |
| 4975 | case 1: |
| 4976 | aGameListing->moves = 0; |
| 4977 | break; |
| 4978 | case 2: |
| 4979 | aGameListing->moves = 50; |
| 4980 | break; |
| 4981 | case 3: |
| 4982 | aGameListing->moves = 120; |
| 4983 | break; |
| 4984 | case 4: |
| 4985 | aGameListing->moves = (unsigned)-1; //over |
| 4986 | break; |
| 4987 | case 5: |
| 4988 | aGameListing->FR += "V"; //review |
| 4989 | break; |
| 4990 | case 6: |
| 4991 | aGameListing->FR += "A"; //adjourned due to drop? |
| 4992 | break; |
| 4993 | case 11: |
| 4994 | aGameListing->FR += "W"; //wished opponent? |
| 4995 | break; |
| 4996 | } |
| 4997 | p++; |
| 4998 | p++; |
| 4999 | //name_length = p[0]; |
| 5000 | p++; |
| 5001 | //p += name_length; |
| 5002 | //2 bytes here, 0001 or 0004 or 0006 or 0003, etc.. |
| 5003 | p += 2; |
| 5004 | name_length = (p[0] << 8) + p[1]; |
| 5005 | //if(different_game_record) |
| 5006 | //apparently has nothing to do with earlier, |
| 5007 | //might be another flag FIXME |
| 5008 | p += 2; |
| 5009 | if(1) |
| 5010 | { |
| 5011 | if(name_length) |
| 5012 | { |
| 5013 | char * comment = new char[name_length + 1]; |
| 5014 | comment[name_length] = '\0'; |
| 5015 | strncpy(comment, (char *)p, name_length); //FIXME next line strlen |
| 5016 | aGameListing->comment = serverCodec->toUnicode(comment, strlen(comment)); |
| 5017 | delete comment; |
| 5018 | } |
| 5019 | p += name_length; |
| 5020 | } |
| 5021 | |
| 5022 | /* Don't remember why this can't get the info from the players? FIXME*/ |
| 5023 | if(aGameListing->white_first_flag) |
| 5024 | { |
| 5025 | aGameListing->_white_name = encoded_nameA2; |
| 5026 | aGameListing->_white_rank = rankA; |
| 5027 | aGameListing->_white_rank_score = rankToScore(rankA); |
| 5028 | aGameListing->_black_name = encoded_nameB2; |
| 5029 | aGameListing->_black_rank = rankB; |
| 5030 | aGameListing->_black_rank_score = rankToScore(rankB); |
| 5031 | } |
| 5032 | else |
| 5033 | { |
| 5034 | aGameListing->_white_name = encoded_nameB2; |
| 5035 | aGameListing->_white_rank = rankB; |
| 5036 | aGameListing->_white_rank_score = rankToScore(rankB); |
| 5037 | aGameListing->_black_name = encoded_nameA2; |
| 5038 | aGameListing->_black_rank = rankA; |
| 5039 | aGameListing->_black_rank_score = rankToScore(rankA); |
| 5040 | } |
| 5041 | room->recvGameListing(aGameListing); |
| 5042 | if(initial_connect && match_negotiation_state->newMatchAllowed()) |
| 5043 | { |
| 5044 | if(encoded_nameA2 == getUsername()) |
| 5045 | match_negotiation_state->setupRematchAdjourned(id, encoded_nameB2); |
| 5046 | else if(encoded_nameB2 == getUsername()) |
| 5047 | match_negotiation_state->setupRematchAdjourned(id, encoded_nameA2); |
| 5048 | } |
| 5049 | } |
| 5050 | initial_connect = false; |
| 5051 | received_games = true; |
| 5052 | if(received_players && match_negotiation_state->canEnterRematchAdjourned()) |
| 5053 | promptResumeMatch(); |
| 5054 | } |
| 5055 | |
| 5056 | //shouldn't this be somewhere more standard? FIXME |
| 5057 | void TygemConnection::promptResumeMatch(void) |
| 5058 | { |
| 5059 | unsigned short game_number = match_negotiation_state->getGameId(); |
| 5060 | GameListing * gameListing = getDefaultRoom()->getGameListing(game_number); |
| 5061 | |
| 5062 | QString opp_name = (gameListing->white_name() == getUsername() ? gameListing->black_name() : gameListing->white_name()); |
| 5063 | if (QMessageBox::question(0, tr("Resume match?"), |
| 5064 | QString(tr("Resume match in progress with %1?\n").arg(opp_name))) |
| 5065 | == QMessageBox::Yes) |
| 5066 | { |
| 5067 | qDebug("Trying to rejoin %d", game_number); |
| 5068 | sendJoin(game_number); |
| 5069 | sendObserversRequest(game_number); |
| 5070 | sendResume(game_number); |
| 5071 | } |
| 5072 | else |
| 5073 | { |
| 5074 | //we have to do something else here, FIXME either, nothing, no choice, |
| 5075 | //or we need to change the match negotiation state or send a resign or... nothing is |
| 5076 | //probably best.... i.e., no "Yes" but informational |
| 5077 | //check official client, might be able to resign |
| 5078 | /* Actually, hitting "No" is a-okay in the official client and it does nothing, |
| 5079 | * opponent still has to wait out the five minutes. */ |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | void TygemConnection::handleRequestKeepAlive(unsigned char * msg, unsigned int size) |
| 5084 | { |
| 5085 | if(size != 4) |
| 5086 | { |
| 5087 | qDebug("setup keep alive of size %d", size); |
| 5088 | } |
| 5089 | #ifdef RE_DEBUG |
| 5090 | //printf("Keepalive IV %8x\n", *(uint32_t *)msg); |
| 5091 | #endif //RE_DEBUG |
| 5092 | uint32_t keepaliveIV = encodeKeepAliveIV(*(uint32_t *)msg); |
| 5093 | #ifdef RE_DEBUG |
| 5094 | //printf("Keepalive IV encoded %8x\n", keepaliveIV); |
| 5095 | #endif //RE_DEBUG |
| 5096 | sendServerKeepAlive(keepaliveIV); |
| 5097 | } |
| 5098 | |
| 5099 | void TygemConnection::handleServerAnnouncement(unsigned char * msg, unsigned int size) |
| 5100 | { |
| 5101 | unsigned char * p = msg; |
| 5102 | |
| 5103 | #ifdef RE_DEBUG |
| 5104 | printf("0xf659: "); |
| 5105 | for(unsigned int i = 0; i < size; i++) |
| 5106 | printf("%02x", msg[i]); |
| 5107 | printf("\n"); |
| 5108 | #endif //RE_DEBUG |
| 5109 | p += 2; |
| 5110 | |
| 5111 | /* FIXME, likely first two bytes are something else */ |
| 5112 | QString u; |
| 5113 | |
| 5114 | u = serverCodec->toUnicode((const char *)p, size - 2); |
| 5115 | //u = codec->toUnicode(b, size - 4); |
| 5116 | if(console_dispatch) |
| 5117 | console_dispatch->recvText("*** " + u); |
| 5118 | } |
| 5119 | |
| 5120 | void TygemConnection::handleServerRoomChat(unsigned char * msg, unsigned int size) |
| 5121 | { |
| 5122 | unsigned char * p = msg; |
| 5123 | unsigned char * text = new unsigned char[size - 0x23]; |
| 5124 | unsigned char name[15]; |
| 5125 | name[14] = 0x00; |
| 5126 | QString encoded_name, encoded_name2, rank; |
| 5127 | #ifdef RE_DEBUG |
| 5128 | printf("serverchat: \n"); |
| 5129 | for(unsigned int i = 0; i < size; i++) |
| 5130 | printf("%02x", msg[i]); |
| 5131 | printf("\n"); |
| 5132 | #endif //RE_DEBUG |
| 5133 | p += 4; |
| 5134 | strncpy((char *)name, (char *)&(msg[4]), 14); |
| 5135 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5136 | p += 15; |
| 5137 | if(msg[15] < 0x12) |
| 5138 | rank = QString::number(0x12 - msg[15]) + 'k'; |
| 5139 | else if(msg[15] > 0x1a) |
| 5140 | rank = QString::number(msg[15] - 0x1a) + 'p'; |
| 5141 | else |
| 5142 | rank = QString::number(msg[15] - 0x11) + 'd'; |
| 5143 | p++; |
| 5144 | strncpy((char *)name, (char *)&(msg[16]), 11); |
| 5145 | //we lost first three letters here last I checked |
| 5146 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5147 | p += 0x10; |
| 5148 | //don't know what that one byte is, but its not part of text |
| 5149 | p++; |
| 5150 | strncpy((char *)text, (char *)&(msg[37]), size - 0x24); |
| 5151 | |
| 5152 | //Room * room = getDefaultRoom(); //3 |
| 5153 | //PlayerListing * player = room->getPlayerListing(player_id); |
| 5154 | //if(player) |
| 5155 | //{ |
| 5156 | #ifdef RE_DEBUG |
| 5157 | printf("%s says: \n", name); |
| 5158 | for(int i = 0; i < (int)size - 0x24; i++) |
| 5159 | printf("%02x", text[i]); |
| 5160 | printf("\n"); |
| 5161 | #endif //RE_DEBUG |
| 5162 | QString u; |
| 5163 | |
| 5164 | u = serverCodec->toUnicode((const char *)text, strlen((char *)text)); |
| 5165 | //u = codec->toUnicode(b, size - 4); |
| 5166 | if(console_dispatch) |
| 5167 | console_dispatch->recvText(encoded_name2 + '[' + rank + "]: " + u); |
| 5168 | /*} |
| 5169 | else |
| 5170 | printf("unknown player says something");*/ |
| 5171 | delete[] text; |
| 5172 | } |
| 5173 | |
| 5174 | //has garbled stuff on the end, maybe due to unicode, still FIXME |
| 5175 | //0661 |
| 5176 | void TygemConnection::handleGameChat(unsigned char * msg, unsigned int size) |
| 5177 | { |
| 5178 | unsigned char * p = msg; |
| 5179 | unsigned char * text; |
| 5180 | unsigned char name[15]; |
| 5181 | name[14] = 0x00; |
| 5182 | BoardDispatch * boarddispatch; |
| 5183 | int room_number; |
| 5184 | unsigned int size_of_message; |
| 5185 | QString encoded_name, encoded_name2; |
| 5186 | QString rank; |
| 5187 | #ifdef RE_DEBUG |
| 5188 | int i; |
| 5189 | |
| 5190 | printf("** game chat size: %d: ", size); |
| 5191 | for(i = 0; i < (int)size; i++) |
| 5192 | printf("%02x", p[i]); |
| 5193 | printf("\n"); |
| 5194 | #endif //RE_DEBUG |
| 5195 | //Room * room = getDefaultRoom(); |
| 5196 | room_number = (p[0] << 8) + p[1]; |
| 5197 | boarddispatch = getIfBoardDispatch(room_number); |
| 5198 | if(!boarddispatch) |
| 5199 | { |
| 5200 | qDebug("Can't get board for %d for chat msg", room_number); |
| 5201 | return; |
| 5202 | } |
| 5203 | p += 0x14; |
| 5204 | strncpy((char *)name, (char *)p, 14); |
| 5205 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5206 | p += 15; |
| 5207 | if(p[0] < 0x12) |
| 5208 | rank = QString::number(0x12 - p[0]) + 'k'; |
| 5209 | else if(p[0] > 0x1a) |
| 5210 | rank = QString::number(p[0] - 0x1a) + 'p'; |
| 5211 | else |
| 5212 | rank = QString::number(p[0] - 0x11) + 'd'; |
| 5213 | p++; |
| 5214 | strncpy((char *)name, (char *)p, 11); |
| 5215 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5216 | if(encoded_name2 == getUsername()) |
| 5217 | { |
| 5218 | //block as echo |
| 5219 | //but then our rank doesn't appear ... FIXME check with oro, igs |
| 5220 | return; |
| 5221 | } |
| 5222 | p += 0x1c; |
| 5223 | size_of_message = p[0]; |
| 5224 | if(size_of_message > size - 66) |
| 5225 | { |
| 5226 | qDebug("Bad in game chat message size"); |
| 5227 | return; |
| 5228 | } |
| 5229 | |
| 5230 | p += 4; |
| 5231 | //again, init number not part of text |
| 5232 | p++; |
| 5233 | text = new unsigned char[size_of_message + 1]; |
| 5234 | strncpy((char *)text, (char *)p, size_of_message); |
| 5235 | text[size_of_message] = 0x00; |
| 5236 | //PlayerListing * player = room->getPlayerListing(player_id); |
| 5237 | //if(player) |
| 5238 | //{ |
| 5239 | |
| 5240 | QString u; |
| 5241 | |
| 5242 | u = serverCodec->toUnicode((const char *)text, strlen((char *)text)); |
| 5243 | //u = codec->toUnicode(b, size - 4); |
| 5244 | boarddispatch->recvKibitz(encoded_name2 + "[" + rank + "]", u); |
| 5245 | /*} |
| 5246 | else |
| 5247 | printf("unknown player says something");*/ |
| 5248 | delete[] text; |
| 5249 | } |
| 5250 | |
| 5251 | void TygemConnection::handleOpenConversation(unsigned char * msg, unsigned int size) |
| 5252 | { |
| 5253 | /* We're not going to bother negotiating this, get the player |
| 5254 | * and send a reply */ |
| 5255 | PlayerListing * player; |
| 5256 | QString encoded_name, encoded_name2; |
| 5257 | QString rank; |
| 5258 | unsigned char name[15]; |
| 5259 | name[14] = 0x00; |
| 5260 | |
| 5261 | if(size < 30) |
| 5262 | { |
| 5263 | qDebug("Bad conversation open size: %d", size); |
| 5264 | return; |
| 5265 | } |
| 5266 | |
| 5267 | strncpy((char *)name, (char *)msg, 14); |
| 5268 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5269 | if(msg[15] < 0x12) |
| 5270 | rank = QString::number(0x12 - msg[15]) + 'k'; |
| 5271 | else if(msg[15] > 0x1a) |
| 5272 | rank = QString::number(msg[15] - 0x1a) + 'p'; |
| 5273 | else |
| 5274 | rank = QString::number(msg[15] - 0x11) + 'd'; |
| 5275 | strncpy((char *)name, (char *)&(msg[16]), 11); |
| 5276 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5277 | |
| 5278 | player = getDefaultRoom()->getPlayerListing(encoded_name2); |
| 5279 | sendConversationReply(player, accept); |
| 5280 | Talk * t = getDefaultRoom()->getTalk(player); |
| 5281 | t->setConversationOpened(true); |
| 5282 | t->updatePlayerListing(); |
| 5283 | } |
| 5284 | |
| 5285 | void TygemConnection::handleConversationReply(unsigned char * msg, unsigned int size) |
| 5286 | { |
| 5287 | PlayerListing * player; |
| 5288 | QString encoded_name, encoded_name2; |
| 5289 | QString rank; |
| 5290 | unsigned char name[15]; |
| 5291 | name[14] = 0x00; |
| 5292 | if(size < 63) |
| 5293 | { |
| 5294 | qDebug("Bad conversation reply size: %d", size); |
| 5295 | return; |
| 5296 | } |
| 5297 | |
| 5298 | strncpy((char *)name, (char *)msg, 14); |
| 5299 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5300 | if(msg[15] < 0x12) |
| 5301 | rank = QString::number(0x12 - msg[15]) + 'k'; |
| 5302 | else if(msg[15] > 0x1a) |
| 5303 | rank = QString::number(msg[15] - 0x1a) + 'p'; |
| 5304 | else |
| 5305 | rank = QString::number(msg[15] - 0x11) + 'd'; |
| 5306 | strncpy((char *)name, (char *)&(msg[16]), 11); |
| 5307 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5308 | |
| 5309 | player = getDefaultRoom()->getPlayerListing(encoded_name2); |
| 5310 | Talk * t = getDefaultRoom()->getIfTalk(player); |
| 5311 | if(!t) |
| 5312 | { |
| 5313 | qDebug("No talk for: %s", encoded_name2.toLatin1().constData()); |
| 5314 | return; |
| 5315 | } |
| 5316 | |
| 5317 | if(msg[62] == 0x01) |
| 5318 | { |
| 5319 | t->setConversationOpened(true); |
| 5320 | } |
| 5321 | else if(msg[62] == 0x00) |
| 5322 | { |
| 5323 | t->recvTalk(QString("** Declines conversation **")); |
| 5324 | } |
| 5325 | else |
| 5326 | { |
| 5327 | #ifdef RE_DEBUG |
| 5328 | printf("Conversation reply msg 62 = %02x\n", msg[62]); |
| 5329 | #endif //RE_DEBUG |
| 5330 | } |
| 5331 | |
| 5332 | if(pendingConversationMsg.find(player) != pendingConversationMsg.end()) |
| 5333 | { |
| 5334 | sendConversationMsg(player, pendingConversationMsg[player].toLatin1().constData()); |
| 5335 | pendingConversationMsg.erase(player); |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | void TygemConnection::handleConversationMsg(unsigned char * msg, unsigned int size) |
| 5340 | { |
| 5341 | PlayerListing * player; |
| 5342 | QString encoded_name, encoded_name2; |
| 5343 | QString rank; |
| 5344 | unsigned char name[15]; |
| 5345 | int size_of_message; |
| 5346 | int nickname_len; |
| 5347 | bool closing = false; |
| 5348 | char * text; |
| 5349 | name[14] = 0x00; |
| 5350 | if(size < 61) |
| 5351 | { |
| 5352 | qDebug("Received bad conversation msg"); |
| 5353 | return; |
| 5354 | } |
| 5355 | #ifdef RE_DEBUG |
| 5356 | printf("Conversation msg:\n"); |
| 5357 | for(unsigned int i = 0; i < size; i++) |
| 5358 | printf("%02x ", msg[i]); |
| 5359 | printf("\n"); |
| 5360 | #endif //RE_DEBUG |
| 5361 | |
| 5362 | strncpy((char *)name, (char *)msg, 14); |
| 5363 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5364 | if(msg[15] < 0x12) |
| 5365 | rank = QString::number(0x12 - msg[15]) + 'k'; |
| 5366 | else if(msg[15] > 0x1a) |
| 5367 | rank = QString::number(msg[15] - 0x1a) + 'p'; |
| 5368 | else |
| 5369 | rank = QString::number(msg[15] - 0x11) + 'd'; |
| 5370 | strncpy((char *)name, (char *)&(msg[16]), 11); |
| 5371 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5372 | nickname_len = strlen((char *)name); |
| 5373 | //FIXME 255 chat size limit ?!?! |
| 5374 | size_of_message = msg[60]; |
| 5375 | unsigned int adjusted_size = size_of_message; |
| 5376 | if(size_of_message % 4 == 0) |
| 5377 | adjusted_size += 4; |
| 5378 | else |
| 5379 | { |
| 5380 | while(adjusted_size % 4 != 0) |
| 5381 | adjusted_size++; |
| 5382 | } |
| 5383 | if(size != adjusted_size + 60 + 4) |
| 5384 | { |
| 5385 | //76 72 |
| 5386 | qDebug("Bad conversation message size %d %d", size, adjusted_size + 64); |
| 5387 | return; |
| 5388 | } |
| 5389 | |
| 5390 | player = getDefaultRoom()->getPlayerListing(encoded_name2); |
| 5391 | Talk * t = getDefaultRoom()->getIfTalk(player); |
| 5392 | if(!t) |
| 5393 | { |
| 5394 | qDebug("Unknown player replies to conversation: %s", encoded_name2.toLatin1().constData()); |
| 5395 | return; |
| 5396 | } |
| 5397 | |
| 5398 | if(msg[62] == 0x04) |
| 5399 | { |
| 5400 | //FIXME do we need to do anything else? |
| 5401 | qDebug("Conversation closing by other person"); |
| 5402 | closing = true; |
| 5403 | } |
| 5404 | else if(msg[62] != 0x05) |
| 5405 | { |
| 5406 | #ifdef RE_DEBUG |
| 5407 | printf("Conversation msg 62 = %02x\n", msg[62]); |
| 5408 | #endif //RE_DEBUG |
| 5409 | } |
| 5410 | |
| 5411 | text = new char[size_of_message + 1]; |
| 5412 | strncpy(text, (char *)&(msg[64]), size_of_message); |
| 5413 | |
| 5414 | text[size_of_message] = 0x00; //doublecheck FIXME |
| 5415 | |
| 5416 | QString u; |
| 5417 | |
| 5418 | if(closing) |
| 5419 | { |
| 5420 | u = serverCodec->toUnicode((const char *)&(text[nickname_len + 2]), strlen((char *)&(text[nickname_len + 2]))); |
| 5421 | u = "** " + u + " **"; |
| 5422 | } |
| 5423 | else |
| 5424 | u = serverCodec->toUnicode((const char *)text, strlen((char *)text)); |
| 5425 | t->recvTalk(u); |
| 5426 | if(closing) |
| 5427 | t->setConversationOpened(false); |
| 5428 | delete[] text; |
| 5429 | } |
| 5430 | |
| 5431 | /* I think these are actually supposed to be little email type msgs even though |
| 5432 | * they're small and conversations are supposed to be held over |
| 5433 | * something else that we have to respond to to open up */ |
| 5434 | void TygemConnection::handlePersonalChat(unsigned char * msg, unsigned int size) |
| 5435 | { |
| 5436 | unsigned char * p = msg; |
| 5437 | unsigned char * text = new unsigned char[size - 7]; |
| 5438 | unsigned char name[15]; |
| 5439 | name[14] = 0x00; |
| 5440 | unsigned int subject_size, msg_size; |
| 5441 | QString our_name, encoded_name, encoded_name2, rank, subject; |
| 5442 | #ifdef RE_DEBUG |
| 5443 | printf("** msg size: %d: ", size); |
| 5444 | for(unsigned int i = 0; i < size; i++) |
| 5445 | printf("%02x", p[i]); |
| 5446 | printf("\n"); |
| 5447 | #endif //RE_DEBUG |
| 5448 | |
| 5449 | strncpy((char *)name, (char *)p, 14); |
| 5450 | our_name = QString((char *)name); |
| 5451 | p += 15; |
| 5452 | p++; |
| 5453 | strncpy((char *)name, (char *)p, 14); |
| 5454 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5455 | p += 15; |
| 5456 | |
| 5457 | //rank byte |
| 5458 | if(p[0] < 0x12) |
| 5459 | rank = QString::number(0x12 - p[0]) + 'k'; |
| 5460 | else if(p[0] > 0x1a) |
| 5461 | rank = QString::number(p[0] - 0x1a) + 'p'; |
| 5462 | else |
| 5463 | rank = QString::number(p[0] - 0x11) + 'd'; |
| 5464 | p++; |
| 5465 | strncpy((char *)name, (char *)p, 11); |
| 5466 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5467 | p += 12; |
| 5468 | p++; |
| 5469 | subject_size = p[0]; |
| 5470 | p++; |
| 5471 | printf("Subjectsize: %d\n", subject_size); |
| 5472 | if(subject_size > (size - 47)) |
| 5473 | { |
| 5474 | qDebug("Bad message subject size: %d", subject_size); |
| 5475 | delete[] text; |
| 5476 | return; |
| 5477 | } |
| 5478 | strncpy((char *)text, (char *)p, subject_size); |
| 5479 | subject = QString((char *)text); |
| 5480 | p += subject_size; |
| 5481 | msg_size = (p[0] << 8) + p[1]; //tygem caps at 1024 |
| 5482 | p += 2; |
| 5483 | printf("msg_size: %d\n", msg_size); |
| 5484 | if(msg_size > (size - (47 + subject_size + 2))) |
| 5485 | { |
| 5486 | qDebug("Bad message msg size: %d", msg_size); |
| 5487 | delete[] text; |
| 5488 | return; |
| 5489 | } |
| 5490 | strncpy((char *)text, (char *)p, msg_size + 1); |
| 5491 | |
| 5492 | Room * room = getDefaultRoom(); |
| 5493 | PlayerListing * player = room->getPlayerListing(encoded_name2); |
| 5494 | |
| 5495 | #ifdef RE_DEBUG |
| 5496 | printf("%s says to you ", player->name.toLatin1().constData()); |
| 5497 | for(unsigned int i = 0; i < size - 8; i++) |
| 5498 | printf("%02x", text[i]); |
| 5499 | printf("\n"); |
| 5500 | #endif //RE_DEBUG |
| 5501 | QString u; |
| 5502 | u = serverCodec->toUnicode((const char *)text, strlen((char *)text)); |
| 5503 | Talk * talk = getDefaultRoom()->getTalk(player); |
| 5504 | if(talk) |
| 5505 | { |
| 5506 | talk->recvTalk(u); |
| 5507 | talk->updatePlayerListing(); |
| 5508 | } |
| 5509 | //FIXME highlight or something |
| 5510 | //if(console_dispatch) |
| 5511 | // console_dispatch->recvText(player->name + "-> " + QString((char *)text)); |
| 5512 | |
| 5513 | delete[] text; |
| 5514 | } |
| 5515 | |
| 5516 | //0666 |
| 5517 | void TygemConnection::handleObserverList(unsigned char * msg, unsigned int size) |
| 5518 | { |
| 5519 | unsigned char * p = msg; |
| 5520 | unsigned int game_number; |
| 5521 | Room * room = getDefaultRoom(); |
| 5522 | BoardDispatch * boarddispatch; |
| 5523 | PlayerListing * aPlayer; |
| 5524 | //PlayerListing * newPlayer = new PlayerListing(); |
| 5525 | QString encoded_name, encoded_name2, rank; |
| 5526 | int country_size; |
| 5527 | unsigned char name[20]; //country can be larger than 11 |
| 5528 | game_number = (p[0] << 8) + p[1]; |
| 5529 | boarddispatch = getIfBoardDispatch(game_number); |
| 5530 | if(!boarddispatch) |
| 5531 | { |
| 5532 | qDebug("no board dispatch for observer list"); |
| 5533 | return; |
| 5534 | } |
| 5535 | |
| 5536 | p += 2; |
| 5537 | // unsigned short number_of_observers = (p[0] << 8) + p[1]; |
| 5538 | p += 2; |
| 5539 | #ifdef RE_DEBUG |
| 5540 | printf("observers %d:\n", number_of_observers); |
| 5541 | for(unsigned int i = 0; i < size - 4; i++) |
| 5542 | printf("%02x", p[i]); |
| 5543 | printf("\n"); |
| 5544 | #endif //RE_DEBUG |
| 5545 | /* FIXME possible issue with country name which is not included in |
| 5546 | * 0x34 */ |
| 5547 | |
| 5548 | while(p < (msg + size - 0x33) || (p[0] == 0x01 && p < (msg + size - 19))) |
| 5549 | { |
| 5550 | if(p[0] == 0x01) |
| 5551 | { |
| 5552 | //0131 1140 b9d9 b6f7 b3ad c1f8 c0cc 0000 0000 0013 c900 0000 |
| 5553 | |
| 5554 | |
| 5555 | //0100 0000 696e 7472 7573 696f 6e00 0000 0000 0009 |
| 5556 | p += 4; |
| 5557 | strncpy((char *)name, (char *)p, 14); |
| 5558 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5559 | aPlayer = room->getPlayerListingByNotNickname(encoded_name); |
| 5560 | boarddispatch->recvObserver(aPlayer, false); |
| 5561 | if(match_negotiation_state->isOngoingMatch(game_number)) |
| 5562 | { |
| 5563 | GameData * gameData = boarddispatch->getGameData(); |
| 5564 | //FIXME nick or username? probably nick but is that ascii or normal? |
| 5565 | if(aPlayer->name == (gameData->black_name == getUsername() ? gameData->white_name : gameData->black_name)) |
| 5566 | { |
| 5567 | if(gameData->fullresult == 0) |
| 5568 | { |
| 5569 | qDebug("opponent left game!!"); |
| 5570 | sendOpponentDisconnect(game_number, opponent_disconnect); |
| 5571 | sendLongOpponentDisconnect(game_number); |
| 5572 | |
| 5573 | opponentDisconnectTimerID = startTimer(1000); |
| 5574 | /* FIXME may or may not be the right place |
| 5575 | * for this, we could get another message or |
| 5576 | * perhaps when we haven't received time |
| 5577 | * from opponent for 4 or 5 seconds. |
| 5578 | * Right now, I'll assume that's time it takes |
| 5579 | * server to pass "observer left" msg */ |
| 5580 | match_negotiation_state->opponentDisconnect(); |
| 5581 | /* FIXME, if its our turn, we need to block playing, and also stop the clocks !! |
| 5582 | * or can we play?FIXME */ |
| 5583 | boarddispatch->stopTime(); //also no play, if our turn |
| 5584 | QString q = QString(tr("Opponent Disconnected")); |
| 5585 | boarddispatch->moveControl(q); |
| 5586 | } |
| 5587 | } |
| 5588 | } |
| 5589 | p += 15; |
| 5590 | #ifdef RE_DEBUG |
| 5591 | printf("Player %s leaving room, last byte: %02x\n", encoded_name.toLatin1().constData(), p[0]); |
| 5592 | #endif //RE_DEBUG |
| 5593 | p++; |
| 5594 | #ifdef RE_DEBUG |
| 5595 | printf("Found leaving observer\n"); |
| 5596 | #endif //RE_DEBUG |
| 5597 | continue; |
| 5598 | } |
| 5599 | if(p[1] == 0x39) |
| 5600 | { |
| 5601 | p += 4; |
| 5602 | printf("observer, what the hell is this\n"); |
| 5603 | continue; |
| 5604 | } |
| 5605 | else if(p[1] != 0x02) |
| 5606 | { |
| 5607 | printf("%02x%02x\n", p[0], p[1]); |
| 5608 | } |
| 5609 | #ifdef RE_DEBUG |
| 5610 | for(int i = 0; i < 0x34; i++) |
| 5611 | printf("%c", p[i]); |
| 5612 | printf("\n"); |
| 5613 | #endif //RE_DEBUG |
| 5614 | p += 2; |
| 5615 | p++; |
| 5616 | if(!p[0]) |
| 5617 | { |
| 5618 | printf("Observer Guest\n"); |
| 5619 | //0002 0000 5b47 3337 3039 5d32 3000 0000 |
| 5620 | //0000 0000 5b47 3337 3039 5d32 3000 0000 |
| 5621 | //0000 0100 0000 0000 0000 0000 0000 0000 |
| 5622 | //0000 0000 |
| 5623 | /* FIXME this looks a little funny. A room with |
| 5624 | * multiple observers that's empty because we don't |
| 5625 | * add guests to the list... at the same time, |
| 5626 | * they'd have to be special player listings.. |
| 5627 | * maybe just duplicates but still... */ |
| 5628 | p += 48; |
| 5629 | p += p[0] + 1; |
| 5630 | continue; |
| 5631 | } |
| 5632 | |
| 5633 | /* I think p[0] is the flag icon, but it can be different |
| 5634 | * pictures too so... */ |
| 5635 | p++; |
| 5636 | strncpy((char *)name, (char *)p, 14); |
| 5637 | //encoded_name = QString((char *)name); |
| 5638 | /* WARNING There's a chance that encoded names are not one |
| 5639 | * to one and since the names are used to look players up, |
| 5640 | * doing toUnicode could write to the wrong player. |
| 5641 | * possible FIXME*/ |
| 5642 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5643 | p += 14; |
| 5644 | p++; |
| 5645 | //rank byte |
| 5646 | if(p[0] < 0x12) |
| 5647 | rank = QString::number(0x12 - p[0]) + 'k'; |
| 5648 | else if(p[0] > 0x1a) |
| 5649 | rank = QString::number(p[0] - 0x1a) + 'p'; |
| 5650 | else |
| 5651 | rank = QString::number(p[0] - 0x11) + 'd'; |
| 5652 | p++; |
| 5653 | strncpy((char *)name, (char *)p, 11); |
| 5654 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 5655 | //another name |
| 5656 | aPlayer = room->getPlayerListing(encoded_name2); |
| 5657 | //unnecessary to set the name since we can't get |
| 5658 | //record without it... probably nickname as well |
| 5659 | //FIXME, but if we didn't need player lists... |
| 5660 | //technically we should be able to display anyway... |
| 5661 | //aPlayer->name = encoded_name; |
| 5662 | //aPlayer->nickname = nickname; |
| 5663 | //aPlayer->rank = rank; |
| 5664 | p += 8; |
| 5665 | p += 4; |
| 5666 | p += 16; |
| 5667 | p += 2; |
| 5668 | p++; |
| 5669 | |
| 5670 | /* Below is player listing packet */ |
| 5671 | //0002 009e 3737 3435 3532 0000 0000 0000 |
| 5672 | //0000 0000 3737 3435 3532 006f 6f6e 0002 |
| 5673 | //0000 0000 0000 0064 0000 012b 0000 0000 |
| 5674 | //ffff ff40 0001 004b |
| 5675 | /****/ |
| 5676 | /* And observer listing */ |
| 5677 | //0002 009e 7065 7465 7269 7573 0000 0000 |
| 5678 | //0000 0008 7065 7465 7269 7573 0000 0002 |
| 5679 | //0000 0000 0000 0000 0000 0000 0000 0000 |
| 5680 | //0000 0005 6368 696e 61 |
| 5681 | |
| 5682 | country_size = p[0]; |
| 5683 | p++; |
| 5684 | if(p > (msg + size - country_size)) |
| 5685 | { |
| 5686 | qDebug("Observer list packet overrun, discarding last record!"); |
| 5687 | return; |
| 5688 | } |
| 5689 | if(country_size > 19) |
| 5690 | { |
| 5691 | qDebug("Strange country name size greater than hard coded array!!!"); |
| 5692 | p += country_size; |
| 5693 | continue; |
| 5694 | } |
| 5695 | strncpy((char *)name, (char *)p, country_size); |
| 5696 | name[country_size] = '\0'; |
| 5697 | p += country_size; |
| 5698 | aPlayer->country = QString((char *)name); |
| 5699 | /*recvObserver does not make a copy at any point. |
| 5700 | *its supposed that the player listing comes from |
| 5701 | *the full listing and will be handled with that.*/ |
| 5702 | |
| 5703 | /* Also notice that below is a problem if someone leaves |
| 5704 | * and rejoins a game */ |
| 5705 | //aPlayer->room_list.push_back(game_number); //moved to boarddispatch doublecheck FIXME |
| 5706 | boarddispatch->recvObserver(aPlayer, true); |
| 5707 | if(match_negotiation_state->isOurGame(game_number)) |
| 5708 | { |
| 5709 | if(match_negotiation_state->justCreatedRoom() && match_negotiation_state->getPlayerListing() == aPlayer) |
| 5710 | { |
| 5711 | /* They've just joined our created room |
| 5712 | * so we send the rest of this. Presuming |
| 5713 | * its not a resume... FIXME */ |
| 5714 | /* This might be a resume or something weird |
| 5715 | * I'm not seeing MatchMsg1 anymore !!! |
| 5716 | * just they join, we pop up the offer dialog |
| 5717 | * and then we send it !!! FIXME*/ |
| 5718 | //#ifdef FIXME //necessary when? |
| 5719 | //tom may require this |
| 5720 | if(connectionType == TypeTOM) |
| 5721 | sendMatchMsg1(aPlayer, game_number); |
| 5722 | //#endif //FIXME |
| 5723 | MatchRequest * mr = new MatchRequest(); |
| 5724 | mr->number = game_number; |
| 5725 | mr->opponent = aPlayer->name; |
| 5726 | mr->opponent_is_challenger = false; |
| 5727 | /* FIXME this is same as "our_invitation" I think |
| 5728 | * in game data, they should be the same name |
| 5729 | * FIXME FIXME FIXME */ |
| 5730 | mr->first_offer = true; |
| 5731 | mr->timeSystem = byoyomi; |
| 5732 | |
| 5733 | GameDialog * gd = getGameDialog(aPlayer); |
| 5734 | gd->recvRequest(mr, getGameDialogFlags()); |
| 5735 | delete mr; |
| 5736 | } |
| 5737 | else |
| 5738 | { |
| 5739 | GameData * gameData = boarddispatch->getGameData(); |
| 5740 | //FIXME nick or username? probably nick but is that ascii or normal? |
| 5741 | if(aPlayer->name == (gameData->black_name == getUsername() ? gameData->white_name : gameData->black_name)) |
| 5742 | { |
| 5743 | if(opponentDisconnectTimerID) |
| 5744 | { |
| 5745 | /* FIXME, quite likely we'll need to |
| 5746 | * send other things here or perhaps |
| 5747 | * we even get another message to |
| 5748 | * trigger this, not this one */ |
| 5749 | killTimer(opponentDisconnectTimerID); |
| 5750 | opponentDisconnectTimerID = 0; |
| 5751 | qDebug("Opponent rejoined after leaving"); |
| 5752 | /* I think these are probably sent in response to something maybe... |
| 5753 | * we shouldn't just send them like this. We need to do it |
| 5754 | * one at a time like match negotiation FIXME */ |
| 5755 | sendEndgameMsg(gameData, opponent_reconnects); |
| 5756 | match_negotiation_state->opponentRejoins(); |
| 5757 | } |
| 5758 | } |
| 5759 | } |
| 5760 | } |
| 5761 | } |
| 5762 | if(p != (msg + size)) |
| 5763 | { |
| 5764 | qDebug("handleObserverList strange size: %d(%d)", (msg + size) - p, size); |
| 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | /* I think we get these when we try to observe broadcasted, or betting games |
| 5769 | * as well. |
| 5770 | * oro FIXME, but tygem may have betting in general*/ |
| 5771 | void TygemConnection::handleBettingMatchStart(unsigned char * , unsigned int ) |
| 5772 | { |
| 5773 | } |
| 5774 | |
| 5775 | /* These are simple, sent by the loser I think |
| 5776 | * note that sendMatchResult sends them which is awkward |
| 5777 | * because of "match" versus "game" FIXME */ |
| 5778 | //0670 |
| 5779 | void TygemConnection::handleGameResult2(unsigned char * msg, unsigned int size) |
| 5780 | { |
| 5781 | BoardDispatch * boarddispatch; |
| 5782 | GameData * gameData; |
| 5783 | //0670 |
| 5784 | //0003 0001 6269 7473 0000 0000 0000 0000 |
| 5785 | //0000 0014 6269 7473 0000 0000 0000 0002 |
| 5786 | //0000 0000 0000 0000 |
| 5787 | if(size != 0x28) |
| 5788 | { |
| 5789 | qDebug("Game result 2 msg of strange size: %d", size); |
| 5790 | } |
| 5791 | char name[15]; |
| 5792 | strncpy((char *)name, (char *)&(msg[4]), 14); |
| 5793 | |
| 5794 | strncpy((char *)name, (char *)&(msg[20]), 11); |
| 5795 | if(getUsername() != serverCodec->toUnicode((char *)name, strlen((char *)name))) |
| 5796 | { |
| 5797 | #ifdef RE_DEBUG |
| 5798 | //FIXME note that if this happens a lot |
| 5799 | //we might need to handle it in observed games |
| 5800 | //actually, we get our own message back so... |
| 5801 | printf("0670 not addressed to us\n"); |
| 5802 | #endif //RE_DEBUG |
| 5803 | /* FIXME, it seems if we resign, maybe only if its early in game, but we'll get our own 0670 back |
| 5804 | * but no 0672, which leads to thinking the opponent dropped because nothing sets RESIGN flag. |
| 5805 | * if nothing is broken, then that means we should check here or when we send the resign in case |
| 5806 | * moves is less than 5 or 10 or something and then set result there FIXME */ |
| 5807 | /* Okay, I think now that that first name is not the addressee necessary, that its the wff thing |
| 5808 | * so we need to handle these FIXME */ |
| 5809 | return; |
| 5810 | } |
| 5811 | boarddispatch = getIfBoardDispatch((msg[0] << 8) + msg[1]); |
| 5812 | if(!boarddispatch) |
| 5813 | { |
| 5814 | qDebug("no board dispatch for %d", (msg[0] << 8) + msg[1]); |
| 5815 | return; |
| 5816 | } |
| 5817 | gameData = boarddispatch->getGameData(); |
| 5818 | //msg[2] is likely color |
| 5819 | #ifdef RE_DEBUG |
| 5820 | if(msg[2]) |
| 5821 | boarddispatch->recvKibitz(0, "0x670 (resign msg? 01)"); |
| 5822 | else |
| 5823 | boarddispatch->recvKibitz(0, "0x670 (resign msg? 00)"); |
| 5824 | printf("0x0670: "); |
| 5825 | for(unsigned int i = 0; i < size; i++) |
| 5826 | printf("%02x", msg[i]); |
| 5827 | printf("\n"); |
| 5828 | #endif //RE_DEBUG |
| 5829 | //I think we need to respond to this with 0672 |
| 5830 | //otherwise no result posted |
| 5831 | //sendLongMatchResult FIXME but only if this is |
| 5832 | //from opp |
| 5833 | //note also that this is very likely the resign |
| 5834 | //message and we should set result as such REGARDLESS |
| 5835 | //of being in score mode or not, we should switch out if it |
| 5836 | //if we are |
| 5837 | GameResult aGameResult; |
| 5838 | aGameResult.result = GameResult::RESIGN; |
| 5839 | aGameResult.winner_name = getUsername(); |
| 5840 | //FIXME, we should be able to use |
| 5841 | //color byte above, msg[2] or whatever |
| 5842 | if(gameData->white_name == getUsername()) |
| 5843 | { |
| 5844 | aGameResult.winner_color = stoneWhite; |
| 5845 | aGameResult.loser_name = gameData->black_name; |
| 5846 | } |
| 5847 | else |
| 5848 | { |
| 5849 | aGameResult.winner_color = stoneBlack; |
| 5850 | aGameResult.loser_name = gameData->white_name; |
| 5851 | } |
| 5852 | boarddispatch->recvResult(&aGameResult); |
| 5853 | sendResult(gameData, &aGameResult); |
| 5854 | |
| 5855 | if(match_negotiation_state->isOurGame(gameData->number)) //redundant with 0672? |
| 5856 | match_negotiation_state->reset(); |
| 5857 | } |
| 5858 | |
| 5859 | //0672 |
| 5860 | void TygemConnection::handleGameResult(unsigned char * msg, unsigned int size) |
| 5861 | { |
| 5862 | BoardDispatch * boarddispatch; |
| 5863 | unsigned char * p = msg; |
| 5864 | unsigned short game_number = (msg[0] << 8) + msg[1]; |
| 5865 | //bool white_loses_on_time = false, black_loses_on_time = false; |
| 5866 | //unsigned int moves; |
| 5867 | unsigned int margin; |
| 5868 | unsigned char victory_condition_code; |
| 5869 | if(size != 0x24) |
| 5870 | { |
| 5871 | qDebug("Game result of strange size: %d", size); |
| 5872 | } |
| 5873 | //this is sent if we join an ended game as well |
| 5874 | //must be result where 0671 is match opened |
| 5875 | //0670 is sent if, for instance, resign |
| 5876 | //while we're watching it, but not after over |
| 5877 | |
| 5878 | //but 0670 can be sent without 0672 apparently |
| 5879 | //during game, but again, we would get 0672 alone |
| 5880 | //if it was after game |
| 5881 | //lastly, looks like 0672 might happen without SUR? |
| 5882 | //maybe only when adjourned or something? |
| 5883 | //but we definitely need to handle it and we might |
| 5884 | //want to have it send result (i.e., W+T, etc. ) FIXME |
| 5885 | |
| 5886 | //white has 4 seconds wins by R |
| 5887 | //0002 0001 d6ee b8f0 d0a1 bba8 0000 0000 |
| 5888 | //0000 001a 0400 0000 0076 0000 0204 00a0 |
| 5889 | //021c 0060 |
| 5890 | |
| 5891 | //gets this after sends a timeloss prematurely due to some 0 period count error... |
| 5892 | //FIXME |
| 5893 | //038d0001696e74727573696f6e000000000000090900000000010000000000a0011d0060 |
| 5894 | |
| 5895 | boarddispatch = getIfBoardDispatch(game_number); |
| 5896 | if(!boarddispatch) |
| 5897 | { |
| 5898 | qDebug("Can't get board for game number %d", game_number); |
| 5899 | return; |
| 5900 | } |
| 5901 | GameData * gd = boarddispatch->getGameData(); |
| 5902 | p += 2; |
| 5903 | // bool white_wins2 = msg[2] ^ gd->white_first_flag; //NOT VALID as is |
| 5904 | #ifdef RE_DEBUG |
| 5905 | printf("0672 %02x vs %02x!!", msg[2], gd->our_invitation); |
| 5906 | #endif //RE_DEBUG |
| 5907 | /* FIXME double check this, but as is, it seems in our games, this is addressed |
| 5908 | * to us and this is not the winner name, winner rank, but just the usual msg |
| 5909 | * header, doublecheck against other games */ |
| 5910 | |
| 5911 | //00 00 their invitation, they win |
| 5912 | //01 00 their invitation, we win |
| 5913 | p += 2; |
| 5914 | //winner name //probably not, dest |
| 5915 | p += 14; |
| 5916 | //winner rank //probably not, dest |
| 5917 | p += 2; |
| 5918 | //0300 0000 0115 0000 |
| 5919 | victory_condition_code = msg[20]; //p[0] |
| 5920 | //03 B + R |
| 5921 | //04 W + R |
| 5922 | //00 W + X |
| 5923 | //07 B + T |
| 5924 | //0000 0956 002e 0000 |
| 5925 | p += 2; |
| 5926 | margin = (msg[22] << 8) + msg[23]; |
| 5927 | p += 2; |
| 5928 | // moves = (msg[24] << 8) + msg[25]; |
| 5929 | p += 2; |
| 5930 | printf("%02x%02x is in 0672 after margin and moves\n", p[0], p[1]); //p[0] may need to be xored with wff |
| 5931 | p += 2; |
| 5932 | //times |
| 5933 | |
| 5934 | // enum TimeFlags f = handleTimeChunk(boarddispatch, &(msg[28]), gd->white_first_flag); //this can still get time flipped FIXME |
| 5935 | /* FIXME unreliable !! not necessary */ |
| 5936 | /* if(f == BLACK_LOSES) |
| 5937 | black_loses_on_time = true; |
| 5938 | else if(f == WHITE_LOSES) |
| 5939 | white_loses_on_time = true; */ |
| 5940 | |
| 5941 | #ifdef RE_DEBUG |
| 5942 | //B + R: white_wins2 = 1 here, FIXME |
| 5943 | if(boarddispatch) |
| 5944 | boarddispatch->recvKibitz(0, "0x672 " + QString::number(white_wins2)); |
| 5945 | |
| 5946 | printf("***** 0x0672: "); |
| 5947 | for(unsigned int i = 0; i < size; i++) |
| 5948 | printf("%02x", msg[i]); |
| 5949 | printf("\n"); |
| 5950 | if(black_loses_on_time) |
| 5951 | qDebug("black loses on time?\n"); |
| 5952 | if(white_loses_on_time) |
| 5953 | qDebug("white loses on time?\n"); |
| 5954 | #endif //RE_DEBUG |
| 5955 | |
| 5956 | //FIXME not necessary: |
| 5957 | |
| 5958 | GameResult aGameResult; |
| 5959 | #ifdef FIXME //also broken I think |
| 5960 | if(white_loses_on_time || black_loses_on_time) |
| 5961 | { |
| 5962 | aGameResult.result = GameResult::TIME; |
| 5963 | aGameResult.game_number = game_number; |
| 5964 | if(white_loses_on_time) |
| 5965 | { |
| 5966 | aGameResult.loser_name = gd->white_name; |
| 5967 | aGameResult.winner_name = gd->black_name; |
| 5968 | aGameResult.winner_color = stoneBlack; |
| 5969 | } |
| 5970 | else |
| 5971 | { |
| 5972 | aGameResult.loser_name = gd->black_name; |
| 5973 | aGameResult.winner_name = gd->white_name; |
| 5974 | aGameResult.winner_color = stoneWhite; |
| 5975 | } |
| 5976 | boarddispatch->recvResult(&aGameResult); |
| 5977 | return; |
| 5978 | } |
| 5979 | #endif //FIXME |
| 5980 | |
| 5981 | //awkward, redundant with white_loses_on_time, black_loses_on_time above |
| 5982 | bool white_wins = false, black_wins = false; |
| 5983 | switch(victory_condition_code) |
| 5984 | { |
| 5985 | case 0x00: |
| 5986 | aGameResult.result = GameResult::SCORE; |
| 5987 | black_wins = true; |
| 5988 | aGameResult.winner_score = (float)margin/10.0; |
| 5989 | aGameResult.loser_score = 0; |
| 5990 | break; |
| 5991 | case 0x01: |
| 5992 | aGameResult.result = GameResult::SCORE; |
| 5993 | white_wins = true; |
| 5994 | aGameResult.winner_score = (float)margin/10.0; |
| 5995 | aGameResult.loser_score = 0; |
| 5996 | break; |
| 5997 | case 0x02: |
| 5998 | aGameResult.result = GameResult::DRAW; |
| 5999 | /* Don't know whether we get this or what */ |
| 6000 | aGameResult.winner_color = stoneNone; |
| 6001 | boarddispatch->recvResult(&aGameResult); |
| 6002 | return; //awkward |
| 6003 | break; |
| 6004 | case 0x03: |
| 6005 | aGameResult.result = GameResult::RESIGN; |
| 6006 | black_wins = true; |
| 6007 | break; |
| 6008 | case 0x04: |
| 6009 | aGameResult.result = GameResult::RESIGN; |
| 6010 | white_wins = true; |
| 6011 | break; |
| 6012 | case 0x05: |
| 6013 | aGameResult.result = GameResult::FORFEIT; |
| 6014 | black_wins = true; |
| 6015 | break; |
| 6016 | case 0x06: |
| 6017 | aGameResult.result = GameResult::FORFEIT; |
| 6018 | white_wins = true; |
| 6019 | break; |
| 6020 | case 0x07: |
| 6021 | aGameResult.result = GameResult::TIME; |
| 6022 | black_wins = true; |
| 6023 | break; |
| 6024 | case 0x08: |
| 6025 | aGameResult.result = GameResult::TIME; |
| 6026 | white_wins = true; |
| 6027 | break; |
| 6028 | /* FIXME doublecheck these, I think we get these |
| 6029 | * if its an overwhelming victory. It displays a |
| 6030 | * cute message, its not really a resign but |
| 6031 | * it may as well be. */ |
| 6032 | case 0x09: |
| 6033 | aGameResult.result = GameResult::RESIGN; |
| 6034 | black_wins = true; |
| 6035 | break; |
| 6036 | case 0x0a: |
| 6037 | aGameResult.result = GameResult::RESIGN; |
| 6038 | white_wins = true; |
| 6039 | break; |
| 6040 | default: |
| 6041 | qDebug("%02x is unhandled victory condition code", victory_condition_code); |
| 6042 | break; |
| 6043 | } |
| 6044 | |
| 6045 | //definitely where we pop up result |
| 6046 | //EXCEPT that if its our match and we sent the 0672, I don't think we get one |
| 6047 | //back, but we don't want the countdialog to be looking at our_invitation |
| 6048 | //which is certainly tygem specific even though, at this point, the |
| 6049 | //distinction is meaningless |
| 6050 | //so we'll let countdialog set the result no matter what and we'll |
| 6051 | //drop the 0672 here if its our match, unless its loss on time, not after |
| 6052 | //countdialog is active |
| 6053 | //no unless its a resign, really, according to tygem FIXME |
| 6054 | //this messaage is where result is set |
| 6055 | //this is hideously awkward but its fine for now FIXME |
| 6056 | /*if(game_number == playing_game_number) |
| 6057 | { |
| 6058 | if(aGameResult.result == GameResult::SCORE) |
| 6059 | { |
| 6060 | //apparently, game result doesn't come up |
| 6061 | //from accept count at all or properly |
| 6062 | //which might be better anyway |
| 6063 | //playing_game_number = 0; //good place?? FIXME |
| 6064 | //return; |
| 6065 | } |
| 6066 | }*/ |
| 6067 | |
| 6068 | aGameResult.game_number = game_number; |
| 6069 | if(black_wins) |
| 6070 | { |
| 6071 | aGameResult.loser_name = gd->white_name; |
| 6072 | aGameResult.winner_name = gd->black_name; |
| 6073 | aGameResult.winner_color = stoneBlack; |
| 6074 | } |
| 6075 | else |
| 6076 | { |
| 6077 | aGameResult.loser_name = gd->black_name; |
| 6078 | aGameResult.winner_name = gd->white_name; |
| 6079 | aGameResult.winner_color = stoneWhite; |
| 6080 | } |
| 6081 | boarddispatch->recvResult(&aGameResult); |
| 6082 | if(match_negotiation_state->isOurGame(game_number)) |
| 6083 | match_negotiation_state->reset(); |
| 6084 | } |
| 6085 | |
| 6086 | //0x0683: //clock stop? //enter score? |
| 6087 | /* As I recall, these are also used to set the time in professional games |
| 6088 | * being broadcast |
| 6089 | * But on second thought, this is a 0683: |
| 6090 | //018e 0b01 bfee bfb5 c0da 3832 0000 0000 |
| 6091 | //0000 0016 0000 0000 0000 0000 0000 0000 |
| 6092 | //0000 1a00 c1a6 3134 b1e2 2047 53c4 aec5 |
| 6093 | //d8bd bab9 e820 babb bcb1 b8ae b1d7 0000 |
| 6094 | * so not really sure what they are... that's got some names in there, could |
| 6095 | * even be a comment of some kind |
| 6096 | * likely a game update |
| 6097 | */ |
| 6098 | #ifdef RE_DEBUG |
| 6099 | void TygemConnection::handleScoreMsg1(unsigned char * msg, unsigned int size) |
| 6100 | #else |
| 6101 | void TygemConnection::handleScoreMsg1(unsigned char * msg, unsigned int /*size*/) |
| 6102 | #endif //RE_DEBUG |
| 6103 | { |
| 6104 | /* Change name FIXME, definitely not enter score, more likely |
| 6105 | * time stop. Except, they're variable size... Its even possible |
| 6106 | * that they're image sends... maybe... */ |
| 6107 | BoardDispatch * boarddispatch; |
| 6108 | //0087 aa01 0101 0000 031e 00a0 0334 0040 |
| 6109 | #ifdef RE_DEBUG |
| 6110 | printf("0x0683: "); |
| 6111 | for(unsigned int i = 0; i < size; i++) |
| 6112 | printf("%02x", msg[i]); |
| 6113 | printf("\n"); |
| 6114 | #endif //RE_DEBUG |
| 6115 | boarddispatch = getIfBoardDispatch((msg[0] << 8) + msg[1]); |
| 6116 | if(!boarddispatch) |
| 6117 | return; |
| 6118 | #ifdef RE_DEBUG |
| 6119 | boarddispatch->recvKibitz(0, "0x683"); |
| 6120 | #endif //RE_DEBUG |
| 6121 | //boarddispatch->stopTime(); |
| 6122 | |
| 6123 | //after this 7b |
| 6124 | //0087 0101 6261 696e 6974 6500 0000 0000 |
| 6125 | //0000 0011 6261 696e 6974 6500 0000 0002 |
| 6126 | //00c5 0000 0000 0000 |
| 6127 | //0087 aa01 0300 0000 031e 00a0 0334 0040 |
| 6128 | //FIXME |
| 6129 | //black_first = ?? |
| 6130 | //handleTimeChunk(boarddispatch, &(msg[7]), black_first); |
| 6131 | //another 7b 83 pair: |
| 6132 | //0087 0001 baac d0a6 bba8 0000 0000 0000 |
| 6133 | //0000 0011 6861 6e78 6961 6f68 7561 0002 |
| 6134 | //00c5 0000 0000 0000 |
| 6135 | |
| 6136 | //0087 aa01 0201 0000 031e 00a0 0334 0040 |
| 6137 | |
| 6138 | // and two more pairs, same game |
| 6139 | //0x067b: |
| 6140 | //00870001baacd0a6bba80000000000000000001168616e7869616f687561000201c6000000000000 |
| 6141 | //0087aa0104010100031e00a003340040 |
| 6142 | |
| 6143 | //008701016261696e6974650000000000000000116261696e697465000000000201c6000000000000 |
| 6144 | |
| 6145 | //0087aa0105000100031e00a003340040 |
| 6146 | //followed by 0672 |
| 6147 | //0087 0101 6261 696e 6974 6500 0000 0000 |
| 6148 | //0000 0011 0000 0055 0116 0000 031e 00a0 |
| 6149 | //0334 0040 0024 0668 |
| 6150 | |
| 6151 | //0x0683: 0183aa01050000000318008003260040 |
| 6152 | } |
| 6153 | |
| 6154 | //an REM -1 -1 likely proceeds these as meaning done? |
| 6155 | void TygemConnection::handleEndgameMsg(unsigned char * msg, unsigned int size) |
| 6156 | { |
| 6157 | BoardDispatch * boarddispatch; |
| 6158 | GameData * game; |
| 6159 | unsigned short game_number; |
| 6160 | unsigned char name[15]; |
| 6161 | name[14] = 0x00; |
| 6162 | QString encoded_name, encoded_name2; |
| 6163 | if(size < 34) |
| 6164 | { |
| 6165 | qDebug("Bad endgame msg, size: %d", size); |
| 6166 | return; |
| 6167 | } |
| 6168 | //I"m thinking actually draw request |
| 6169 | //no I think this is a request, count maybe? or is that 6a? |
| 6170 | //maybe winner message?, unlikely, several sent and 0201c5 as well below |
| 6171 | //00020101616438380000000000000000000000 |
| 6172 | //16616438380000000000000002 00 c5000000000000 |
| 6173 | //we saw alternating 067b and 0683, with the 83 starting the sequence |
| 6174 | //had something to do with hitting done or agree, in the end black |
| 6175 | //(I think black) just resigned |
| 6176 | //01c50101c4f4d2bbb5b6000000000000000000124e6965596944616f0000000200c7000000000000 |
| 6177 | //01c5 aa01 0600 0000 030b 00a0 031e 0060 |
| 6178 | //01c50001caa4c1fa3200000000000000000000127368656e676c6f6e6732000201c9000000000000 |
| 6179 | //and then right before DSC move message: |
| 6180 | |
| 6181 | //0x067b: 01c50101c4f4d2bbb5b6000000000000000000124e6965596944616f0000000201c6000000000000 |
| 6182 | //0x0683: 01c5 aa01 0401 0100 030b 00a0 031e 0060 |
| 6183 | //0x067b: 01c50001caa4c1fa3200000000000000000000127368656e676c6f6e6732000200c6000000000000 |
| 6184 | //0x0683: 01c5 aa01 0500 0000 030b 00a0 031e 0060 |
| 6185 | //0x066b: 00da0001c6f7bcbcc0ccb5b700000000000000166d696e676d696e67303100000100000000000000 |
| 6186 | |
| 6187 | //008701016261696e6974650000000000000000116261696e697465000000000200c5000000000000 |
| 6188 | game_number = (msg[0] << 8) + msg[1]; |
| 6189 | boarddispatch = getIfBoardDispatch(game_number); |
| 6190 | if(!boarddispatch) |
| 6191 | { |
| 6192 | qDebug("Unexpected: got 067b for board %d", game_number); |
| 6193 | return; |
| 6194 | } |
| 6195 | game = boarddispatch->getGameData(); |
| 6196 | #ifdef RE_DEBUG |
| 6197 | printf("***** 0x067b: "); |
| 6198 | for(unsigned int i = 0; i < size; i++) |
| 6199 | printf("%02x", msg[i]); |
| 6200 | printf("\n"); |
| 6201 | #endif //RE_DEBUG |
| 6202 | |
| 6203 | //this is addressee |
| 6204 | strncpy((char *)name, (char *)&(msg[4]), 14); |
| 6205 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 6206 | strncpy((char *)name, (char *)&(msg[20]), 11); |
| 6207 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 6208 | /* encoded_name2 is NOT reliable, for instance in resuming adjourned. */ |
| 6209 | if(encoded_name2 != QString()) |
| 6210 | encoded_name = encoded_name2; |
| 6211 | |
| 6212 | /*player = getDefaultroom->getPlayerListing(encoded_name); |
| 6213 | if(!player) |
| 6214 | { |
| 6215 | qDebug("Can't get player listing for %s in 067b", encoded_name.toLatin1().constData()); |
| 6216 | return; |
| 6217 | }*/ |
| 6218 | //035d 0001 696e 7472 7573 696f 6e00 0000 0000 0009 |
| 6219 | //696e 7472 7573 696f 6e00 0002 00c3 0000 0000 0000 |
| 6220 | |
| 6221 | //030a 0001 696e 7472 7573 696f 6e00 0000 0000 0009 |
| 6222 | //696e 7472 7573 696f 6e00 0002 0000 0000 0000 0000 |
| 6223 | if(msg[33] == 0xc1) |
| 6224 | { |
| 6225 | if(encoded_name != getUsername()) //probably FIXME |
| 6226 | return; |
| 6227 | MoveRecord * move = new MoveRecord(); |
| 6228 | move->flags = MoveRecord::REQUESTUNDO; |
| 6229 | move->number = NOMOVENUMBER; |
| 6230 | boarddispatch->recvMove(move); |
| 6231 | delete move; |
| 6232 | //sendEndgameMsg(game, refuse_undo_request); |
| 6233 | return; |
| 6234 | } |
| 6235 | else if(msg[33] == 0xc2) |
| 6236 | { |
| 6237 | if(encoded_name != getUsername()) |
| 6238 | return; |
| 6239 | if(msg[32] == 0x00) |
| 6240 | { |
| 6241 | boarddispatch->recvKibitz(QString(), QString("%1 refused undo.").arg( |
| 6242 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6243 | return; |
| 6244 | } |
| 6245 | else |
| 6246 | { |
| 6247 | /*MoveRecord * move = new MoveRecord(); |
| 6248 | move->flags = MoveRecord::UNDO; |
| 6249 | move->number = NOMOVENUMBER; |
| 6250 | boarddispatch->recvMove(move); |
| 6251 | delete move;*/ |
| 6252 | /* There's a WIT move message that triggers the UNDO */ |
| 6253 | return; |
| 6254 | } |
| 6255 | } |
| 6256 | else if(msg[33] == 0xc3) //this is a pre-pass request count |
| 6257 | { |
| 6258 | if(encoded_name != getUsername()) |
| 6259 | return; //awkward, should be consistent but we check it below |
| 6260 | //actually 066a is the request |
| 6261 | //boarddispatch->recvKibitz(QString(), QString("%1 requests count...").arg( |
| 6262 | // (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6263 | /*if(encoded_name == getUsername()) |
| 6264 | boarddispatch->recvRequestCount();*/ |
| 6265 | /* FIXME, I've come to believe this is a draw request or |
| 6266 | * less likely, an adjournment request */ |
| 6267 | boarddispatch->recvRequestDraw(); |
| 6268 | } |
| 6269 | else if(msg[33] == 0xc4) |
| 6270 | { |
| 6271 | /* Shouldn't this be the opposite? Since the message is meant |
| 6272 | * for us?!? i.e., encoded_name != FIXME*/ |
| 6273 | if(encoded_name != getUsername()) |
| 6274 | return; //awkward, should be consistent but we check it below |
| 6275 | if(msg[32] == 0x01) |
| 6276 | { |
| 6277 | /* FIXME maybe all these sorts of messages should be on the boarddispatch. |
| 6278 | * only issue is making sure we get the player name right but maybe we should rewrite |
| 6279 | * in a more generic way */ |
| 6280 | /*boarddispatch->recvKibitz(QString(), QString("%1 has accepted count request...").arg( |
| 6281 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6282 | if(encoded_name == getUsername()) |
| 6283 | boarddispatch->recvAcceptCountRequest();*/ |
| 6284 | //we also get this ?!? |
| 6285 | //we need to do a real count I think |
| 6286 | //because we're not getting the count request from opponent for |
| 6287 | //whatever reason, possibly button isn't working on eweiqi client |
| 6288 | //but anyway, I've wasted enough time on this tonight FIXME |
| 6289 | //0x067b: 009100010000000000000000000000000000000000000000000000000000000000cc000000000000 |
| 6290 | //0x0674: 00910101 |
| 6291 | boarddispatch->recvAcceptDrawRequest(); |
| 6292 | GameResult aGameResult; |
| 6293 | aGameResult.result = GameResult::DRAW; |
| 6294 | boarddispatch->recvResult(&aGameResult); |
| 6295 | sendLongMatchResult(game_number); |
| 6296 | } |
| 6297 | else |
| 6298 | { |
| 6299 | /*boarddispatch->recvKibitz(QString(), QString("%1 has rejected count request...").arg( |
| 6300 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6301 | if(encoded_name == getUsername()) |
| 6302 | boarddispatch->recvRejectCountRequest();*/ |
| 6303 | boarddispatch->recvRefuseDrawRequest(); |
| 6304 | } |
| 6305 | } |
| 6306 | else if(msg[33] == 0xc5) |
| 6307 | { |
| 6308 | boarddispatch->recvKibitz(QString(), QString("%1 has hit done...").arg( |
| 6309 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6310 | // if we've already hit done, we need to popup a countdialog |
| 6311 | if(match_negotiation_state->isOurGame(game_number)) |
| 6312 | { |
| 6313 | if(encoded_name == getUsername()) |
| 6314 | { |
| 6315 | if(match_negotiation_state->sentDoneCounting()) |
| 6316 | boarddispatch->createCountDialog(); |
| 6317 | else |
| 6318 | match_negotiation_state->receiveDoneCounting(); |
| 6319 | } |
| 6320 | else //can we get this more than once for some reason? duplicate count dialogs? FIXME |
| 6321 | { |
| 6322 | if(match_negotiation_state->receivedDoneCounting()) |
| 6323 | boarddispatch->createCountDialog(); |
| 6324 | else |
| 6325 | match_negotiation_state->sendDoneCounting(); |
| 6326 | } |
| 6327 | } |
| 6328 | } |
| 6329 | else if(msg[33] == 0xc6) |
| 6330 | { |
| 6331 | /* Its not entirely standard to have our reject/accept sends |
| 6332 | * set those flags without response from server. But alternative |
| 6333 | * is to complicate recvRejectCount with who is sending it. |
| 6334 | * Note that presumably we don't bring up a CountDialog for games |
| 6335 | * we're observing, but if we need to leave score mode during |
| 6336 | * observed game, ... well we probably need to handle that from |
| 6337 | * DSCs and REMs or some other 7b or time message or the like. |
| 6338 | * We really haven't dealt with score rejection protocol yet |
| 6339 | * FIXME */ |
| 6340 | if(msg[32] == 0x00) |
| 6341 | { |
| 6342 | boarddispatch->recvKibitz(QString(), QString("%1 rejects result").arg( |
| 6343 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6344 | if(encoded_name == getUsername()) //i.e., meant for us |
| 6345 | { |
| 6346 | /* FIXME: We don't need to send this if sendRejectCount already sent it or vice |
| 6347 | * versa. FIXME */ |
| 6348 | boarddispatch->recvRejectCount(); |
| 6349 | MoveRecord * aMove = new MoveRecord(); //here? FIXME |
| 6350 | //aMove->color = stoneErase; |
| 6351 | aMove->flags = MoveRecord::UNREMOVE; |
| 6352 | sendMove(game_number, aMove); |
| 6353 | delete aMove; |
| 6354 | /* I think tygem is updated so you can no longer mark dead stones, maybe |
| 6355 | * so it just does the score and tells you and you can reject it if you want |
| 6356 | * but I'm not sure what that does. So I'm altering this and the accept |
| 6357 | * below for now since its a pain to get the score thing totally right |
| 6358 | * right now ... but obviously I should. */ |
| 6359 | match_negotiation_state->startMatch(); //reset passes |
| 6360 | } |
| 6361 | } |
| 6362 | else //if(msg[36] == 0x01) |
| 6363 | { |
| 6364 | boarddispatch->recvKibitz(QString(), QString("%1 accepts result").arg( |
| 6365 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6366 | if(encoded_name == getUsername()) //i.e., meant for us |
| 6367 | { |
| 6368 | boarddispatch->recvAcceptCount(); |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | else if(msg[33] == 0xf0) |
| 6373 | { |
| 6374 | /* Both players get this, presumably on a restarted game, note that there aren't necessarily |
| 6375 | * any names in it: |
| 6376 | * 002c 067b 000b 0101 |
| 6377 | * 0000 0000 0000 0000 0000 0000 0000 0000 |
| 6378 | * 0000 0000 0000 0000 0000 0000 01f0 0000 |
| 6379 | * 0000 0000 */ |
| 6380 | #ifdef RE_DEBUG |
| 6381 | //FIXME do we need to handle these? |
| 6382 | printf("Received endgame resume f0\n"); |
| 6383 | #endif //RE_DEBUG |
| 6384 | if(match_negotiation_state->opponentRejoining()) |
| 6385 | { |
| 6386 | /* FIXME FIXME Also, we should pop up a window with timer so that if its our turn |
| 6387 | * player doesn't suddenly lose on time because opponent dropped. That should postpone |
| 6388 | * sending sendOpponentDisconnect with the opponent_reconnect type here (or maybe its after, before UNREMOVE:*/ |
| 6389 | sendOpponentDisconnect(game_number, opponent_reconnect); //pretty sure it gets this, time is updated... maybe |
| 6390 | } |
| 6391 | } |
| 6392 | else if(msg[33] == 0xf1) |
| 6393 | { |
| 6394 | //opponent_resumes |
| 6395 | //0063 0001 7065 7465 7269 7573 0000 0000 0000 0000 |
| 6396 | //0000 0000 0000 0000 0000 0000 01f1 0c00 031b 0060 |
| 6397 | //0305 00a0 0201 0000 |
| 6398 | /* These means, I think, our opponent has seen us |
| 6399 | * rejoin... but what's weird is the above is sent |
| 6400 | * from peterius to intrusion */ |
| 6401 | /* And then after we get this: |
| 6402 | //0063 0000 696e 7472 7573 696f 6e00 0000 0000 0000 |
| 6403 | //0000 0000 0000 0000 0000 0000 00f1 0800 031b 0060 |
| 6404 | //0305 00a0 |
| 6405 | */ |
| 6406 | /* Its possible that these are requesting our reply |
| 6407 | * they might be sent out to all involved players but |
| 6408 | * like they suggest color, that 4th byte, or the byte |
| 6409 | * before the 00... again, intrusion dropped and was |
| 6410 | * white I think. Its weird to think we just |
| 6411 | * echo one of these back though... and they clearly |
| 6412 | * have the time also */ |
| 6413 | //000c 0000 696e 7472 7573 696f 6e00 0000 |
| 6414 | //0000 0000 0000 0000 0000 0000 0000 0000 |
| 6415 | //00f1 0800 031b 00a0 010d 0060 |
| 6416 | /* Looks like 32 is always off for the [34]08 and on for the [34]0c |
| 6417 | * the 08 seeming to be the one to respond too... */ |
| 6418 | /*if(msg[32] == 0x00) |
| 6419 | {*/ |
| 6420 | if(msg[34] == 0x08) |
| 6421 | { |
| 6422 | if(encoded_name == getUsername()) |
| 6423 | { |
| 6424 | sendEndgameMsg(game, we_resume); |
| 6425 | MoveRecord * aMove = new MoveRecord(); |
| 6426 | aMove->flags = MoveRecord::UNREMOVE; |
| 6427 | sendMove(game_number, aMove); |
| 6428 | delete aMove; |
| 6429 | sendResumeFromDisconnect(game_number); |
| 6430 | /* FIXME need to set time here, especially for opp but which color is which */ |
| 6431 | //?? |
| 6432 | /*enum TimeFlags f = */handleTimeChunk(boarddispatch, &(msg[36]), !game->white_first_flag); |
| 6433 | } |
| 6434 | else |
| 6435 | { |
| 6436 | } |
| 6437 | } |
| 6438 | /*} |
| 6439 | else //if(msg[32] == 0x01) |
| 6440 | { |
| 6441 | |
| 6442 | }*/ |
| 6443 | } |
| 6444 | } |
| 6445 | |
| 6446 | void TygemConnection::handleGameStateChange(unsigned char * msg, unsigned int size) |
| 6447 | { |
| 6448 | //0681 01f7 0201 0305 0340 033b 0280 0100 0000 |
| 6449 | if(size != 20) |
| 6450 | { |
| 6451 | qDebug("game state change msg of strange size %d", size); |
| 6452 | } |
| 6453 | #ifdef RE_DEBUG |
| 6454 | printf("0x0681: "); |
| 6455 | for(unsigned int i = 0; i < size; i++) |
| 6456 | printf("%02x", msg[i]); |
| 6457 | printf("\n"); |
| 6458 | #endif //RE_DEBUG |
| 6459 | unsigned short game_number = (msg[0] << 8) + msg[1]; |
| 6460 | BoardDispatch * boarddispatch = getIfBoardDispatch(game_number); |
| 6461 | if(!boarddispatch) |
| 6462 | { |
| 6463 | qDebug("Unexpected: got 0681 for board %d", game_number); |
| 6464 | return; |
| 6465 | } |
| 6466 | |
| 6467 | if(match_negotiation_state->opponentRejoining()) |
| 6468 | { |
| 6469 | //pretty sure this is where we popup the dialog |
| 6470 | |
| 6471 | /* -- allows our time to start again, but they can't play |
| 6472 | * nothing and -=2 don't have our time start */ |
| 6473 | //move_message_number_ack--; //who knows?!?! only when they drop during our turn? |
| 6474 | MoveRecord * aMove = new MoveRecord(); |
| 6475 | aMove->flags = MoveRecord::UNREMOVE; |
| 6476 | aMove->color = stoneErase; //flip the colors, FIXME check for other UNREMOVE sends |
| 6477 | sendMove(game_number, aMove); |
| 6478 | delete aMove; |
| 6479 | //move_message_number_ack++; |
| 6480 | |
| 6481 | //match_negotiation_state->opponentRejoins(); |
| 6482 | sendResumeFromDisconnect(game_number); |
| 6483 | |
| 6484 | /* We have to wait here for them to hit okay. FIXME */ |
| 6485 | match_negotiation_state->opponentReconnect(); //just startMatch |
| 6486 | boarddispatch->startTime(); |
| 6487 | //if((boarddispatch->getBlackTurn() && gameData->black_name == getUsername()) || |
| 6488 | // (!boarddispatch->getBlackTurn() && gameData->white_name == getUsername())) |
| 6489 | QString q; |
| 6490 | boarddispatch->moveControl(q); //whos turn is it FIXME |
| 6491 | /* When we accept invite, and they drop and rejoin, this doesn't work FIXME |
| 6492 | * also, as white, our time got reset to full for some reason, but that might have been |
| 6493 | * from when we dropped to test it. */ |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | /* This has been removed I think. If its still possible, it has to be flagged in the match settings |
| 6498 | * somewhere */ |
| 6499 | void TygemConnection::handleCountRequest(unsigned char * msg, unsigned int size) |
| 6500 | { |
| 6501 | unsigned char * p = msg; |
| 6502 | BoardDispatch * boarddispatch; |
| 6503 | GameData * game; |
| 6504 | unsigned short game_number; |
| 6505 | unsigned char name[15]; |
| 6506 | name[14] = 0x00; |
| 6507 | QString encoded_name; |
| 6508 | if(size != 40) |
| 6509 | { |
| 6510 | qDebug("Count request of strange size: %d", size); |
| 6511 | } |
| 6512 | //0x066a |
| 6513 | //030a 0001 696e 7472 7573 696f 6e00 0000 0000 0009 |
| 6514 | //696e 7472 7573 696f 6e00 0002 0000 0000 0000 0000 |
| 6515 | //0x066b: 00da0001c6f7bcbcc0ccb5b700000000000000166d696e676d696e67303100000100000000000000 |
| 6516 | |
| 6517 | game_number = (msg[0] << 8) + msg[1]; |
| 6518 | boarddispatch = getIfBoardDispatch(game_number); |
| 6519 | if(!boarddispatch) |
| 6520 | { |
| 6521 | qDebug("Unexpected: got 066a for board %d", game_number); |
| 6522 | return; |
| 6523 | } |
| 6524 | game = boarddispatch->getGameData(); |
| 6525 | #ifdef RE_DEBUG |
| 6526 | printf("***** 0x066a: "); |
| 6527 | for(unsigned int i = 0; i < size; i++) |
| 6528 | printf("%02x", msg[i]); |
| 6529 | printf("\n"); |
| 6530 | #endif //RE_DEBUG |
| 6531 | |
| 6532 | p += 2; |
| 6533 | p += 2; |
| 6534 | //this is name for not the player who hit done |
| 6535 | strncpy((char *)name, (char *)p, 14); |
| 6536 | p += 16; |
| 6537 | strncpy((char *)name, (char *)p, 11); |
| 6538 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 6539 | /*player = getDefaultroom->getPlayerListing(encoded_name); |
| 6540 | if(!player) |
| 6541 | { |
| 6542 | qDebug("Can't get player listing for %s in 067b", encoded_name.toLatin1().constData()); |
| 6543 | return; |
| 6544 | }*/ |
| 6545 | boarddispatch->recvKibitz(QString(), QString("%1 requests count...").arg( |
| 6546 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6547 | if(encoded_name == getUsername()) |
| 6548 | boarddispatch->recvRequestCount(); |
| 6549 | } |
| 6550 | |
| 6551 | void TygemConnection::handleCountRequestResponse(unsigned char * msg, unsigned int size) |
| 6552 | { |
| 6553 | unsigned char * p = msg; |
| 6554 | BoardDispatch * boarddispatch; |
| 6555 | GameData * game; |
| 6556 | unsigned short game_number; |
| 6557 | unsigned char name[15]; |
| 6558 | name[14] = 0x00; |
| 6559 | QString encoded_name; |
| 6560 | if(size != 0x24) //eweiqi 40 FIXME, 40 on tygem as well |
| 6561 | { |
| 6562 | qDebug("Count request response of strange size: %d", size); |
| 6563 | } |
| 6564 | //0x066a |
| 6565 | //030a 0001 696e 7472 7573 696f 6e00 0000 0000 0009 |
| 6566 | //696e 7472 7573 696f 6e00 0002 0000 0000 0000 0000 |
| 6567 | //0x066b: 00da 0001 c6f7 bcbc c0cc b5b7 0000 0000 |
| 6568 | //0000 0016 6d69 6e67 6d69 6e67 3031 0000 |
| 6569 | //0100 0000 0000 0000 |
| 6570 | |
| 6571 | game_number = (msg[0] << 8) + msg[1]; |
| 6572 | boarddispatch = getIfBoardDispatch(game_number); |
| 6573 | if(!boarddispatch) |
| 6574 | { |
| 6575 | qDebug("Unexpected: got 066b for board %d", game_number); |
| 6576 | return; |
| 6577 | } |
| 6578 | game = boarddispatch->getGameData(); |
| 6579 | #ifdef RE_DEBUG |
| 6580 | printf("***** 0x066b: "); |
| 6581 | for(unsigned int i = 0; i < size; i++) |
| 6582 | printf("%02x", msg[i]); |
| 6583 | printf("\n"); |
| 6584 | #endif //RE_DEBUG |
| 6585 | |
| 6586 | p += 2; |
| 6587 | p += 2; |
| 6588 | //this is name for not the player who hit done |
| 6589 | strncpy((char *)name, (char *)p, 14); |
| 6590 | p += 16; |
| 6591 | strncpy((char *)name, (char *)p, 11); |
| 6592 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 6593 | /*player = getDefaultroom->getPlayerListing(encoded_name); |
| 6594 | if(!player) |
| 6595 | { |
| 6596 | qDebug("Can't get player listing for %s in 067b", encoded_name.toLatin1().constData()); |
| 6597 | return; |
| 6598 | }*/ |
| 6599 | if(msg[32] == 0x01) |
| 6600 | { |
| 6601 | boarddispatch->recvKibitz(QString(), QString("%1 has accepted count request...").arg( |
| 6602 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6603 | if(encoded_name == getUsername()) |
| 6604 | boarddispatch->recvAcceptCountRequest(); |
| 6605 | else |
| 6606 | { |
| 6607 | boarddispatch->recvEnterScoreMode(); |
| 6608 | match_negotiation_state->enterScoreMode(); |
| 6609 | } |
| 6610 | } |
| 6611 | else |
| 6612 | { |
| 6613 | boarddispatch->recvKibitz(QString(), QString("%1 has rejected count request...").arg( |
| 6614 | (encoded_name == game->black_name ? game->white_name : game->black_name))); |
| 6615 | if(encoded_name == getUsername()) |
| 6616 | { |
| 6617 | boarddispatch->recvRejectCountRequest(); |
| 6618 | } |
| 6619 | } |
| 6620 | } |
| 6621 | |
| 6622 | void TygemConnection::handleTime(unsigned char * msg, unsigned int size) |
| 6623 | { |
| 6624 | BoardDispatch * boarddispatch; |
| 6625 | unsigned char * p = msg; |
| 6626 | GameData * gr; |
| 6627 | unsigned short game_number = (p[0] << 8) + p[1]; |
| 6628 | bool player_is_black; |
| 6629 | if(size != 12) |
| 6630 | { |
| 6631 | qDebug("time message of size: %d\n", size); |
| 6632 | //FIXME |
| 6633 | } |
| 6634 | boarddispatch = getIfBoardDispatch(game_number); |
| 6635 | if(!boarddispatch) |
| 6636 | { |
| 6637 | qDebug("No board dispatch for %02x%02x", p[0], p[1]); |
| 6638 | return; |
| 6639 | } |
| 6640 | gr = boarddispatch->getGameData(); |
| 6641 | if(!gr) |
| 6642 | { |
| 6643 | qDebug("Can't get game record for board: %d", game_number); |
| 6644 | return; |
| 6645 | } |
| 6646 | #ifdef RE_DEBUG |
| 6647 | //#ifdef NOISY_DEBUG |
| 6648 | printf("TIME: "); |
| 6649 | for(unsigned int i = 0; i < size; i++) |
| 6650 | printf("%02x", p[i]); |
| 6651 | printf("\n"); |
| 6652 | //#endif //NOISY_DEBUG |
| 6653 | #endif //RE_DEBUG |
| 6654 | |
| 6655 | p += 2; |
| 6656 | // this gets reversed |
| 6657 | player_is_black = p[0] ^ gr->white_first_flag; |
| 6658 | p += 2; |
| 6659 | enum TimeFlags f = handleTimeChunk(boarddispatch, p, player_is_black); |
| 6660 | /* double check the below */ |
| 6661 | if(f == BLACK_LOSES && gr->white_name == getUsername()) |
| 6662 | { |
| 6663 | GameResult aGameResult; |
| 6664 | aGameResult.result = GameResult::TIME; |
| 6665 | aGameResult.winner_color = stoneWhite; |
| 6666 | aGameResult.winner_name = gr->white_name; |
| 6667 | aGameResult.loser_name = gr->black_name; |
| 6668 | boarddispatch->recvResult(&aGameResult); |
| 6669 | sendResult(gr, &aGameResult); |
| 6670 | } |
| 6671 | else if(f == WHITE_LOSES && gr->black_name == getUsername()) |
| 6672 | { |
| 6673 | GameResult aGameResult; |
| 6674 | aGameResult.result = GameResult::TIME; |
| 6675 | aGameResult.winner_color = stoneBlack; |
| 6676 | aGameResult.winner_name = gr->black_name; |
| 6677 | aGameResult.loser_name = gr->white_name; |
| 6678 | boarddispatch->recvResult(&aGameResult); |
| 6679 | sendResult(gr, &aGameResult); |
| 6680 | } |
| 6681 | } |
| 6682 | |
| 6683 | enum TygemConnection::TimeFlags TygemConnection::handleTimeChunk(BoardDispatch * boarddispatch, unsigned char chunk[8], bool black_first) |
| 6684 | { |
| 6685 | unsigned char c_periods, o_periods; |
| 6686 | unsigned char c_flags, o_flags; |
| 6687 | unsigned int c_seconds, o_seconds; |
| 6688 | int white_seconds, white_periods; |
| 6689 | int black_seconds, black_periods; |
| 6690 | bool black_loses_on_time = false, white_loses_on_time = false; |
| 6691 | |
| 6692 | /* Since we send time, we block it if its from us */ |
| 6693 | if(black_first && boarddispatch->getGameData()->black_name == getUsername()) |
| 6694 | return GAME_ON; |
| 6695 | if(!black_first && boarddispatch->getGameData()->white_name == getUsername()) |
| 6696 | return GAME_ON; |
| 6697 | /* shouldn't we really be recving this time |
| 6698 | * to sync with server lag? maybe FIXME*/ |
| 6699 | |
| 6700 | //first four are this current players turn |
| 6701 | c_periods = chunk[0]; |
| 6702 | c_seconds = chunk[1] + (chunk[2] * 60); |
| 6703 | c_flags = chunk[3]; |
| 6704 | //second four are other player |
| 6705 | o_periods = chunk[4]; |
| 6706 | o_seconds = chunk[5] + (chunk[6] * 60); |
| 6707 | o_flags = chunk[7]; |
| 6708 | if(black_first) |
| 6709 | { |
| 6710 | black_seconds = c_seconds; |
| 6711 | white_seconds = o_seconds; |
| 6712 | if(c_flags & 0x20) |
| 6713 | black_periods = c_periods; |
| 6714 | else |
| 6715 | black_periods = -1; |
| 6716 | //if(c_flags & 0x80 && black_periods == 0) |
| 6717 | if(black_periods == 0 && black_seconds == 0) |
| 6718 | black_loses_on_time = true; |
| 6719 | if(o_flags & 0x20) |
| 6720 | white_periods = o_periods; |
| 6721 | else |
| 6722 | white_periods = -1; |
| 6723 | //if(o_flags & 0x80 && white_periods == 0) |
| 6724 | if(white_periods == 0 && white_seconds == 0) |
| 6725 | white_loses_on_time = true; |
| 6726 | } |
| 6727 | else |
| 6728 | { |
| 6729 | black_seconds = o_seconds; |
| 6730 | white_seconds = c_seconds; |
| 6731 | if(c_flags & 0x20) |
| 6732 | white_periods = c_periods; |
| 6733 | else |
| 6734 | white_periods = -1; |
| 6735 | //if(c_flags & 0x80 && white_periods == 0) |
| 6736 | if(white_periods == 0 && white_seconds == 0) |
| 6737 | white_loses_on_time = true; |
| 6738 | if(o_flags & 0x20) |
| 6739 | black_periods = o_periods; |
| 6740 | else |
| 6741 | black_periods = -1; |
| 6742 | //if(o_flags & 0x80 && black_periods == 0) |
| 6743 | if(black_periods == 0 && black_seconds == 0) |
| 6744 | black_loses_on_time = true; |
| 6745 | } |
| 6746 | |
| 6747 | /* |
| 6748 | if(boarddispatch->getGameData()->black_name == getUsername()) |
| 6749 | black_seconds = 0; |
| 6750 | if(boarddispatch->getGameData()->white_name == getUsername()) |
| 6751 | white_seconds = 0;*/ |
| 6752 | |
| 6753 | /* We should most likely kill the timer then... */ |
| 6754 | boarddispatch->recvTime(TimeRecord(white_seconds, white_periods), TimeRecord(black_seconds, black_periods)); |
| 6755 | if(black_loses_on_time) |
| 6756 | return BLACK_LOSES; |
| 6757 | else if(white_loses_on_time) |
| 6758 | return WHITE_LOSES; |
| 6759 | return GAME_ON; |
| 6760 | } |
| 6761 | |
| 6762 | /* FIXME disturbing implication. When we were sending the moves one off, |
| 6763 | * it would draw them on the place we clicked, but then draw the last move |
| 6764 | * on the place we got back. This indicates inconsistencies in move |
| 6765 | * placement code, serious issue */ |
| 6766 | /* FIXME Consider adding code to actually filter out by the move message |
| 6767 | * number per game in case that's a kind of prevention against replays |
| 6768 | * or something. */ |
| 6769 | /* FIXME When resuming a game "Opp sends bad move message number" comes up |
| 6770 | * repeatedly because we're passed all the moves at once and sends none of |
| 6771 | * them. bad move message number stuff needs to be fixed up somehow anyway. */ |
| 6772 | //0668 |
| 6773 | void TygemConnection::handleMove(unsigned char * msg, unsigned int size) |
| 6774 | { |
| 6775 | BoardDispatch * boarddispatch; |
| 6776 | unsigned char * p = msg; |
| 6777 | GameData * gr; |
| 6778 | int x, y; |
| 6779 | unsigned short game_number = (p[0] << 8) + p[1]; |
| 6780 | int player_number, player_number2, number0; |
| 6781 | bool opponents_move = false; |
| 6782 | if(size < 24) //FIXME |
| 6783 | { |
| 6784 | qDebug("Bad move msg, size: %d", size); |
| 6785 | return; |
| 6786 | } |
| 6787 | #ifdef RE_DEBUG |
| 6788 | printf("Move msg:\n"); |
| 6789 | for(unsigned int i = 0; i < size; i++) |
| 6790 | printf("%02x ", p[i]); |
| 6791 | printf("\n"); |
| 6792 | #endif //RE_DEBUG |
| 6793 | boarddispatch = getIfBoardDispatch(game_number); |
| 6794 | if(!boarddispatch) |
| 6795 | { |
| 6796 | qDebug("No board dispatch for %02x%02x", p[0], p[1]); |
| 6797 | return; |
| 6798 | } |
| 6799 | gr = boarddispatch->getGameData(); |
| 6800 | if(!gr) |
| 6801 | { |
| 6802 | //FIXME |
| 6803 | qDebug("Shouldn't be a board dispatch here\n"); |
| 6804 | return; |
| 6805 | } |
| 6806 | p += 2; |
| 6807 | |
| 6808 | if(p[0] == 0x00) //from inviter |
| 6809 | player_number2 = 0; |
| 6810 | else if(p[0] == 0x01) //from invitee |
| 6811 | player_number2 = 1; |
| 6812 | else |
| 6813 | { |
| 6814 | player_number2 = -1; |
| 6815 | qDebug("Strange invitation byte in move"); |
| 6816 | } |
| 6817 | #ifdef RE_DEBUG |
| 6818 | printf("%02x vs %02x!!\n", p[0], gr->our_invitation); |
| 6819 | #endif //RE_DEBUG |
| 6820 | //00 00 their invitation, their move |
| 6821 | //01 00 their invitation our move |
| 6822 | //00 01 02 after a possible screw up, their invitation, they rejoin |
| 6823 | p += 2; |
| 6824 | p += 4; //ffff ffff |
| 6825 | if(match_negotiation_state->isOngoingMatch(game_number) || match_negotiation_state->opponentRejoining()) |
| 6826 | { |
| 6827 | if(player_number2 == gr->our_invitation) |
| 6828 | opponents_move = true; |
| 6829 | else |
| 6830 | { |
| 6831 | //FIXME trying this assuming that its a rejoin and these are our moves we're getting: |
| 6832 | //likely still off depending on who goes first |
| 6833 | /* Doesn't work because we need to weed out DSC (UNREMOVE), etc. */ |
| 6834 | //move_message_number = (p[0] << 8) + p[1]; |
| 6835 | } |
| 6836 | } |
| 6837 | p += 4; |
| 6838 | //move msg number is also around here too |
| 6839 | //STO 0 move_number player_number x y 0a |
| 6840 | //SUR 0 move_number player_number 0a |
| 6841 | //REM 0 312 -1 -1 0 1 |
| 6842 | //REM 0 307 5 6 1 0 |
| 6843 | //REM 0 306 10 14 2 0 un remove? |
| 6844 | //REM 0 number x y player_number 0 |
| 6845 | //DSC 1 0 1 //dead stone clear |
| 6846 | //DSC 1 0 2 |
| 6847 | //SKI 0 player_number //skip? pass? |
| 6848 | |
| 6849 | //BAC 0 246 1 //part of review |
| 6850 | //CSP 0 233 0 //possibly something to do with sscoring (This might start the review) actually i think it resets it |
| 6851 | //CST 0 234 //this might start placing the new stones (CSP and CST are opposites) |
| 6852 | //FOR 0 186 1 |
| 6853 | //CRE 0 119 //maybe resets the board to the game, or just the tree. |
| 6854 | // END 0 119 //end review? |
| 6855 | |
| 6856 | // don't know, something on pro broadcast |
| 6857 | //WIT 0 8 2 |
| 6858 | //WIT 0 9 1 |
| 6859 | //WIT 0 10 2 |
| 6860 | //WIT 0 11 1 |
| 6861 | //WIT 0 12 2 |
| 6862 | #ifdef RE_DEBUG |
| 6863 | printf("%d %s\n", player_number2, (char *)p); |
| 6864 | #endif //RE_DEBUG |
| 6865 | if(strncmp((char *)p, "STO ", 4) == 0) |
| 6866 | { |
| 6867 | MoveRecord * aMove = new MoveRecord(); |
| 6868 | p += 4; |
| 6869 | if(sscanf((char *)p, "%d %d %d %d %d", &number0, &aMove->number, &player_number, &aMove->x, &aMove->y) != 5) |
| 6870 | { |
| 6871 | qDebug("Bad Move"); |
| 6872 | delete aMove; |
| 6873 | return; |
| 6874 | } |
| 6875 | /* The move message number is just that, it doesn't have to |
| 6876 | * have anything to do with the move number. Also, we FIXME |
| 6877 | * can't use a global in case we're watching multiple games? */ |
| 6878 | if(opponents_move) |
| 6879 | { |
| 6880 | if(aMove->number != (unsigned)move_message_number + 1) |
| 6881 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 6882 | move_message_number = aMove->number; |
| 6883 | move_message_number_ack = aMove->number; |
| 6884 | } |
| 6885 | //aMove->number = move_message_number; |
| 6886 | //aMove->number--; //first move is... handicap? |
| 6887 | aMove->number = NOMOVENUMBER; |
| 6888 | aMove->x++; |
| 6889 | aMove->y++; |
| 6890 | if(player_number == 1) |
| 6891 | aMove->color = stoneBlack; |
| 6892 | else if(player_number == 2) |
| 6893 | aMove->color = stoneWhite; |
| 6894 | else |
| 6895 | qDebug("Strange player number: %d", player_number); |
| 6896 | if(number0 != 0) |
| 6897 | qDebug("Number0 = %d\n", number0); |
| 6898 | //if(aMove->x == 0 || aMove->y == 0) //doubt it FIXME |
| 6899 | // aMove->flags = MoveRecord::PASS; |
| 6900 | boarddispatch->recvMove(aMove); |
| 6901 | delete aMove; |
| 6902 | } |
| 6903 | else if(strncmp((char *)p, "SKI ", 4) == 0) |
| 6904 | { |
| 6905 | p += 4; |
| 6906 | int move_number; |
| 6907 | if(sscanf((char *)p, "%d %d %d", &number0, &move_number, &player_number) != 2) |
| 6908 | { |
| 6909 | qDebug("Bad skip message"); |
| 6910 | return; |
| 6911 | } |
| 6912 | if(opponents_move) |
| 6913 | { |
| 6914 | if(move_number != (int)(move_message_number + 1)) |
| 6915 | qDebug("Opp sends bad move message number: %d", move_number); |
| 6916 | move_message_number = move_number; |
| 6917 | move_message_number_ack = move_number; |
| 6918 | } |
| 6919 | #ifdef RE_DEBUG |
| 6920 | boarddispatch->recvKibitz(QString(), "SKI"); |
| 6921 | #endif //RE_DEBUG |
| 6922 | } |
| 6923 | else if(strncmp((char *)p, "SUR ", 4) == 0) |
| 6924 | { |
| 6925 | p += 4; |
| 6926 | int move_number; |
| 6927 | if(sscanf((char *)p, "%d %d %d", &number0, &move_number, &player_number) != 3) |
| 6928 | { |
| 6929 | qDebug("Bad resign message"); |
| 6930 | return; |
| 6931 | } |
| 6932 | if(opponents_move) |
| 6933 | { |
| 6934 | if(move_number != move_message_number + 1) |
| 6935 | qDebug("Opp sends bad move message number: %d", move_number); |
| 6936 | move_message_number_ack = move_number; //necessary? FIXME |
| 6937 | } |
| 6938 | /* If opponent resigns, we act on the 0670 we receive */ |
| 6939 | } |
| 6940 | else if(strncmp((char *)p, "REM ", 4) == 0) |
| 6941 | { |
| 6942 | MoveRecord * aMove = new MoveRecord(); |
| 6943 | /* FIXME, fix potential bug with unremoving as on cyberoro ?!?? */ |
| 6944 | aMove->flags = MoveRecord::REMOVE_AREA; |
| 6945 | int remove_flag; |
| 6946 | p += 4; |
| 6947 | if(sscanf((char *)p, "%d %d %d %d %d", &number0, &aMove->number, &x, &y, &remove_flag) != 5) |
| 6948 | { |
| 6949 | qDebug("Bad Move"); |
| 6950 | delete aMove; |
| 6951 | return; |
| 6952 | } |
| 6953 | if(opponents_move) |
| 6954 | { |
| 6955 | if(aMove->number != (unsigned)move_message_number + 1) |
| 6956 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 6957 | move_message_number = aMove->number; |
| 6958 | move_message_number_ack = aMove->number; |
| 6959 | } |
| 6960 | if(x == -1 && y == -1) |
| 6961 | { |
| 6962 | #ifdef RE_DEBUG |
| 6963 | boarddispatch->recvKibitz(QString(), "REM -1 -1"); |
| 6964 | #endif //RE_DEBUG |
| 6965 | /* We also get this on a resume and send it as well */ |
| 6966 | //done scoring! recvKibitz, etc. |
| 6967 | //seems to be only one of these |
| 6968 | //no, I see two |
| 6969 | delete aMove; |
| 6970 | return; |
| 6971 | } |
| 6972 | aMove->x = x + 1; |
| 6973 | aMove->y = y + 1; |
| 6974 | aMove->number--; |
| 6975 | /*if(player_number == 1) |
| 6976 | aMove->color = stoneBlack; |
| 6977 | else if(player_number == 2) |
| 6978 | aMove->color = stoneWhite; |
| 6979 | else |
| 6980 | qDebug("Strange player number: %d", player_number);*/ |
| 6981 | if(number0 != 0) |
| 6982 | qDebug("Number0 = %d\n", number0); |
| 6983 | //if(aMove->x == 0 || aMove->y == 0) //doubt it FIXME |
| 6984 | // aMove->flags = MoveRecord::PASS; |
| 6985 | boarddispatch->recvMove(aMove); |
| 6986 | delete aMove; |
| 6987 | } |
| 6988 | else if(strncmp((char *)p, "DSC ", 4) == 0) |
| 6989 | { |
| 6990 | MoveRecord * aMove = new MoveRecord(); |
| 6991 | /* FIXME, fix potential bug with unremoving as on cyberoro ?!?? */ |
| 6992 | aMove->flags = MoveRecord::UNREMOVE_AREA; |
| 6993 | int number2; |
| 6994 | p += 4; |
| 6995 | if(sscanf((char *)p, "%d %d %d", &number0, &number2, &player_number) != 3) |
| 6996 | { |
| 6997 | qDebug("Bad Move"); |
| 6998 | delete aMove; |
| 6999 | return; |
| 7000 | } |
| 7001 | if(player_number == 2) |
| 7002 | aMove->color = stoneWhite; |
| 7003 | else |
| 7004 | aMove->color = stoneBlack; |
| 7005 | /* This is disgusting FIXME we need to figure out what the move_message_number s are |
| 7006 | * supposed to be. The thing here is that the DSC one seems to expect the next one |
| 7007 | * but not be the next one, meaning we can't just set it here on our previous move |
| 7008 | * but then I guess it shifts the opponents off too... I just need to think about it */ |
| 7009 | /* FIXME FIXME */ |
| 7010 | //if(match_negotiation_state->isOngoingMatch(game_number) && !opponents_move) |
| 7011 | // move_message_number--; |
| 7012 | /*if(player_number == 1) |
| 7013 | aMove->color = stoneBlack; |
| 7014 | else if(player_number == 2) |
| 7015 | aMove->color = stoneWhite; |
| 7016 | else |
| 7017 | qDebug("Strange player number: %d", player_number);*/ |
| 7018 | //1 DSC 1 0 1 |
| 7019 | if(number0 != 1) |
| 7020 | qDebug("Number1 = %d\n", number0); //??? |
| 7021 | //if(aMove->x == 0 || aMove->y == 0) //doubt it FIXME |
| 7022 | // aMove->flags = MoveRecord::PASS; |
| 7023 | boarddispatch->recvMove(aMove); |
| 7024 | delete aMove; |
| 7025 | if(opponents_move && match_negotiation_state->opponentRejoining()) |
| 7026 | { |
| 7027 | qDebug("so we were getting here after all"); |
| 7028 | //have to get this if we're rejoining, BEFORE we start playing again FIXME |
| 7029 | //or we could start playing on 0x68e |
| 7030 | } |
| 7031 | } |
| 7032 | else if(strncmp((char *)p, "WIT ", 4) == 0) |
| 7033 | { |
| 7034 | MoveRecord * aMove = new MoveRecord(); |
| 7035 | /* WIT looks like a kind of review undo (withdraw?), player_number here is |
| 7036 | * who's turn it now is.*/ |
| 7037 | p += 4; |
| 7038 | if(sscanf((char *)p, "%d %d %d", &number0, &aMove->number, &player_number) != 3) |
| 7039 | { |
| 7040 | qDebug("Bad WIT move"); |
| 7041 | delete aMove; |
| 7042 | return; |
| 7043 | } |
| 7044 | if(opponents_move) |
| 7045 | { |
| 7046 | //can this even happen? |
| 7047 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7048 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7049 | move_message_number = aMove->number; |
| 7050 | move_message_number_ack = aMove->number; |
| 7051 | } |
| 7052 | |
| 7053 | aMove->number = NOMOVENUMBER; |
| 7054 | aMove->flags = MoveRecord::UNDO; |
| 7055 | if(player_number == 2) |
| 7056 | aMove->color = stoneBlack; |
| 7057 | else |
| 7058 | aMove->color = stoneWhite; |
| 7059 | boarddispatch->recvMove(aMove); |
| 7060 | delete aMove; |
| 7061 | } |
| 7062 | else if(strncmp((char *)p, "BAC ", 4) == 0) |
| 7063 | { |
| 7064 | MoveRecord * aMove = new MoveRecord(); |
| 7065 | int moves_to_move; |
| 7066 | /* I assume this is backup, or go back to a particular point. I'm not quite sure |
| 7067 | * what to do with it right now, since currently observer can look at any move. |
| 7068 | * but quite likely it just sets the move to something. */ |
| 7069 | /* Can go back and forward 1 to 5 */ |
| 7070 | p += 4; |
| 7071 | if(sscanf((char *)p, "%d %d %d", &number0, &aMove->number, &moves_to_move) != 3) |
| 7072 | { |
| 7073 | qDebug("Bad BAC move"); |
| 7074 | delete aMove; |
| 7075 | return; |
| 7076 | } |
| 7077 | aMove->flags = MoveRecord::BACKWARD; |
| 7078 | aMove->number = moves_to_move; |
| 7079 | qDebug("BAC msg %d %d", aMove->number, moves_to_move); |
| 7080 | /*if(opponents_move) |
| 7081 | { |
| 7082 | //can this even happen? |
| 7083 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7084 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7085 | move_message_number = aMove->number; |
| 7086 | move_message_number_ack = aMove->number; |
| 7087 | }*/ |
| 7088 | boarddispatch->recvMove(aMove); |
| 7089 | delete aMove; |
| 7090 | } |
| 7091 | else if(strncmp((char *)p, "FOR ", 4) == 0) |
| 7092 | { |
| 7093 | MoveRecord * aMove = new MoveRecord(); |
| 7094 | int moves_to_move; |
| 7095 | p += 4; |
| 7096 | if(sscanf((char *)p, "%d %d %d", &number0, &aMove->number, &moves_to_move) != 3) |
| 7097 | { |
| 7098 | qDebug("Bad FOR move"); |
| 7099 | delete aMove; |
| 7100 | return; |
| 7101 | } |
| 7102 | aMove->flags = MoveRecord::FORWARD; |
| 7103 | aMove->number = moves_to_move; |
| 7104 | qDebug("FOR msg %d %d", aMove->number, moves_to_move); |
| 7105 | /*if(opponents_move) |
| 7106 | { |
| 7107 | //can this even happen? |
| 7108 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7109 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7110 | move_message_number = aMove->number; |
| 7111 | move_message_number_ack = aMove->number; |
| 7112 | }*/ |
| 7113 | boarddispatch->recvMove(aMove); |
| 7114 | delete aMove; |
| 7115 | } |
| 7116 | else if(strncmp((char *)p, "CRE ", 4) == 0) |
| 7117 | { |
| 7118 | MoveRecord * aMove = new MoveRecord(); |
| 7119 | p += 4; |
| 7120 | if(sscanf((char *)p, "%d %d", &number0, &aMove->number) != 2) |
| 7121 | { |
| 7122 | qDebug("Bad CRE move"); |
| 7123 | delete aMove; |
| 7124 | return; |
| 7125 | } |
| 7126 | aMove->flags = MoveRecord::DELETEBRANCH; |
| 7127 | aMove->number = NOMOVENUMBER; |
| 7128 | qDebug("CRE msg %d", aMove->number); |
| 7129 | /*if(opponents_move) |
| 7130 | { |
| 7131 | //can this even happen? |
| 7132 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7133 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7134 | move_message_number = aMove->number; |
| 7135 | move_message_number_ack = aMove->number; |
| 7136 | }*/ |
| 7137 | boarddispatch->recvMove(aMove); |
| 7138 | delete aMove; |
| 7139 | } |
| 7140 | else if(strncmp((char *)p, "CSP ", 4) == 0) |
| 7141 | { |
| 7142 | MoveRecord * aMove = new MoveRecord(); |
| 7143 | p += 4; |
| 7144 | if(sscanf((char *)p, "%d %d %d", &number0, &aMove->number, &player_number) != 3) |
| 7145 | { |
| 7146 | qDebug("Bad CSP move"); |
| 7147 | delete aMove; |
| 7148 | return; |
| 7149 | } |
| 7150 | aMove->flags = MoveRecord::RESETBRANCH; |
| 7151 | aMove->number = NOMOVENUMBER; |
| 7152 | qDebug("CSP msg %d %d", aMove->number, player_number); |
| 7153 | /*if(opponents_move) |
| 7154 | { |
| 7155 | //can this even happen? |
| 7156 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7157 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7158 | move_message_number = aMove->number; |
| 7159 | move_message_number_ack = aMove->number; |
| 7160 | }*/ |
| 7161 | boarddispatch->recvMove(aMove); |
| 7162 | boarddispatch->setReviewInVariation(false); |
| 7163 | delete aMove; |
| 7164 | } |
| 7165 | else if(strncmp((char *)p, "CST ", 4) == 0) |
| 7166 | { |
| 7167 | MoveRecord * aMove = new MoveRecord(); |
| 7168 | p += 4; |
| 7169 | if(sscanf((char *)p, "%d %d", &number0, &aMove->number) != 2) |
| 7170 | { |
| 7171 | qDebug("Bad CST move"); |
| 7172 | delete aMove; |
| 7173 | return; |
| 7174 | } |
| 7175 | //aMove->flags = MoveRecord::FORWARD; |
| 7176 | //aMove->number = NOMOVENUMBER; |
| 7177 | qDebug("CST msg %d", aMove->number); |
| 7178 | /*if(opponents_move) |
| 7179 | { |
| 7180 | //can this even happen? |
| 7181 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7182 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7183 | move_message_number = aMove->number; |
| 7184 | move_message_number_ack = aMove->number; |
| 7185 | }*/ |
| 7186 | //boarddispatch->recvMove(aMove); |
| 7187 | boarddispatch->setReviewInVariation(true); |
| 7188 | delete aMove; |
| 7189 | } |
| 7190 | else if(strncmp((char *)p, "BEG ", 4) == 0) |
| 7191 | { |
| 7192 | MoveRecord * aMove = new MoveRecord(); |
| 7193 | p += 4; |
| 7194 | if(sscanf((char *)p, "%d %d", &number0, &aMove->number) != 2) |
| 7195 | { |
| 7196 | qDebug("Bad BEG move"); |
| 7197 | delete aMove; |
| 7198 | return; |
| 7199 | } |
| 7200 | if(boarddispatch->getReviewInVariation()) |
| 7201 | aMove->flags = MoveRecord::RESETBRANCH; |
| 7202 | else |
| 7203 | aMove->flags = MoveRecord::RESETGAME; |
| 7204 | aMove->number = NOMOVENUMBER; |
| 7205 | qDebug("BEG msg %d", aMove->number); |
| 7206 | /*if(opponents_move) |
| 7207 | { |
| 7208 | //can this even happen? |
| 7209 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7210 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7211 | move_message_number = aMove->number; |
| 7212 | move_message_number_ack = aMove->number; |
| 7213 | }*/ |
| 7214 | boarddispatch->recvMove(aMove); |
| 7215 | delete aMove; |
| 7216 | } |
| 7217 | else if(strncmp((char *)p, "END ", 4) == 0) |
| 7218 | { |
| 7219 | MoveRecord * aMove = new MoveRecord(); |
| 7220 | p += 4; |
| 7221 | if(sscanf((char *)p, "%d %d", &number0, &aMove->number) != 2) |
| 7222 | { |
| 7223 | qDebug("Bad END move"); |
| 7224 | delete aMove; |
| 7225 | return; |
| 7226 | } |
| 7227 | aMove->flags = MoveRecord::TOEND; |
| 7228 | aMove->number = NOMOVENUMBER; |
| 7229 | qDebug("END msg %d", aMove->number); |
| 7230 | /*if(opponents_move) |
| 7231 | { |
| 7232 | //can this even happen? |
| 7233 | if(aMove->number != (unsigned)move_message_number + 1) |
| 7234 | qDebug("Opp sends bad move message number: %d", aMove->number); |
| 7235 | move_message_number = aMove->number; |
| 7236 | move_message_number_ack = aMove->number; |
| 7237 | }*/ |
| 7238 | boarddispatch->recvMove(aMove); |
| 7239 | delete aMove; |
| 7240 | } |
| 7241 | else |
| 7242 | printf("Unknown: %s\n", (char *)p); |
| 7243 | } |
| 7244 | |
| 7245 | void TygemConnection::handlePass(unsigned char * msg, unsigned int size, int /* FIXME */) |
| 7246 | { |
| 7247 | if(size != 40) //or 0x24?? |
| 7248 | { |
| 7249 | qDebug("Pass msg of strange size: %d", size); |
| 7250 | } |
| 7251 | unsigned char * p = msg; |
| 7252 | BoardDispatch * boarddispatch; |
| 7253 | int game_number = (p[0] << 8) + p[1]; |
| 7254 | p += 2; |
| 7255 | boarddispatch = getIfBoardDispatch(game_number); |
| 7256 | if(!boarddispatch) |
| 7257 | { |
| 7258 | qDebug("Can't get board for %d for pass\n", game_number); |
| 7259 | return; |
| 7260 | } |
| 7261 | GameData * game = boarddispatch->getGameData(); |
| 7262 | bool white; |
| 7263 | /* Don't think so |
| 7264 | if(game->our_invitation) |
| 7265 | white = !p[0]; //doublecheck |
| 7266 | else |
| 7267 | white = p[0];*/ |
| 7268 | p += 2; |
| 7269 | unsigned char name[15]; |
| 7270 | name[14] = 0x00; |
| 7271 | QString encoded_name, encoded_name2; |
| 7272 | |
| 7273 | strncpy((char *)name, (char *)p, 14); |
| 7274 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7275 | |
| 7276 | p += 15; |
| 7277 | |
| 7278 | //rank byte |
| 7279 | p++; |
| 7280 | strncpy((char *)name, (char *)p, 11); |
| 7281 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7282 | p += 10; |
| 7283 | p++; |
| 7284 | if(encoded_name2 == game->white_name) |
| 7285 | white = false; |
| 7286 | else |
| 7287 | white = true; |
| 7288 | //white = (p[0] != 0x02); |
| 7289 | //there's an 02 much later here and that must be... black color? |
| 7290 | /* No, this is likely the 02 country code that floats around, 02 meaning |
| 7291 | * china. Its just part of the header */ |
| 7292 | /* Do we do this here or on the handleMove SKI? FIXME |
| 7293 | * I think this message is more reliable, but not sure for observed games |
| 7294 | * probably here */ |
| 7295 | MoveRecord * move = new MoveRecord(); |
| 7296 | move->flags = MoveRecord::PASS; |
| 7297 | move->color = white ? stoneWhite : stoneBlack; |
| 7298 | |
| 7299 | //boarddispatch->recvKibitz(0, QString(encoded_name) + " passes " + QString::number(move->color)); |
| 7300 | //not right |
| 7301 | //still not right, for instance if we offer the game and play black |
| 7302 | //it gets reversed |
| 7303 | boarddispatch->recvMove(move); |
| 7304 | match_negotiation_state->incrementPasses(); |
| 7305 | if(match_negotiation_state->twoPasses()) |
| 7306 | { |
| 7307 | match_negotiation_state->enterScoreMode(); |
| 7308 | /* Change to Tygem has some special score thing, I'd have to look into getting it exactly |
| 7309 | * but you can't mark stones dead anymore */ |
| 7310 | boarddispatch->createCountDialog(); |
| 7311 | } |
| 7312 | //black |
| 7313 | //00870101 6261 696e 6974 6500 0000 0000 0000 00116261696e69746500000000020000000000000000 |
| 7314 | #ifdef RE_DEBUG |
| 7315 | printf("**** 0x0669: "); |
| 7316 | for(unsigned int i = 0; i < size; i++) |
| 7317 | printf("%02x", msg[i]); |
| 7318 | printf("\n"); |
| 7319 | #endif //RE_DEBUG |
| 7320 | } |
| 7321 | |
| 7322 | QString TygemConnection::getStatusFromCode(unsigned char /* FIXME */, QString /* FIXME */) |
| 7323 | { |
| 7324 | return ""; |
| 7325 | } |
| 7326 | |
| 7327 | int TygemConnection::compareRanks(QString rankA, QString rankB) |
| 7328 | { |
| 7329 | if(rankA.contains("p") && !rankB.contains("p")) |
| 7330 | return 1; |
| 7331 | if(rankB.contains("p") && !rankA.contains("p")) |
| 7332 | return -1; |
| 7333 | if(rankA.contains("d") && !rankB.contains("d")) |
| 7334 | return 1; |
| 7335 | if(rankB.contains("d") && !rankA.contains("d")) |
| 7336 | return -1; |
| 7337 | if(rankA.contains("k")) |
| 7338 | { |
| 7339 | QString buffer = rankA; |
| 7340 | buffer.replace(QRegExp("[pdk]"), ""); |
| 7341 | int ordinalA = buffer.toInt(); |
| 7342 | buffer = rankB; |
| 7343 | buffer.replace(QRegExp("[pdk]"), ""); |
| 7344 | int ordinalB = buffer.toInt(); |
| 7345 | if(ordinalA > ordinalB) |
| 7346 | return -1; |
| 7347 | else if(ordinalA == ordinalB) |
| 7348 | return 0; |
| 7349 | else |
| 7350 | return 1; |
| 7351 | } |
| 7352 | else |
| 7353 | { |
| 7354 | QString buffer = rankA; |
| 7355 | buffer.replace(QRegExp("[pdk]"), ""); |
| 7356 | int ordinalA = buffer.toInt(); |
| 7357 | buffer = rankB; |
| 7358 | buffer.replace(QRegExp("[pdk]"), ""); |
| 7359 | int ordinalB = buffer.toInt(); |
| 7360 | if(ordinalA > ordinalB) |
| 7361 | return 1; |
| 7362 | else if(ordinalA == ordinalB) |
| 7363 | return 0; |
| 7364 | else |
| 7365 | return -1; |
| 7366 | } |
| 7367 | } |
| 7368 | |
| 7369 | //0639 |
| 7370 | void TygemConnection::handleBoardOpened(unsigned char * msg, unsigned int size) |
| 7371 | { |
| 7372 | BoardDispatch * boarddispatch; |
| 7373 | unsigned char * p = msg; |
| 7374 | GameData * aGameData; |
| 7375 | unsigned short game_number; |
| 7376 | if(size != 0x30) |
| 7377 | { |
| 7378 | qDebug("board opened msg of strange size: %d\n", size); |
| 7379 | } |
| 7380 | |
| 7381 | game_number = (p[0] << 8) + p[1]; |
| 7382 | p += 2; |
| 7383 | |
| 7384 | #ifdef RE_DEBUG |
| 7385 | printf("0639 match: \n"); |
| 7386 | for(unsigned int i = 0; i < size; i++) |
| 7387 | printf("%02x", msg[i]); |
| 7388 | printf("\n"); |
| 7389 | #endif //RE_DEBUG |
| 7390 | |
| 7391 | //I suspect the below is a passworded private game, though I'm not sure: |
| 7392 | //0258 0000 0000 0000 0000 0000 0000 0000 |
| 7393 | //0000 0015 7368 656e 7368 6938 0065 0002 |
| 7394 | //0000 0001 0000 0000 00ff 0000 0003 00ff |
| 7395 | //that 3 maybe? not sure... not even sure its passworded |
| 7396 | |
| 7397 | //normal game |
| 7398 | //0308 0100 b5b6 c7ef d2b9 0000 0000 0000 |
| 7399 | //0000 001a 6461 6f71 7900 0000 0079 0002 |
| 7400 | //0000 024f 00df 0000 00ff 0000 0000 0000 |
| 7401 | |
| 7402 | //a game we're joining |
| 7403 | //0006 0100 696e 7472 7573 696f 6e00 0000 |
| 7404 | //0000 000a 696e 7472 7573 696f 6e00 0002 |
| 7405 | //0000 0001 0000 0000 00ff 0000 0000 0000 |
| 7406 | |
| 7407 | |
| 7408 | |
| 7409 | boarddispatch = getBoardDispatch(game_number); |
| 7410 | /* We have to pass the game data record on the 0639 |
| 7411 | * because the boardwindow is created by the dispatch |
| 7412 | * upon recv a record. The board window is necessary |
| 7413 | * to receive the observers from 0666, 0667 illicited |
| 7414 | * by the 0665 message, the 0665 message also illicits |
| 7415 | * the 0671 info message. So this is the way it must |
| 7416 | * be done */ |
| 7417 | aGameData = boarddispatch->getGameData(); |
| 7418 | aGameData->number = game_number; |
| 7419 | |
| 7420 | /* Its possibly that we're getting this on response to 0638, |
| 7421 | * trying to join a game we got disconnected from. In which |
| 7422 | * case ... well actually, I think we should get a proper |
| 7423 | * debug, something else is screwed up, like player names |
| 7424 | * or something FIXME */ |
| 7425 | |
| 7426 | if(match_negotiation_state->isOurGame(game_number)) |
| 7427 | aGameData->gameMode = modeMatch; |
| 7428 | /*GameListing * gl = room->getGameListing(game_number); |
| 7429 | //FIXME get first time records |
| 7430 | if(gl) |
| 7431 | { |
| 7432 | aGameData->black_name = gl->black_name(); |
| 7433 | aGameData->black_rank = gl->black_rank(); |
| 7434 | aGameData->white_name = gl->white_name(); |
| 7435 | aGameData->white_rank = gl->white_rank(); |
| 7436 | // Unfortunately relevant to time records |
| 7437 | aGameData->white_first_flag = gl->white_first_flag; |
| 7438 | }*/ |
| 7439 | boarddispatch->openBoard(); |
| 7440 | |
| 7441 | /* Moved here from 0639 because 0671 probably |
| 7442 | * clears list or something on recv first data |
| 7443 | * and then back to 0639 because apparently, |
| 7444 | * it is the observer request that illicits 0671?*/ |
| 7445 | if(!match_negotiation_state->isOurGame(game_number)) |
| 7446 | sendObserversRequest(game_number); |
| 7447 | |
| 7448 | return; |
| 7449 | } |
| 7450 | |
| 7451 | //0671 |
| 7452 | void TygemConnection::handleMatchOpened(unsigned char * msg, unsigned int size) |
| 7453 | { |
| 7454 | Room * room = getDefaultRoom(); |
| 7455 | BoardDispatch * boarddispatch; |
| 7456 | unsigned char * p = msg; |
| 7457 | PlayerListing * opponent = 0; |
| 7458 | GameData * aGameData; |
| 7459 | unsigned short game_number; |
| 7460 | unsigned char name[15]; |
| 7461 | name[14] = 0x00; |
| 7462 | bool first_name_is_client; |
| 7463 | QString encoded_nameA, encoded_nameB; |
| 7464 | QString encoded_nameA2, encoded_nameB2; |
| 7465 | QString rankA, rankB; |
| 7466 | unsigned short maintime; |
| 7467 | unsigned char periodtime, stones_periods; |
| 7468 | if(size != 92) //eweiqi 88 FIXME |
| 7469 | { |
| 7470 | qDebug("Match open msg of strange size: %d", size); |
| 7471 | } |
| 7472 | /* Is there a board_size FIXME or not? */ |
| 7473 | game_number = (msg[0] << 8) + msg[1]; |
| 7474 | p += 2; |
| 7475 | |
| 7476 | boarddispatch = getIfBoardDispatch(game_number); |
| 7477 | if(!boarddispatch) |
| 7478 | { |
| 7479 | qDebug("No board dispatch for %d", game_number); |
| 7480 | return; |
| 7481 | } |
| 7482 | if(match_negotiation_state->isOurGame(game_number) && |
| 7483 | !match_negotiation_state->startMatchAcceptable()) |
| 7484 | { |
| 7485 | qDebug("received match start at strange time"); //we get here when we create room FIXME why, I set MSCREATEDROOM, how is it not that |
| 7486 | //return; |
| 7487 | } |
| 7488 | aGameData = boarddispatch->getGameData(); |
| 7489 | |
| 7490 | aGameData->number = game_number; |
| 7491 | |
| 7492 | /* Don't want to use gl for names, but maybe need it for |
| 7493 | * white first flag? Hope not */ |
| 7494 | GameListing * gl = room->getGameListing(game_number); |
| 7495 | //FIXME get first time records |
| 7496 | //aGameData->black_name = gl->black_name(); |
| 7497 | //aGameData->black_rank = gl->black_rank(); |
| 7498 | //aGameData->white_name = gl->white_name(); |
| 7499 | //aGameData->white_rank = gl->white_rank(); |
| 7500 | /* Unfortunately relevant to time records */ |
| 7501 | aGameData->white_first_flag = gl->white_first_flag; |
| 7502 | |
| 7503 | #ifdef RE_DEBUG |
| 7504 | printf("0671 match: \n"); |
| 7505 | for(unsigned int i = 0; i < size; i++) |
| 7506 | printf("%02x", msg[i]); |
| 7507 | printf("\n"); |
| 7508 | for(unsigned int i = 0; i < size; i++) |
| 7509 | printf("%c", msg[i]); |
| 7510 | printf("\n"); |
| 7511 | #endif //RE_DEBUG |
| 7512 | //after declining match offer: |
| 7513 | //0009 0001 7065 7465 7269 7573 0000 0000 |
| 7514 | //0000 0009 696e 7472 7573 696f 6e00 0000 |
| 7515 | //0000 000a 0000 0000 0000 0000 0100 0002 |
| 7516 | //ffff ffff ffff ffff 0001 0001 7065 7465 |
| 7517 | //7269 7573 0000 0002 696e 7472 7573 696f |
| 7518 | //6e00 0002 0000 61ea 4af4 8c0e |
| 7519 | |
| 7520 | first_name_is_client = msg[2]; |
| 7521 | // we should see if there's time info in here or the |
| 7522 | // one we're currently using. we need to initially set time |
| 7523 | // we also seem to get this one when rematch started |
| 7524 | |
| 7525 | /* FIXME If this is our match, we should |
| 7526 | * most likely do bd->startGame(); here */ |
| 7527 | |
| 7528 | //0087 0001 baac d0a6 bba8 0000 0000 0000 |
| 7529 | //0000 0011 6261 696e 6974 6500 0000 0000 |
| 7530 | //0000 0011 012c 1e03 0000 0000 0100 0002 |
| 7531 | //ffff ffff ffff ffff 0002 0002 6861 6e78 |
| 7532 | //6961 6f68 7561 0002 6261 696e 6974 6500 |
| 7533 | //0000 0002 48f6 56cd |
| 7534 | //definitely thinking game info |
| 7535 | //00050001c0cfcdb7000000000000000000000014ced2d0cdced2d0e30000000000000014 |
| 7536 | //012c1e030000010001000002ffffffffffffffff000100044a61636b44616e6e7900000267647368 |
| 7537 | //6a0000000000000248f6a64e |
| 7538 | |
| 7539 | //second option high minutes |
| 7540 | //0003000170657465726975730000000000000008696e74727573696f6e00000000000009 |
| 7541 | //012c28010100010001000002ffffff |
| 7542 | //ffffffffff00010001706574657269757300000002696e74727573696f6e00000248fcebe0 |
| 7543 | |
| 7544 | //probably players turn 0001 |
| 7545 | |
| 7546 | //below is a rematch packet |
| 7547 | //one issue though is that there doesn't seem to be a rematch flag |
| 7548 | //and 0639 opens the board dispatch. Which means this has to |
| 7549 | //check that the game has ended, I guess just check for a result |
| 7550 | //after getting the 0671, since presumably any result messages |
| 7551 | //from broadcast games would come later and then if there is a result |
| 7552 | //close and reopen the boarddispatch. But then we need to be careful |
| 7553 | //that we don't lose anything that the 0639 put up that the 0671 |
| 7554 | //neglected to. I don't think there's anything but... FIXME |
| 7555 | //00490001c4c1d2b0b5b6bfcd0000000000000015797031323131373400000000000000 |
| 7556 | //14012c1e030100000001000002ffffffffffffffff000b0000xc6d79646b003137340000 |
| 7557 | //0002797031323131373400000002498b58e4 |
| 7558 | |
| 7559 | p += 2; |
| 7560 | //name and rank |
| 7561 | strncpy((char *)name, (char *)&(msg[4]), 14); |
| 7562 | encoded_nameA = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7563 | p += 15; |
| 7564 | //rank byte |
| 7565 | if(msg[19] < 0x12) |
| 7566 | rankA = QString::number(0x12 - msg[19]) + 'k'; |
| 7567 | else if(msg[19] > 0x1a) |
| 7568 | rankA = QString::number(msg[19] - 0x1a) + 'p'; |
| 7569 | else |
| 7570 | rankA = QString::number(msg[19] - 0x11) + 'd'; |
| 7571 | p++; |
| 7572 | //name and rank |
| 7573 | strncpy((char *)name, (char *)&(msg[20]), 14); |
| 7574 | encoded_nameB = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7575 | p += 15; |
| 7576 | //rank byte |
| 7577 | if(msg[35] < 0x12) |
| 7578 | rankB = QString::number(0x12 - msg[35]) + 'k'; |
| 7579 | else if(msg[35] > 0x1a) |
| 7580 | rankB = QString::number(msg[35] - 0x1a) + 'p'; |
| 7581 | else |
| 7582 | rankB = QString::number(msg[35] - 0x11) + 'd'; |
| 7583 | p++; |
| 7584 | //time settings |
| 7585 | //type seconds minutes periods |
| 7586 | /*switch(p[0]) |
| 7587 | { |
| 7588 | case 0x01: |
| 7589 | aGameData->timeSystem = byoyomi; |
| 7590 | break; |
| 7591 | default: |
| 7592 | printf("Time system: %02x!!!\n", p[0]); |
| 7593 | break; |
| 7594 | }*/ |
| 7595 | // TIME is wrong FIXME |
| 7596 | //0178 0001 696e 7472 7573 696f 6e00 0000 |
| 7597 | //0000 000a 7065 7465 7269 7573 0000 0000 |
| 7598 | //0000 0009 0000 0000 0000 0000 0100 0002 |
| 7599 | //ffff ffff ffff ffff 0000 0001 696e 7472 |
| 7600 | //7573696f6e000002706574657269757300000002000061ea4b3170da |
| 7601 | maintime = (msg[36] << 8) + msg[37]; |
| 7602 | periodtime = msg[38]; |
| 7603 | stones_periods = msg[39]; |
| 7604 | if(maintime == 0 && periodtime == 0 && stones_periods == 0) |
| 7605 | { |
| 7606 | //this can't be the only indicator |
| 7607 | //no time settings agreed upon, no game |
| 7608 | //FIXME double check we aren't accidentally sending this |
| 7609 | qDebug("no match"); |
| 7610 | return; |
| 7611 | } |
| 7612 | |
| 7613 | if(aGameData->fullresult) |
| 7614 | { |
| 7615 | qDebug("0671 received for game with result already set\nassuming rematch\n"); |
| 7616 | class ObserverListModel * olm = boarddispatch->getObserverListModelForRematch(); |
| 7617 | NetworkConnection::closeBoardDispatch(game_number); |
| 7618 | boarddispatch = getBoardDispatch(game_number); |
| 7619 | if(!boarddispatch) |
| 7620 | { |
| 7621 | qDebug("Can't create board dispatch for %d", game_number); |
| 7622 | return; |
| 7623 | } |
| 7624 | boarddispatch->setObserverListModel(olm); |
| 7625 | //below is copied from 0639, awkward FIXME |
| 7626 | boarddispatch->openBoard(); |
| 7627 | |
| 7628 | if(match_negotiation_state->isOurGame(game_number)) |
| 7629 | { |
| 7630 | match_negotiation_state->startMatch(); |
| 7631 | //verify name and settings, possibly with switched color FIXME |
| 7632 | } |
| 7633 | aGameData = boarddispatch->getGameData(); |
| 7634 | |
| 7635 | aGameData->number = game_number; |
| 7636 | } |
| 7637 | |
| 7638 | aGameData->timeSystem = byoyomi; //anything else?? |
| 7639 | aGameData->maintime = maintime; |
| 7640 | aGameData->periodtime = periodtime; |
| 7641 | aGameData->stones_periods = stones_periods; |
| 7642 | #ifdef RE_DEBUG |
| 7643 | printf("0671 TIME SETTINGS: %d %d %d %d %d %d\n", p[0], p[1], p[2], p[3], p[4], p[5]); |
| 7644 | #endif //RE_DEBUG |
| 7645 | aGameData->handicap = msg[40]; |
| 7646 | /* Either all komi is 6.5 or its not in the 0671 anymore */ |
| 7647 | /*if(gl) |
| 7648 | aGameData->komi = gl->komi; |
| 7649 | else*/ |
| 7650 | aGameData->komi = (float)msg[41] + .5; |
| 7651 | p += 4; |
| 7652 | p += 2; |
| 7653 | /* This isn't white first flag I don't think, its first player |
| 7654 | * white I think... or maybe first player black, so I'll negate it...*/ |
| 7655 | |
| 7656 | aGameData->white_first_flag = !msg[42]; |
| 7657 | /* FIXME okay, obviously this isn't right, at the same time, we use it below BIG FIXME */ |
| 7658 | aGameData->undoAllowed = msg[43]; |
| 7659 | if(msg[44]) |
| 7660 | aGameData->free_rated = FREE; |
| 7661 | else if(!msg[44]) |
| 7662 | aGameData->free_rated = RATED; |
| 7663 | else |
| 7664 | qDebug("strange rated byte on board open %02x", msg[43]); |
| 7665 | p += 2; |
| 7666 | //696e74727573696f6e0000000000000b |
| 7667 | //70657465726975730000000000000009 |
| 7668 | //0032 |
| 7669 | //04b01e03 02 00 00 01 01 0200000000000000000000000000000000000000000000000000000000706574657269757300000002000061ea |
| 7670 | |
| 7671 | //00320001 |
| 7672 | //696e74727573696f6e0000000000000b |
| 7673 | //70657465726975730000000000000009 |
| 7674 | //04b01e03 00 00 00 01 01 010002ffffffffffffffff00000000696e74727573696f6e000002706574657269757300000002000061ea4d5fc88e |
| 7675 | |
| 7676 | #ifdef RE_DEBUG |
| 7677 | //FIXME really hope I don't use this anymore |
| 7678 | if(gl && gl->white_first_flag != aGameData->white_first_flag) |
| 7679 | qDebug("gl and 0671 white first flag differ !!!!!"); |
| 7680 | printf("Setting white first flag to: %d\n", aGameData->white_first_flag); |
| 7681 | #endif //RE_DEBUG |
| 7682 | //msg44 |
| 7683 | p += 4; |
| 7684 | p += 4; |
| 7685 | p += 4; |
| 7686 | //this is white/black or our picture code: p[1] |
| 7687 | p += 2; |
| 7688 | //possibly this is black or their picture code: p[1] |
| 7689 | p += 2; |
| 7690 | strncpy((char *)name, (char *)&(msg[60]), 11); |
| 7691 | encoded_nameA2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7692 | p += 10; |
| 7693 | |
| 7694 | |
| 7695 | //p[1] is color byte |
| 7696 | /* We assume ascii names are in same order */ |
| 7697 | //if(p[1] == 0x02) |
| 7698 | #ifdef RE_DEBUG |
| 7699 | boarddispatch->recvKibitz(QString(), "0671 wff " + QString::number(aGameData->white_first_flag) |
| 7700 | + " gl " + (gl ? QString::number(gl->white_first_flag) : "NA") + |
| 7701 | + " " + QString::number(p[1], 16) + " " + QString::number(p[13], 16)); |
| 7702 | #endif //RE_DEBUG |
| 7703 | if(aGameData->white_first_flag) |
| 7704 | { |
| 7705 | aGameData->white_name = encoded_nameA2; |
| 7706 | aGameData->white_rank = rankA; |
| 7707 | qDebug("white is %s", encoded_nameA2.toLatin1().constData()); |
| 7708 | } |
| 7709 | //else if(p[1] == 0x01) |
| 7710 | else |
| 7711 | { |
| 7712 | aGameData->black_name = encoded_nameA2; |
| 7713 | aGameData->black_rank = rankA; |
| 7714 | qDebug("black is %s", encoded_nameA2.toLatin1().constData()); |
| 7715 | } |
| 7716 | //else |
| 7717 | // qDebug("Strange color byte %d, line: %d", p[1], __LINE__); |
| 7718 | p += 2; |
| 7719 | strncpy((char *)name, (char *)&(msg[72]), 11); |
| 7720 | encoded_nameB2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7721 | p += 10; |
| 7722 | if(match_negotiation_state->sentAdjournResume()) |
| 7723 | { |
| 7724 | if(encoded_nameB2 != match_negotiation_state->getOpponent()) |
| 7725 | { |
| 7726 | qDebug("rematch start by unknown player"); //I highly doubt encoded_nameB2 works if we originally created the game |
| 7727 | if(encoded_nameA2 == match_negotiation_state->getOpponent()) |
| 7728 | { |
| 7729 | qDebug("Rematch is a2"); //FIXME, yes, we get here if we created the game originally (offered it, etc. ) |
| 7730 | //probably okay below because we normally only get 0672 if we didn't create the game |
| 7731 | //rejoin on disconnect is exception |
| 7732 | opponent = room->getPlayerListing(encoded_nameA2); |
| 7733 | } |
| 7734 | //return; |
| 7735 | |
| 7736 | } |
| 7737 | else |
| 7738 | opponent = room->getPlayerListing(encoded_nameB2); |
| 7739 | match_negotiation_state->setPlayer(opponent); |
| 7740 | } |
| 7741 | else if(match_negotiation_state->isOurGame(game_number)) |
| 7742 | { |
| 7743 | opponent = room->getPlayerListing(encoded_nameB2); |
| 7744 | if(!match_negotiation_state->verifyPlayer(opponent)) |
| 7745 | { |
| 7746 | qDebug("start match by unknown player"); |
| 7747 | //return; //bugs out here on nigiri |
| 7748 | } |
| 7749 | if(!match_negotiation_state->verifyGameData(*aGameData)) |
| 7750 | { |
| 7751 | qDebug("opponent attempted starting game with different match settings then negotiated upon"); |
| 7752 | //happens FIXME and its not like we can send an error |
| 7753 | //return; |
| 7754 | } |
| 7755 | } |
| 7756 | //p[1] is color byte |
| 7757 | //if(p[1] == 0x02) |
| 7758 | if(!aGameData->white_first_flag) |
| 7759 | { |
| 7760 | aGameData->white_name = encoded_nameB2; |
| 7761 | aGameData->white_rank = rankB; |
| 7762 | qDebug("white is %s", encoded_nameB2.toLatin1().constData()); |
| 7763 | } |
| 7764 | //else if(p[1] == 0x01) |
| 7765 | else |
| 7766 | { |
| 7767 | aGameData->black_name = encoded_nameB2; |
| 7768 | aGameData->black_rank = rankB; |
| 7769 | qDebug("black is %s", encoded_nameB2.toLatin1().constData()); |
| 7770 | } |
| 7771 | //else |
| 7772 | // qDebug("Strange color byte %d, line: %d", p[1], __LINE__); |
| 7773 | p += 2; |
| 7774 | //msg84 |
| 7775 | boarddispatch->gameDataChanged(); |
| 7776 | if(aGameData->handicap) |
| 7777 | { |
| 7778 | /* This should really be cleaned up, done in some |
| 7779 | * general place. Handicap games on tygem are |
| 7780 | * really rare and I don't know how to really |
| 7781 | * set them up yet in matches. For all the |
| 7782 | * protocols this should be setup better. |
| 7783 | * So this is just temporary. FIXME */ |
| 7784 | MoveRecord aMove; |
| 7785 | aMove.number = NOMOVENUMBER; |
| 7786 | aMove.x = aGameData->handicap; |
| 7787 | aMove.flags = MoveRecord::HANDICAP; |
| 7788 | boarddispatch->recvMove(&aMove); |
| 7789 | } |
| 7790 | if(match_negotiation_state->isOurGame(game_number)) |
| 7791 | { |
| 7792 | //our_invitation is also set in handleCreateRoomResponse, so doublecheck, the former may be unnecessary FIXME |
| 7793 | if(encoded_nameA2 == getUsername()) |
| 7794 | { |
| 7795 | aGameData->our_invitation = first_name_is_client; |
| 7796 | //we do this only if we didn't send the 0671 out |
| 7797 | if(!opponent) |
| 7798 | qDebug("Can't get opponent to close game dialog"); |
| 7799 | else |
| 7800 | getAndCloseGameDialog(opponent); |
| 7801 | /* Note that getAndClose will fail if we're rejoining |
| 7802 | * a match in progress FIXME, networkconnection reports |
| 7803 | * error. */ |
| 7804 | } |
| 7805 | else |
| 7806 | aGameData->our_invitation = !first_name_is_client; |
| 7807 | //FIXME time |
| 7808 | //doublecheck this same issue with 0 maintime on IGS and ORO |
| 7809 | if(aGameData->maintime == 0) |
| 7810 | boarddispatch->recvTime(TimeRecord(aGameData->periodtime, aGameData->stones_periods), TimeRecord(aGameData->periodtime, aGameData->stones_periods)); |
| 7811 | else |
| 7812 | boarddispatch->recvTime(TimeRecord(aGameData->maintime, -1), TimeRecord(aGameData->maintime, -1)); |
| 7813 | /* I don't think this is a good place to set this: */ |
| 7814 | /*if(aGameData->free_rated == FREE) |
| 7815 | aGameData->undoAllowed = true;*/ |
| 7816 | |
| 7817 | boarddispatch->startGame(); //starts timers |
| 7818 | boarddispatch->swapColors(true); |
| 7819 | match_negotiation_state->startMatch(); |
| 7820 | } |
| 7821 | /* Actually, first move message number appears to be 2, maybe 1 is for handicap? |
| 7822 | * this needs to be right for when we play black!! */ |
| 7823 | move_message_number = 1; //changed from 0... only relevant for our games!! FIXME |
| 7824 | move_message_number_ack = 1; |
| 7825 | /* Note that we increment on most sends ... */ |
| 7826 | |
| 7827 | return; |
| 7828 | } |
| 7829 | |
| 7830 | //join with 0646 as well |
| 7831 | //0645 |
| 7832 | void TygemConnection::handleMatchOffer(unsigned char * msg, unsigned int size, MIVersion version) |
| 7833 | { |
| 7834 | unsigned char * p = msg; |
| 7835 | if(size != 88) //84 |
| 7836 | { |
| 7837 | qDebug("Match offer msg of strange size: %d", size); |
| 7838 | } |
| 7839 | |
| 7840 | unsigned short game_number; |
| 7841 | unsigned char name[15]; |
| 7842 | name[14] = 0x00; |
| 7843 | QString encoded_name, encoded_name2; |
| 7844 | PlayerListing * opponent; |
| 7845 | |
| 7846 | strncpy((char *)name, (char *)p, 14); |
| 7847 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7848 | if(encoded_name != getUsername()) //FIXME in case response doesn't readdress |
| 7849 | return; |
| 7850 | p += 15; |
| 7851 | //rank |
| 7852 | p++; |
| 7853 | strncpy((char *)name, (char *)p, 14); |
| 7854 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7855 | p += 15; |
| 7856 | //rank |
| 7857 | p++; |
| 7858 | game_number = (p[0] << 8) + p[1]; |
| 7859 | p += 2; |
| 7860 | //time settings |
| 7861 | MatchRequest * tempmr = new MatchRequest(); |
| 7862 | tempmr->number = game_number; |
| 7863 | tempmr->timeSystem = byoyomi; |
| 7864 | tempmr->maintime = (p[0] << 8) + p[1]; |
| 7865 | tempmr->periodtime = p[2]; |
| 7866 | tempmr->stones_periods = p[3]; |
| 7867 | tempmr->handicap = p[4]; |
| 7868 | tempmr->komi = (float)p[5] + .5; |
| 7869 | switch(p[6]) |
| 7870 | { |
| 7871 | /* FIXME NO!! either here or at one of the sendMatch Offers, there's something wrong |
| 7872 | * might be that it depends on who offers the game!! */ |
| 7873 | case 0x00: |
| 7874 | if(version == modify) |
| 7875 | tempmr->color_request = MatchRequest::BLACK; |
| 7876 | else |
| 7877 | tempmr->color_request = MatchRequest::WHITE; |
| 7878 | break; |
| 7879 | case 0x01: |
| 7880 | if(version == modify) |
| 7881 | tempmr->color_request = MatchRequest::WHITE; |
| 7882 | else |
| 7883 | tempmr->color_request = MatchRequest::BLACK; |
| 7884 | break; |
| 7885 | case 0x02: |
| 7886 | tempmr->color_request = MatchRequest::NIGIRI; |
| 7887 | break; |
| 7888 | default: |
| 7889 | printf("Strange byte in match offer: %02x", p[6]); |
| 7890 | break; |
| 7891 | } |
| 7892 | p += 7; |
| 7893 | //696e74727573696f6e0000000000000b |
| 7894 | //70657465726975730000000000000009 |
| 7895 | //0032 04b0 1e03 0200 0001 0102 00000000000000000000000000000000000000000000000000000000706574657269757300000002000061ea |
| 7896 | |
| 7897 | #ifdef RE_DEBUG |
| 7898 | if(p[0] != p[1]) |
| 7899 | printf("match offer with friendly/rated %02x %02x", p[0], p[1]); |
| 7900 | #endif //RE_DEBUG |
| 7901 | tempmr->undoAllowed = p[0]; |
| 7902 | //01 00 looks like rated with undo checked |
| 7903 | if(p[1] == 0x01) |
| 7904 | tempmr->free_rated = FREE; |
| 7905 | else if(p[1] == 0x00) |
| 7906 | tempmr->free_rated = RATED; |
| 7907 | else |
| 7908 | printf("match offer with friendly/rated %02x %02x", p[0], p[1]); |
| 7909 | p += 2; |
| 7910 | //message version byte |
| 7911 | p++; |
| 7912 | p += 4; //ffff ffff |
| 7913 | p += 12; |
| 7914 | strncpy((char *)name, (char *)p, 11); //our nickname |
| 7915 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7916 | p += 11; |
| 7917 | p++; //country_id |
| 7918 | strncpy((char *)name, (char *)p, 11); |
| 7919 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 7920 | p += 11; |
| 7921 | p++; //country_id |
| 7922 | |
| 7923 | if(version == offer) |
| 7924 | { |
| 7925 | //0645 doesn't have opponent's nickname, apparently, |
| 7926 | //our nickname if we send it, but not opponents |
| 7927 | tempmr->opponent = encoded_name; |
| 7928 | tempmr->opponent_is_challenger = true; |
| 7929 | opponent = getDefaultRoom()->getPlayerListingByNotNickname(encoded_name); |
| 7930 | } |
| 7931 | else |
| 7932 | { |
| 7933 | tempmr->opponent = encoded_name2; |
| 7934 | opponent = getDefaultRoom()->getPlayerListing(encoded_name2); |
| 7935 | } |
| 7936 | if(!match_negotiation_state->verifyPlayer(opponent)) |
| 7937 | { |
| 7938 | qDebug("Match offer/accept from unknown opponent"); |
| 7939 | return; |
| 7940 | } |
| 7941 | |
| 7942 | //0004 0e10 1e03 0100 |
| 7943 | if(version == offer || version == modify) |
| 7944 | { |
| 7945 | //apparently acknowledge no longer necessary? |
| 7946 | //sendMatchOffer(*tempmr, acknowledge); |
| 7947 | |
| 7948 | if(version == offer && !match_negotiation_state->waitingForMatchOffer()) |
| 7949 | { |
| 7950 | qDebug("received match offer, not waiting for match offer"); //this is happening when they modify sometimes, or if |
| 7951 | //we modify I think and they modify ours or something |
| 7952 | //return; |
| 7953 | } |
| 7954 | else if(version == modify && !match_negotiation_state->sentMatchOffer()) |
| 7955 | { |
| 7956 | qDebug("received modified match offer, didn't send offer"); |
| 7957 | //return; |
| 7958 | } |
| 7959 | //Here, we actually want to pop up game dialog |
| 7960 | GameDialog * gameDialogDispatch = getGameDialog(opponent); |
| 7961 | //for game dialog time checks |
| 7962 | lastMainTimeChecked = tempmr->maintime; |
| 7963 | lastPeriodTimeChecked = tempmr->periodtime; |
| 7964 | lastPeriodsChecked = tempmr->stones_periods; |
| 7965 | gameDialogDispatch->recvRequest(tempmr, getGameDialogFlags()); |
| 7966 | } |
| 7967 | else if(version == accept) |
| 7968 | { |
| 7969 | if(!match_negotiation_state->sentMatchOffer()) |
| 7970 | return; |
| 7971 | //match offer accept |
| 7972 | //00580646 |
| 7973 | //0x0646: 70657465726975730000000000000008696e74727573696f6e000000000000090032012c2801010001 |
| 7974 | //000001ffffffff000000000001000000000000706574657269757300000002696e74727573696f6e000002 |
| 7975 | //00a00616 |
| 7976 | MatchRequest * mr = getAndCloseGameDialog(opponent); |
| 7977 | if(!mr) |
| 7978 | { |
| 7979 | qDebug("Can't get match request for opponent"); |
| 7980 | //return; |
| 7981 | } |
| 7982 | if(!match_negotiation_state->verifyMatchRequest(*tempmr)) |
| 7983 | { |
| 7984 | qDebug("opponent accepted with strange match sttings"); //we get here |
| 7985 | //return; |
| 7986 | } |
| 7987 | //FIXME, maybe should be mr here, but tempmr from filled out |
| 7988 | //above is probably better |
| 7989 | //FIXME there's an issue here |
| 7990 | //when we negotiate an offer... not clear yet |
| 7991 | match_negotiation_state->acceptMatchTerms(tempmr); //FIXME awkward, even though they're accepting? but is necessary here, just lousy name |
| 7992 | sendStartGame(*tempmr); |
| 7993 | } |
| 7994 | else if(version == decline) |
| 7995 | { |
| 7996 | GameDialog * gameDialogDispatch = getIfGameDialog(opponent); |
| 7997 | if(!gameDialogDispatch) |
| 7998 | { |
| 7999 | qDebug("No game dialog open to be declined"); |
| 8000 | } |
| 8001 | else |
| 8002 | gameDialogDispatch->recvRefuseMatch(GD_REFUSE_DECLINE); |
| 8003 | } |
| 8004 | delete tempmr; |
| 8005 | return; |
| 8006 | } |
| 8007 | |
| 8008 | void TygemConnection::handleResumeMatch(unsigned char * msg, unsigned int size) |
| 8009 | { |
| 8010 | BoardDispatch * boarddispatch; |
| 8011 | int i; |
| 8012 | unsigned short game_number; |
| 8013 | unsigned char name[15]; |
| 8014 | name[14] = 0x00; |
| 8015 | QString encoded_name, encoded_name2; |
| 8016 | |
| 8017 | game_number = (msg[0] << 8) + msg[1]; |
| 8018 | boarddispatch = getIfBoardDispatch(game_number); |
| 8019 | if(!boarddispatch) |
| 8020 | { |
| 8021 | qDebug("Unexpected: got 068e for board %d", game_number); |
| 8022 | return; |
| 8023 | } |
| 8024 | |
| 8025 | //first name is not addressee!! |
| 8026 | strncpy((char *)name, (char *)&(msg[4]), 14); |
| 8027 | encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 8028 | |
| 8029 | strncpy((char *)name, (char *)&(msg[20]), 14); |
| 8030 | encoded_name2 = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 8031 | |
| 8032 | //I'm not certain that we're sending this in the right order, |
| 8033 | //but I know from something else we send or just because, official |
| 8034 | //client will get us sent one of these when opponent resumes |
| 8035 | //a game they disconnected from |
| 8036 | //the last byte msg[35] is 0x01 |
| 8037 | //made a mistake, that was from us.. |
| 8038 | printf("***0x068e: "); |
| 8039 | for(i = 0; i < (int)size; i++) |
| 8040 | printf("%02x", msg[i]); |
| 8041 | printf("\n"); |
| 8042 | |
| 8043 | if(encoded_name == getUsername() && msg[35] == 0x01) |
| 8044 | { |
| 8045 | /* This might actually be from us ?? */ |
| 8046 | //this is where we need to reenable playing!!! FIXME |
| 8047 | } |
| 8048 | //068e 0005 0000 |
| 8049 | //7065 7465 7269 7573 0000 0000 0000 0000 |
| 8050 | //696e 7472 7573 696f 6e00 0000 0000 0001 |
| 8051 | |
| 8052 | //here we get this and we are peterius black, its not our invitation |
| 8053 | //0000 0028 068e 000b 0000 P..h.....(...... |
| 8054 | //696e 7472 7573 696f 6e00 0000 0000 0000 intrusion....... |
| 8055 | //7065 7465 7269 7573 0000 0000 0000 0001 peterius........ |
| 8056 | |
| 8057 | //0010 067d 000b 0001 0312 1d80 0314 1d40 |
| 8058 | } |
| 8059 | |
| 8060 | //0643 |
| 8061 | void TygemConnection::handleMatchInvite(unsigned char * msg, unsigned int size) |
| 8062 | { |
| 8063 | unsigned char * p = msg; |
| 8064 | unsigned short room_number; |
| 8065 | if(size != 60) |
| 8066 | { |
| 8067 | qDebug("MatchInvite of size %d\n", size); |
| 8068 | // probably should print it out and exit FIXME |
| 8069 | } |
| 8070 | #ifdef RE_DEBUG |
| 8071 | printf("0x0643: "); |
| 8072 | for(unsigned int i = 0; i < size; i++) |
| 8073 | printf("%02x", msg[i]); |
| 8074 | printf("\n"); |
| 8075 | |
| 8076 | //tom |
| 8077 | //7065 7465 7269 7573 0000 0000 0000 0008 |
| 8078 | //7065 7465 7269 7573 0000 0000 696e 7472 |
| 8079 | //7573 696f 6e00 0000 0000 0008 696e 7472 |
| 8080 | //7573 696f 6e00 0002 0100 ffff |
| 8081 | #endif //RE_DEBUG |
| 8082 | p += 14; |
| 8083 | //invite_byte = p[1]; //probably not |
| 8084 | p += 2; |
| 8085 | p += 12; |
| 8086 | |
| 8087 | p += 16; |
| 8088 | |
| 8089 | PlayerListing * player = getOrCreatePlayerListingFromRecord((char *)&(msg[28])); |
| 8090 | p += 10; |
| 8091 | p++; |
| 8092 | opponent_is_challenger = (bool)(p[0] - 1); |
| 8093 | p++; |
| 8094 | p += 2; //possible first, second player settings |
| 8095 | room_number = p[0] + (p[1] << 8); |
| 8096 | if(room_number != 0xffff) |
| 8097 | { |
| 8098 | if(!match_negotiation_state->verifyPlayer(player)) |
| 8099 | { |
| 8100 | qDebug("Got room to join from bad player"); |
| 8101 | return; |
| 8102 | } |
| 8103 | if(!match_negotiation_state->waitingForRoomNumber()) |
| 8104 | { |
| 8105 | qDebug("Got room to join, haven't accepted"); |
| 8106 | return; |
| 8107 | } |
| 8108 | // this is created room |
| 8109 | sendJoin(room_number); |
| 8110 | match_negotiation_state->sendJoinRoom(room_number); |
| 8111 | //this also asks for an observer list request which may |
| 8112 | //FIXME not be necessary if its our match, don't think |
| 8113 | //they send it, we're first ones in |
| 8114 | /* Do we need to wait for observe reply?? or no? */ |
| 8115 | sendObserversRequest(room_number); |
| 8116 | } |
| 8117 | else if(std::find(decline_all_invitations.begin(), decline_all_invitations.end(), player) != decline_all_invitations.end()) |
| 8118 | { |
| 8119 | //client is responsible for this: |
| 8120 | sendMatchInvite(player, decline_all); |
| 8121 | } |
| 8122 | else if(getBoardDispatches() == 3) |
| 8123 | { |
| 8124 | // if we already have 3 games open, there's no warning, just decline |
| 8125 | sendMatchInvite(player, decline); //decline handles getBoardDispatches() |
| 8126 | } |
| 8127 | else if(match_negotiation_state->inGame()) |
| 8128 | { |
| 8129 | sendMatchInvite(player, alreadyingame); //doublecheck this isn't block |
| 8130 | } |
| 8131 | else |
| 8132 | { |
| 8133 | if(!match_negotiation_state->newMatchAllowed()) |
| 8134 | { |
| 8135 | sendMatchInvite(player, decline); |
| 8136 | return; |
| 8137 | } |
| 8138 | /* FIXME |
| 8139 | * yet another point, it seems somehow that the dialog |
| 8140 | * can close prematurely, somehow and then the seconds |
| 8141 | * will start low for another invite or something, its weird */ |
| 8142 | MatchInviteDialog * mid = new MatchInviteDialog(player->name, player->rank, true); |
| 8143 | int mid_return = mid->exec(); |
| 8144 | |
| 8145 | if(mid_return == 1) |
| 8146 | sendMatchInvite(player, accept); |
| 8147 | else if(mid_return == -1) |
| 8148 | sendMatchInvite(player, decline); |
| 8149 | else if(mid_return == -2) |
| 8150 | { |
| 8151 | decline_all_invitations.push_back(player); |
| 8152 | sendMatchInvite(player, decline_all); |
| 8153 | } |
| 8154 | } |
| 8155 | } |
| 8156 | |
| 8157 | //0644 |
| 8158 | void TygemConnection::handleMatchInviteResponse(unsigned char * msg, unsigned int size) |
| 8159 | { |
| 8160 | unsigned char * p = msg; |
| 8161 | unsigned char name[15]; |
| 8162 | name[14] = 0x00; |
| 8163 | if(size != 60) |
| 8164 | { |
| 8165 | qDebug("MatchInvite of size %d\n", size); |
| 8166 | // probably should print it out and exit FIXME |
| 8167 | } |
| 8168 | //actually maybe our offer is still screwed up FIXME |
| 8169 | #ifdef RE_DEBUG |
| 8170 | printf("*** 0x0644: "); |
| 8171 | for(unsigned int i = 0; i < size; i++) |
| 8172 | printf("%02x", msg[i]); |
| 8173 | printf("\n"); |
| 8174 | #endif //RE_DEBUG |
| 8175 | Room * room = getDefaultRoom(); |
| 8176 | p += 28; |
| 8177 | /* Can't we make a special thing to do all these repetitive string copies? */ |
| 8178 | strncpy((char *)name, (char *)p, 14); |
| 8179 | QString encoded_name = QString((char *)name); |
| 8180 | //QString encoded_name = serverCodec->toUnicode((char *)name, strlen((char *)name)); |
| 8181 | p += 14; |
| 8182 | p += 2; |
| 8183 | strncpy((char *)name, (char *)p, 11); |
| 8184 | QString encoded_name2 = QString((char *)name); |
| 8185 | PlayerListing * player = room->getPlayerListing(encoded_name2); |
| 8186 | if(!match_negotiation_state->verifyPlayer(player)) |
| 8187 | { |
| 8188 | qDebug("Match invite response failed to verify player"); |
| 8189 | return; |
| 8190 | } |
| 8191 | p += 12; |
| 8192 | p++; |
| 8193 | //this byte seems to determine accept versus decline, etc. |
| 8194 | //0x0b is autodecline, 0x00 is offer or decline here, 0x01 |
| 8195 | switch(p[0]) |
| 8196 | { |
| 8197 | case 0x00: |
| 8198 | { |
| 8199 | QMessageBox::information(0,tr("Invite declined"), tr("%1 has declined invitation").arg(player->name)); |
| 8200 | } |
| 8201 | break; |
| 8202 | case 0x01: |
| 8203 | sendCreateRoom(); |
| 8204 | return; |
| 8205 | break; |
| 8206 | case 0x0b: |
| 8207 | { |
| 8208 | /* FIXME, I'm thinking 0b may mean that they're already in a game |
| 8209 | * in addition?*/ |
| 8210 | QMessageBox::information(0, tr("In game?"), tr("%1 is not accepting invitations").arg(player->name)); |
| 8211 | } |
| 8212 | break; |
| 8213 | case 0x0c: |
| 8214 | { |
| 8215 | /* We shouldn't really get here, this means they have "decline" |
| 8216 | * set */ |
| 8217 | QMessageBox::information(0, tr("Not open"), tr("%1 is not accepting invitations").arg(player->name)); |
| 8218 | } |
| 8219 | break; |
| 8220 | case 0x0d: |
| 8221 | { |
| 8222 | QMessageBox::information(0, tr("Not open"), tr("%1 has the maximum boards (3) open").arg(player->name)); |
| 8223 | } |
| 8224 | break; |
| 8225 | case 0x0e: |
| 8226 | { |
| 8227 | /* I think you get this if you check the box */ |
| 8228 | QMessageBox::information(0, tr("Invite declined"), tr("%1 has declined all invitations").arg(player->name)); |
| 8229 | } |
| 8230 | break; |
| 8231 | default: |
| 8232 | #ifdef RE_DEBUG |
| 8233 | qDebug("Received strange byte %02x in match invite response", p[0]); |
| 8234 | #endif //RE_DEBUG |
| 8235 | break; |
| 8236 | } |
| 8237 | } |
| 8238 | |
| 8239 | //0637 |
| 8240 | void TygemConnection::handleCreateRoomResponse(unsigned char * msg, unsigned int size) |
| 8241 | { |
| 8242 | unsigned short room_number = (msg[0] << 8) + msg[1]; |
| 8243 | |
| 8244 | if(size != 12) //4? |
| 8245 | { |
| 8246 | qDebug("create room response of size: %d", size); |
| 8247 | //return; |
| 8248 | } |
| 8249 | /* FIXME We're going to assume for now that we're getting |
| 8250 | * this because we received a match response accept and |
| 8251 | * so we'll just send the join message and pray for rain */ |
| 8252 | if(msg[2] != 0x01 || msg[3] != 0xff) |
| 8253 | { |
| 8254 | printf("roomcreate: %02x %02x\n", msg[2], msg[3]); |
| 8255 | } |
| 8256 | //0x0637 room create?: 0129 01ff 0000 0000 0000 0000 |
| 8257 | #ifdef RE_DEBUG |
| 8258 | printf("0x0637 room create?: "); |
| 8259 | for(unsigned int i = 0; i < size; i++) |
| 8260 | printf("%02x", msg[i]); |
| 8261 | printf("\n"); |
| 8262 | #endif //RE_DEBUG |
| 8263 | if(!match_negotiation_state->sentCreateRoom()) |
| 8264 | return; |
| 8265 | sendObserversRequest(room_number); |
| 8266 | //join should also put us in room with board? |
| 8267 | sendMatchInvite(match_negotiation_state->getPlayerListing(), create, room_number); |
| 8268 | //not sure of timing of this |
| 8269 | |
| 8270 | //when we offer and its accepted and we create, we don't |
| 8271 | //get a starting message, I think we're supposed to open the |
| 8272 | //board on receipt of 0637 maybe, 0665 send is strange, don't |
| 8273 | //know what that's for, no response anyway, that we know of |
| 8274 | |
| 8275 | /* Try creating board here FIXME with MatchOpened 0639*/ |
| 8276 | BoardDispatch * boarddispatch = getBoardDispatch(room_number); |
| 8277 | GameData * aGameData = boarddispatch->getGameData(); |
| 8278 | aGameData->number = room_number; |
| 8279 | aGameData->gameMode = modeMatch; |
| 8280 | aGameData->our_invitation = true; |
| 8281 | /*GameListing * gl = room->getGameListing(game_number); |
| 8282 | //FIXME get first time records |
| 8283 | if(gl) |
| 8284 | { |
| 8285 | aGameData->black_name = gl->black_name(); |
| 8286 | aGameData->black_rank = gl->black_rank(); |
| 8287 | aGameData->white_name = gl->white_name(); |
| 8288 | aGameData->white_rank = gl->white_rank(); |
| 8289 | // Unfortunately relevant to time records |
| 8290 | //aGameData->white_first_flag = gl->white_first_flag; |
| 8291 | }*/ |
| 8292 | boarddispatch->openBoard(); |
| 8293 | } |
| 8294 | |
| 8295 | /* These needs to be more versatile later */ |
| 8296 | void TygemConnection::onReady(void) |
| 8297 | { |
| 8298 | //sendInvitationSettings(true); //for now |
| 8299 | qDebug("Ready!\n"); |
| 8300 | NetworkConnection::onReady(); |
| 8301 | } |
| 8302 | |
| 8303 | int TygemConnection::time_to_seconds(const QString & time) |
| 8304 | { |
| 8305 | QRegExp re = QRegExp("([0-9]{1,2}):([0-9]{1,2})"); |
| 8306 | int min, sec; |
| 8307 | |
| 8308 | if(re.indexIn(time) >= 0) |
| 8309 | { |
| 8310 | min = re.cap(1).toInt(); |
| 8311 | sec = re.cap(2).toInt(); |
| 8312 | } |
| 8313 | else |
| 8314 | { |
| 8315 | qDebug("Bad time string"); |
| 8316 | return 0xffff; |
| 8317 | } |
| 8318 | |
| 8319 | return (60 * min) + sec; |
| 8320 | } |
| 8321 | |
| 8322 | void TygemConnection::secondsToDate(unsigned short & year, unsigned char & month, unsigned char & day, unsigned char & hour, unsigned char & minute, unsigned char & second) |
| 8323 | { |
| 8324 | unsigned long seconds = time(0); |
| 8325 | year = (seconds / 31536000); |
| 8326 | seconds -= ((year * 31536000) + ((year / 4) * 86400)); |
| 8327 | year += 1970; |
| 8328 | bool leap = (year % 4 == 0); |
| 8329 | day = (seconds / 86400); |
| 8330 | seconds -= (day * 86400); |
| 8331 | hour = (seconds / 3600); |
| 8332 | seconds -= (hour * 3600); |
| 8333 | minute = (seconds / 60); |
| 8334 | seconds -= (minute * 60); |
| 8335 | second = seconds; |
| 8336 | int days_in_each_month[12] = { 31, 28 + leap, 31, 30, 31, 30, 31, 31, 31, 31, 30, 31}; |
| 8337 | int i; |
| 8338 | |
| 8339 | for(i = 1; i < 13; i++) |
| 8340 | { |
| 8341 | int sum, j; |
| 8342 | sum = 0; |
| 8343 | for(j = 0; j < i; j++) |
| 8344 | sum += days_in_each_month[j]; |
| 8345 | if(day <= sum) |
| 8346 | { |
| 8347 | month = i; |
| 8348 | day -= (sum - days_in_each_month[i - 1]); |
| 8349 | break; |
| 8350 | } |
| 8351 | } |
| 8352 | |
| 8353 | printf("Todays date is: %d %d %d - %d %d %d\n", year, month, day, hour, minute, second); |
| 8354 | } |