Debian Codegraph @

src/network/igsconnection.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22 #include <QtDebug>
23 #include "igsconnection.h"
24 #include "consoledispatch.h"
25 #include "room.h" // Should not depend on room FIXME
26 #include "boarddispatch.h"
27 #include "gamedialog.h"
28 #include "talk.h"
29 #include "gamedialogflags.h"
30 #include "playergamelistings.h"
31 #include "matchnegotiationstate.h"
32
33 #define PLAYERSLISTREFRESH_SECONDS 300
34 #define GAMESLISTREFRESH_SECONDS 180
35
36 IGSConnection::IGSConnection(const ConnectionCredentials credentials)
37 : NetworkConnection(credentials)
38 {
39 if(openConnection(hostname,port))
40 {
41 setState(Login);
42 }
43 else
44 qDebug("Can't open Connection\n"); //throw error?
45
46 init();
47 }
48
49 void IGSConnection::init(void)
50 {
51 keepAliveTimer = 0;
52 playersListRefreshTimer = 0;
53 gamesListRefreshTimer = 0;
54 textCodec = QTextCodec::codecForLocale();
55 btime = new TimeRecord();
56 wtime = new TimeRecord();
57 protocol_save_int = -1;
58 game_were_playing = 0;
59 guestAccount = false;
60 needToSendClientToggle = true;
61 }
62
63 IGSConnection::~IGSConnection()
64 {
65 qDebug("Destroying IGS connection");
66 /* Maybe closeConnection shouldn't be here?
67 * at any rate, if there's been an error it
68 * really shouldn't be here */
69 sendDisconnect();
70 closeConnection();
71 delete wtime;
72 delete btime;
73 }
74
75 QString IGSConnection::getPlaceString(void)
76 {
77 return "IGS";
78 }
79
80 void IGSConnection::sendText(QString text)
81 {
82 // FIXME We're getting some nonsense on the end of this sometimes for
83 // some reason
84 //text += "\r\n";
85 qDebug() << "IGSConnection::sendText: " << text;
86 QByteArray raw = textCodec->fromUnicode(text);
87 if(write(raw.data(), raw.size()) < 0)
88 qWarning("*** failed sending to host: %s", raw.data());
89 }
90
91 /* This is kind of ugly, but we can't add sending return-newline to
92 * superclass, which means either reallocing text here to send both
93 * or this little flag trick to sneak in a second write when we
94 * got the first through. Note that the real solution would be
95 * to change every sendText command in this file to have a "\r\n"
96 * on it. That is sort of the better way. FIXME
97 * Note also that network connection does readLine in writeFromBuffer
98 * and also write an additional newline...*/
99 void IGSConnection::sendText(const char * text)
100 {
101 sendText(QString(text));
102 }
103
104 void IGSConnection::sendDisconnect(void)
105 {
106 sendText("exit\r\n");
107 }
108
109 /* What about a room_id?? */
110 void IGSConnection::sendMsg(unsigned int game_id, QString text)
111 {
112 BoardDispatch * bd = getIfBoardDispatch(game_id);
113 if(!bd)
114 {
115 qDebug("IGSConnection::sendMsg(): No board dispatch to send message from");
116 return;
117 }
118 GameData * g = bd->getGameData();
119 switch(g->gameMode)
120 {
121 case modeReview:
122 case modeMatch:
123 sendText("say " + text + "\r\n");
124 break;
125 case modeObserve:
126 sendText("kibitz " + QString::number(game_id) + " " + text + "\r\n");
127 break;
128 default:
129 qDebug("IGSConnection::sendMsg(): no game type");
130 break;
131 }
132 }
133
134 void IGSConnection::sendMsg(PlayerListing * player, QString text)
135 {
136 sendText("tell " + player->name + " " + text + "\r\n");
137 }
138
139 void IGSConnection::sendToggle(const QString & param, bool val)
140 {
141 QString value;
142 if (val)
143 value = " true";
144 else
145 value = " false";
146 sendText("toggle " + param + " " + value + "\r\n");
147 }
148
149 /* If we can observe something besides games,
150 * like rooms or people, then this name is a bit
151 * iffy too... */
152 void IGSConnection::sendObserve(const GameListing *game)
153 {
154 unsigned int game_id = game->number;
155 if(getIfBoardDispatch(game_id)) //don't observe twice, it unobserves
156 return;
157 protocol_save_int = game_id; //since this isn't always reported back
158 match_negotiation_state->sendJoinRoom(game_id);
159 sendText("observe " + QString::number(game_id) + "\r\n");
160 }
161
162 void IGSConnection::stopObserving(const GameListing *game)
163 {
164 sendText("unobserve " + QString::number(game->number) + "\r\n");
165 }
166
167 void IGSConnection::stopReviewing(const GameListing * game)
168 {
169 sendText("review quit " + QString::number(game->number) + "\r\n");
170 }
171
172 void IGSConnection::sendStatsRequest(const PlayerListing * opponent)
173 {
174 sendText("stats " + opponent->name + "\r\n");
175 }
176
177 void IGSConnection::sendPlayersRequest(void)
178 {
179 sendText("userlist\r\n");
180 }
181
182 void IGSConnection::sendGamesRequest(void)
183 {
184 sendText("games\r\n");
185 }
186
187 void IGSConnection::sendRoomListRequest(void)
188 {
189 sendText("room\r\n");
190 }
191
192 void IGSConnection::sendJoinRoom(const RoomListing & room, const char * /*password*/)
193 {
194 sendText("join " + QString::number(room.number) + "\r\n");
195 qDebug() << "IGSConnection::sendJoinRoom(): Joining " << room.number;
196 setCurrentRoom(room); //FIXME unless we get a message after joining?
197 //but then the other send requests should
198 //be there
199 }
200
201 void IGSConnection::sendJoinChannel(const ChannelListing & room)
202 {
203 if(room.number == 0)
204 sendText("yell \\-1\r\n");
205 else
206 sendText("yell \\" + QString::number(room.number) + "\r\n");
207 //set the current room somehow?
208 }
209
210 void IGSConnection::sendMatchInvite(const PlayerListing * player)
211 {
212 MatchRequest * m = 0;
213 /* No match Invites, just popup the dialog */
214 GameDialog * gd = getGameDialog(player);
215 if(player->nmatch)
216 {
217 const PlayerListing * us = getOurListing();
218 m = new MatchRequest();
219 m->opponent = player->name;
220 m->opponent_id = player->id;
221 m->their_rank = player->rank;
222 m->our_name = us->name;
223 m->our_rank = us->rank;
224
225 m->timeSystem = player->nmatch_timeSystem;
226 m->maintime = player->nmatch_timeMin;
227 m->periodtime = player->nmatch_BYMin;
228 m->stones_periods = 25;
229 m->nmatch_timeMax = player->nmatch_timeMax;
230 m->nmatch_BYMax = player->nmatch_BYMax;
231 if(!player->nmatch_handicapMin)
232 m->handicap = 1;
233 else
234 m->handicap = player->nmatch_handicapMin;
235 m->nmatch_handicapMax = player->nmatch_handicapMax;
236 m->opponent_is_challenger = false;
237 m->first_offer = true; //because we're sending it!!
238 //mr->first_offer = false; //because we don't want to toggle to match from nmatch
239 //mr->rated = false;
240 }
241 gd->recvRequest(m, getGameDialogFlags());
242 }
243
244 void IGSConnection::sendAddTime(int minutes)
245 {
246 sendText("addtime " + QString::number(minutes) + "\r\n");
247 }
248
249 /* I'm thinking you can only play one game at a time on IGS,
250 * but the game_ids are there in case it changes its mind */
251 void IGSConnection::adjournGame(const GameListing */*game_id*/)
252 {
253 /* double check this one */
254 /* There are three of these adjournGame, sendAdjournGame, and sendAdjournRequest FIXME */
255 sendText("adjourn\r\n");
256 }
257
258 void IGSConnection::sendMove(unsigned int game_id, MoveRecord * move)
259 {
260 switch(move->flags)
261 {
262 case MoveRecord::PASS:
263 sendText("pass\r\n");
264 break;
265 case MoveRecord::REQUESTUNDO:
266 sendText("undoplease\r\n"); //so polite
267 break;
268 case MoveRecord::UNDO:
269 sendText("undo\r\n");
270 break;
271 case MoveRecord::REFUSEUNDO:
272 sendText("noundo\r\n");
273 break;
274 case MoveRecord::RESIGN:
275 sendText("resign\r\n");
276 break;
277 case MoveRecord::DONE_SCORING:
278 sendText("done\r\n");
279 break;
280 case MoveRecord::NONE:
281 case MoveRecord::REMOVE: //handled simply
282 case MoveRecord::UNREMOVE:
283 {
284 char c1 = move->x - 1 + 'A';
285 if(move->x > 8) // no I in IGS
286 c1++;
287 BoardDispatch * bd = getIfBoardDispatch(game_id);
288 if(!bd)
289 {
290 qDebug("No board dispatch to send move from");
291 return;
292 }
293 GameData * g = bd->getGameData();
294 int c2 = g->board_size + 1 - move->y;
295 /* Why do we send the id here but
296 * not with the others? Can we play
297 * multiple games?? */
298 sendText(QString(c1) + QString::number(c2) + " " + QString::number(game_id) + "\r\n");
299 }
300 break;
301 default:
302 qDebug("IGSConnection: unhandled type");
303 break;
304 }
305 }
306
307 void IGSConnection::sendMatchRequest(MatchRequest * mr)
308 {
309 QString color;
310 switch(mr->color_request)
311 {
312 case MatchRequest::BLACK:
313 color = " B ";
314 break;
315 case MatchRequest::WHITE:
316 color = " W ";
317 break;
318 case MatchRequest::NIGIRI:
319 color = " N ";
320 break;
321 }
322 /* FIXME nmatch almost shouldn't be a setting on the match
323 * request. We should check the settings here and if they're
324 * fairly simple, change it to match in order to be compatible
325 * with more other clients... we need to check if this is
326 * our first offer as well we don't want to change to match
327 * mid negotiation.
328 * nmatch does have to be a setting though so we can't change
329 * it through negotiation on game dialog*/
330 if(mr->first_offer)
331 {
332 mr->first_offer = false;
333 if(mr->timeSystem == canadian && color != " N ")
334 mr->nmatch = false;
335 if(mr->handicap)
336 mr->nmatch = true;
337 }
338 if(mr->nmatch)
339 {
340 if(mr->timeSystem == byoyomi)
341 sendText("nmatch " + mr->opponent +
342 color +
343 QString::number(mr->handicap) + " " +
344 QString::number(mr->board_size) + " " +
345 QString::number(mr->maintime) + " " +
346 QString::number(mr->periodtime) + " " +
347 QString::number(mr->stones_periods) + " " +
348 "0 0 0\r\n");
349 else if(mr->timeSystem == canadian)
350 sendText("nmatch " + mr->opponent +
351 color +
352 QString::number(mr->handicap) + " " +
353 QString::number(mr->board_size) + " " +
354 QString::number(mr->maintime) + " " +
355 QString::number(mr->periodtime) + " " +
356 QString::number(mr->stones_periods) + " " +
357 "0 0 0\r\n");
358
359 }
360 else
361 {
362 sendText("match " + mr->opponent +
363 color +
364 QString::number(mr->board_size) + " " +
365 QString::number(mr->maintime / 60) + " " +
366 QString::number(mr->periodtime / 60) + "\r\n");
367 }
368 match_playerName = mr->opponent;
369 }
370
371 unsigned long IGSConnection::getGameDialogFlags(void)
372 {
373 //GDF_STONES25_FIXED is only set in "match" not "nmatch"
374 //there's also pmatch and tdmatch now I think
375 return (GDF_CANADIAN | /*GDF_BYOYOMI |*/ GDF_CANADIAN300 | GDF_BY_CAN_MAIN_MIN |
376 GDF_NIGIRI_EVEN | GDF_KOMI_FIXED6
377 /*| GDF_STONES25_FIXED*/);
378 }
379
380 void IGSConnection::declineMatchOffer(const PlayerListing * opponent)
381 {
382 // also possibly a "withdraw" message before opponent has responded FIXME
383 sendText("decline " + opponent->name + "\r\n");
384 }
385
386 void IGSConnection::acceptMatchOffer(const PlayerListing * /*opponent*/, MatchRequest * mr)
387 {
388 /* For IGS, we just send the same match request, assuming it hasn't changed.
389 * It would be bad to have the connection code hold the match request, so
390 * we'll have the gamedialogdispatch pass it along and we will trust
391 * that its the same.*/
392 sendMatchRequest(mr);
393 }
394
395 QTime IGSConnection::gd_checkMainTime(TimeSystem s, const QTime & t)
396 {
397 // match settings are between 1 and 530 with seemingly
398 // infinite byo yomi time
399 // nmatch is 0 to 3600 for byotime
400 // byomoves 0 to 25
401 // koryotimecount 0 - 100
402 // koryosec 0 - 600
403 // preboyoyomi 0 - 10
404 // no handicap nigiri
405 // if byomoves is not 1 koryo count must be 0
406 // if koryosec is not zero koryo count must not be 0
407 // time mod 60, but they're all like that
408 // note that we currently have match settings preventing
409 // pre byoyomi as well as koryo
410 QTime c = NetworkConnection::gd_checkMainTime(s, t);
411 int seconds = (c.minute() * 60) + c.second();
412 int minutes = c.minute();
413 switch(s)
414 {
415 case canadian:
416 if(minutes < 1)
417 return QTime(0, 1, 0);
418 else if(minutes > 530)
419 return QTime(8, 50, 0);
420 break;
421 case byoyomi:
422 //FIXME isn't only periodtime supposed to be less than 5?
423 if(seconds >= 300)
424 return QTime(0, 4, 0);
425 else
426 return QTime(0, c.minute(), 0);
427 break;
428 default:
429 break;
430 }
431 return c;
432 }
433
434 QTime IGSConnection::gd_checkPeriodTime(TimeSystem s, const QTime & t)
435 {
436 int seconds = (t.minute() * 60) + t.second();
437 switch(s)
438 {
439 case canadian:
440 if(seconds < 300)
441 return QTime(0, 5, 0);
442 break;
443 case byoyomi:
444 if(seconds > 299)
445 return QTime(0, 4, 59);
446 else if(seconds < 5) //actually should depend on maintime but...
447 return QTime(0, 0, 5);
448 break;
449 default:
450 qDebug("unsupported IGS time type p");
451 break;
452 }
453 return t;
454 }
455
456 unsigned int IGSConnection::gd_checkPeriods(TimeSystem s, unsigned int p)
457 {
458 switch(s)
459 {
460 case canadian:
461 if(p > 25)
462 return 25;
463 else
464 return p;
465 break;
466 case byoyomi:
467 if(p > 100)
468 return 100;
469 else if(p < 1)
470 return 1;
471 else
472 return p;
473 break;
474 default:
475 qDebug("unsupported IGS time type");
476 break;
477 }
478 return p;
479 }
480
481
482 /* Perhaps IGS does not allow multiple games, but this here will be trickier
483 * possibly, with other services that might since there's no game id here */
484 void IGSConnection::sendAdjournRequest(void)
485 {
486 sendText("adjourn\r\n");
487 }
488
489 void IGSConnection::sendAdjourn(void)
490 {
491 sendText("adjourn\r\n");
492 }
493
494 void IGSConnection::sendRefuseAdjourn(void)
495 {
496 sendText("decline adjourn\r\n");
497 }
498
499 /* There's honestly not much of a reason why we moved this out
500 * of the mainwindow_server except that it doesn't really belong
501 * there and we don't want the mainwindow ui code to be coupled
502 * to the IGS code. That said, since we've yet to add
503 * another seek facility to a different protocol, its completely
504 * tailored to IGS, but I guess its a start */
505 void IGSConnection::sendSeek(SeekCondition * s)
506 {
507 //seek entry 1 19 5 3 0
508 QString send_seek = "seek entry " +
509 QString::number(s->number) +
510 " 19 " + s->strength_wished + "\r\n";
511 sendText(send_seek);
512 }
513
514 void IGSConnection::sendSeekCancel(void)
515 {
516 sendText("seek entry_cancel\r\n");
517 }
518
519 /*void IGSConnection::setAccountAttrib(AccountAttib * aa)
520 {
521
522 }*/
523
524 /* So server sends type codes */
525 void IGSConnection::sendToggleClientOn(void)
526 {
527 sendText("toggle client on\r\n");
528 }
529
530 void IGSConnection::sendListChannels(void)
531 {
532 //could also clear channel list but unnecessary
533 ChannelListing * c = new ChannelListing();
534 c->number = 0;
535 c->name = "No Channel";
536 recvChannelListing(c);
537 sendText("channels\r\n");
538 }
539
540 void IGSConnection::handlePendingData()
541 {
542 int bytes;
543
544 switch(connectionState)
545 {
546 case Login:
547 bytes = qsocket->bytesAvailable();
548 if(bytes)
549 {
550 QByteArray data = qsocket->readAll();
551 handleLogin(QString(data));
552 }
553 break;
554 case Password:
555 bytes = qsocket->bytesAvailable();
556 if(bytes)
557 {
558 QByteArray data = qsocket->readAll();
559 handlePassword(QString(data));
560 }
561 break;
562 case PasswordSent:
563 case Connected:
564 while(qsocket->canReadLine())
565 {
566 QByteArray data = qsocket->readLine();
567 handleMessage(QString(data));
568 }
569 break;
570 case AuthFailed:
571 qWarning("Auth failed");
572 break;
573 case PassFailed:
574 qWarning("Pass failed");
575 break;
576 case ProtocolError:
577 //wait for someone to destroy the connection
578 //can't imagine why we're even here
579 break;
580 default:
581 qWarning("IGSConnection::handlePendingData(): invalid connection state");
582 break;
583 }
584 }
585
586 void IGSConnection::handleLogin(QString msg)
587 {
588 if(msg.contains("Login:") > 0)
589 {
590 qDebug("IGSConnection::handleLogin(): Login found");
591 QString u = username + "\r\n";
592 sendText(u.toLatin1().constData());
593 setState(Password);
594 }
595 else if(msg.contains("sorry") > 0)
596 {
597 setState(AuthFailed);
598 if(console_dispatch)
599 console_dispatch->recvText("Sorry");
600 }
601 else if(console_dispatch)
602 console_dispatch->recvText(msg.toLatin1().constData());
603 }
604
605 /* FIXME onAuthenticationNegotiated may need to
606 * be called earlier */
607 void IGSConnection::handlePassword(QString msg)
608 {
609 qDebug() << "IGSConnection::handlePassword(): " << msg;
610 if(msg.contains("Password:") > 0 || msg.contains("1 1") > 0)
611 {
612 qDebug("Password prompt or 1 1 found");
613 if(password == QString())
614 {
615 onAuthenticationNegotiated();
616 }
617 else
618 {
619 QString p = password + "\r\n";
620 sendText(p.toLatin1().constData());
621 }
622 setState(PasswordSent);
623 }
624 else if(msg.contains("guest"))
625 {
626 qDebug("Guest account");
627 setState(PasswordSent);
628 guestAccount = true;
629 }
630 }
631
632 void IGSConnection::onAuthenticationNegotiated(void)
633 {
634 needToSendClientToggle = false;
635 sendToggleClientOn();
636 NetworkConnection::onAuthenticationNegotiated();
637 NetworkConnection::onReady();
638 }
639
640 void IGSConnection::onReady(void)
641 {
642 // qDebug() << "IGSConnection::onReady()";
643 if(firstonReadyCall)
644 {
645 /* Check here because I've forgotten exactly which kinds of "1"
646 * msgs might call onReady() and apparently it can happen
647 * after our password has been refused. I'll add this to
648 * WING and LGS as well*/
649 if(connectionState != PasswordSent)
650 return;
651 firstonReadyCall = 0;
652 setState(Connected);
653 setKeepAlive(600);
654 /* This gets called too much, we need a better
655 * way to call it */
656 /* also needs to be earlier */
657 /* Below is kind of round-about. We need the account name in order to make our
658 * name in the players list blue. We could also use something similar to
659 * affect colors of observed or played games, although I'm not sure why we'd
660 * do any of this. But it means setting the account name on the listview model
661 * from the room. The room has no other real use for the account name, so we
662 * pull it from the connection just for that. Its a lot of misdirection just
663 * preserve the encapsulation but I suppose its okay for now. We might
664 * fix it along the lines of the above at some point. */
665 /* Specifically, when we figure out how we're going to deal with rooms in the
666 * future, then we'll find a better place to do this and set the connection
667 * etc.. the idea of a "DefaultRoom" will sort of drop away. FIXME */
668 /* There's a bug here where a person who's name is "" gets painted blue FIXME */
669 //getDefaultRoom()->setAccountName(username);
670
671 QString v = "id qGov" + QString(VERSION) + "\r\n";
672 sendText(v.toLatin1().constData());
673
674 //onAuthenticationNegotiated();
675
676 if(!guestAccount)
677 {
678 sendText("toggle newundo on\r\n"); //undoplease undo requests
679 //sendText("toggle client on\r\n"); //adds type codes, done earlier
680 sendText("toggle nmatch on\r\n"); //allows nmatch
681 sendText("toggle seek on\r\n");
682 sendText("toggle newrating\r\n"); //?s and +s
683
684 //sendText("toggle quiet on\r\n"); //FIXME do we want this?
685 //sendText("toggle quiet off\r\n");
686 //sendText("toggle review on\r\n");
687
688 //sendPlayersRequest();
689
690 sendNmatchParameters();
691 sendText("seek config_list\r\n");
692 }
693
694 //sendGamesRequest();
695 recvRoomListing(new RoomListing(0, "Lobby"));
696 sendRoomListRequest();
697 sendListChannels();
698 }
699 }
700
701 /* In case we want to add updates every so often */
702 void IGSConnection::timerEvent(QTimerEvent* e)
703 {
704 if(e->timerId() == keepAliveTimer)
705 sendText("ayt\r\n");
706 }
707
708 void IGSConnection::setKeepAlive(int seconds)
709 {
710 if(keepAliveTimer)
711 killTimer(keepAliveTimer);
712
713 if(seconds > 0)
714 keepAliveTimer = startTimer(seconds * 1000);
715 else
716 keepAliveTimer = 0;
717 }
718
719 /* Because the IGS protocol is garbage, we have to break encapsulation here
720 * and in BoardDispatch */
721 BoardDispatch * IGSConnection::getBoardFromAttrib(QString black_player, unsigned int black_captures, float black_komi, QString white_player, unsigned int white_captures, float white_komi)
722 {
723 BoardDispatch * board;
724 QMap<unsigned int, class BoardDispatch *>::iterator i;
725 for(i = boardDispatchMap.begin(); i != boardDispatchMap.end(); i++)
726 {
727 board = i.value();
728 if(board->isAttribBoard(black_player, black_captures, black_komi, white_player, white_captures, white_komi))
729 return board;
730 }
731 return NULL;
732 }
733
734 BoardDispatch * IGSConnection::getBoardFromOurOpponent(QString opponent)
735 {
736 BoardDispatch * board;
737 QMap<unsigned int, class BoardDispatch *>::iterator i;
738 /* Parser may supply our name from the IGS protocol... this is ugly
739 * but I'm really just trying to reconcile what I think a real
740 * protocol would be like with the IGS protocol */
741 if(opponent == username)
742 opponent = "";
743 for(i = boardDispatchMap.begin(); i != boardDispatchMap.end(); i++)
744 {
745 board = i.value();
746 if(board->isOpponentBoard(username, opponent))
747 return board;
748 }
749 return NULL;
750 }
751
752 void IGSConnection::requestGameInfo(unsigned int game_id)
753 {
754 char string[20];
755 snprintf(string, 20, "moves %d\r\n", game_id);
756 sendText(string);
757 snprintf(string, 20, "all %d\r\n", game_id);
758 sendText(string);
759 }
760
761 void IGSConnection::requestGameStats(unsigned int game_id)
762 {
763 qDebug() << "IGSConnection::requestGameStats " << game_id;
764 char string[20];
765 snprintf(string, 20, "game %d\r\n", game_id);
766 sendText(string);
767 }
768
769 int IGSConnection::time_to_seconds(const QString & time)
770 {
771 QRegExp re = QRegExp("([0-9]{1,2}):([0-9]{1,2})");
772 int min, sec;
773
774 if(re.indexIn(time) >= 0)
775 {
776 min = re.cap(1).toInt();
777 sec = re.cap(2).toInt();
778 }
779 else
780 {
781 qDebug("IGSConnection::time_to_seconds(): Bad time string");
782 return 0xffff;
783 }
784
785 return (60 * min) + sec;
786 }
787
788 /* formulas come from old MainWindow::rkToKey for sorting.
789 * might be worth fixing them to match some real score
790 * system*/
791 /* This is a kind of utility function but I'm not sure where to
792 * put it yet. FIXME
793 * If I had to guess, the best place for it is probably on
794 * a rank object or something used in messages... but we don't
795 * want to overload those.*/
796 unsigned int IGSConnection::rankToScore(QString rank)
797 {
798 if(rank == "NR")
799 return 0;
800 if(rank == "BC") //according to IGS value of 23k
801 return 800;
802
803 QString buffer = rank;
804 //buffer.replace(QRegExp("[pdk+?\\*\\s]"), "");
805 buffer.replace(QRegExp("[pdk+?]"), "");
806 int ordinal = buffer.toInt();
807 unsigned int score;
808
809 if(rank.contains("k"))
810 score = (31 - ordinal) * 100;
811 else if(rank.contains("d"))
812 score = 3000 + (ordinal * 100);
813 else if(rank.contains("p"))
814 score = 3600 + (ordinal * 100);
815 else
816 return 0;
817 if(rank.contains("?"))
818 score--;
819 return score;
820 }
821
822 /*
823 * on IGS, sends the 'nmatch'time, BY, handicap ranges
824 * command syntax : "nmatchrange BWN 0-9 19-19 60-60 60-3600 25-25 0 0 0-0"
825 * (B/W/ nigiri) Hcp Sz Main time (secs) BY time (secs) BY stones Koryo time
826 */
827 void IGSConnection::sendNmatchParameters(void)
828 {
829 //note that official client doesn't reload an nmatchrange cancel
830 //until after you reconnect
831 //who the hell knows
832 return;
833 QString c = "nmatchrange ";
834 QSettings settings;
835
836 c.append(preferences.nmatch_black ? "B" : "");
837 c.append(preferences.nmatch_white ? "W" : "");
838 c.append(preferences.nmatch_nigiri ? "N" : "");
839 c.append(" 0-");
840 c.append(preferences.nmatch_handicap);
841 c.append(" ");
842 c.append(QString::number(preferences.default_size));
843 c.append("-19 ");
844
845 c.append(QString::number(settings.value("DEFAULT_TIME").toInt()*60));
846 c.append("-");
847 c.append(QString::number(settings.value("NMATCH_MAIN_TIME").toInt()*60));
848 c.append(" ");
849 c.append(QString::number(settings.value("DEFAULT_BY").toInt()*60));
850 c.append("-");
851 c.append(QString::number(settings.value("NMATCH_BYO_TIME").toInt()*60));
852 c.append(" 25-25 0 0 0-0\r\n"); //FIXME we need to get and send KORYO time as well, or do we
853 /* If a line is used like below, match requests won't get to client but will be refused
854 * by server with a line like "wants Koryotime count 50 - 50 */
855 //c.append(" 5-25 50 30 1-3\r\n");
856 qDebug() << "IGSConnection::sendNmatchParameters(): " << c;
857 sendText(c);
858 }
859
860 #define IGS_LOGINMSG 0
861 #define IGS_PROMPT 1
862 #define IGS_BEEP 2
863 #define IGS_DOWN 4
864 #define IGS_ERROR 5
865 #define IGS_GAMES 7
866 #define IGS_FILE 8
867 #define IGS_INFO 9
868 #define IGS_KIBITZ 11
869 #define IGS_MESSAGES 14
870 #define IGS_MOVE 15
871 #define IGS_SAY 19
872 #define IGS_SCORE_M 20
873 #define IGS_SHOUT 21
874 #define IGS_STATUS 22
875 #define IGS_STORED 23
876 #define IGS_TELL 24
877 #define IGS_THIST 25
878 #define IGS_WHO 27
879 #define IGS_UNDO 28
880 #define IGS_YELL 32
881 #define IGS_AUTOMATCH 36
882 #define IGS_SERVERINFO 39
883 #define IGS_DOT 40
884 #define IGS_USERLIST 42
885 #define IGS_REMOVED 49
886 #define IGS_INGAMESAY 51
887 #define IGS_ADJOURNDECLINED 53
888 #define IGS_REVIEW 56
889 #define IGS_SEEK 63
890
891 void IGSConnection::handleMessage(QString msg)
892 {
893 unsigned int type = 0;
894 // qDebug() << msg;
895
896 if(msg[0].toLatin1() >= '0' && msg[0].toLatin1() <= '9')
897 {
898 type = (int)msg[0].toLatin1() - '0';
899 }
900 if(msg[1].toLatin1() >= '0' && msg[1].toLatin1() <= '9')
901 {
902 type *= 10;
903 type += (int)msg[1].toLatin1() - '0';
904 }
905
906 if(needToSendClientToggle)
907 onAuthenticationNegotiated();
908 if(msg.contains("You have logged in as a guest")) //WING, doesn't work, plus ugly
909 {
910 guestAccount = true;
911 setState(PasswordSent);
912 }
913 if(!type)
914 {
915
916 //line = line.remove(0, 2).trimmed();
917 //if(msg.size() > 4)
918 // qDebug("***%02x %02x %02x %02x", msg[msg.size() - 1].toLatin1(), msg[msg.size() - 2].toLatin1(), msg[msg.size() -3].toLatin1(), msg[msg.size() -4].toLatin1());
919
920 if(msg[3].toLatin1() == '1')
921 {
922 msg = msg.remove(0,2).trimmed();
923 handle_prompt(msg);
924 return;
925 }
926 if(msg.size() > 1)
927 {
928 //additional newline unnecessary //0a0d
929 if(msg[msg.size() - 1].toLatin1() == 0x0a)
930 msg = msg.remove(msg.size() - 2, msg.size()).trimmed();
931 }
932 if(console_dispatch && msg.size() > 2)
933 console_dispatch->recvText(msg.toLatin1().constData());
934 return;
935 }
936 //qDebug("***Type %d %c %c",type, msg[0].toLatin1(), msg[1].toLatin1());
937 switch(type)
938 {
939 case IGS_LOGINMSG:
940 //FIXME we never get here because !type returns
941 handle_loginmsg(msg);
942 break;
943 case IGS_PROMPT:
944 handle_prompt(msg);
945 break;
946 case IGS_BEEP:
947 handle_beep(msg);
948 break;
949 case IGS_DOWN:
950 handle_down(msg);
951 break;
952 case IGS_ERROR:
953 handle_error(msg);
954 break;
955 case IGS_GAMES:
956 handle_games(msg);
957 break;
958 case IGS_FILE:
959 handle_file(msg);
960 break;
961 case IGS_INFO:
962 handle_info(msg);
963 break;
964 case IGS_KIBITZ:
965 handle_kibitz(msg);
966 break;
967 case IGS_MESSAGES:
968 handle_messages(msg);
969 break;
970 case IGS_MOVE:
971 handle_move(msg);
972 break;
973 case IGS_SAY:
974 handle_say(msg);
975 break;
976 case IGS_SCORE_M:
977 handle_score_m(msg);
978 break;
979 case IGS_SHOUT:
980 handle_shout(msg);
981 break;
982 case IGS_STATUS:
983 handle_status(msg);
984 break;
985 case IGS_STORED:
986 handle_stored(msg);
987 break;
988 case IGS_TELL:
989 handle_tell(msg);
990 break;
991 case IGS_THIST:
992 handle_thist(msg);
993 break;
994 case IGS_WHO:
995 handle_who(msg);
996 break;
997 case IGS_UNDO:
998 handle_undo(msg);
999 break;
1000 case IGS_YELL:
1001 handle_yell(msg);
1002 break;
1003 case IGS_AUTOMATCH:
1004 handle_automatch(msg);
1005 break;
1006 case IGS_SERVERINFO:
1007 handle_serverinfo(msg);
1008 break;
1009 case IGS_DOT:
1010 handle_dot(msg);
1011 break;
1012 case IGS_USERLIST:
1013 handle_userlist(msg);
1014 break;
1015 case IGS_REMOVED:
1016 handle_removed(msg);
1017 break;
1018 case IGS_INGAMESAY:
1019 handle_ingamesay(msg);
1020 break;
1021 case IGS_ADJOURNDECLINED:
1022 handle_adjourndeclined(msg);
1023 break;
1024 case IGS_REVIEW:
1025 handle_review(msg);
1026 break;
1027 case IGS_SEEK:
1028 handle_seek(msg);
1029 break;
1030 }
1031 }
1032
1033 /* I think it would be neat if the QString was tokenized, just like with a compiler. I mean basically there's
1034 * signed ints and signed floating points, and then there's a few strings, like names, and then everything else
1035 * is specific go related "keywords". So we could process the msg before we pass it to the handler and then the
1036 * handlers wouldn't have any ugly text manipulation stuff... I really like this idea. And then I think it would
1037 * become very clear the messages that require something from a previous message, like the 9s and 15s that we
1038 * had some issues with
1039 *
1040 * Actually, this reminds me a little of "element" in the old parser, except element only did delimited strings
1041 * The one question is whether to do QVariants or have our own simple variant like object. I can imagine the
1042 * code looking like nested case statements.
1043 *
1044 * The thing is, the original parser was kind of ugly, but at least it was consistent. This, I'm not proud of
1045 * because its like the original ugly code, chopped up and made to work in a different form. Its even uglier.
1046 cd* But I think I can make it nicer, and then the individual handlers just become case statements based on the type
1047 * of these element objects... we could call it... TBPToken text based protocol token...
1048 *
1049 * This is a good idea, but since IGS is basically all keywords, the point would just be to take all the string
1050 * manipulation out of the handlers and put it somewhere else where it could be ugly but consistent.
1051 *
1052 * Even though this might ferret out some bugs, its still cosmetic and a big change so I'm going to hold off on it.
1053 *
1054 * Now I'm thinking, instead of this also being a big list of string conditionals, we could instead have, much like
1055 * other protocols protocolcodes header files, we could have a list of strings and following values organized in some
1056 * sensible way. So this would just go through the list and if it was an int or a float, it would go with that,
1057 * otherwise it would find the string from the list and go with that, or just create a string object at the end...*/
1058
1059 /* We want two functions, the tokenizer, and then something that's just a list of std::map adds
1060 * the tokens can be an abstract class with the type within... I got to stop writing things and just
1061 * program them like I used to, but I shouldn't be working on this right now*/
1062
1063 /* I'm having second thoughts about this idea. First, it doesn't make anything faster. We could try to remove
1064 * some of the regular expressions, that would make things a bit faster but that's hardly an issue anyway.
1065 * Second, it does take the text strings out of the function, but we just replace them with "IGS_" defines and a lot
1066 * of token handling and parsing stuff. It might look kind of clean to have a list of text strings with their
1067 * associated defines in another file but... its a significant amount of effort and I'm not sure I can
1068 * see how it would improve the code all that much. I certainly like the idea... I could even return the multiple
1069 * functions to the original parser's giant case statement, except instead of hard coded numbers it would be defines
1070 * all over the place. I think some of the game/move code and updates need to be fixed up but...
1071 * I guess this is just a really low priority.*/
1072 #ifdef FIXME
1073 returnsomekindoflist IGSConnection::tokenize(QString msg)
1074 {
1075
1076 }
1077 #endif //FIXME
1078
1079 /* IGS Protocol messages */
1080 /* I know WING has these 0 messages... move to WING if specific FIXME*/
1081 void IGSConnection::handle_loginmsg(QString line)
1082 {
1083 /* These seem to have extra newlines on them */
1084 line = line.remove(line.length() - 1, line.length()).trimmed();
1085 if(line.length() > 1)
1086 {
1087 getConsoleDispatch()->recvText(line.toLatin1().constData());
1088 }
1089 }
1090
1091 void IGSConnection::handle_prompt(QString line)
1092 {
1093 line = line.remove(0, 2).trimmed();
1094 /* There's some other ones here, that might be an issue, besides 5 and 8,
1095 * we should consider just calling onReady() here */
1096 if(line.contains("5") || line.contains("6") || line.contains("8"))
1097 {
1098 }
1099 onReady();
1100 /* From NNGS source:
1101 #define STAT_LOGON 0 // Unused
1102 #define STAT_PASSWORD 1 // Unused
1103 #define STAT_PASSWORD_NEW 2 // Unused
1104 #define STAT_PASSWORD_CONFIRM 3 // Unused
1105 #define STAT_REGISTER 4 // Unused
1106 #define STAT_WAITING 5
1107 #define STAT_PLAYING_GO 6
1108 #define STAT_SCORING 7
1109 #define STAT_OBSERVING 8
1110 #define STAT_TEACHING 9 // Unused
1111 #define STAT_COMPLETE 10 // Unused
1112 */
1113 }
1114 // BEEP
1115 void IGSConnection::handle_beep(QString line)
1116 {
1117 if (line.contains("Game saved"))
1118 {
1119 #ifdef FIXME
1120 return IT_OTHER;
1121 #endif //FIXME
1122 }
1123 }
1124
1125 void IGSConnection::handle_down(QString line)
1126 {
1127 //4 **** Server shutdown started by admin. ****
1128 if(line.contains("Server shutdown"))
1129 {
1130 getConsoleDispatch()->recvText("Server shutdown started by admin");
1131 qDebug("Closing connection in accordance with server shutdown");
1132 closeConnection();
1133 }
1134 }
1135 // ERROR message
1136 // 5 Player "xxxx" is not open to match requests.
1137 // 5 You cannot observe a game that you are playing.
1138 // 5 You cannot undo in this game
1139 // 5 Opponent's client does not support undoplease
1140 // 5 noldo is currently involved in a match against someone else.
1141 // case 5:
1142 void IGSConnection::handle_error(QString line)
1143 {
1144 static QString memory_str;
1145
1146 Room * room = getDefaultRoom();
1147 qDebug() << "IGSConnection::handle_error()" << line;
1148 line = line.remove(0, 2).trimmed();
1149 if(line.contains("Invalid password"))
1150 {
1151 setState(PassFailed);
1152 getConsoleDispatch()->recvText(line.toLatin1().constData());
1153 //closeConnection();
1154 return;
1155 }
1156 else if(line.contains("string you have typed is illegal"))
1157 {
1158 setState(ProtocolError);
1159 getConsoleDispatch()->recvText(line.toLatin1().constData());
1160 //closeConnection();
1161 return;
1162 }
1163 else if (line.contains("No user named"))
1164 {
1165 #ifdef FIXME
1166 QString name = element(line, 1, "\"");
1167 // //emit signal_talk(name, "@@@", true);
1168 #endif //FIXME
1169 }
1170 else if (line.contains("is currently involved in a match") ||
1171 line.contains("is playing a game") ||
1172 line.contains("is involved in another game"))
1173 {
1174 QString opp;
1175 if(line.contains("is playing a game")) //WING
1176 opp = element(line, 1, " ", "\"");
1177 else
1178 opp = element(line, 0, " ");
1179 PlayerListing * p = getPlayerListingNeverFail(opp);
1180 GameDialog * gameDialog = getGameDialog(p);
1181 gameDialog->recvRefuseMatch(GD_REFUSE_INGAME);
1182 }
1183 else if (line.contains("is not open to match requests"))
1184 {
1185 QString opp = element(line, 0, "\"", "\"");
1186 PlayerListing * p = getPlayerListingNeverFail(opp);
1187 GameDialog * gameDialog = getGameDialog(p);
1188 gameDialog->recvRefuseMatch(GD_REFUSE_NOTOPEN);
1189 }
1190 else if(line.contains("does not accept direct match"))
1191 {
1192 QString opp = element(line, 0, " ");//, " ");
1193 PlayerListing * p = getPlayerListingNeverFail(opp);
1194 GameDialog * gameDialog = getGameDialog(p);
1195 gameDialog->recvRefuseMatch(GD_REFUSE_NODIRECT);
1196 }
1197 else if (line.contains("player is currently not accepting matches"))
1198 {
1199 // IGS: 5 That player is currently not accepting matches.
1200 //GameDialog * gameDialog = getGameDialog(opp);
1201 //gameDialog->recvMatchRequest(0, 0);
1202 qDebug("player not currently accepting matches");
1203 /* There's no opponent from this message. A
1204 * message window called from the room makes sense.
1205 * Maybe even like a recvServerAlert function FIXME
1206 * I'll just do this for now*/
1207 if(match_playerName.size())
1208 {
1209 PlayerListing * p = getPlayerListingNeverFail(match_playerName);
1210 GameDialog * gameDialog = getGameDialog(p);
1211 gameDialog->recvRefuseMatch(GD_REFUSE_NOTOPEN);
1212 }
1213 }
1214 else if(line.contains("Invalid parameters"))
1215 {
1216 if(match_playerName.size())
1217 {
1218 PlayerListing * p = getPlayerListingNeverFail(match_playerName);
1219 GameDialog * gameDialog = getGameDialog(p);
1220 gameDialog->recvRefuseMatch(GD_INVALID_PARAMETERS);
1221 }
1222 }
1223 //5 Opponent's client does not support nmatch.
1224 else if(line.contains("does not support nmatch"))
1225 {
1226 if(match_playerName.size())
1227 {
1228 PlayerListing * p = getPlayerListingNeverFail(match_playerName);
1229 GameDialog * gameDialog = getGameDialog(p);
1230 gameDialog->recvRefuseMatch(GD_OPP_NO_NMATCH);
1231 }
1232 }
1233 else if (line.contains("You cannot undo") || line.contains("client does not support undoplease"))
1234 {
1235 BoardDispatch * boarddispatch = getIfBoardDispatch(game_were_playing);
1236 if(boarddispatch)
1237 {
1238 boarddispatch->recvKibitz(0, line);
1239 boarddispatch->getGameData()->undoAllowed = false;
1240 }
1241 //maybe should be a message box? FIXME
1242 }
1243 else if(line.contains("There is no such game") || line.contains("Invalid game number"))
1244 {
1245 /* This comes from both observe and unobserve messages,
1246 * meaning that we can't quite use it to remove gamelistings
1247 * unless we fix up the protocol_save_int stuff a little
1248 * which needs it anyhow. FIXME */
1249 //connection->protocol_save_int
1250 }
1251 else if (line.contains("Setting you open for matches"))
1252 room->recvToggle(0, true);
1253
1254 // 5 There is a dispute regarding your nmatch:
1255 // 5 yfh2test request: B 3 19 420 900 25 0 0 0
1256 // 5 yfh22 request: W 3 19 600 900 25 0 0 0
1257 //
1258 // 5 There is a dispute regarding your match:
1259 // 5 yfh2test wants White on a 19x19 in 10 mins (10 byoyomi).
1260 // 5 eb5 wants Black on a 19x19 in 10 mins (12 byoyomi).
1261
1262 else if(line.contains("wants Time"))
1263 {
1264 /* FIXME note that we should really look up their match conditions
1265 * before even creating game dialog !!! */
1266 //5 lemon wants Time 60 - 60.
1267 //5 seinosuke wants Time 300 - 300
1268 QString opponent = element(line, 0, " ");
1269 PlayerListing * pl = getPlayerListingNeverFail(opponent);
1270 //QString timetochange = element(line, 2, " ");
1271 GameDialog * gameDialog = getGameDialog(pl);
1272 MatchRequest * m = gameDialog->getMatchRequest();
1273 MatchRequest * aMatch = new MatchRequest(*m);
1274 aMatch->maintime = element(line, 3, " ").toInt();
1275 aMatch->periodtime = element(line, 4, " ", ".").toInt();
1276 if(aMatch->periodtime > 299)
1277 aMatch->timeSystem = canadian;
1278 else
1279 aMatch->timeSystem = byoyomi;
1280 gameDialog->recvRequest(aMatch);
1281 gameDialog->recvRefuseMatch(GD_RESET);
1282 }
1283 else if(line.contains("wants Byomoves"))
1284 {
1285 //5 x wants Byomoves 1 - 1.
1286 QString opponent = element(line, 0, " ");
1287 PlayerListing * pl = getPlayerListingNeverFail(opponent);
1288 //QString timetochange = element(line, 2, " ");
1289 GameDialog * gameDialog = getGameDialog(pl);
1290 MatchRequest * m = gameDialog->getMatchRequest();
1291 MatchRequest * aMatch = new MatchRequest(*m);
1292 aMatch->stones_periods = element(line, 3, " ").toInt();
1293 gameDialog->recvRequest(aMatch);
1294 gameDialog->recvRefuseMatch(GD_RESET);
1295 }
1296 else if(line.contains("wants Byotime"))
1297 {
1298 //5 x wants Byomoves 1 - 1.
1299 QString opponent = element(line, 0, " ");
1300 PlayerListing * pl = getPlayerListingNeverFail(opponent);
1301 //QString timetochange = element(line, 2, " ");
1302 GameDialog * gameDialog = getGameDialog(pl);
1303 MatchRequest * m = gameDialog->getMatchRequest();
1304 MatchRequest * aMatch = new MatchRequest(*m);
1305 aMatch->periodtime = element(line, 3, " ").toInt();
1306 gameDialog->recvRequest(aMatch);
1307 gameDialog->recvRefuseMatch(GD_RESET);
1308 }
1309 else if(line.contains("wants Handicap"))
1310 {
1311 //5 x wants Handicap 0 - 0.
1312 QString opponent = element(line, 0, " ");
1313 PlayerListing * pl = getPlayerListingNeverFail(opponent);
1314 GameDialog * gameDialog = getGameDialog(pl);
1315 MatchRequest * m = gameDialog->getMatchRequest();
1316 MatchRequest * aMatch = new MatchRequest(*m);
1317 aMatch->handicap = element(line, 3, " ").toInt();
1318 gameDialog->recvRequest(aMatch);
1319 gameDialog->recvRefuseMatch(GD_RESET);
1320 }
1321 else if (line.contains("request:") || line.contains("wants"))
1322 {
1323 QString p = element(line, 0, " ");
1324 if (p == getUsername())
1325 {
1326 memory_str = line;
1327 return;
1328 }
1329
1330 if (memory_str.contains(getUsername() + " request"))
1331 {
1332 memory_str = "";
1333 return;
1334 }
1335 MatchRequest * aMatch = 0;
1336 GameDialog * gameDialog = 0;
1337 PlayerListing * pl = 0;
1338 if(line.contains("wants")) //match
1339 {
1340 if(line.contains("turn"))
1341 {
1342 pl = getPlayerListingNeverFail(p);
1343 gameDialog = getGameDialog(pl);
1344 MatchRequest * m = gameDialog->getMatchRequest();
1345 aMatch = new MatchRequest(*m);
1346 if(element(line, 3, " ") == "[B]")
1347 {
1348 aMatch->color_request = MatchRequest::BLACK;
1349 }
1350 else if(element(line, 3, " ") == "[W]")
1351 {
1352 aMatch->color_request = MatchRequest::WHITE;
1353 }
1354 else
1355 {
1356 qDebug("Unknown: %s", line.toLatin1().constData());
1357 delete aMatch;
1358 return;
1359 }
1360 }
1361 else
1362 {
1363 aMatch = new MatchRequest();
1364 aMatch->opponent = p;
1365 aMatch->nmatch = false;
1366
1367 if(element(line, 2, " ") == "Black")
1368 aMatch->color_request = MatchRequest::BLACK;
1369 else if(element(line, 2, " ") == "Nigiri")//IGS handles this??
1370 aMatch->color_request = MatchRequest::NIGIRI;
1371 else
1372 aMatch->color_request = MatchRequest::WHITE;
1373 QString s = element(line, 5, " ");
1374
1375 aMatch->board_size = element(s, 0, "x").toInt();
1376 aMatch->maintime = element(line, 7, " ").toInt() * 60;
1377 s = element(line, 9, " ");
1378 aMatch->periodtime = element(s, 1, "(").toInt();
1379 aMatch->stones_periods = 25; // I assume IGS assumes this is always 25
1380 if(getCurrentRoom()->name.contains("No rated"))
1381 aMatch->free_rated = FREE;
1382 else
1383 aMatch->free_rated = RATED;
1384 }
1385 }
1386 else //nmatch
1387 {
1388 aMatch = new MatchRequest();
1389 aMatch->opponent = p;
1390 aMatch->nmatch = true;
1391 if(element(line, 2, " ") == "B")
1392 aMatch->color_request = MatchRequest::BLACK;
1393 else if(element(line, 2, " ") == "N")//IGS handles this??
1394 aMatch->color_request = MatchRequest::NIGIRI;
1395 else
1396 aMatch->color_request = MatchRequest::WHITE;
1397 //N 0 9 120 30 1 3 1 0
1398
1399 aMatch->handicap = element(line, 3, " ").toInt();
1400 aMatch->board_size = element(line, 4, " ").toInt();
1401 aMatch->maintime = element(line, 5, " ").toInt();
1402 aMatch->periodtime = element(line, 6, " ").toInt();
1403 aMatch->stones_periods = element(line, 7, " ").toInt();
1404 if(aMatch->periodtime >= 300)
1405 aMatch->timeSystem = canadian;
1406 else
1407 aMatch->timeSystem = byoyomi;
1408 if(getCurrentRoom()->name.contains("No rated"))
1409 aMatch->free_rated = FREE;
1410 else
1411 aMatch->free_rated = RATED;
1412 }
1413
1414 if(!pl)
1415 pl = getPlayerListingNeverFail(aMatch->opponent);
1416 const PlayerListing * us = getOurListing();
1417 if(us)
1418 {
1419 aMatch->our_name = us->name;
1420 aMatch->our_rank = us->rank;
1421 }
1422 aMatch->their_rank = pl->rank;
1423
1424 if(!gameDialog)
1425 gameDialog = getGameDialog(pl);
1426 gameDialog->recvRequest(aMatch);
1427 delete aMatch;
1428 }
1429 else if(line.contains("is a private game"))
1430 {
1431 /* FIXME we need a dialog box saying that its a private game */
1432 int number = element(line, 2, " ").toInt();
1433 qDebug("%d is a private game", number);
1434 }
1435 else if(line.contains("Could not add you to the channel"))
1436 {
1437 //i.e., trying to join pro channel
1438 //actually should change back to the channel we were in probably
1439 //but to do that FIXME, we'd have to set that when we actually
1440 //entered a channel and I haven't seen a msg for that
1441 changeChannel("No Channel");
1442 }
1443 getConsoleDispatch()->recvText(line.toLatin1().constData());
1444 }
1445 // games
1446 // 7 [##] white name [ rk ] black name [ rk ] (Move size H Komi BY FR) (###)
1447 // 7 [41] xxxx10 [ 1k*] vs. xxxx12 [ 1k*] (295 19 0 5.5 12 I) ( 1)
1448 // 7 [118] larske [ 7k*] vs. T08811 [ 7k*] (208 19 0 5.5 10 I) ( 0)
1449 // 7 [255] YK [ 7d*] vs. SOJ [ 7d*] ( 56 19 0 5.5 4 I) ( 18)
1450 // 7 [42] TetsuyaK [ 1k*] vs. ezawa [ 1k*] ( 46 19 0 5.5 8 I) ( 0)
1451 // 7 [237] s2884 [ 3d*] vs. csc [ 2d*] (123 19 0 0.5 6 I) ( 0)
1452 // 7 [67] atsukann [14k*] vs. mitsuo45 [15k*] ( 99 19 0 0.5 10 I) ( 0)
1453 // 7 [261] lbeach [ 3k*] vs. yurisuke [ 3k*] (194 19 0 5.5 3 I) ( 0)
1454 // 7 [29] ppmmuu [ 1k*] vs. Natta [ 2k*] (141 19 0 0.5 2 I) ( 0)
1455 // 7 [105] Clarky [ 2k*] vs. gaosg [ 2k*] ( 65 19 0 5.5 10 I) ( 1)
1456 //case 7:
1457 void IGSConnection::handle_games(QString line)
1458 {
1459 if (line.contains("##"))
1460 // skip first line
1461 return;
1462 line.remove(QRegularExpression("[\\[\\]\\(\\)]"));
1463 QStringList words = line.split(QChar(' '),QString::SkipEmptyParts);
1464 bool ok;
1465 int number = words.at(1).toInt(&ok);
1466 if (!ok)
1467 return;
1468 GameListing * aGame = getDefaultRoom()->getGameListing(number);
1469
1470 aGame->_white_name = words.at(2);
1471 aGame->_white_rank = words.at(3);
1472 fixRankString(&(aGame->_white_rank));
1473 // skip "vs."
1474 aGame->_black_name = words.at(5);
1475 aGame->_black_rank = words.at(6);
1476 fixRankString(&(aGame->_black_rank));
1477 aGame->moves = words.at(7).toInt();
1478 aGame->board_size = words.at(8).toInt();
1479 aGame->handicap = words.at(9).toInt();
1480 aGame->komi = words.at(10).toFloat();
1481 aGame->By = words.at(11);
1482 aGame->FR = words.at(12);
1483 aGame->observers = words.at(13).toInt();
1484
1485 aGame->white = getPlayerListingNeverFail(aGame->_white_name);
1486 aGame->black = getPlayerListingNeverFail(aGame->_black_name);
1487
1488 aGame->running = true;
1489 #ifdef FIXME
1490 aGame->oneColorGo = false ;
1491 #endif //FIXME
1492
1493 emit gameListingReceived(aGame);
1494 if(protocol_save_string == "restoring")
1495 {
1496 BoardDispatch * boarddispatch = getBoardDispatch(number);
1497 /* This is ugly, we can get bad info here so we have
1498 * to create a half record to send. FIXME*/
1499 GameData * aGameData = boarddispatch->getGameData();
1500 aGameData->number = aGame->number;
1501 aGameData->white_name = aGame->white_name();
1502 aGameData->black_name = aGame->black_name();
1503 aGameData->white_rank = aGame->white_rank();
1504 aGameData->black_rank = aGame->black_rank();
1505 aGameData->board_size = aGame->board_size;
1506 aGameData->handicap = aGame->handicap;
1507 aGameData->komi = aGame->komi;
1508 boarddispatch->openBoard();
1509
1510 requestGameInfo(number);
1511 protocol_save_string = QString();
1512 }
1513 }
1514 // "8 File"
1515 // case 8:
1516 void IGSConnection::handle_file(QString line)
1517 {
1518 qDebug() << line;
1519 }
1520 // INFO: stats, channelinfo
1521 // NNGS, LGS: (NNGS new: 2nd line w/o number!)
1522 // 9 Channel 20 Topic: [xxx] don't pay your NIC bill and only get two players connected
1523 // 9 xxxx1 xxxx2 xxxx3 xxxx4 frosla
1524 // 9 Channel 49 Topic: Der deutsche Kanal (German)
1525 // 9 frosla
1526 //
1527 // --> channel 49
1528 // 9 Channel 49 turned on.
1529 // 9 Channel 49 Title:
1530 //
1531 // 9 guest has left channel 49.
1532 //
1533 // 9 Komi set to -3.5 in match 10
1534 // - in my game:
1535 // - opponent:
1536 // 9 Komi is now set to -3.5
1537 // - me:
1538 // 9 Set the komi to -3.5
1539 // NNGS, LGS:
1540 // 9 I suggest that ditto play White against made:
1541 // 9 For 19x19: Play an even game and set komi to 1.5.
1542 // 9 For 13x13: Play an even game and set komi to 6.5.
1543 // 9 For 9x9: Play an even game and set komi to 4.5.
1544 // or:
1545 // 9 I suggest that pem play Black against eek:
1546 // For 19x19: Take 3 handicap stones and set komi to -2.5.
1547 // For 13x13: Take 1 handicap stones and set komi to -6.5.
1548 // For 9x9: Take 1 handicap stones and set komi to 0.5.
1549 // or:
1550 // I suggest that you play White against Cut:
1551 //
1552 // 9 Match [19x19] in 1 minutes requested with xxxx as White.
1553 // 9 Use <match xxxx B 19 1 10> or <decline xxxx> to respond.
1554 //
1555 // 9 Match [5] with guest17 in 1 accepted.
1556 // 9 Creating match [5] with guest17.
1557 //
1558 // 9 Requesting match in 10 min with frosla as Black.
1559 // 9 guest17 declines your request for a match.
1560 // 9 frosla withdraws the match offer.
1561 //
1562 // 9 You can check your score with the score command
1563 // 9 You can check your score with the score command, type 'done' when finished.
1564 // 9 Removing @ K8
1565 //
1566 // 9 Use adjourn to adjourn the game.
1567 //
1568 // NNGS: cmd 'user' NOT PARSED!
1569 // 9 Info Name Rank 19 9 Idle Rank Info
1570 // 9 -------- ---------- ---- --- --- ---- ---------------------------------
1571 // 9 Q -- 5 hhhsss 2k* 0 0 1s NR
1572 // 9 -- 5 saturn 2k 0 0 18s 2k
1573 // 9 -- 6 change1 1k* 0 0 7s NR
1574 // 9 S -- -- mikke 1d 0 18 30s shodan in sweden
1575 // 9 X -- -- ksonney NR 0 0 56s NR
1576 // 9 -- -- kou 6k* 0 0 23s NR
1577 // 9 SQ! -- -- GnuGo 11k* 0 0 5m Estimation based on NNGS rating early
1578 // 9 X -- -- Maurice 3k* 0 0 24s 2d at Hamilton Go Club, Canada; 3d in
1579 void IGSConnection::handle_info(QString line)
1580 {
1581 static PlayerListing * statsPlayer;
1582 BoardDispatch * boarddispatch;
1583 Room * room = getDefaultRoom();
1584 static QString memory_str;
1585 static int memory = 0;
1586 //qDebug("9: %s", line.toLatin1().constData());
1587 line = line.remove(0, 2).trimmed();
1588 // status messages
1589 if (line.contains("Set open to be"))
1590 {
1591 bool val = (line.indexOf("False") == -1);
1592 room->recvToggle(0, val);
1593 }
1594 else if (line.contains("Setting you open for matches"))
1595 room->recvToggle(0, true);
1596 else if (line.contains("Set looking to be"))
1597 {
1598 bool val = (line.indexOf("False") == -1);
1599 room->recvToggle(1, val);
1600 }
1601 // 9 Set quiet to be False.
1602 else if (line.contains("Set quiet to be"))
1603 {
1604 bool val = (line.indexOf("False") == -1);
1605 room->recvToggle(2, val);
1606 }
1607 else if(line.contains("Welcome to the game room"))
1608 {
1609 QString buffer = element(line, 6, " ");
1610 int number = buffer.remove(0,1).toInt();
1611 qDebug("Joined room number %d", number);
1612 //FIXME, we need to reload the player
1613 //and games lists here
1614 //potentially something else does this? doublecheck
1615 }
1616 else if (line.indexOf("Channel") == 0)
1617 {
1618 #ifdef FIXME
1619 // channel messages
1620 QString e1 = element(line, 1, " ");
1621 if (e1.at(e1.length()-1) == ':')
1622 e1.truncate(e1.length()-1);
1623 int nr = e1.toInt();
1624
1625 if (line.contains("turned on."))
1626 {
1627 // turn on channel
1628 emit signal_channelinfo(nr, QString("*on*"));
1629 }
1630 else if (line.contains("turned off."))
1631 {
1632 // turn off channel
1633 emit signal_channelinfo(nr, QString("*off*"));
1634 }
1635 else if (!line.contains("Title:") || gsName == GS_UNKNOWN)
1636 {
1637 // keep in memory to parse next line correct
1638 memory = nr;
1639 emit signal_channelinfo(memory, line);
1640 memory_str = "CHANNEL";
1641 }
1642 #endif //FIXME
1643 // return IT_OTHER;
1644 }
1645 #ifdef FIXME
1646 else if (memory != 0 && !memory_str.isEmpty() && memory_str == "CHANNEL")
1647 {
1648 //emit signal_channelinfo(memory, line);
1649
1650 // reset memory
1651 memory = 0;
1652 memory_str = QString();
1653 // return IT_OTHER;
1654 }
1655 #endif //FIXME
1656 // IGS: channelinfo
1657 // 9 #42 Title: Untitled -- Open
1658 // 9 #42 broesel zero815 Granit
1659 else if (line.contains("#"))
1660 {
1661 QString msg = element(line, 0, " ", "EOL");
1662
1663 if(msg.contains("Title:"))
1664 {
1665 channel = new ChannelListing();
1666 channel->number = element(line, 0, "#", " ").toInt();
1667 channel->name = element(line, 2, " ");
1668 }
1669 else if(channel)
1670 {
1671 //parse member list FIXME
1672 channel = 0;
1673 }
1674 if(channel)
1675 recvChannelListing(channel);
1676 }
1677 // NNGS: channels
1678 else if (line.contains("has left channel") || line.contains("has joined channel"))
1679 {
1680 #ifdef FIXME
1681 QString e1 = element(line, 3, " ", ".");
1682 int nr = e1.toInt();
1683
1684 // turn on channel to get full info
1685 emit signal_channelinfo(nr, QString("*on*"));
1686 #endif //FIXME
1687 }
1688 else if (line.contains("Game is titled:"))
1689 {
1690 #ifdef FIXME
1691 QString t = element(line, 0, ":", "EOL");
1692 emit signal_title(t);
1693 #endif //FIXME
1694 return;
1695 }
1696 else if (line.contains("offers a new komi "))
1697 {
1698 #ifdef FIXME
1699 // NNGS: 9 physician offers a new komi of 1.5.
1700 QString komi = element(line, 6, " ");
1701 if (komi.at(komi.length()-1) == '.')
1702 komi.truncate(komi.length() - 1);
1703 QString opponent = element(line, 0, " ");
1704
1705 // true: request
1706 emit signal_komi(opponent, komi, true);
1707 #endif //FIXME
1708 }
1709 else if (line.contains("Komi set to"))
1710 {
1711 #ifdef FIXME
1712 // NNGS: 9 Komi set to -3.5 in match 10
1713 QString komi = element(line, 3, " ");
1714 QString game_id = element(line, 6, " ");
1715
1716 // false: no request
1717 emit signal_komi(game_id, komi, false);
1718 #endif //FIXME
1719 }
1720 else if (line.contains("wants the komi to be"))
1721 {
1722 #ifdef FIXME
1723 // IGS: 9 qGoDev wants the komi to be 1.5
1724 QString komi = element(line, 6, " ");
1725 QString opponent = element(line, 0, " ");
1726
1727 // true: request
1728 emit signal_komi(opponent, komi, true);
1729 #endif //FIXME
1730 }
1731 else if (line.contains("Komi is now set to"))
1732 {
1733 #ifdef FIXME
1734 // 9 Komi is now set to -3.5. -> oppenent set for our game
1735 QString komi = element(line, 5, " ");
1736 // error? "9 Komi is now set to -3.5.9 Komi is now set to -3.5"
1737 if (komi.contains(".9"))
1738 komi = komi.left(komi.length() - 2);
1739
1740 // false: no request
1741 emit signal_komi(QString(), komi, false);
1742 #endif //FIXME
1743 }
1744 else if (line.contains("Set the komi to"))
1745 {
1746 #ifdef FIXME
1747 // NNGS: 9 Set the komi to -3.5 - I set for own game
1748 QString komi = element(line, 4, " ");
1749
1750 // false: no request
1751 emit signal_komi(QString(), komi, false);
1752 #endif //FIXME
1753 }
1754 else if (line.contains("game will count"))
1755 {
1756 #ifdef FIXME
1757 // IGS: 9 Game will not count towards ratings.
1758 // 9 Game will count towards ratings.
1759 emit signal_freegame(false);
1760 #endif //FIXME
1761 }
1762 else if (line.contains("game will not count", Qt::CaseInsensitive))
1763 {
1764 #ifdef FIXME
1765 // IGS: 9 Game will not count towards ratings.
1766 // 9 Game will count towards ratings.
1767 emit signal_freegame(true);
1768 #endif //FIXME
1769 }
1770 else if ((line.contains("[") || line.contains("yes")) && line.length() < 6)
1771 {
1772 // 9 [20] ... channelinfo
1773 // 9 yes ... ayt
1774 return;
1775 }
1776 // WING has "as" instead of "has"
1777 else if (line.contains("as restarted your game") ||
1778 line.contains("has restored your old game"))
1779 {
1780 // FIXME, we probably need to set something up here, though
1781 // for instance with WING, its a 21 message that really
1782 // heralds things/info
1783 if (line.contains("restarted"))
1784 // memory_str -> see case 15 for continuation
1785 protocol_save_string = element(line, 0, " ");
1786 }
1787 else if (line.contains("I suggest that"))
1788 {
1789 memory_str = line;
1790 return;
1791 }
1792 else if (line.contains("and set komi to"))
1793 {
1794 #ifdef FIXME
1795 //this might be NNGS, LGS only
1796 // suggest message ...
1797 if (!memory_str.isEmpty())
1798 // something went wrong...
1799 return;
1800
1801 line = line.simplified();
1802
1803 QString p1 = element(memory_str, 3, " ");
1804 QString p2 = element(memory_str, 6, " ", ":");
1805 bool p1_play_white = memory_str.contains("play White");
1806
1807 QString h, k;
1808 if (line.contains("even game"))
1809 h = "0";
1810 else
1811 h = element(line, 3, " ");
1812
1813 k = element(line, 9, " ", ".");
1814
1815 int size = 19;
1816 if (line.contains("13x13"))
1817 size = 13;
1818 else if (line.contains("9x 9"))
1819 {
1820 size = 9;
1821 memory_str = QString();
1822 }
1823 if (p1_play_white)
1824 emit signal_suggest(p1, p2, h, k, size);
1825 else
1826 emit signal_suggest(p2, p1, h, k, size);
1827 #endif //FIXME
1828 return;
1829 }
1830 // 9 Match [19x19] in 1 minutes requested with xxxx as White.
1831 // 9 Use <match xxxx B 19 1 10> or <decline xxxx> to respond.
1832 // 9 NMatch requested with yfh2test(B 3 19 60 600 25 0 0 0).
1833 // 9 Use <nmatch yfh2test B 3 19 60 600 25 0 0 0> or <decline yfh2test> to respond.
1834 else if (line.contains("<decline") && line.contains("match"))
1835 {
1836 // false -> not my request: used in mainwin.cpp
1837 line = element(line, 0, "<", ">");
1838 MatchRequest * aMatch = new MatchRequest();
1839 unsigned long flags = 0;
1840 /* All games are rated except for ones in free room on IGS */
1841 if(getCurrentRoom()->name.contains("No rated"))
1842 aMatch->free_rated = FREE;
1843 else
1844 aMatch->free_rated = RATED;
1845 aMatch->opponent = line.section(" ", 1, 1);
1846 if(line.contains("as White"))
1847 aMatch->color_request = MatchRequest::BLACK;
1848 else if(line.contains("as Black"))
1849 aMatch->color_request = MatchRequest::WHITE;
1850 else if(line.section(" ",2,2) == "B")
1851 aMatch->color_request = MatchRequest::BLACK;
1852 else if(line.section(" ",2,2) == "N")
1853 aMatch->color_request = MatchRequest::NIGIRI;
1854 else
1855 aMatch->color_request = MatchRequest::WHITE;
1856 if(line.contains("nmatch"))
1857 {
1858 aMatch->handicap = line.section(" ",3,3).toInt();
1859 aMatch->board_size = line.section(" ",4,4).toInt();
1860 //what kind of time? var name?
1861 //aMatch->maintime = line.section(" ",5,5).toInt();
1862 //aMatch->byoperiodtime = line.section(" ",6,6).toInt();
1863 //aMatch->byoperiods = line.section(" " ,7,7).toInt();
1864 flags = getGameDialogFlags();
1865 aMatch->maintime = line.section(" ",5,5).toInt();
1866 aMatch->periodtime = line.section(" ",6,6).toInt();
1867 aMatch->stones_periods = line.section(" " ,7,7).toInt();
1868 if(aMatch->periodtime < 300)
1869 aMatch->timeSystem = byoyomi;
1870 else
1871 aMatch->timeSystem = canadian;
1872 aMatch->nmatch = true;
1873 }
1874 else
1875 {
1876 aMatch->timeSystem = canadian;
1877 aMatch->board_size = line.section(" ",3,3).toInt();
1878 aMatch->maintime = line.section(" ",4,4).toInt() * 60;
1879 aMatch->periodtime = line.section(" ",5,5).toInt() * 60;
1880 aMatch->stones_periods = 25;
1881 flags |= GDF_CANADIAN;
1882 flags |= GDF_STONES25_FIXED;
1883 aMatch->nmatch = false;
1884 }
1885 PlayerListing * p = getPlayerListingNeverFail(aMatch->opponent);
1886 const PlayerListing * us = getOurListing();
1887 if(us)
1888 {
1889 aMatch->our_name = us->name;
1890 aMatch->our_rank = us->rank;
1891 }
1892 aMatch->their_rank = p->rank;
1893
1894 GameDialog * gameDialog = getGameDialog(p);
1895 gameDialog->recvRequest(aMatch, flags);
1896 delete aMatch;
1897 }
1898 // 9 Match [5] with guest17 in 1 accepted.
1899 // 9 Creating match [5] with guest17.
1900 else if (line.contains("Creating match"))
1901 {
1902 QString nr = element(line, 0, "[", "]");
1903 // maybe there's a blank within the brackets: ...[ 5]...
1904 QString dummy = element(line, 0, "]", ".").trimmed();
1905 QString opp = element(dummy, 1, " ");
1906
1907 // We let the 15 game record message create the board
1908 //GameDialog * gameDialog = connection->getGameDialog(opp);
1909 //gameDialog->closeAndCreate();
1910 //GameDialog * gameDialog =
1911 // connection->getGameDialog(opp);
1912 //MatchRequest * mr = gameDialog->getMatchRequest();
1913 //created_match_request = new MatchRequest(*mr);
1914 //connection->closeGameDialog(opp);
1915 ////emit signal_matchCreate(nr, opp);
1916 // automatic opening of a dialog tab for further conversation
1917 ////emit signal_talk(opp, "", true);
1918 }
1919 else if (line.contains("Match") && line.contains("accepted"))
1920 {
1921 QString nr = element(line, 0, "[", "]");
1922 QString opp = element(line, 3, " ");
1923 ////emit signal_matchCreate(nr, opp);
1924 /* FIXME, this is probably where we should be doing something?
1925 * assuming its reliable */
1926
1927 }
1928 // 9 frosla withdraws the match offer.
1929 // 9 guest17 declines your request for a match.
1930 else if (line.contains("declines your request for a match") ||
1931 line.contains("withdraws the match offer"))
1932 {
1933 QString opp = element(line, 0, " ");
1934 PlayerListing * p = getPlayerListingNeverFail(opp);
1935 GameDialog * gameDialog = getGameDialog(p);
1936 gameDialog->recvRefuseMatch(1);
1937 }
1938 //9 yfh2test declines undo
1939 else if (line.contains("declines undo"))
1940 {
1941 qDebug("%s", line.toLatin1().constData()); //FIXME
1942 // not the cleanest way : we should send this to a message box
1943 //emit signal_kibitz(0, element(line, 0, " "), line);
1944 return;
1945 }
1946
1947 //9 yfh2test left this room
1948 //9 yfh2test entered this room
1949 else if (line.contains("this room"))
1950 {} //emit signal_refresh(10);
1951
1952 //9 Requesting match in 10 min with frosla as Black.
1953 else if (line.contains("Requesting match in"))
1954 {
1955 QString opp = element(line, 6, " ");
1956 //emit signal_opponentopen(opp);
1957 }
1958 // NNGS: 9 Removing @ K8
1959 // IGS: 9 Removing @ B5
1960 // 49 Game 265 qGoDev is removing @ B5
1961 else if (line.contains("emoving @"))
1962 {
1963 /* FIXME DOUBLE CHECK!!! */
1964 /*if (gsName != IGS)
1965 {
1966 QString pt = element(line, 2, " ");
1967 //emit signal_reStones(pt, 0);
1968 }*/
1969 /* 49 handles this for IGS... but what about observed games? */
1970 #ifdef FIXME
1971
1972 if(protocol_save_int < 0)
1973 {
1974 qDebug("Received stone removal message without game in scoring mode");
1975 return;
1976 }
1977 else
1978 {
1979 QString pt = element(line, 2, " ");
1980 handleRemovingAt(protocol_save_int, pt);
1981 // FIXME Set protocol_save_int to -1???
1982 }
1983 #endif //FIXME
1984
1985 }
1986 // 9 You can check your score with the score command, type 'done' when finished.
1987 else if (line.contains("check your score with the score command"))
1988 {
1989 // if (gsName == IGS)
1990 // IGS: store and wait until game number is known
1991 // memory_str = QString("rmv@"); // -> continuation see 15
1992 // else
1993 ////emit signal_restones(0, 0);
1994 ////emit signal_enterScoreMode();
1995 qDebug("Getting boarddispatch from memory: %d", protocol_save_int);
1996 boarddispatch = getIfBoardDispatch(protocol_save_int);
1997 if(boarddispatch)
1998 {
1999 boarddispatch->recvEnterScoreMode();
2000 //connection->protocol_save_string = QString("rmv@");
2001 }
2002 }
2003 // IGS: 9 Board is restored to what it was when you started scoring
2004 else if (line.contains("what it was when you"))
2005 {
2006 //emit signal_restoreScore();
2007 qDebug("Getting boarddispatch from memory: %d", protocol_save_int);
2008 boarddispatch = getIfBoardDispatch(protocol_save_int);
2009 if(boarddispatch)
2010 {
2011 MoveRecord * move = new MoveRecord();
2012 move->flags = MoveRecord::UNDO_TERRITORY;
2013 boarddispatch->recvMove(move);
2014 delete move;
2015 boarddispatch->recvKibitz("", line);
2016 }
2017 }
2018 else if(line.contains("has typed done"))
2019 {
2020 qDebug("Getting boarddispatch from memory: %d", protocol_save_int);
2021 boarddispatch = getIfBoardDispatch(protocol_save_int);
2022 if(boarddispatch)
2023 boarddispatch->recvKibitz("", line);
2024 }
2025 // WING: 9 Use <adjourn> to adjourn, or <decline adjourn> to decline.
2026 else if (line.contains("Use adjourn to") || line.contains("Use <adjourn> to"))
2027 {
2028 ////emit signal_requestDialog("adjourn", "decline adjourn", 0, 0);
2029 boarddispatch = getIfBoardDispatch(match_negotiation_state->getGameId());
2030 if(boarddispatch)
2031 boarddispatch->recvRequestAdjourn();
2032 }
2033 // 9 frosla requests to pause the game.
2034 else if (line.contains("requests to pause"))
2035 {
2036 //emit signal_requestDialog("pause", 0, 0, 0);
2037 }
2038 else if (line.contains("been adjourned"))
2039 {
2040 // re game from list - special case: own game
2041 #ifdef FIXME
2042 aGame->nr = "@";
2043 aGame->running = false;
2044
2045 //emit signal_game(aGame);
2046 #endif //FIXME
2047 /* There's several after the fact server INFO messages about
2048 * games adjourning so we only need to close for one of them
2049 * The 21 message might actually be better FIXME*/
2050 boarddispatch = getIfBoardDispatch(protocol_save_int);
2051 if(boarddispatch)
2052 {
2053 qDebug("adjourning game!!");
2054 boarddispatch->adjournGame();
2055 closeBoardDispatch(protocol_save_int);
2056 }
2057 }
2058 // 9 Game 22: frosla vs frosla has adjourned.
2059 else if (line.contains("has adjourned"))
2060 {
2061 int number = element(line, 0, " ", ":").toInt();
2062 GameListing * aGame = room->getGameListing(number);
2063 aGame->running = false;
2064
2065 // for information
2066 //aGame->Sz = "has adjourned.";
2067
2068 #ifdef OLD
2069 //emit signal_game(aGame);
2070 // //emit signal_(aGame);
2071 #endif //OLD
2072 // No need to get existing listing because
2073 // this is just to falsify the listing
2074 room->recvGameListing(aGame);
2075 /* Also notify board if we're watching */
2076 boarddispatch = getIfBoardDispatch(aGame->number);
2077 if(boarddispatch)
2078 {
2079 boarddispatch->adjournGame();
2080 closeBoardDispatch(aGame->number);
2081 }
2082 }
2083 // 9 Removing game 30 from observation list.
2084 else if (line.contains("from observation list"))
2085 {
2086 // is done from qGoIF
2087 // //emit signal_addToObservationList(-1);
2088 // aGame->nr = element(line, 2, " ").toInt();
2089 // aGame->Sz = "-";
2090 // aGame->running = false;
2091 //emit signal_observedGameClosed(element(line, 2, " ").toInt());
2092 return;
2093 }
2094 // 9 Adding game to observation list.
2095 else if (line.contains("to observation list"))
2096 {
2097 /* Unfortunately, LGS and IGS have no number here, so
2098 * we have to either guess or get it from the observe send
2099 * which is easiest */
2100 getBoardDispatch(protocol_save_int);
2101 protocol_save_int = -1;
2102 return;
2103 }
2104 // 9 Games currently being observed: 31, 36, 45.
2105 else if (line.contains("Games currently being observed"))
2106 {
2107 if (line.contains("None"))
2108 {
2109 //emit signal_addToObservationList(0);
2110 }
2111 else
2112 {
2113 #ifdef FIXME
2114 // don't work correct at IGS!!!
2115 int i = line.count(',');
2116
2117 qDebug(QString("observing %1 games").arg(i+1).toLatin1());
2118 #endif //FIXME
2119 // //emit signal_addToObservationList(i+1);
2120 }
2121
2122 // return IT_OTHER;
2123 }
2124 // 9 1 minutes were added to your opponents clock
2125 else if (line.contains("minutes were added"))
2126 {
2127 //boarddispatch = getIfBoardDispatch(protocol_save_int);
2128 //need to see this in action FIXME addTime
2129 //also check LGS/WING code
2130 int t = element(line, 0, " ").toInt();
2131 boarddispatch = getIfBoardDispatch(match_negotiation_state->getGameId());
2132 if(boarddispatch)
2133 boarddispatch->recvAddTime(t, boarddispatch->getOpponentName());
2134
2135 }
2136 // 9 Your opponent has added 1 minutes to your clock.
2137 else if (line.contains("opponent has added"))
2138 {
2139 int t = element(line, 4, " ").toInt();
2140 boarddispatch = getIfBoardDispatch(match_negotiation_state->getGameId());
2141 if(boarddispatch)
2142 boarddispatch->recvAddTime(t, getUsername());
2143 }
2144 // NNGS: 9 Game clock paused. Use "unpause" to resume.
2145 else if (line.contains("Game clock paused"))
2146 {
2147 #ifdef FIXME
2148 emit signal_timeAdded(-1, true);
2149 #endif //FIXME
2150 }
2151 // NNGS: 9 Game clock resumed.
2152 else if (line.contains("Game clock resumed"))
2153 {
2154 #ifdef FIXME
2155 emit signal_timeAdded(-1, false);
2156 #endif //FIXME
2157 }
2158 // 9 Increase frosla's time by 1 minute
2159 else if (line.contains("s time by"))
2160 {
2161 //not sure how this works if we're observing the game FIXME
2162 int t = element(line, 4, " ").toInt();
2163 boarddispatch = getIfBoardDispatch(match_negotiation_state->getGameId());
2164 if(boarddispatch)
2165 {
2166 if (line.contains(getUsername()))
2167 boarddispatch->recvAddTime(t, getUsername());
2168 else
2169 boarddispatch->recvAddTime(t, boarddispatch->getOpponentName());
2170 }
2171 }
2172 // 9 Setting your . to Banana [text] (idle: 0 minutes)
2173 else if (line.contains("Setting your . to"))
2174 {
2175 QString busy = element(line, 0, "[", "]");
2176 if (!busy.isEmpty())
2177 {
2178 QString player = element(line, 4, " ");
2179 // true = player
2180 ////emit signal_talk(player, "[" + busy + "]", true);
2181 }
2182 }
2183 /* I'm pretty sure, as with LGS and WING, we want to
2184 * update the game lists here but not use 9 messages
2185 * for the board
2186 * 9 message maybe only score with IGS and if we can't
2187 * reliably use 22... because of internal difficulties...*/
2188 else if (line.contains("resigns.") ||
2189 line.contains("adjourned.") ||
2190 //don't intefere with status UNLESS IGS (real solution is to
2191 // interpret status message and ignore here FIXME FIXME)
2192 line.contains(" : W ", Qt::CaseSensitive) ||
2193 line.contains(" : B ", Qt::CaseSensitive) ||
2194 line.contains("forfeits on") ||
2195 line.contains("lost by"))
2196 {
2197 GameResult * aGameResult;
2198 int number = element(line, 0, " ", ":").toInt();
2199 GameListing * aGame = room->getGameListing(number);
2200 aGame->running = false;
2201 // for information
2202 aGame->result = element(line, 4, " ", "}");
2203
2204 boarddispatch = getIfBoardDispatch(aGame->number);
2205 /* FIXME: This shouldn't create a new board if
2206 * we're not watching it.
2207 * Also WING sometimes sends 9 and sometimes sends 21 perhaps
2208 * depending on whether its observed or resign versus score/result
2209 * we don't want to send double messages.*/
2210 if(boarddispatch)
2211 {
2212 aGameResult = new GameResult();
2213
2214 if(line.contains(" : W ", Qt::CaseSensitive) ||
2215 line.contains(" : B ", Qt::CaseSensitive))
2216 {
2217 aGameResult->result = GameResult::SCORE;
2218
2219 int posw, posb;
2220 posw = aGame->result.indexOf("W ");
2221 posb = aGame->result.indexOf("B ");
2222 bool wfirst = posw < posb;
2223 float sc1, sc2;
2224 sc1 = aGame->result.mid(posw+1, posb-posw-2).toFloat();
2225 //sc2 = aGame->result.right(aGame->result.length()-posb-1).toFloat();
2226 /* FIXME This may be right for IGS and wrong for WING */
2227 //sc2 = aGame->result.mid(posb +1, posb + 6).toFloat();
2228 sc2 = element(aGame->result, 4, " ").toFloat();
2229 qDebug("sc1: %f sc2: %f\n", sc1, sc2);
2230 if(sc1 > sc2)
2231 {
2232 aGameResult->winner_score = sc1;
2233 aGameResult->loser_score = sc2;
2234 }
2235 else
2236 {
2237 aGameResult->winner_score = sc2;
2238 aGameResult->loser_score = sc1;
2239
2240 }
2241
2242 if (!wfirst)
2243 {
2244 int h = posw;
2245 posw = posb;
2246 posb = h;
2247 if(sc2 > sc1)
2248
2249 aGameResult->winner_color = stoneWhite;
2250 else
2251 aGameResult->winner_color = stoneBlack;
2252 }
2253 else
2254 {
2255 if(sc1 > sc2)
2256 aGameResult->winner_color = stoneWhite;
2257 else
2258 aGameResult->winner_color = stoneBlack;
2259 }
2260
2261
2262 }
2263 else if(line.contains("forfeits on time"))
2264 {
2265 aGameResult->result = GameResult::TIME;
2266 if(line.contains("Black"))
2267 {
2268 aGameResult->winner_color = stoneWhite;
2269 }
2270 else
2271 {
2272 aGameResult->winner_color = stoneBlack;
2273 }
2274 }
2275 else if(line.contains("resign", Qt::CaseInsensitive))
2276 {
2277 aGameResult->result = GameResult::RESIGN;
2278 if(line.contains("Black"))
2279 {
2280 aGameResult->winner_color = stoneWhite;
2281 }
2282 else
2283 {
2284 aGameResult->winner_color = stoneBlack;
2285 }
2286 }
2287 else if(line.contains("lost by"))
2288 {
2289 aGameResult->result = GameResult::SCORE;
2290 if(line.contains("Black"))
2291 {
2292 aGameResult->winner_color = stoneWhite;
2293 }
2294 else
2295 {
2296 aGameResult->winner_color = stoneBlack;
2297
2298 }
2299
2300 }
2301 }
2302
2303
2304 if (aGame->result.isEmpty())
2305 aGame->result = "-";
2306 else if (aGame->result.indexOf(":") != -1)
2307 aGame->result.remove(0,2);
2308
2309 if(boarddispatch)
2310 {
2311 qDebug("Receiving result!!!");
2312 boarddispatch->recvResult(aGameResult);
2313 /* is kibitzing this here what we want?*/
2314 /* FIXME... should be a shortened list, not the full
2315 * result msg kibitz */
2316 /* More, looks like there is possible
2317 * crash here if we already received a result
2318 * and there's no good way to turn this off.
2319 * FIXME All of the recvResult stuff needs
2320 * to be cleaned up really */
2321 //boarddispatch->recvKibitz("", line);
2322 }
2323 room->recvGameListing(aGame);
2324 return;
2325 }
2326 // NNGS: 9 Messages: (after 'message' cmd)
2327 // 8 File
2328 // <msg>
2329 // 8 File
2330 // 9 Please type "erase" to erase your messages after reading
2331 else if (line.contains("Messages:"))
2332 {
2333 // parse like IGS cmd nr 14: Messages
2334 memory = 14;
2335 }
2336 // IGS: 9 You have a message. To read your messages, type: message
2337 // NNGS: 9 You have 1 messages. Type "messages" to display them
2338 else if (line.contains("You have") && line.contains("messages"))
2339 {
2340 // re cmd nr
2341 line = line.trimmed();
2342 line = line.remove(0, 2);
2343 getConsoleDispatch()->recvText(line.toLatin1().constData());
2344 return;
2345 }
2346 // 9 Observing game 2 (chmeng vs. myao) :
2347 // 9 shanghai 9k* henry 15k
2348 // 9 Found 2 observers.
2349 else if (line.contains("Observing game ", Qt::CaseSensitive))
2350 {
2351 // right now: only need for observers of teaching game
2352 // game number
2353 bool ok;
2354 memory = element(line, 2, " ").toInt(&ok);
2355 if (ok)
2356 {
2357 memory_str = "observe";
2358 // FIXME
2359 //emit signal_clearObservers(memory);
2360 BoardDispatch * boarddispatch = getIfBoardDispatch(memory);
2361 if(boarddispatch)
2362 {
2363 GameData * g = boarddispatch->getGameData();
2364 g->white_name = element(line, 0, "(", " ");
2365 g->black_name = element(line, 4, " ", ")");
2366 boarddispatch->gameDataChanged();
2367 }
2368 return;
2369 }
2370 }
2371 else if (!memory_str.isEmpty() && memory_str == "observe" && line.contains("."))
2372 {
2373 // QString cnt = element(line, 1, " ");
2374 // //emit signal_kibitz(memory, "00", "");
2375
2376 memory = 0;
2377 memory_str = QString();
2378
2379 return;
2380 }
2381 else if (!memory_str.isEmpty() && memory_str == "observe")
2382 {
2383 QString name = element(line, 0, " ");
2384 QString rank;
2385 boarddispatch = getBoardDispatch(memory);
2386 if(!boarddispatch)
2387 {
2388 qDebug("No boarddispatch for observer list\n");
2389 return;
2390 }
2391
2392 for (int i = 1; ! name.isEmpty(); i++)
2393 {
2394 rank = element(line, i, " ");
2395 fixRankString(&rank);
2396 // send as kibitz from "0"
2397 PlayerListing * p = getPlayerListingNeverFail(name);
2398 boarddispatch->recvObserver(p, true);
2399 name = element(line, ++i , " ");
2400 }
2401
2402 return;
2403 }
2404 else if(line.contains("Found") && line.contains("observers"))
2405 {
2406 memory = 0;
2407 memory_str = QString();
2408 }
2409 else if (line.contains("****") && line.contains("Players"))
2410 {
2411 return;
2412 }
2413 // 9 qGoDev has resigned the game.
2414 else if (line.contains("has resigned the game"))
2415 {
2416 if(!protocol_save_int)
2417 {
2418 qDebug("no memory for resign game message");
2419 return;
2420 }
2421 /* If this is our game, there's a 21 message maybe later that
2422 * should be handling it FIXME */
2423 boarddispatch = getIfBoardDispatch(protocol_save_int);
2424 if(!boarddispatch)
2425 {
2426 qDebug("No board dispatch for \"resigned the game\"\n");
2427 return;
2428 }
2429 #ifdef FIXME
2430 aGame->running = false;
2431 #endif //FIXME
2432
2433 GameResult * aGameResult = new GameResult();
2434 aGameResult->loser_name = element(line, 0, " ");
2435 aGameResult->result = GameResult::RESIGN;
2436 /* We need to set unknown/none color here */
2437 boarddispatch->recvResult(aGameResult);
2438 return;
2439
2440 }
2441 else if (line.contains("has run out of time"))
2442 {
2443 boarddispatch = getBoardDispatch(protocol_save_int);
2444 if(!boarddispatch)
2445 {
2446 qDebug("No board dispatch for \"has run out of time\"\n");
2447 return;
2448 }
2449 #ifdef FIXME
2450 aGame->running = false;
2451 #endif //FIXME
2452
2453 GameResult * aGameResult = new GameResult();
2454 aGameResult->loser_name = element(line, 0, " ");
2455 aGameResult->result = GameResult::TIME;
2456 /* We need to set unknown/none color here */
2457 boarddispatch->recvResult(aGameResult);
2458 return;
2459
2460 }
2461
2462
2463
2464 //9 Player: yfh2
2465 //9 Game: go (1)
2466 //9 Language: default
2467 //9 Rating: 6k* 23
2468 //9 Rated Games: 21
2469 //9 Rank: 8k 21
2470 //9 Wins: 13
2471 //9 Losses: 16
2472 //9 Idle Time: (On server) 0s
2473 //9 Address: [email protected]
2474 //9 Country: France
2475 //9 Reg date: Tue Nov 18 04:01:05 2003
2476 //9 Info: yfh2
2477 //9 Defaults (help defs): time 0, size 0, byo-yomi time 0, byo-yomi stones 0
2478 //9 Verbose Bell Quiet Shout Automail Open Looking Client Kibitz Chatter
2479 //9 Off On On On On Off Off On On On
2480
2481 else if (line.contains("Player:"))
2482 {
2483 //statsPlayer = new PlayerListing();
2484 QString name = element(line, 1, " ");
2485 statsPlayer = getPlayerListingNeverFail(name);
2486 #ifdef FIXME
2487 /* So this would have cleared the structure, but
2488 * we're just creating a new empty object later.
2489 * One HUGE FIXME question is what we might be
2490 * overriding on the other side. Maybe we need
2491 * a bit vector specifying what has updated when
2492 * the message is passed. So the notion before
2493 * was that the structure was held until
2494 * completely filled and then passed up*/
2495
2496 statsPlayer->extInfo = "";
2497 statsPlayer->won = "";
2498 statsPlayer->lost = "";
2499 statsPlayer->country = "";
2500 statsPlayer->nmatch_settings = "";
2501 statsPlayer->rank = "";
2502 statsPlayer->info = "";
2503 statsPlayer->address = "";
2504 statsPlayer->play_str = "";
2505 statsPlayer->obs_str = "";
2506 statsPlayer->idle = "";
2507 statsPlayer->rated = "";
2508 // not sure it is the best way : above code seem to make use of "signal"
2509 // but we don't need this apparently for handling stats
2510 #endif //FIXME
2511 protocol_save_string = "STATS";
2512 return;
2513 }
2514
2515 else if (line.contains("Address:"))
2516 {
2517 statsPlayer->email_address = element(line, 1, " ");
2518 return;
2519 }
2520
2521 else if (line.contains("Last Access"))
2522 {
2523 statsPlayer->idletime = element(line, 4, " ")+ " " + element(line, 5, " ")+" " + element(line, 6, " ");
2524 statsPlayer->seconds_idle = idleTimeToSeconds(statsPlayer->idletime);
2525 return;
2526 }
2527
2528 else if (line.contains("Rating:"))
2529 {
2530 statsPlayer->rank = element(line, 1, " ");
2531 fixRankString(&(statsPlayer->rank));
2532 statsPlayer->rank_score = rankToScore(statsPlayer->rank);
2533 return;
2534 }
2535
2536 else if (line.contains("Wins:"))
2537 {
2538 statsPlayer->wins = element(line, 1, " ").toInt();
2539 return;
2540 }
2541
2542 else if (line.contains("Losses:"))
2543 {
2544 statsPlayer->losses = element(line, 1, " ").toInt();
2545 return;
2546 }
2547
2548 else if ((line.contains("Country:"))||(line.contains("From:"))) //IGS || LGS
2549 {
2550 statsPlayer->country = element(line, 0, " ","EOL");
2551 return;
2552 }
2553
2554 else if (line.contains("Defaults")) //IGS
2555 {
2556 statsPlayer->extInfo = element(line, 2, " ","EOL");
2557 Talk * talk = getDefaultRoom()->getTalk(statsPlayer);
2558 if(talk)
2559 talk->updatePlayerListing();
2560 statsPlayer = 0;
2561 return;
2562 }
2563 else if (line.contains("User Level:")) //LGS
2564 {
2565 statsPlayer->extInfo = line;
2566 return;
2567 }
2568
2569
2570 else if ((line.contains("Info:"))&& !(line.contains("Rank Info:")))
2571 {
2572 #ifdef FIXME
2573 if (! statsPlayer->info.isEmpty())
2574 statsPlayer->info.append("\n");
2575 statsPlayer->info.append(line);
2576 //emit signal_statsPlayer(statsPlayer);
2577 #endif //FIXME
2578 return;
2579 }
2580
2581 else if (line.contains("Playing in game:")) //IGS and LGS
2582 {
2583 statsPlayer->playing = element(line, 3, " ").toInt();
2584
2585 return;
2586 }
2587 else if (line.contains("Rated Games:"))
2588 {
2589 statsPlayer->rated_games = element(line, 2, " ").toInt();
2590 return;
2591 }
2592 else if (line.contains("Idle Time:"))
2593 {
2594 statsPlayer->idletime = element(line, 4, " ");
2595 statsPlayer->seconds_idle = idleTimeToSeconds(statsPlayer->idletime);
2596 return;
2597 }
2598
2599 else if (line.contains("Last Access"))
2600 {
2601 statsPlayer->idletime = "not on";
2602 return;
2603 }
2604
2605 else if (line.left(5) == "19x19") //LGS syntax
2606 {
2607 statsPlayer->rank = element(line, 4, " ");
2608 fixRankString(&(statsPlayer->rank));
2609 statsPlayer->rank_score = rankToScore(statsPlayer->rank);
2610 statsPlayer->rated_games = element(line, 7, " ").toInt();
2611 return;
2612 }
2613
2614 else if (line.contains("Wins/Losses")) //LGS syntax
2615 {
2616 statsPlayer->wins = element(line, 2, " ").toInt();
2617 statsPlayer->losses = element(line, 4, " ").toInt();
2618 return;
2619 }
2620 else if(line.contains("Off") || line.contains("On"))
2621 {
2622 /* Scratch this, we're claiming "Defaults" is the last line */
2623 /*Talk * talk = connection->getTalk(statsPlayer->name);
2624 if(talk)
2625 talk->recvPlayerListing(statsPlayer);
2626 //talk->recvTalk(e2);
2627
2628 delete statsPlayer;
2629 statsPlayer = 0;*/
2630 }
2631
2632 //9 ROOM 01: FREE GAME ROOM;PSMNYACF;自由対局室;19;1;10;1,19,60,600,30,25,10,60,0
2633 //9 ROOM 10: SLOW GAME ROOM;PSMNYACF;スロー対局室;19;1;20
2634 //9 ROOM 92: PANDA OPEN ROOM;X;月例大会;19;10;15
2635 else if (! line.left(5).compare("ROOM "))
2636 {
2637 QString room = element(line, 0, " ",";");
2638 RoomListing * r = new RoomListing(room.section(":", 0, 0).toInt(),
2639 room.section(": ", 1, 1));
2640 r->locked = (element(line, 1,";")=="X") || (element(line, 1,";")=="P");
2641 recvRoomListing(r);
2642 //dont delete r
2643 return;
2644 }
2645 else if(line.contains("File"))
2646 return;
2647 if (protocol_save_string != "STATS") //FIXME
2648 getConsoleDispatch()->recvText(line.toLatin1().constData());
2649 }
2650
2651 // 11 Kibitz Achim [ 3d*]: Game TELRUZU vs Anacci [379]
2652 // 11 will B resign?
2653 //case 11:
2654 void IGSConnection::handle_kibitz(QString line)
2655 {
2656 static QString memory_str;
2657 static int memory = 0;
2658 line = line.remove(0, 2).trimmed();
2659 BoardDispatch * boarddispatch;
2660 if (line.contains("Kibitz"))
2661 {
2662 // who is kibitzer
2663 memory_str = element(line, 0, " ", ":");
2664 // game number
2665 memory = element(line, 1, "[", "]").toInt();
2666 }
2667 else
2668 {
2669 if (memory_str.isEmpty())
2670 // something went wrong...
2671 return;
2672
2673 //emit signal_kibitz(memory, memory_str, line);
2674 boarddispatch = getBoardDispatch(memory);
2675 if(boarddispatch)
2676 boarddispatch->recvKibitz(memory_str, line);
2677 memory = 0;
2678 memory_str.clear();
2679 }
2680 }
2681
2682 // messages
2683 // 14 File
2684 // frosla 11/15/02 14:00: Hallo
2685 // 14 File
2686 void IGSConnection::handle_messages(QString line)
2687 {
2688 }
2689
2690 // MOVE
2691 // 15 Game 43 I: xxxx (4 223 16) vs yyyy (5 59 13)
2692 // 15 TIME:21:lowlow(W): 1 0/60 479/600 14/25 0/0 0/0 0/0
2693 // 15 TIME:442:MIYASAN(W): 1 0/60 18/30 0/1 10/10 0/60 0/0
2694 // 15 144(B): B12
2695 // IGS: teaching game:
2696 // 15 Game 167 I: qGoDev (0 0 -1) vs qGoDev (0 0 -1)
2697 void IGSConnection::handle_move(QString line)
2698 {
2699 BoardDispatch * boarddispatch;
2700 //case 15:
2701 qDebug() << "IGSConnection::handle_move():" << line;
2702 line = line.remove(0, 2).trimmed();
2703 static bool need_time = false;
2704 int number;
2705 QString white, black;
2706 if (line.contains("Game"))
2707 {
2708 number = element(line, 1, " ").toInt();
2709 white = element(line, 3, " ");
2710 black = element(line, 8, " ");
2711 /* Check if we're reloading the game */
2712 /* Other problem, with IGS, this might be all we get
2713 * for a restarted game Is this okay here? Maybe doesn't
2714 * always get called? It could have a listing already.*/
2715 boarddispatch = getIfBoardDispatch(number);
2716 if(!boarddispatch)
2717 {
2718
2719 if(white == getUsername() || black == getUsername())
2720 {
2721 if(protocol_save_string == white || protocol_save_string == black)
2722 {
2723 qDebug("starting to restore");
2724 requestGameStats(number);
2725 /* Stats will pick up the info and create the board, otherwise
2726 * boardsize can get lost, etc. */
2727 protocol_save_string = "restoring";
2728 protocol_save_int = number;
2729 game_were_playing = number;
2730 }
2731 else
2732 {
2733 /* accept? */
2734 protocol_save_string = QString();
2735 /* This code should be unified from that from the other case, msg 9
2736 * that does this same thing, maybe the 9 should be removed.. FIXME */
2737 QString opp = (white == getUsername() ? black : white);
2738 if((white == getUsername() || black == getUsername()) && !getBoardFromOurOpponent(opp))
2739 {
2740 PlayerListing * p = getPlayerListingNeverFail(opp);
2741 MatchRequest * mr = getAndCloseGameDialog(p);
2742 if(mr)
2743 {
2744 boarddispatch = getBoardDispatch(number);
2745 GameData * aGameData = boarddispatch->getGameData();
2746
2747 aGameData->number = number;
2748 aGameData->white_name = white;
2749 aGameData->black_name = black;
2750 aGameData->board_size = mr->board_size;
2751 aGameData->komi = mr->komi;
2752 aGameData->handicap = mr->handicap;
2753 aGameData->maintime = mr->maintime;
2754 aGameData->periodtime = mr->periodtime;
2755 aGameData->stones_periods = mr->stones_periods;
2756 aGameData->undoAllowed = true;
2757 if(aGameData->white_name == getUsername())
2758 {
2759 aGameData->white_rank = mr->our_rank;
2760 aGameData->black_rank = mr->their_rank;
2761 }
2762 else
2763 {
2764 aGameData->white_rank = mr->their_rank;
2765 aGameData->black_rank = mr->our_rank;
2766 }
2767 boarddispatch->openBoard();
2768 }
2769 else
2770 {
2771 PlayerListing * blackPlayer = getPlayerListingNeverFail(black);
2772 PlayerListing * whitePlayer = getPlayerListingNeverFail(white);
2773 boarddispatch = getBoardDispatch(number);
2774 GameData * aGameData = boarddispatch->getGameData();
2775 aGameData->number = number;
2776 aGameData->white_name = white;
2777 aGameData->black_name = black;
2778 aGameData->black_rank = blackPlayer->rank;
2779 aGameData->white_rank = whitePlayer->rank;
2780 aGameData->undoAllowed = true;
2781 // Could be from seek opponent !!!
2782
2783 boarddispatch->openBoard();
2784 need_time = true;
2785 }
2786 protocol_save_int = number;
2787 game_were_playing = number;
2788 }
2789 /* We probably just accepted a match */
2790
2791 }
2792 match_negotiation_state->setGameId(number);
2793 //requestGameInfo(game_number);
2794 return;
2795 }
2796 else
2797 {
2798 /* In order to allow observing by console
2799 * This all needs to be cleaned up,
2800 * FIXME, like the above should be moved below, I think
2801 * we only added it haphazardly because we were half working
2802 * with the old code and half making allowances for things
2803 * like restores.*/
2804 /* One FIXME major issue though, probably why
2805 * we added that thing to sendObserve in the first
2806 * place is because if we do this here, then closing
2807 * a board isn't enough, if it hits at the wrong
2808 * time, a move can pop the board back open again
2809 * and then nothing more. */
2810 if(match_negotiation_state->sentJoinRoom()) //so no observing by console
2811 boarddispatch = getBoardDispatch(number);
2812 }
2813 //return;
2814 }
2815
2816 GameData * aGameData = boarddispatch->getGameData();
2817 aGameData->number = number;
2818
2819 protocol_save_int = number;
2820 //aGameData->type = element(line, 1, " ", ":");
2821
2822 aGameData->white_prisoners = element(line, 0, "(", " ").toInt();
2823 wtime->time = element(line, 5, " ").toInt();
2824 wtime->stones_periods = element(line, 5, " ", ")").toInt();
2825
2826 aGameData->black_prisoners = element(line, 1, "(", " ").toInt();
2827 btime->time = element(line, 10, " ").toInt();
2828 btime->stones_periods = element(line, 10, " ", ")").toInt();
2829 /* FIXME Doublecheck in WING */
2830 /* Is this a new game?
2831 * This is this ugly, convoluted way of checking, but I guess
2832 * it gets around the IGS protocol, maybe I'll think of another way later.
2833 * We can use the game listing for the s later, but here, there may
2834 * not be a game listing if its a new game. This may be the listing, in
2835 * a sense. */
2836 /* Another issue here is that we do not want to call this when
2837 * restoring a game since there really isn't a gamedialog... FIXME */
2838 //openBoard obviously has to be done first
2839 boarddispatch->openBoard();
2840 boarddispatch->recvTime(*wtime, *btime);
2841 boarddispatch->gameDataChanged();
2842 }
2843 else if (line.contains("TIME"))
2844 {
2845 number = element(line, 0, ":",":").toInt();
2846 // FIXME Does WING have these messages???
2847 // Might not need game record here!!!
2848
2849 boarddispatch = getIfBoardDispatch(number);
2850 if(!boarddispatch)
2851 {
2852 qDebug("TIME message received but no board");
2853 return;
2854 }
2855 protocol_save_int = number;
2856
2857 GameData * aGameData = boarddispatch->getGameData();
2858 #ifdef FIXME
2859 aGameData->mv_col = "T";
2860 #endif //FIXME
2861 aGameData->number = number;
2862 QString time1 = element(line, 1, " ","/");
2863 QString time2 = element(line, 2, " ","/");
2864 QString stones = element(line, 3, " ","/");
2865
2866 if(need_time)
2867 {
2868 need_time = false;
2869 /* This is if we get here from a seek game FIXME
2870 * it would obviously make sense to standardize this
2871 * somehow but the protocol isn't too consistent
2872 * not to mention the official client having
2873 * occasional bugs so... */
2874 if(1) //FIXME when you see a byoyomi game here
2875 {
2876 aGameData->timeSystem = canadian;
2877 aGameData->maintime = time1.toInt();
2878 aGameData->periodtime = time2.toInt();
2879 aGameData->stones_periods = stones.toInt();
2880 }
2881 else
2882 {
2883 aGameData->timeSystem = byoyomi;
2884 }
2885 }
2886 if (line.contains("(W)"))
2887 {
2888 aGameData->white_name = element(line, 1, ":","(");
2889 wtime->time = (time1.toInt()==0 ? time2 : time1).toInt();
2890 wtime->stones_periods = (time1.toInt()==0 ?stones: "-1").toInt();
2891 }
2892 else if (line.contains("(B)"))
2893 {
2894 aGameData->black_name = element(line, 1, ":","(");
2895 btime->time = (time1.toInt()==0 ? time2 : time1).toInt();
2896 btime->stones_periods = (time1.toInt()==0 ? stones:"-1").toInt();
2897 /* IGS seems to do W and then B, each time */
2898 boarddispatch->recvTime(*wtime, *btime);
2899 }
2900 else //never know with IGS ...
2901 return;
2902
2903 //boarddispatch->recvTime(*wtime, *btime);
2904 //gameDataChanged necessary if above white_name, black_name is issue
2905 //FIXME
2906 //boarddispatch->gameDataChanged();
2907 }
2908 else if (line.contains("GAMERPROPS"))
2909 {
2910 QStringList words = line.split(QRegExp("[ :]"), QString::SkipEmptyParts);
2911 GameData * gd;
2912 int game_number = words[2].toInt();
2913
2914 BoardDispatch * boarddispatch = getIfBoardDispatch(game_number);
2915 if(boarddispatch)
2916 {
2917 gd = boarddispatch->getGameData();
2918 /* We need this for komi in our own games sometimes it seems
2919 * or do we... */
2920 //GAMERPROPS:86: 9 0 6.50
2921 gd->board_size = words[3].toInt();
2922 gd->handicap = words[4].toInt();
2923 gd->komi = words[5].toFloat();
2924 }
2925 return;
2926 }
2927 else if(protocol_save_int < 0)
2928 {
2929 qDebug("Ignoring boardless move");
2930 return;
2931 }
2932 else
2933 {
2934 boarddispatch = getIfBoardDispatch(protocol_save_int);
2935 if(!boarddispatch)
2936 {
2937 qDebug("Possible move messages received but no board dispatch");
2938 return;
2939 }
2940 /* If we're getting moves that we can use, then
2941 * they have some existing board, which means a boardrecord
2942 * which should be more reliable than the listing */
2943 GameData * r = boarddispatch->getGameData();
2944 //GameListing * l = room->getGameListing(protocol_save_int);
2945 if(!r)
2946 {
2947 qDebug("Move for unlisted game");
2948 return;
2949 }
2950
2951 /* Not sure what undos look like anymore FIXME */
2952
2953 /* If there's multiple moves on a line, the ones
2954 * after the first are captures */
2955 MoveRecord * aMove = new MoveRecord();
2956 aMove->flags = MoveRecord::NONE;
2957 aMove->number = element(line, 0, "(").toInt();
2958 QString point = element(line, 0, " ", "EOL");
2959 if(aMove->number == 0)
2960 r->move_list_received = true;
2961 else if(!r->move_list_received)
2962 {
2963 delete aMove;
2964 return;
2965 }
2966 if(point.contains("Handicap", Qt::CaseInsensitive))
2967 {
2968 /* As long as handicap is
2969 * set in game data... actually
2970 * this is useful since sending
2971 * 0 sets tree properly*/
2972 aMove->flags = MoveRecord::HANDICAP;
2973 aMove->x = element(line, 1, " ", "EOL").toInt();
2974 qDebug("handicap %d", aMove->x);
2975 /*if(aMove->x != 0)
2976 {
2977 GameData * r = boarddispatch->getGameData();
2978 r->handicap = aMove->x;
2979 boarddispatch->gameDataChanged();
2980 }*/
2981 /* Don't want to set this here because it will
2982 * prevent the handicap from being set here.
2983 * something in boardwindow, etc., code has to
2984 * prevent double setting the handicap so its
2985 * !handicap*/
2986 }
2987 else if(point.contains("Pass", Qt::CaseInsensitive))
2988 {
2989 aMove->flags = MoveRecord::PASS;
2990 }
2991 else
2992 {
2993 if(!r->handicap)
2994 aMove->number++;
2995 //qDebug("board size from record: %d", r->board_size);
2996 aMove->x = (int)(point.toLatin1().at(0));
2997 aMove->x -= 'A';
2998 point.remove(0,1);
2999 //qDebug("move number: %d\n", aMove->number);
3000 aMove->y = element(point, 0, " ").toInt();
3001
3002 if(aMove->x < 9) //no I on IGS
3003 aMove->x++;
3004 //if(l->board_size > 9)
3005 //{
3006 aMove->y = r->board_size + 1 - aMove->y;
3007 //}
3008 //qDebug("%d %d\n", aMove->x, aMove->y);
3009 if(element(line, 0, "(", ")") == "W")
3010 aMove->color = stoneWhite;
3011 else
3012 aMove->color = stoneBlack;
3013 }
3014 /* Any other color options ??? */
3015 boarddispatch->recvMove(aMove);
3016 delete aMove;
3017 return;
3018 }
3019 //ugly, redundant but IGS doesn't give us a good way to know when
3020 //to do this otherwise
3021 //if(boarddispatch)
3022 // boarddispatch->openBoard();
3023 }
3024 // SAY
3025 // 19 *xxxx*: whish you a nice game :)
3026 // NNGS - new:
3027 // 19 --> frosla hallo
3028 void IGSConnection::handle_say(QString line)
3029 {
3030 BoardDispatch * boarddispatch;
3031 line = line.remove(0, 2).trimmed();
3032 //case 19:
3033 // if (line.contains("-->"))
3034 // //emit signal_kibitz(0, 0, element(line, 1, " ", "EOL"));
3035 // else
3036 boarddispatch = getBoardFromOurOpponent(element(line, 0, "*", "*"));
3037 if(boarddispatch)
3038 boarddispatch->recvKibitz(element(line, 0, "*", "*"), element(line, 0, ":", "EOL"));
3039 else
3040 qDebug("Received kibitz without board for player");
3041
3042 }
3043
3044 /* This is tricky but since 21 reports game status anyway,
3045 * for observed and played, we don't really need 20. */
3046 void IGSConnection::handle_score_m(QString line)
3047 {
3048 //20 yfh2 (W:O): 4.5 to NaiWei (B:#): 4.0
3049 //case 20:
3050 //qDebug("Ignoring IGS_score_m message");
3051 //return;
3052 /* we seem to need this message, especially if its our game */
3053 GameResult * aGameResult = new GameResult();
3054 BoardDispatch * boarddispatch;
3055 bool firstname;
3056 // aGame->nr = "@";
3057 // aGame->running = false;
3058 QString res;
3059 line = line.remove(0, 2).trimmed();
3060 QString player = element(line, 0, " ");
3061 if (player == getUsername())
3062 {
3063 player = element(line, 4, " ");
3064 firstname = 1;
3065 }
3066 else
3067 firstname = 0;
3068 aGameResult->winner_name = player;
3069 aGameResult->result = GameResult::SCORE;
3070 boarddispatch = getBoardFromOurOpponent(player);
3071 if(!boarddispatch)
3072 {
3073 qDebug("Can't find board for result message!\n");
3074 return;
3075 }
3076
3077 aGameResult->winner_score = element(line, 2, " ").toFloat();
3078 aGameResult->loser_score = element(line, 6, " ").toFloat();
3079 if(aGameResult->winner_score > aGameResult->loser_score)
3080 {
3081 aGameResult->winner_color = stoneWhite;
3082 if(firstname)
3083 aGameResult->winner_name = getUsername();
3084 }
3085 else
3086 {
3087 float temp = aGameResult->winner_score;
3088 aGameResult->winner_score = aGameResult->loser_score;
3089 aGameResult->loser_score = temp;
3090 aGameResult->winner_color = stoneBlack;
3091 if(!firstname)
3092 aGameResult->winner_name = getUsername();
3093 }
3094 boarddispatch->recvResult(aGameResult);
3095 game_were_playing = 0;
3096 delete aGameResult;
3097 }
3098
3099 /* Note that we get shouts on games and players in other rooms,
3100 * either that, or only the lobby. Find out which. */
3101 // SHOUT - a info from the server
3102 //case 21:
3103 void IGSConnection::handle_shout(QString line)
3104 {
3105 Room * room = getDefaultRoom();
3106 BoardDispatch * boarddispatch;
3107 line = line.remove(0, 2).trimmed();
3108
3109 // case sensitive
3110 if (line.contains(" connected.}"))
3111 {
3112 // {guest1381 [NR ] has connected.}
3113 //line.replace(QRegExp(" "), "");
3114 QString name = element(line, 0, "{", " ");
3115 PlayerListing * aPlayer = room->getPlayerListing(name);
3116 aPlayer->rank = element(line, 0, "[", "]", true);
3117 fixRankString(&(aPlayer->rank));
3118 aPlayer->rank_score = rankToScore(aPlayer->rank);
3119 aPlayer->info = "??";
3120 aPlayer->playing = 0;
3121 aPlayer->observing = 0;
3122 aPlayer->idletime = "-";
3123 aPlayer->online = true;
3124 emit playerListingReceived(aPlayer);
3125 return;
3126 }
3127 else if (line.contains("has disconnected"))
3128 {
3129 // {xxxx has disconnected}
3130 PlayerListing * aPlayer = room->getPlayerListing(element(line, 0, "{", " "));
3131 aPlayer->online = false;
3132 emit playerListingReceived(aPlayer);
3133 return;
3134 }
3135 else if (line.contains("{Game"))
3136 {
3137 // {Game 198: xxxx1 vs xxxx2 @ Move 33}
3138 if (line.contains("@"))
3139 {
3140 /* FIXME: This may mean that an old game was just restarted
3141 * and so we might want to set it up here */
3142 /* We also get this message at weird times that makes
3143 * boards we're not watching pop up!! FIXME */
3144 qDebug() << "IGSConnection::handle_shout(" << line << ")";
3145
3146 int game_number = element(line, 0, " ", ":").toInt();
3147 QString white = element(line, 2, " ");
3148 QString black = element(line, 4, " ");
3149
3150 if(white == getUsername() || black == getUsername())
3151 {
3152 if(protocol_save_string == white || protocol_save_string == black)
3153 {
3154 /* Then this is a restarted game, create
3155 * board dispatch */
3156 //getBoardDispatch(game_number);
3157
3158 //boarddispatch = getBoardDispatch(aGame->nr);
3159 //boarddispatch->recvGameListing(aGame);
3160 requestGameStats(game_number);
3161 /* Stats will pick up the info and create the board, otherwise
3162 * boardsize can get lost, etc. */
3163 protocol_save_string = "restoring";
3164 }
3165 else
3166 protocol_save_string = QString();
3167 //requestGameInfo(game_number);
3168 }
3169 return;
3170 }
3171
3172 // {Game 155: xxxx vs yyyy has adjourned.}
3173 // {Game 76: xxxx vs yyyy : W 62.5 B 93.0}
3174 // {Game 173: xxxx vs yyyy : White forfeits on time.}
3175 // {Game 116: xxxx17 vs yyyy49 : Black resigns.}
3176 // IGS:
3177 // 21 {Game 124: Redmond* vs NaiWei* : Black lost by Resign}
3178 // 21 {Game 184: Redmond* vs NaiWei* : White lost by 1.0}
3179 /* This results in double kibitz on LGS, maybe we drop it
3180 * altogether? */
3181 if (line.contains("resigns.") ||
3182 line.contains("adjourned.") ||
3183 line.contains(" : W ", Qt::CaseSensitive) ||
3184 line.contains(" : B ", Qt::CaseSensitive) ||
3185 line.contains("forfeits on") ||
3186 line.contains("lost by"))
3187 {
3188 GameResult * aGameResult;
3189 int number = element(line, 0, " ", ":").toInt();
3190 GameListing * aGame = room->getGameListing(number);
3191 aGame->running = false;
3192 // for information
3193 aGame->result = element(line, 4, " ", "}");
3194
3195 boarddispatch = getIfBoardDispatch(aGame->number);
3196 if(boarddispatch)
3197 {
3198 aGameResult = new GameResult();
3199
3200 if(line.contains(" : W ", Qt::CaseSensitive) ||
3201 line.contains(" : B ", Qt::CaseSensitive))
3202 {
3203 aGameResult->result = GameResult::SCORE;
3204
3205 int posw, posb;
3206 posw = aGame->result.indexOf("W ");
3207 posb = aGame->result.indexOf("B ");
3208 bool wfirst = posw < posb;
3209 float sc1, sc2;
3210 sc1 = aGame->result.mid(posw+1, posb-posw-2).toFloat();
3211 //sc2 = aGame->result.right(aGame->result.length()-posb-1).toFloat();
3212 /* FIXME This may be right for IGS and wrong for WING */
3213 //sc2 = aGame->result.mid(posb +1, posb + 6).toFloat();
3214 sc2 = element(aGame->result, 4, " ").toFloat();
3215 qDebug("sc1: %f sc2: %f\n", sc1, sc2);
3216 if(sc1 > sc2)
3217 {
3218 aGameResult->winner_score = sc1;
3219 aGameResult->loser_score = sc2;
3220 }
3221 else
3222 {
3223 aGameResult->winner_score = sc2;
3224 aGameResult->loser_score = sc1;
3225
3226 }
3227
3228 if (!wfirst)
3229 {
3230 int h = posw;
3231 posw = posb;
3232 posb = h;
3233 if(sc2 > sc1)
3234
3235 aGameResult->winner_color = stoneWhite;
3236 else
3237 aGameResult->winner_color = stoneBlack;
3238 }
3239 else
3240 {
3241 if(sc1 > sc2)
3242 aGameResult->winner_color = stoneWhite;
3243 else
3244 aGameResult->winner_color = stoneBlack;
3245 }
3246 }
3247 else if(line.contains("forfeits on time"))
3248 {
3249 aGameResult->result = GameResult::TIME;
3250 if(line.contains("Black"))
3251 {
3252 aGameResult->winner_color = stoneWhite;
3253 }
3254 else
3255 {
3256 aGameResult->winner_color = stoneBlack;
3257 }
3258 }
3259 else if(line.contains("resign", Qt::CaseInsensitive))
3260 {
3261 aGameResult->result = GameResult::RESIGN;
3262 if(line.contains("Black"))
3263 {
3264 aGameResult->winner_color = stoneWhite;
3265 }
3266 else
3267 {
3268 aGameResult->winner_color = stoneBlack;
3269 }
3270 }
3271 else if(line.contains("lost by"))
3272 {
3273 aGameResult->result = GameResult::SCORE;
3274 if(line.contains("Black"))
3275 {
3276 aGameResult->winner_color = stoneWhite;
3277 }
3278 else
3279 {
3280 aGameResult->winner_color = stoneBlack;
3281
3282 }
3283
3284 }
3285 else if(line.contains("adjourned"))
3286 {
3287 /* A little ugly to have this and return here
3288 * but if it works with protocol... */
3289 boarddispatch->adjournGame();
3290 closeBoardDispatch(aGame->number);
3291 delete aGameResult;
3292 return;
3293 }
3294 }
3295
3296
3297 if (aGame->result.isEmpty())
3298 aGame->result = "-";
3299 else if (aGame->result.indexOf(":") != -1)
3300 aGame->result.remove(0,2);
3301
3302 if(boarddispatch)
3303 {
3304 qDebug("Receiving 21 result!!!");
3305 /* This can be redundant with 21 */
3306 boarddispatch->recvResult(aGameResult);
3307 delete aGameResult; //conditional on boarddispatch!
3308 /* is kibitzing this here what we want?*/
3309 boarddispatch->recvKibitz("", line);
3310 }
3311 room->recvGameListing(aGame);
3312 return;
3313 }
3314 }
3315 else if (line.contains("{Match"))
3316 {
3317 // {Match 116: xxxx [19k*] vs. yyyy1 [18k*] }
3318 // {116:xxxx[19k*]yyyy1[18k*]}
3319 // WING: {Match 41: o4641 [10k*] vs. Urashima [11k*] H:2 Komi:3.5}
3320 line.replace(QRegExp("vs. "), "");
3321 line.replace(QRegExp("Match "), "");
3322 line.replace(QRegExp(" "), "");
3323 int number = element(line, 0, "{", ":").toInt();
3324
3325 GameListing * aGame = room->getGameListing(number);
3326 /* No reason to wait for player listing since info is here */
3327 QString name = element(line, 0, ":", "[");
3328 aGame->white = getPlayerListingNeverFail(name);
3329 aGame->_white_name = element(line, 0, ":", "[");
3330 aGame->_white_rank = element(line, 0, "[", "]");
3331 fixRankString(&(aGame->_white_rank));
3332 aGame->_white_rank_score = rankToScore(aGame->_white_rank);
3333 name = element(line, 0, "]", "[");
3334 aGame->black = getPlayerListingNeverFail(name);
3335 aGame->_black_name = element(line, 0, "]", "[");
3336 aGame->_black_rank = element(line, 1, "[", "]");
3337 fixRankString(&(aGame->_black_rank));
3338 aGame->_black_rank_score = rankToScore(aGame->_black_rank);
3339
3340 aGame->moves = 0; //right???
3341 //aGame->s = "-";
3342 //aGame->Sz = "-";
3343 //aGame->handicap = QString();
3344 aGame->running = true;
3345
3346 #ifdef OLD
3347 if (gsName == WING && aGame->wname == aGame->bname)
3348 // WING doesn't send 'create match' msg in case of teaching game
3349 //emit signal_matchCreate(aGame->nr, aGame->bname);
3350
3351 //emit signal_game(aGame);
3352 #endif //OLD
3353 room->recvGameListing(aGame);
3354 return;
3355 }
3356 else if(line.contains("Match"))
3357 {
3358 //Match 220 gogal gogal
3359 //what is this? a request?
3360 }
3361 // !xxxx!: a game anyone ?
3362 else if (line.contains("!: "))
3363 {
3364 QString player = element(line, 0, "!");
3365 //emit signal_shout(player, line);
3366 return;
3367 }
3368 getConsoleDispatch()->recvText(line.toLatin1().constData());
3369 }
3370
3371 void IGSConnection::handle_status(QString line)
3372 {
3373 qDebug() << "IGSConnection::handle_status " << line;
3374 line = line.remove(0, 2).trimmed();
3375 // CURRENT GAME STATUS
3376 // 22 Pinkie 3d* 21 218 9 T 5.5 0
3377 // 22 aura 3d* 24 276 16 T 5.5 0
3378 // 22 0: 4441000555033055001
3379 // 22 1: 1441100000011000011
3380 // 22 2: 4141410105013010144
3381 // 22 3: 1411411100113011114
3382 // 22 4: 1131111111100001444
3383 // 22 5: 0100010001005011111
3384 // 22 6: 0055000101105000010
3385 // 22 7: 0050011110055555000
3386 // 22 8: 1005000141005055010
3387 // 22 9: 1100113114105500011
3388 // 22 10: 1111411414105010141
3389 // 22 11: 4144101101100111114
3390 // 22 12: 1411300103100000144
3391 // 22 13: 1100005000101001144
3392 // 22 14: 1050505550101011144
3393 // 22 15: 0505505001111001444
3394 // 22 16: 0050050111441011444
3395 // 22 17: 5550550001410001144
3396 // 22 18: 5555000111411300144
3397 //case 22:
3398 /* Status messages are a bit screwy:
3399 * the columns are actually the y axis on the board
3400 * and the rows the x axis, so for instance on our
3401 * board p19 would appear on their board as 0,14
3402 * In addition, some things are marked wrong like
3403 * false eyes that are surrounded by stones are
3404 * marked as territory when they shouldn't be, but
3405 * that's not our problem.
3406 * 0 is a black stone, 1 is a white stone, 3 is
3407 * dame (no territory (did I get term right?)) 4 is
3408 * white territory, 5 is black territory */
3409 static QString player = "";
3410 static int cap;
3411 static float komi;
3412 static BoardDispatch * statusDispatch;
3413 if (!line.contains(":"))
3414 {
3415 if(player == "")
3416 {
3417 player = element(line, 0, " ");
3418 cap = element(line, 2, " ").toInt();
3419 komi = element(line, 6, " ").toFloat();
3420 }
3421 else
3422 {
3423 statusDispatch = getBoardFromAttrib(element(line, 0, " "), element(line, 2, " ").toInt(), element(line, 6, " ").toFloat(), player, cap, komi);
3424 if(!statusDispatch)
3425 {
3426 // FIXME this happens an awful lot. I think
3427 // every time we play a game, though perhaps
3428 // observing games is okay... anyway, either
3429 // we need to change something so that we don't
3430 // even check here, or this doesn't warrant
3431 // a debug message
3432 qDebug("No status board found!!!\n");
3433 player = "";
3434 return;
3435 }
3436 statusDispatch->recvEnterScoreMode();
3437 player = "";
3438 }
3439 }
3440 else
3441 {
3442 if(!statusDispatch)
3443 return;
3444 int row = element(line, 0, ":").toInt();
3445 QString results = element(line, 1, " ");
3446 /* X and Y are swapped below */
3447 MoveRecord * aMove = new MoveRecord();
3448 aMove->x = row + 1;
3449 aMove->flags = MoveRecord::TERRITORY;
3450 for(int column = 1; column <= results.length(); column++)
3451 {
3452 aMove->y = column;
3453 if(results.at(column - 1) == '4')
3454 aMove->color = stoneWhite;
3455 else if(results.at(column - 1) == '5')
3456 aMove->color = stoneBlack;
3457 else
3458 continue;
3459 statusDispatch->recvMove(aMove);
3460 }
3461
3462 /* FIXME If there's no more moves, and there's no result
3463 * on the game (like an earlier resign at the same time? etc.,
3464 * before it? Then we need to recvResult here */
3465 if(row + 1 == results.length())
3466 {
3467 /* Since its a square board */
3468 aMove->flags = MoveRecord::DONE_SCORING;
3469 statusDispatch->recvMove(aMove);
3470 }
3471 delete aMove;
3472 }
3473
3474 // //emit signal_message(line);
3475 }
3476
3477 void IGSConnection::handle_stored(QString line)
3478 {
3479 qDebug("%s", line.toLatin1().constData());
3480 // STORED
3481 // 9 Stored games for frosla:
3482 // 23 frosla-physician
3483 //TODO case 23:
3484 //TODO break;
3485 }
3486
3487 void IGSConnection::handle_tell(QString line)
3488 {
3489 line = line.remove(0, 2).trimmed();
3490 // 24 *xxxx*: CLIENT: <cgoban 1.9.12> match xxxx wants handicap 0, komi 5.5, free
3491 // 24 *jirong*: CLIENT: <cgoban 1.9.12> match jirong wants handicap 0, komi 0.5
3492 // 24 *SYSTEM*: shoei canceled the nmatch request.
3493 // 24 *SYSTEM*: xxx requests undo.
3494
3495 // NNGS - new version:
3496 // 24 --> frosla CLIENT: <qGo 0.0.15b7> match frosla wants handicap 0, komi 0.5, free
3497 // 24 --> frosla Hallo
3498 //case 24:
3499 //{
3500 qDebug("24: %s", line.toLatin1().constData());
3501 int pos;
3502 BoardDispatch * boarddispatch;
3503 #ifdef FIXME
3504 if (((((pos = line.indexOf("*")) != -1) && (pos < 3)) ||
3505 (((pos = line.indexOf("-->")) != -1) && (pos < 3) &&
3506 line.contains("CLIENT:"))))
3507 {
3508
3509 line = line.simplified();
3510 QString opp = element(line, 1, "*");
3511 int offset = 0;
3512 if (opp.isEmpty())
3513 {
3514 offset++;
3515 opp = element(line, 1, " ");
3516 }
3517
3518 QString h = element(line, 7+offset, " ", ",");
3519 QString k = element(line, 10+offset, " ");
3520
3521 if (k.at(k.length()-1) == ',')
3522 k.truncate(k.length() - 1);
3523 int komi = (int) (k.toFloat() * 10);
3524
3525 bool free;
3526 if (line.contains("free"))
3527 free = true;
3528 else
3529 free = false;
3530
3531 emit signal_komirequest(opp, h.toInt(), komi, free);
3532 getConsoleDispatch()->recvText(line.toLatin1().constData());
3533
3534 // it's tell, but don't open a window for that kind of message...
3535 return;
3536 }
3537 #endif //FIXME
3538
3539 //check for cancelled game offer
3540 if (line.contains("*SYSTEM*"))
3541 {
3542 QString opp = element(line, 1, " ");
3543 PlayerListing * p = getPlayerListingNeverFail(opp);
3544 line = line.remove(0,10);
3545 getConsoleDispatch()->recvText(line.toLatin1().constData());
3546
3547 if (line.contains("canceled") && line.contains("match request"))
3548 {
3549 /* FIXME We need to close any open gamedialog boxes
3550 * pertaining to this opponent. We'll assume
3551 * one is created I guess */
3552
3553 GameDialog * gameDialog = getGameDialog(p);
3554 gameDialog->recvRefuseMatch(2);
3555 ////emit signal_matchCanceled(opp);
3556 }
3557 if (line.contains("requests undo"))
3558 {
3559 boarddispatch = getBoardDispatch(protocol_save_int);
3560 if(!boarddispatch)
3561 {
3562 printf("No boarddispatch for undo message");
3563 return;
3564 }
3565 MoveRecord * aMove = new MoveRecord();
3566 aMove->flags = MoveRecord::REQUESTUNDO;
3567 boarddispatch->recvMove(aMove);
3568 delete aMove;
3569 ////emit signal_requestDialog("undo","noundo",0,opp);
3570 }
3571 return;
3572 }
3573
3574 // check for NNGS type of msg
3575 QString e1,e2;
3576 if ((pos = line.indexOf("-->")) != -1 && pos < 3)
3577 {
3578 e1 = element(line, 1, " ");
3579 //e2 = "> " + element(line, 1, " ", "EOL").trimmed();
3580 e2 = element(line, 1, " ", "EOL").trimmed();
3581 }
3582 else
3583 {
3584 e1 = element(line, 0, "*", "*");
3585 //e2 = "> " + element(line, 0, ":", "EOL").trimmed();
3586 e2 = element(line, 0, ":", "EOL").trimmed();
3587 }
3588
3589 /* FIXME, what's weird here is that this is usually where you get "Thanks for game"
3590 * not in the board window and I'm not sure that's appropriate. */
3591 // //emit player + message + true (=player)
3592 ////emit signal_talk(e1, e2, true);
3593 PlayerListing * p = getPlayerListingNeverFail(e1);
3594 Talk * talk = getDefaultRoom()->getIfTalk(p);
3595 if(!talk)
3596 {
3597 sendStatsRequest(p);
3598 talk = getDefaultRoom()->getTalk(p);
3599 }
3600 if(talk)
3601 talk->recvTalk(e2);
3602 }
3603
3604
3605 // results
3606 //25 File
3607 //curio [ 5d*](W) : lllgolll [ 4d*](B) H 0 K 0.5 19x19 W+Resign 22-04-47 R
3608 //curio [ 5d*](W) : was [ 4d*](B) H 0 K 0.5 19x19 W+Time 22-05-06 R
3609 //25 File
3610 //case 25:
3611 void IGSConnection::handle_thist(QString line)
3612 {
3613 //FIXME if this is something that happens then
3614 // we should be picking it up
3615 qDebug("%s", line.toLatin1().constData());
3616 }
3617 // who
3618 // 27 Info Name Idle Rank | Info Name Idle Rank
3619 // 27 SX -- -- xxxx03 1m NR | Q! -- -- xxxx101 33s NR
3620 // 0 4 6 11 15 26 33 38 41
3621 // 27 -- 221 DAISUKEY 33s 8k | X172 -- hiyocco 2m 19k*
3622 // 27 Q -- 216 Saiden 7s 1k* | -- 53 toshiao 11s 10k
3623 // 27 48 -- kyouji 3m 11k* | -- 95 bengi 5s 4d*
3624 // IGS:
3625 // -- -- kimisa 1m 2k* | Q -- 206 takabo 45s 1k*
3626 // X 39 -- Marin 5m 2k* | -- 53 KT713 18s 2d*
3627 // -- 34 mat21 2m 14k* | -- 9 entropy 28s 4d
3628 // NNGS:
3629 // 27 X -- -- arndt 21s 5k | X -- -- biogeek 58s 20k
3630 // 27 X -- -- buffel 21s 4k | S -- 5 frosla 12s 7k
3631 // 27 S -- -- GoBot 1m NR | -- 5 guest17 0s NR
3632 // 27 -- 3 hama 3s 15k | -- -- niki 5m NR
3633 // 27 ! -- -- viking4 55s 19k* | S -- 3 GnuGo 6s 14k*
3634 // 27 X -- -- kossa 21m 5k | X -- -- leif 5m 3k*
3635 // 27 -- 6 ppp 18s 2k* | -- 6 chmeng 20s 1d*
3636 // 27 ******** 14 Players 3 Total Games ********
3637
3638 // WING:
3639 // 0 9 14 38 46 51
3640 // 27 X 13 -- takeo6 1m 2k | ! 26 -- ooide 6m 2k*
3641 // 27 ! -- -- hide1234 9m 2k* | QX -- -- yochi 0s 2k*
3642 // 27 g -- 86 karasu 45s 2k* | Sg -- 23 denakechi 16s 1k*
3643 // 27 g 23 43 kH03 11s 1k* | g -- 43 kazusige 24s 1k*
3644 // 27 g -- 50 jan 24s 1k* | QX -- -- maigo 32s 1d
3645 // 27 g -- 105 kume1 5s 1d* | g -- 30 yasumitu 24s 1d
3646 // 27 Qf -- 13 motono 2m 1d* | X 13 -- tak7 57s 1d*
3647 // 27 ! 50 -- okiek 8m 1d* | X -- -- DrO 11s 1d*
3648 // 27 f -- 103 hiratake 35s 8k | g -- 33 kushinn 21s 8k*
3649 // 27 g -- 102 teacup 1m 1d* | g -- 102 Tadao 32s 1d*
3650 //case 27:
3651 void IGSConnection::handle_who(QString line)
3652 {
3653 PlayerListing * aPlayer = NULL;
3654 Room * room = getDefaultRoom();
3655 int pos;
3656 if (line.contains("****") && line.contains("Players"))
3657 return;
3658 if (line.contains("Info") && line.contains("Idle"))
3659 return;
3660
3661 if (line[15] != ' ')
3662 {
3663 //27 X -- -- truetest 7m 1d* | X -- -- aajjoo 6s 9k
3664 // parse line
3665 QString info = line.mid(4,2);
3666 int observing = line.mid(6,3).toInt();
3667 int playing = line.mid(11,3).trimmed().toInt();
3668 QString name = line.mid(15,11).trimmed();
3669 aPlayer = room->getPlayerListing(name);
3670 aPlayer->online = true;
3671 aPlayer->info = info;
3672 aPlayer->observing = observing;
3673 aPlayer->playing = playing;
3674 aPlayer->idletime = line.mid(26,3);
3675 aPlayer->seconds_idle = idleTimeToSeconds(aPlayer->idletime);
3676 //aPlayer->nmatch = false;
3677 if (line[33] == ' ')
3678 {
3679 if (line[36] == ' ')
3680 aPlayer->rank = line.mid(34,2);
3681 else
3682 aPlayer->rank = line.mid(34,3);
3683 fixRankString(&(aPlayer->rank));
3684 aPlayer->rank_score = rankToScore(aPlayer->rank);
3685 }
3686 else
3687 {
3688 if (line[36] == ' ')
3689 aPlayer->rank = line.mid(33,3);
3690 else
3691 aPlayer->rank = line.mid(33,4);
3692 fixRankString(&(aPlayer->rank));
3693 aPlayer->rank_score = rankToScore(aPlayer->rank);
3694 }
3695
3696 // check if line ok, true -> cmd "players" preceded
3697 emit playerListingReceived(aPlayer);
3698 }
3699 #ifdef FIXME
3700 else
3701 qDebug("player27 dropped (1): %s" + line.toLatin1());
3702 #endif //FIXME
3703
3704 // position of delimiter between two players
3705 pos = line.indexOf('|');
3706 // check if 2nd player in a line && player (name) exists
3707 if (pos != -1 && line[52] != ' ')
3708 {
3709 // parse line
3710 QString info = line.mid(41,2);
3711 int observing = line.mid(43,3).toInt();
3712 int playing = line.mid(48,3).trimmed().toInt();
3713 QString name = line.mid(52,11).trimmed();
3714 aPlayer = room->getPlayerListing(name);
3715 aPlayer->online = true;
3716 aPlayer->info = info;
3717 aPlayer->observing = observing;
3718 aPlayer->playing = playing;
3719 aPlayer->idletime = line.mid(63,3);
3720 aPlayer->seconds_idle = idleTimeToSeconds(aPlayer->idletime);
3721 //aPlayer->nmatch = false;
3722 if (line[70] == ' ')
3723 {
3724 if (line[73] == ' ')
3725 aPlayer->rank = line.mid(71,2);
3726 else
3727 aPlayer->rank = line.mid(71,3);
3728 fixRankString(&(aPlayer->rank));
3729 aPlayer->rank_score = rankToScore(aPlayer->rank);
3730 }
3731 else
3732 {
3733 if (line[73] == ' ')
3734 aPlayer->rank = line.mid(70,3);
3735 else
3736 aPlayer->rank = line.mid(70,4);
3737 fixRankString(&(aPlayer->rank));
3738 aPlayer->rank_score = rankToScore(aPlayer->rank);
3739 }
3740
3741 // true -> cmd "players" preceded
3742 emit playerListingReceived(aPlayer);
3743 }
3744 #ifdef FIXME
3745 else
3746 qDebug("player27 dropped (2): %s" + line.toLatin1());
3747 #endif //FIXME
3748 }
3749
3750 void IGSConnection::handle_undo(QString line)
3751 // 28 guest17 undid the last (J16).
3752 // 15 Game 7 I: frosla (0 5363 -1) vs guest17 (0 5393 -1)
3753 // 15 2(B): F17
3754 // IGS:
3755 // 28 Undo in game 64: MyMaster vs MyMaster: N15
3756 //case 28:
3757 {
3758 BoardDispatch * boarddispatch;
3759 qDebug("*** %s", line.toLatin1().constData());
3760 line = line.remove(0, 2).trimmed();
3761 if (line.contains("undid the last "))
3762 {
3763 // now: just look in qgo_interface if _nr has decreased...
3764 // but send undo-signal anyway: in case of undo while scoring it's necessary
3765 MoveRecord * aMove = new MoveRecord();
3766 aMove->flags = MoveRecord::UNDO;
3767 QString player = element(line, 0, " ");
3768 QString point = element(line, 0, "(", ")");
3769
3770 aMove->x = (int)(point.toLatin1().at(0));
3771 aMove->x -= 'A';
3772 if(aMove->x < 9) //no I on IGS
3773 aMove->x++;
3774 point.remove(0,1);
3775 aMove->y = element(point, 0, " ").toInt();
3776 /* Do we need the board size here???*/
3777 aMove->y = 20 - aMove->y;
3778
3779 boarddispatch = getBoardFromOurOpponent(player);
3780 boarddispatch->recvMove(aMove);
3781 delete aMove;
3782 }
3783 else if (line.contains("Undo in game"))
3784 {
3785 MoveRecord * aMove = new MoveRecord();
3786 aMove->flags = MoveRecord::UNDO;
3787 QString nr = element(line, 3, " ");
3788 nr.truncate(nr.length() - 1);
3789 QString point = element(line, 7, " ");
3790 aMove->x = (int)(point.toLatin1().at(0));
3791 aMove->x -= 'A';
3792 if(aMove->x < 9) //no I on IGS
3793 aMove->x++;
3794 point.remove(0,1);
3795 aMove->y = element(point, 0, " ").toInt();
3796 /* Do we need the board size here???*/
3797 aMove->y = 20 - aMove->y;
3798 boarddispatch = getBoardDispatch(nr.toInt());
3799 boarddispatch->recvMove(aMove);
3800 delete aMove;
3801 }
3802
3803 // message anyway
3804 getConsoleDispatch()->recvText(line.toLatin1().constData());
3805 }
3806
3807 void IGSConnection::handle_yell(QString line)
3808 // different from shout??!?!?
3809 // IGS
3810 // --> ; \20
3811 // 32 Changing into channel 20.
3812 // 32 Welcome to cyberspace.
3813 //
3814 // 32 49:qgodev: Title is now: (qgodev) qGo development
3815 // 32 20:qGoDev: Person joining channel
3816 // 32 20:frosla: hi
3817 // 32 20:qGoDev: Person leaving channel
3818
3819 //
3820 // 1 5
3821 // --> channels
3822 // 9 #1 Title: Professional Channel -- Open
3823 //
3824 // 9 #20 Title: Untitled -- Open
3825 // 9 #20 frosla
3826 // #> 23 till: hello all <-- this is what the members in channel 23 see:
3827 // (channel number and who sent the message)
3828 //
3829 // NNGS:
3830 // channel talk: "49:xxxx: hi"
3831 //case 32:
3832 {
3833 /* As far as I can tell, the channel info stuff was
3834 * never hooked up to anything. The talk should be
3835 * hooked up to something, but this is a FIXME*/
3836 QString e1,e2;
3837 qDebug("%s\n", line.toLatin1().constData());
3838 line = line.remove(0, 2).trimmed();
3839 if (line.contains("Changing into channel"))
3840 {
3841 #ifdef FIXME
3842 e1 = element(line, 2, " ",".");
3843 int nr = e1.toInt();//element(line, 3, " ").toInt();
3844 emit signal_channelinfo(nr, QString("*on*"));
3845 // //emit signal_talk(e1, "", false);
3846 ////emit signal_message(line);
3847 #endif //FIXME
3848 return;
3849 }
3850 else if (line.contains("Welcome to cyberspace"))
3851 {
3852 ////emit signal_message(line);
3853 getConsoleDispatch()->recvText(line.toLatin1().constData());
3854 return;
3855 }
3856 else if (line.contains("Person joining channel"))
3857 {
3858 #ifdef FIXME
3859 int nr = element(line, 0, ":").toInt();
3860 emit signal_channelinfo(nr, QString("*on*"));
3861 #endif //FIXME
3862 return;
3863 }
3864 else if (line.contains("Person leaving channel"))
3865 {
3866 #ifdef FIXME
3867 int nr = element(line, 0, ":").toInt();
3868 emit signal_channelinfo(nr, QString("*on*"));
3869 #endif //FIXME
3870 return;
3871 }
3872
3873 // //emit (channel number, player + message , false =channel )
3874 /*switch (gsName)
3875 {
3876 case IGS:*/
3877 e1=element(line, 0, ":");
3878 e2="> " + element(line, 0, ":", "EOL").trimmed();
3879 e1 += e2;
3880 getConsoleDispatch()->recvText(e1.toLatin1().constData());
3881 /* break;
3882
3883 default:
3884 e1=element(line, 0, ":");
3885 e2="> " + element(line, 0, ":", "EOL").trimmed();
3886 e1 += e2;
3887 break;
3888 } FIXME */
3889
3890 // //emit signal_talk(e1, e2, false);
3891 }
3892
3893 /* 36
3894 36 terra1 wants a match with you:
3895 36 terra1 wants 19x19 in 1 minutes with 10 byo-yomi and 25 byo-stones
3896 36 To accept match type 'automatch terra1'
3897 */
3898 void IGSConnection::handle_automatch(QString line)
3899 {
3900 line = line.remove(0, 2).trimmed();
3901 if(line.contains("minutes"))
3902 {
3903 Room * room = getDefaultRoom();
3904 MatchRequest * aMatch = new MatchRequest();
3905 aMatch->opponent = element(line, 0, " ");
3906
3907 qDebug("Receieved automatch request from %s", aMatch->opponent.toLatin1().constData());
3908 QString bs = element(line, 2, " ");
3909 if(bs == "19x19")
3910 aMatch->board_size = 19;
3911 else if(bs == "13x13")
3912 aMatch->board_size = 13;
3913 else if(bs == "9x9")
3914 aMatch->board_size = 9;
3915 else
3916 {
3917 qDebug("Match offered board size: %s", bs.toLatin1().constData());
3918 delete aMatch;
3919 return;
3920 }
3921 aMatch->maintime = element(line, 4, " ").toInt();
3922 aMatch->periodtime = element(line, 7, " ").toInt();
3923 aMatch->stones_periods = element(line, 10, " ").toInt();
3924 PlayerListing * p = getPlayerListingNeverFail(aMatch->opponent);
3925 PlayerListing * us = room->getPlayerListing(getUsername());
3926 aMatch->our_name = us->name;
3927 aMatch->our_rank = us->rank;
3928
3929 aMatch->their_rank = p->rank;
3930 /* Maybe we have to do automatch, rather than accept/decline,
3931 * FIXME */
3932 //protocol_save_string = "automatch";
3933 GameDialog * gameDialog = getGameDialog(p);
3934 gameDialog->recvRequest(aMatch);
3935
3936 delete aMatch;
3937 }
3938 }
3939
3940 void IGSConnection::handle_serverinfo(QString)
3941 {}
3942
3943 // Setting your . to xxxx
3944 //case 40:
3945 void IGSConnection::handle_dot(QString)
3946 {}
3947 // long user report equal to 7
3948 // 7 [255] YK [ 7d*] vs. SOJ [ 7d*] ( 56 19 0 5.5 4 I) ( 18)
3949 // 42 [88] AQ [ 2d*] vs. lang [ 2d*] (177 19 0 5.5 6 I) ( 1)
3950
3951 // IGS: cmd 'user'
3952 // 42 Name Info Country Rank Won/Lost Obs Pl Idle Flags Language
3953 // 0 3 15 31 40 45 48 54 59 62 67 72
3954 // 42 guest15 -- NR 0/ 0 - - 10s Q- default
3955 // 42 guest13 -- NR 0/ 0 - - 24s Q- default
3956 // 42 zz You may be rec France NR 0/ 0 - - 0s -X default
3957 // 42 zz0002 <none> Japan NR 0/ 0 - - 1m QX default
3958 // 42 zz0003 <none> () Japan NR 0/ 0 - - 1m SX default
3959 // 42 anko Taiwan 2k* 168/ 97 - - 1s -- default
3960 // 42 MUKO12 1/12 〜 1/15 Japan 4k* 666/ 372 17 - 1m -X default
3961 // 42 mof [Igc2000 1.1] Sweden 2k* 509/ 463 124 - 0s -X default
3962 // 42 obiyan <None> Japan 3k* 1018/ 850 - 50 11s -- default
3963 // 42 uzumaki COM 1k 0/ 0 - - 1s -X default
3964 // 42 HansWerner [Igc2000 1.2] Germany 1k 11/ 3 111 - 1m QX default
3965 // 42 genesis Germany 1k* 592/ 409 - 136 14s -- default
3966 // 42 hamburg Germany 3k* 279/ 259 - 334 13s -- default
3967 // 42 Sie Germany 1k* 6/ 7 - 68 39s -- default
3968 // 42 borkum Germany 4k* 163/ 228 - 100 7s -- default
3969 // 42 casumy 45-3 Germany 1d 0/ 0 - 133 2s Q- default
3970 // 42 stoni Germany 2k* 482/ 524 - 166 7s -- default
3971 // 42 Beholder <None> Germany NR 0/ 0 263 - 5s QX default
3972 // 42 xiejun 1/10-15 Germany 3d* 485/ 414 179 - 49s -X default
3973 // 9 ******** 1120 Players 247 Total Games ********
3974
3975 // IGS: new cmd 'userlist' for nmatch information
3976 // 10 20 30 40 50 60 70 80 80 90 100 110 120
3977 // 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
3978 // 42 Name Info Country Rank Won/Lost Obs Pl Idle Flags Language
3979 // 42 ----- <none> Japan 15k+ 2108/2455 - - 1m Q- default T BWN 0-9 19-19 600-600 1200-1200 25-25 0-0 0-0 0-0
3980 // 42 ----- Japan 3d 11/ 13 - 222 46s Q- default T BWN 0-9 19-19 60-60 600-600 25-25 0-0 0-0 0-0
3981 // 42 Neil <None> USA 12k 136/ 86 - - 0s -X default T BWN 0-9 19-19 60-60 60-3600 25-25 0-0 0-0 0-0
3982 //case 42:
3983
3984
3985 //42 koumas* - 11k 0000/0000 - 4 11m 9a E?E----
3986
3987 // 42 koumas* - 11k 0000/0000 - 4 11m 9a E?E----
3988 // No match
3989
3990
3991
3992 // 42 spaman* - 11k* 0000/0000 - 4 11m 9a E?E----
3993
3994 // 42 spaman* - 11k* 0000/0000 - 4 11m 9a E?E----
3995 // No match
3996
3997 void IGSConnection::handle_userlist(QString line)
3998 {
3999 //line = line.remove(0, 2).trimmed();
4000 PlayerListing * aPlayer = NULL;
4001 Room * room = getDefaultRoom();
4002 if (line[40] == 'R') //this was line ???
4003 {
4004 // skip
4005 return;
4006 }
4007 // 1
4008 // Name
4009 QRegExp re1 = QRegExp("42 ([A-Za-z0-9 ]{,10}) ");
4010 // 2
4011 // Info
4012 QRegExp re2 = QRegExp("(.{1,14}) ");
4013 // 3
4014 // Country
4015 QRegExp re3 = QRegExp("([a-zA-Z][a-zA-Z. /&#92;']{,6}|-- ) "
4016 // 4
4017 // Rank
4018 "([0-9 ][0-9][kdp].?| +BC| +NR) +"
4019 // 5 6
4020 // Won Lost
4021 "([0-9 ]+[0-9]+)/([0-9 ]+[0-9]+) +"
4022 // 7 8
4023 // Obs Pl
4024 "([0-9]+|-) +([0-9]+|-) +"
4025 // 9 10 11 12
4026 // Idle Flags
4027 "([A-Za-z0-9]+) +([^ ]{,2})");// +default");
4028 QRegExp re4 = QRegExp(" ([TF]) (.*)");
4029 if(re1.indexIn(line) < 0 || re3.indexIn(line) < 0)
4030 {
4031 qDebug("\n%s", line.toUtf8().data());
4032 qDebug("No match\n");
4033 return;
4034 }
4035
4036 // 42 Neil <None> USA 12k 136/ 86 - - 0s -X default T BWN 0-9 19-19 60-60 60-3600 25-25 0-0 0-0 0-0
4037
4038 QString name = re1.cap(1).trimmed();
4039 aPlayer = room->getPlayerListing(name);
4040
4041 if(re2.indexIn(line) >= 0)
4042 {
4043 aPlayer->extInfo = re2.cap(1).trimmed();
4044 }
4045 else
4046 aPlayer->extInfo = "<None>";
4047 if(aPlayer->extInfo == "")
4048 aPlayer->extInfo = "<None>";
4049
4050 aPlayer->country = re3.cap(1).trimmed();
4051 if(aPlayer->country == "--")
4052 aPlayer->country = "";
4053 aPlayer->rank = re3.cap(2).trimmed();
4054 fixRankString(&(aPlayer->rank));
4055 aPlayer->rank_score = rankToScore(aPlayer->rank);
4056 aPlayer->wins = re3.cap(3).trimmed().toInt();
4057 aPlayer->losses = re3.cap(4).trimmed().toInt();
4058 aPlayer->observing = re3.cap(5).trimmed().toInt();
4059 aPlayer->playing = re3.cap(6).trimmed().toInt();
4060 aPlayer->idletime = re3.cap(7).trimmed();
4061 aPlayer->seconds_idle = idleTimeToSeconds(aPlayer->idletime);
4062 aPlayer->info = re3.cap(8).trimmed();
4063
4064 if(re4.indexIn(line) < 0)
4065 aPlayer->nmatch = 0;
4066 else
4067 aPlayer->nmatch = re4.cap(1) == "T";
4068 aPlayer->nmatch_settings = "";
4069 QString nmatchString = "";
4070 // we want to format the nmatch settings to a readable string
4071 if (aPlayer->nmatch)
4072 {
4073 // BWN 0-9 19-19 60-60 600-600 25-25 0-0 0-0 0-0
4074 nmatchString = re4.cap(2).trimmed();
4075 if ( ! nmatchString.isEmpty())
4076 {
4077
4078 aPlayer->nmatch_black = (nmatchString.contains("B"));
4079 aPlayer->nmatch_white = (nmatchString.contains("W"));
4080 aPlayer->nmatch_nigiri = (nmatchString.contains("N"));
4081
4082
4083 aPlayer->nmatch_timeMin = element(nmatchString, 2, " ", "-").toInt();
4084 aPlayer->nmatch_timeMax = element(nmatchString, 2, "-", " ").toInt();
4085 QString t1min = QString::number(aPlayer->nmatch_timeMin / 60);
4086 QString t1max = QString::number(aPlayer->nmatch_timeMax / 60);
4087 if (t1min != t1max)
4088 t1min.append(" to ").append(t1max) ;
4089
4090 QString t2min = element(nmatchString, 3, " ", "-");
4091 QString t2max = element(nmatchString, 3, "-", " ");
4092 aPlayer->nmatch_BYMin = t2min.toInt();
4093 aPlayer->nmatch_BYMax = t2max.toInt();
4094
4095 QString t3min = QString::number(t2min.toInt() / 60);
4096 QString t3max = QString::number(t2max.toInt() / 60);
4097
4098 if (t2min != t2max)
4099 t2min.append(" to ").append(t2max) ;
4100
4101 if (t3min != t3max)
4102 t3min.append(" to ").append(t3max) ;
4103
4104 QString s1 = element(nmatchString, 4, " ", "-");
4105 QString s2 = element(nmatchString, 4, "-", " ");
4106 aPlayer->nmatch_stonesMin = s1.toInt();
4107 aPlayer->nmatch_stonesMax = s2.toInt();
4108
4109 if (s1 != s2)
4110 s1.append("-").append(s2) ;
4111
4112 if (s1 == "1")
4113 {
4114 aPlayer->nmatch_timeSystem = byoyomi;
4115 aPlayer->nmatch_settings = "Jap. BY : " +
4116 t1min + " min. + " +
4117 t2min + " secs./st. " ;
4118 }
4119 else
4120 {
4121 aPlayer->nmatch_timeSystem = canadian;
4122 aPlayer->nmatch_settings = "Can. BY : " +
4123 t1min + " min. + " +
4124 t3min + " min. /" +
4125 s1 + " st. ";
4126 }
4127
4128 QString h1 = element(nmatchString, 0, " ", "-");
4129 QString h2 = element(nmatchString, 0, "-", " ");
4130 aPlayer->nmatch_handicapMin = h1.toInt();
4131 aPlayer->nmatch_handicapMax = h2.toInt();
4132 if (h1 != h2)
4133 h1.append("-").append(h2) ;
4134
4135 aPlayer->nmatch_settings.append("(h " + h1 + ")") ;
4136 }
4137 else
4138 aPlayer->nmatch_settings = "No match conditions";
4139 }
4140
4141 // indicate player to be online
4142 aPlayer->online = true;
4143 emit playerListingReceived(aPlayer);
4144 }
4145
4146 // IGS: 49 Game 42 qGoDev is removing @ C5
4147 void IGSConnection::handle_removed(QString line)
4148 {
4149 line = line.remove(0, 2).trimmed();
4150 if (line.contains("is removing @"))
4151 {
4152 int game = element(line, 1, " ").toInt();
4153 QString pt = element(line, 6, " ");
4154 handleRemovingAt(game, pt);
4155 }
4156 }
4157
4158 void IGSConnection::handleRemovingAt(unsigned int game, QString pt)
4159 {
4160 BoardDispatch * boarddispatch = getIfBoardDispatch(game);
4161 if(!boarddispatch)
4162 {
4163 qDebug("removing for game we don't have");
4164 return;
4165 }
4166 GameData * r = boarddispatch->getGameData();
4167 if(!r)
4168 {
4169 qDebug("Can't get game record");
4170 return;
4171 }
4172
4173 MoveRecord * aMove = new MoveRecord();
4174 aMove->flags = MoveRecord::REMOVE;
4175 aMove->x = (int)(pt.toLatin1().at(0));
4176 aMove->x -= 'A';
4177 if(aMove->x < 9) //no I on IGS
4178 aMove->x++;
4179 pt.remove(0,1);
4180 aMove->y = element(pt, 0, " ").toInt();
4181 /* Do we need the board size here???*/
4182 aMove->y = r->board_size + 1 - aMove->y;
4183 boarddispatch->recvMove(aMove);
4184 delete aMove;
4185 }
4186
4187 //FIXME
4188 //51 Say in game 432
4189 void IGSConnection::handle_ingamesay(QString s)
4190 {
4191 qDebug("Ingamesay: %s", s.toLatin1().constData());
4192 }
4193
4194 // 53 Game 75 adjournment is declined
4195 void IGSConnection::handle_adjourndeclined(QString line)
4196 {
4197 line = line.remove(0, 2).trimmed();
4198 BoardDispatch * boarddispatch;
4199 if (line.contains("adjournment is declined"))
4200 {
4201 unsigned int game_nr = element(line, 1, " ").toInt();
4202 boarddispatch = getBoardDispatch(game_nr);
4203 if(boarddispatch)
4204 boarddispatch->recvRefuseAdjourn();
4205 else
4206 {
4207 qDebug("Adjourn decline recv %d", game_nr);
4208 }
4209 }
4210 }
4211
4212 // IGS : seek syntax, answer to 'seek config_list' command
4213 // 63 CONFIG_LIST_START 4
4214 // 63 CONFIG_LIST 0 60 600 25 0 0
4215 // 63 CONFIG_LIST 1 60 480 25 0 0
4216 // 63 CONFIG_LIST 2 60 300 25 0 0
4217 // 63 CONFIG_LIST 3 60 900 25 0 0
4218 // 63 CONFIG_LIST_END
4219
4220 // 63 OPPONENT_FOUND
4221
4222 // 63 ENTRY_LIST_START 5
4223 // 63 ENTRY_LIST HKL 60 480 25 0 0 19 1 1 0
4224 // 63 ENTRY_LIST tgor 60 480 25 0 0 19 1 1 0
4225 // 63 ENTRY_LIST sun756 60 600 25 0 0 19 1 1 0
4226 // 63 ENTRY_LIST horse 60 600 25 0 0 19 2 2 0
4227 // 63 ENTRY_LIST masaeaki 60 300 25 0 0 19 1 1 0
4228 // 63 ENTRY_LIST_END
4229 void IGSConnection::handle_seek(QString line)
4230 {
4231 line = line.remove(0, 2).trimmed();
4232 if (line.contains("OPPONENT_FOUND"))
4233 {
4234 QString oppname = element(line, 1, " ");
4235 ConsoleDispatch * console = getConsoleDispatch();
4236 if(console)
4237 console->recvText("Opponent found: " + oppname);
4238 recvSeekCancel();
4239 }
4240 else if (line.contains("CONFIG_LIST "))
4241 {
4242 SeekCondition * seekCondition = new SeekCondition();
4243 seekCondition->number = element(line, 1, " ").toInt();
4244 seekCondition->maintime = element(line, 2, " ").toInt();
4245 seekCondition->periodtime = element(line, 3, " ").toInt();
4246 seekCondition->periods = element(line, 4, " ").toInt();
4247 seekCondition->bline = element(line, 1, " ", "EOL");
4248 recvSeekCondition(seekCondition);
4249 }
4250 else if (line.contains("CONFIG_LIST_START"))
4251 {
4252 recvSeekCondition(0);
4253 }
4254 else if (line.contains("ENTRY_CANCEL"))
4255 {
4256 recvSeekCancel();
4257 }
4258 else if ((line.contains("ERROR")) )
4259 {
4260 recvSeekCancel();
4261 ConsoleDispatch * console = getConsoleDispatch();
4262 if(console)
4263 console->recvText(line.toLatin1().constData());
4264 }
4265 else if ((line.contains("ENTRY_LIST_START")) )
4266 {
4267 //emit signal_clearSeekList();
4268 recvSeekPlayer("", "");
4269 }
4270 else if ((line.contains("ENTRY_LIST ")) )
4271 {
4272 //FIXME what does this do??? what does it mean? who am I?
4273 QString player = element(line, 1, " ");
4274 QString condition =
4275 element(line, 7, " ")+"x"+element(line, 7, " ")+" - " +
4276 QString::number(int(element(line, 2, " ").toInt()/60))+
4277 " +" +
4278 QString::number(int(element(line, 3, " ").toInt()/60)).rightJustified(3) +
4279 "' (" +
4280 element(line, 4, " ")+
4281 ") H -"+
4282 element(line, 8, " ") +
4283 " +" +
4284 element(line, 9, " ");
4285
4286 recvSeekPlayer(player, condition);
4287 }
4288 }
4289
4290 // IGS review protocol
4291 // 56 CREATE 107
4292 // 56 DATA 107
4293 // 56 OWNER 107 eb5 3k
4294 // 56 BOARDSIZE 107 19
4295 // 56 OPEN 107 1
4296 // 56 KIBITZ 107 1
4297 // 56 KOMI 107 0.50
4298 // 56 TITLE 107 yfh22-eb5(B) IGS
4299 // 56 SGFNAME 107 yfh22-eb5-09-03-24
4300 // 56 WHITENAME 107 yfh22
4301 // 56 WHITERANK 107 1d?
4302 // 56 BLACKNAME 107 eb5
4303 // 56 BLACKRANK 107 3k
4304 // 56 GAMERESULT 107 B+Resign
4305 // 56 NODE 107 1 0 0 0
4306 // 56 NODE 107 2 1 16 4
4307 // 56 NODE 107 3 2 16 16
4308 // 56 NODE 107 4 1 4 4
4309 // 56 NODE 107 5 2 4 16
4310 // 56 NODE 107 1 0 0 0
4311 // 56 CONTROL 107 eb5
4312 // 56 DATAEND 107
4313 // 56 ERROR That user's client does not support review.
4314 // 56 INVITED_PLAY 58 yfh2test
4315 void IGSConnection::handle_review(QString line)
4316 {
4317 line = line.remove(0, 2).trimmed();
4318 #ifdef FIXME
4319 if (line.contains("CREATE"))
4320 aGame->number = element(line, 1, " ").toInt();
4321 if (line.contains("OWNER"))
4322 aGame->player = element(line, 2, " ");
4323 if (line.contains("BOARDSIZE"))
4324 {
4325 aGame->board_size = element(line, 2, " ").toInt();
4326 memory = aGame->board_size;
4327 }
4328 if (line.contains("KOMI"))
4329 aGame->K = element(line, 2, " ");
4330 if (line.contains("WHITENAME"))
4331 aGame->wname = element(line, 2, " ");
4332 if (line.contains("WHITERANK"))
4333 {
4334 aGame->white_rank = element(line, 2, " ");
4335 fixRankString(&(aGame->white_rank));
4336 aGame->white_rank_score = rankToScore(aGame->white_rank);
4337 }
4338 if (line.contains("BLACKNAME"))
4339 aGame->black_name = element(line, 2, " ");
4340 if (line.contains("BLACKRANK"))
4341 {
4342 aGame->black_rank = element(line, 2, " ");
4343 fixRankString(&(aGame->black_rank));
4344 aGame->black_rank_score = rankToScore(aGame->black_rank);
4345 }
4346 if (line.contains("GAMERESULT"))
4347 {
4348 aGame->res = element(line, 2, " ");
4349 emit signal_gameReview(aGame);
4350 break;
4351 }
4352
4353 if (line.contains("INVITED_PLAY"))
4354 {
4355 emit signal_reviewInvite(element(line, 1, " "), element(line, 2, " "));
4356 break ;
4357 }
4358
4359 if (line.contains("NODE"))
4360 {
4361
4362 StoneColor sc = stoneNone;
4363 int c = element(line, 3, " ").toInt();
4364
4365 if (c==1)
4366 sc = stoneBlack;
4367 else if(c==2)
4368 sc=stoneWhite;
4369
4370
4371 emit signal_reviewNode(element(line, 1, " ").toInt(), element(line, 2, " ").toInt(), sc, element(line, 4, " ", "EOL").toInt() ,element(line, 3, " ").toInt() );
4372 break ;
4373 }
4374 #endif //FIXME
4375 }
4376
4377 QString IGSConnection::element(const QString &line, int index, const QString &del1, const QString &del2, bool killblanks)
4378 {
4379 int len = line.length();
4380 int idx = index;
4381 int i;
4382 QString sub;
4383
4384 // kill trailing white spaces
4385 while (/*(int)*/ len > 0)
4386 {
4387 if (line[len-1] < 33)
4388 len--;
4389 else
4390 break;
4391 }
4392
4393 // right delimiter given?
4394 if (del2.isEmpty())
4395 {
4396 // ... no right delimiter
4397 // element("a b c", 0, " ") -> "a"
4398 // element(" a b c", 0, " ") -> "a" spaces at the beginning are skipped
4399 // element("a b c", 1, " ") -> "b"
4400 // element(" a b c", 1, " ") -> "b", though two spaces between "a" and "b"
4401
4402 // skip (delimiters) spaces at the beginning
4403 i = 0;
4404 // while (i < len && line[i] == del1)
4405 while (i < len && line[i] == ' ')
4406 i++;
4407
4408 for (; idx != -1 && i < len; i++)
4409 {
4410 // skip multiple (delimiters) spaces before wanted sequence starts
4411 // while (idx > 0 && line[i] == del1 && i < len-1 && line[i+1] == del1)
4412 while (idx > 0 && line[i] == ' ' && i < len-1 && line[i+1] == ' ')
4413 i++;
4414
4415 // look for delimiters, maybe more in series
4416 if (line.mid(i,del1.length()) == del1)
4417 idx--;
4418 else if (idx == 0)
4419 sub += line[i];
4420 }
4421 }
4422 else
4423 {
4424 // ... with right delimiter
4425 // element("a b c", 0, " ", " ") -> "b"
4426 // element("(a) (b c)", 0, "(", ")") -> "a"
4427 // element("(a) (b c)", 1, "(", ")") -> "b c"
4428 // element("(a) (b c)", 0, " ", ")") -> "(b c"
4429 // element("(a) (b c)", 1, " ", ")") -> "c"
4430 // element("(a) (b c)", 1, "(", "EOL") -> "b c)"
4431
4432 // skip spaces at the beginning
4433 i = 0;
4434 while (i < len && line[i] == ' ')
4435 i++;
4436
4437 // seek left delimiter
4438 idx++;
4439
4440 for (; idx != -1 && i < len; i++)
4441 {
4442 // skip multiple (delimiters) spaces before wanted sequence starts
4443 // while (idx > 0 && line[i] == del1 && i < len-1 && line[i+1] == del1)
4444 while (idx > 0 && line[i] == ' ' && i < len-1 && line[i+1] == ' ')
4445 i++;
4446
4447 if ((idx != 0 && line.mid(i,del1.length()) == del1) ||
4448 (idx == 0 && line.mid(i,del2.length()) == del2))
4449 {
4450 idx--;
4451 }
4452 else if (idx == 0)
4453 {
4454 // EOL (End Of Line)?
4455 if (del2 == QString("EOL"))
4456 {
4457 // copy until end of line
4458 for (int j = i; j < len; j++)
4459 if (!killblanks || line[j] != ' ')
4460 sub += line[j];
4461
4462 // leave loop
4463 idx--;
4464 }
4465 else if (!killblanks || line[i] != ' ')
4466 sub += line[i];
4467 }
4468 }
4469 }
4470
4471 return sub;
4472 }
4473
4474 unsigned int IGSConnection::idleTimeToSeconds(QString time)
4475 {
4476 QString i = time;
4477 /* I guess its either minutes or seconds here, not both */
4478 if(time.contains("m"))
4479 {
4480 i.replace(QRegExp("[m*]"), "");
4481 return (60 * i.toInt());
4482 }
4483 else
4484 {
4485 i.replace(QRegExp("s"), "");
4486 return i.toInt();
4487 }
4488 /*m1 = time1; m2 = time2;
4489 s1 = time1; s2 = time2;
4490 m1.replace(QRegExp("[m*]"), "");
4491 m2.replace(QRegExp("[m*]"), "");
4492 s1.replace(QRegExp("[*m]"), "");
4493 s1.replace(QRegExp("s"), "");
4494 s2.replace(QRegExp("[*m]"), "");
4495 s2.replace(QRegExp("s"), "");
4496 int seconds1 = (m1.toInt() * 60) + s1.toInt();
4497 int seconds2 = (m2.toInt() * 60) + s2.toInt();*/
4498 }
4499
4500 void IGSConnection::fixRankString(QString * rank)
4501 {
4502 if(rank->at(rank->length() - 1) == '*')
4503 rank->truncate(rank->length() - 1);
4504 else if(*rank == "NR") {}
4505 #ifdef IGS_OLDRATING //doublecheck WING/LGS FIXME
4506 else
4507 *rank += "?";
4508 #endif //IGS_OLDRATING
4509 }
4510
4511 PlayerListing * IGSConnection::getPlayerListingNeverFail(QString & name)
4512 {
4513 return getDefaultRoom()->getPlayerListing(name);
4514 }