Debian Codegraph @

src/network/wing.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 #include <string.h>
24 #include "wing.h"
25 #include "consoledispatch.h"
26 #include "room.h"
27 #include "boarddispatch.h"
28 #include "gamedialog.h"
29 #include "talk.h"
30 #include "playergamelistings.h"
31
32 WingConnection::WingConnection(const ConnectionCredentials credentials) :
33 IGSConnection(credentials) {}
34
35 QString WingConnection::getPlaceString(void)
36 {
37 return "WING";
38 }
39
40 void WingConnection::sendPlayersRequest(void)
41 {
42 sendText("user\r\n");
43 }
44
45 void WingConnection::requestGameInfo(unsigned int game_id)
46 {
47 char string[20];
48 snprintf(string, 20, "refresh %d\r\n", game_id);
49 sendText(string);
50 snprintf(string, 20, "all %d\r\n", game_id);
51 sendText(string);
52 }
53
54 void WingConnection::sendMatchRequest(MatchRequest * mr)
55 {
56 /* Copied from gamedialog.cpp */
57 QString color;
58 switch(mr->color_request)
59 {
60 case MatchRequest::BLACK:
61 color = " B ";
62 break;
63 case MatchRequest::WHITE:
64 color = " W ";
65 break;
66 case MatchRequest::NIGIRI:
67 color = " N ";
68 break;
69 }
70
71 if(mr->nmatch)
72 {
73 qDebug("Wing has no nmatch");
74 /*qDebug("nmatch times: %s %s %d %d", mr->maintime.toLatin1().constData(), mr->byoperiodtime.toLatin1().constData(), mr->maintime.toInt(), mr->byoperiodtime.toInt());
75 qDebug("time to seconds %d %d", time_to_seconds(mr->maintime), time_to_seconds(mr->byoperiodtime));
76 qDebug("hc: %d", mr->handicap);
77 sendText("nmatch " + mr->opponent +
78 color +
79 QString::number(mr->handicap) + " " +
80 QString::number(mr->board_size) + " " +
81 QString::number(time_to_seconds(mr->maintime)) + " " +
82 QString::number(time_to_seconds(mr->byoperiodtime)) + " " +
83 QString::number(mr->byoperiods) + " " +
84 "0 0 0");*/
85
86 }
87 else
88 {
89 sendText("match " + mr->opponent + " " +
90 mr->maintime + " " +
91 mr->periodtime + " " +
92 QString::number(mr->board_size) + " " +
93 //extra time?
94 color + "\r\n");
95 }
96 }
97
98 void WingConnection::sendMove(unsigned int game_id, MoveRecord * move)
99 {
100 switch(move->flags)
101 {
102 case MoveRecord::PASS:
103 sendText("pass\r\n");
104 break;
105 case MoveRecord::REQUESTUNDO:
106 sendText("undo\r\n");
107 break;
108 /* WING is such that opponent has to undo the move, one has
109 * to ask in chat, no requesting it as far as I can see. So
110 * if one hits undo, it undos their move, and it has to be
111 * one's turn. We should probably gray undo button out if
112 * its not one's turn, but this is tricky. FIXME */
113 //case MoveRecord::UNDO:
114 // sendText("undo");
115 // break;
116 //case MoveRecord::REFUSEUNDO:
117 // sendText("noundo");
118 // break;
119 case MoveRecord::RESIGN:
120 sendText("resign\r\n");
121 break;
122 case MoveRecord::NONE:
123 {
124 char c1 = move->x - 1 + 'A';
125
126 if(move->x > 8) // no I in IGS
127 c1++;
128 BoardDispatch *bd = getIfBoardDispatch(game_id);
129 if(!bd)
130 {
131 qDebug("Can't get board dispatch for sending move");
132 return;
133 }
134 GameData * g = bd->getGameData();
135 //if(g->board_size > 9)
136 int c2 = g->board_size + 1 - move->y;
137 //else
138 // c2 = move->y;
139 /* Why do we send the id here but
140 * not with the others? Can we play
141 * multiple games?? */
142 sendText(QString(c1) + QString::number(c2) + " " + game_id);
143 }
144 break;
145 case MoveRecord::DONE_SCORING:
146 sendText("done\r\n");
147 break;
148 default:
149 qDebug("IGSc: unhandled type\n");
150 break;
151 }
152 }
153
154 void WingConnection::onReady(void)
155 {
156 if(firstonReadyCall)
157 {
158 if(connectionState != PasswordSent)
159 return;
160 firstonReadyCall = 0;
161 setState(Connected);
162 setKeepAlive(600);
163 /* FIXME Note that wing and lgs use "set" instead of
164 * "toggle" possibly, so that's maybe an issue */
165 sendPlayersRequest();
166 sendGamesRequest();
167 qDebug("Ready!");
168 }
169 }
170
171 void WingConnection::handle_info(QString line)
172 {
173 //PlayerListing * aPlayer;
174 static PlayerListing * statsPlayer;
175 BoardDispatch * boarddispatch;
176 Room * room = getDefaultRoom();
177 static QString memory_str;
178 static int memory = 0;
179 qDebug("9: %s", line.toLatin1().constData());
180 line = line.remove(0, 2).trimmed(); //remove command code
181 // status messages
182 if (line.contains("Set open to be"))
183 {
184 bool val = (line.indexOf("False") == -1);
185 room->recvToggle(0, val);
186 }
187 else if (line.contains("Setting you open for matches"))
188 room->recvToggle(0, true);
189 else if (line.contains("Set looking to be"))
190 {
191 bool val = (line.indexOf("False") == -1);
192 room->recvToggle(1, val);
193 }
194 // 9 Set quiet to be False.
195 else if (line.contains("Set quiet to be"))
196 {
197 bool val = (line.indexOf("False") == -1);
198 room->recvToggle(2, val);
199 }
200 else if (line.indexOf("Channel") == 0)
201 {
202 // channel messages
203 QString e1 = element(line, 1, " ");
204 if (e1.at(e1.length()-1) == ':')
205 e1.truncate(e1.length()-1);
206 #ifdef FIXME
207 int nr = e1.toInt();
208
209 if (line.contains("turned on."))
210 {
211 // turn on channel
212 emit signal_channelinfo(nr, QString("*on*"));
213 }
214 else if (line.contains("turned off."))
215 {
216 // turn off channel
217 emit signal_channelinfo(nr, QString("*off*"));
218 }
219 else if (!line.contains("Title:") || gsName == GS_UNKNOWN)
220 {
221 // keep in memory to parse next line correct
222 memory = nr;
223 emit signal_channelinfo(memory, line);
224 memory_str = "CHANNEL";
225 }
226 #endif //FIXME
227 // return IT_OTHER;
228 }
229 #ifdef FIXME
230 else if (memory != 0 && !memory_str.isEmpty() && memory_str == "CHANNEL")
231 {
232 //emit signal_channelinfo(memory, line);
233
234 // reset memory
235 memory = 0;
236 memory_str = QString();
237 // return IT_OTHER;
238 }
239 #endif //FIXME
240 // IGS: channelinfo
241 // 9 #42 Title: Untitled -- Open
242 // 9 #42 broesel zero815 Granit
243 else if (line.contains("#"))
244 {
245 #ifdef FIXME
246 int nr = element(line, 0, "#", " ").toInt();
247 QString msg = element(line, 0, " ", "EOL");
248 emit signal_channelinfo(nr, msg);
249 #endif //FIXME
250 }
251 // NNGS: channels
252 else if (line.contains("has left channel") || line.contains("has joined channel"))
253 {
254 #ifdef FIXME
255 QString e1 = element(line, 3, " ", ".");
256 int nr = e1.toInt();
257
258 // turn on channel to get full info
259 emit signal_channelinfo(nr, QString("*on*"));
260 #endif //FIXME
261 }
262 else if (line.contains("Game is titled:"))
263 {
264 #ifdef FIXME
265 QString t = element(line, 0, ":", "EOL");
266 emit signal_title(t);
267 #endif //FIXME
268 return;
269 }
270 else if (line.contains("offers a new komi "))
271 {
272 // NNGS: 9 physician offers a new komi of 1.5.
273 QString komi = element(line, 6, " ");
274 if (komi.at(komi.length()-1) == '.')
275 komi.truncate(komi.length() - 1);
276 QString opponent = element(line, 0, " ");
277
278 // true: request
279 //emit signal_komi(opponent, komi, true);
280 }
281 else if (line.contains("Komi set to"))
282 {
283 // NNGS: 9 Komi set to -3.5 in match 10
284 QString komi = element(line, 3, " ");
285 QString game_id = element(line, 6, " ");
286
287 // false: no request
288 //emit signal_komi(game_id, komi, false);
289 }
290 else if (line.contains("wants the komi to be"))
291 {
292 // IGS: 9 qGoDev wants the komi to be 1.5
293 QString komi = element(line, 6, " ");
294 QString opponent = element(line, 0, " ");
295
296 // true: request
297 //emit signal_komi(opponent, komi, true);
298 }
299 else if (line.contains("Komi is now set to"))
300 {
301 // 9 Komi is now set to -3.5. -> oppenent set for our game
302 QString komi = element(line, 5, " ");
303 // error? "9 Komi is now set to -3.5.9 Komi is now set to -3.5"
304 if (komi.contains(".9"))
305 komi = komi.left(komi.length() - 2);
306
307 // false: no request
308 //emit signal_komi(QString(), komi, false);
309 }
310 else if (line.contains("Set the komi to"))
311 {
312 // NNGS: 9 Set the komi to -3.5 - I set for own game
313 QString komi = element(line, 4, " ");
314
315 // false: no request
316 //emit signal_komi(QString(), komi, false);
317 }
318 else if (line.contains("game will count"))
319 {
320 // IGS: 9 Game will not count towards ratings.
321 // 9 Game will count towards ratings.
322 //emit signal_freegame(false);
323 }
324 else if (line.contains("game will not count", Qt::CaseInsensitive))
325 {
326 // IGS: 9 Game will not count towards ratings.
327 // 9 Game will count towards ratings.
328 //emit signal_freegame(true);
329 }
330 else if ((line.contains("[") || line.contains("yes")) && line.length() < 6)
331 {
332 // 9 [20] ... channelinfo
333 // 9 yes ... ayt
334 return;
335 }
336 else if (line.contains("as restarted your game") ||
337 line.contains("has restored your old game"))
338 {
339 if (line.contains("restarted"))
340 // memory_str -> see case 15 for continuation
341 protocol_save_string = element(line, 0, " ");
342 }
343 else if (line.contains("I suggest that"))
344 {
345 memory_str = line;
346 return;
347 }
348 else if (line.contains("and set komi to"))
349 {
350 #ifdef FIXME
351 // suggest message ...
352 if (!memory_str.isEmpty())
353 // something went wrong...
354 return;
355
356 line = line.simplified();
357
358 QString p1 = element(memory_str, 3, " ");
359 QString p2 = element(memory_str, 6, " ", ":");
360 bool p1_play_white = memory_str.contains("play White");
361
362 QString h, k;
363 if (line.contains("even game"))
364 h = "0";
365 else
366 h = element(line, 3, " ");
367
368 k = element(line, 9, " ", ".");
369
370 int size = 19;
371 if (line.contains("13x13"))
372 size = 13;
373 else if (line.contains("9x 9"))
374 {
375 size = 9;
376 memory_str = QString();
377 }
378
379 if (p1_play_white)
380 emit signal_suggest(p1, p2, h, k, size);
381 else
382 emit signal_suggest(p2, p1, h, k, size);
383 #endif //FIXME
384 return;
385 }
386 // 9 Match [19x19] in 1 minutes requested with xxxx as White.
387 // 9 Use <match xxxx B 19 1 10> or <decline xxxx> to respond.
388 // 9 NMatch requested with yfh2test(B 3 19 60 600 25 0 0 0).
389 // 9 Use <nmatch yfh2test B 3 19 60 600 25 0 0 0> or <decline yfh2test> to respond.
390 else if (line.contains("<decline") && line.contains("match"))
391 {
392 // false -> not my request: used in mainwin.cpp
393 line = element(line, 0, "<", ">");
394 MatchRequest * aMatch = new MatchRequest();
395 aMatch->opponent = line.section(" ", 1, 1);
396 if(line.section(" ",2,2) == "B")
397 aMatch->color_request = MatchRequest::BLACK;
398 else if(line.section(" ",2,2) == "N")
399 aMatch->color_request = MatchRequest::NIGIRI;
400 else
401 aMatch->color_request = MatchRequest::WHITE;
402 #ifdef FIXME
403 //WING has no match, ematch is like automach
404 if(line.contains("nmatch"))
405 {
406 aMatch->handicap = line.section(" ",3,3).toInt();
407 aMatch->board_size = line.section(" ",4,4).toInt();
408 //what kind of time? var name?
409 aMatch->maintime = line.section(" ",5,5);
410 aMatch->stonestime = line.section(" ",6,6);
411 aMatch->stones = line.section(" " ,7,7).toInt();
412 //what is "nmatch"?
413 aMatch->nmatch = true;
414 qDebug("nmatch in parser");
415 }
416 else
417 {
418 #endif //FIXME
419 aMatch->board_size = line.section(" ",3,3).toInt();
420 aMatch->maintime = line.section(" ",4,4).toInt();
421 aMatch->periodtime = line.section(" ",5,5).toInt();
422 aMatch->nmatch = false;
423 #ifdef FIXME
424 }
425 #endif //FIXME
426 PlayerListing * p = getPlayerListingNeverFail(aMatch->opponent);
427 const PlayerListing * us = getOurListing();
428 aMatch->our_name = us->name;
429 aMatch->our_rank = us->rank;
430 aMatch->their_rank = p->rank;
431
432 GameDialog * gameDialogDispatch = getGameDialog(p);
433 gameDialogDispatch->recvRequest(aMatch);
434 delete aMatch;
435 }
436 // 9 Match [5] with guest17 in 1 accepted.
437 // 9 Creating match [5] with guest17.
438 else if (line.contains("Creating match"))
439 {
440 QString nr = element(line, 0, "[", "]");
441 // maybe there's a blank within the brackets: ...[ 5]...
442 QString dummy = element(line, 0, "]", ".").trimmed();
443 QString opp = element(dummy, 1, " ");
444
445 // We let the 15 game record message create the board
446 //GameDialog * gameDialogDispatch = getGameDialog(opp);
447 //gameDialogDispatch->closeAndCreate();
448 //GameDialog * gameDialogDispatch =
449 // getGameDialog(opp);
450 //MatchRequest * mr = gameDialogDispatch->getMatchRequest();
451 //created_match_request = new MatchRequest(*mr);
452 //closeGameDialog(opp);
453 ////emit signal_matchCreate(nr, opp);
454 // automatic opening of a dialog tab for further conversation
455 ////emit signal_talk(opp, "", true);
456 }
457 else if (line.contains("Match") && line.contains("accepted"))
458 {
459 QString nr = element(line, 0, "[", "]");
460 QString opp = element(line, 3, " ");
461 ////emit signal_matchCreate(nr, opp);
462
463 }
464 // 9 frosla withdraws the match offer.
465 // 9 guest17 declines your request for a match.
466 else if (line.contains("declines your request for a match") ||
467 line.contains("withdraws the match offer"))
468 {
469 QString opp = element(line, 0, " ");
470 PlayerListing * p = getPlayerListingNeverFail(opp);
471 GameDialog * gameDialogDispatch = getGameDialog(p);
472 gameDialogDispatch->recvRefuseMatch(1);
473 }
474 //9 yfh2test declines undo
475 else if (line.contains("declines undo"))
476 {
477 // not the cleanest way : we should send this to a message box
478 //emit signal_kibitz(0, element(line, 0, " "), line);
479 return;
480 }
481
482 //9 yfh2test left this room
483 //9 yfh2test entered this room
484 else if (line.contains("this room"))
485 {} //emit signal_refresh(10);
486
487 //9 Requesting match in 10 min with frosla as Black.
488 else if (line.contains("Requesting match in"))
489 {
490 QString opp = element(line, 6, " ");
491 //emit signal_opponentopen(opp);
492 }
493 // NNGS: 9 Removing @ K8
494 // IGS: 9 Removing @ B5
495 // 49 Game 265 qGoDev is removing @ B5
496 else if (line.contains("emoving @"))
497 {
498 /* FIXME DOUBLE CHECK!!! */
499 /*if (gsName != IGS)
500 {
501 QString pt = element(line, 2, " ");
502 //emit signal_removeStones(pt, 0);
503 }*/
504 if(protocol_save_int < 0)
505 {
506 qDebug("Received stone removal message without game in scoring mode");
507 return;
508 }
509 else
510 {
511 boarddispatch = getBoardDispatch(protocol_save_int);
512 GameData * r = boarddispatch->getGameData();
513 if(!r)
514 {
515 qDebug("Game has no game Record, line: %d", __LINE__);
516 return;
517 }
518 MoveRecord * aMove = new MoveRecord();
519 QString pt = element(line, 2, " ");
520 aMove->flags = MoveRecord::REMOVE;
521 aMove->x = (int)(pt.toLatin1().at(0));
522 aMove->x -= 'A';
523 if(aMove->x < 9) //no I on IGS
524 aMove->x++;
525 pt.remove(0,1);
526 aMove->y = element(pt, 0, " ").toInt();
527 /* Do we need the board size here???*/
528 aMove->y = r->board_size + 1 - aMove->y;
529 boarddispatch->recvMove(aMove);
530 // FIXME Set protocol_save_int to -1???
531 delete aMove;
532 }
533 }
534 // 9 You can check your score with the score command, type 'done' when finished.
535 else if (line.contains("check your score with the score command"))
536 {
537 // FIXME: I guess we're only playing one game at a time?
538 qDebug("Getting boarddispatch from memory: %d", protocol_save_int);
539 boarddispatch = getBoardDispatch(protocol_save_int);
540 boarddispatch->recvEnterScoreMode();
541 memory_str = QString("rmv@");
542 }
543 // IGS: 9 Board is restored to what it was when you started scoring
544 else if (line.contains("what it was when you"))
545 {
546 //emit signal_restoreScore();
547 }
548 // WING: 9 Use <adjourn> to adjourn, or <decline adjourn> to decline.
549 else if (line.contains("Use adjourn to") || line.contains("Use <adjourn> to"))
550 {
551 qDebug("parser->case 9: Use adjourn to");
552 ////emit signal_requestDialog("adjourn", "decline adjourn", 0, 0);
553 /* memory is not NECESSARILY accurate to the game we're playing !!! */
554 /* FIXME The fix me is to make sure that this gets sent
555 * to the game we're actually playing!!! WING only allows the
556 * playing of one game. */
557 boarddispatch = getBoardDispatch(memory);
558 boarddispatch->recvRequestAdjourn();
559 }
560 // 9 frosla requests to pause the game.
561 else if (line.contains("requests to pause"))
562 {
563 //emit signal_requestDialog("pause", 0, 0, 0);
564 }
565 else if (line.contains("been adjourned"))
566 {
567 // remove game from list - special case: own game
568 #ifdef FIXME
569 aGame->nr = "@";
570 aGame->running = false;
571
572 //emit signal_game(aGame);
573 #endif //FIXME
574 /* There's several after the fact server INFO messages about
575 * games adjourning so we only need to close for one of them
576 * The 21 message might actually be better FIXME*/
577 boarddispatch = getIfBoardDispatch(memory);
578 if(boarddispatch)
579 {
580 boarddispatch->adjournGame();
581 closeBoardDispatch(memory);
582 }
583 }
584 // 9 Game 22: frosla vs frosla has adjourned.
585 else if (line.contains("has adjourned"))
586 {
587 int number = element(line, 0, " ", ":").toInt();
588 GameListing * aGame = room->getGameListing(number);
589 aGame->running = false;
590
591 // for information
592 //aGame->Sz = "has adjourned.";
593
594 #ifdef OLD
595 //emit signal_game(aGame);
596 // //emit signal_move(aGame);
597 #endif //OLD
598 // No need to get existing listing because
599 // this is just to falsify the listing
600 room->recvGameListing(aGame);
601 }
602 // 9 Removing game 30 from observation list.
603 else if (line.contains("from observation list"))
604 {
605 // is done from qGoIF
606 // //emit signal_addToObservationList(-1);
607 // aGame->nr = element(line, 2, " ").toInt();
608 // aGame->Sz = "-";
609 // aGame->running = false;
610 //emit signal_observedGameClosed(element(line, 2, " ").toInt());
611 return;
612 }
613 // 9 Adding game 4 to observation list.
614 else if (line.contains("to observation list"))
615 {
616 /* Create the board dispatch to recv info */
617 getBoardDispatch(element(line, 2, " ").toInt());
618 /* We could also use the below */
619 protocol_save_int = -1;
620 return;
621 }
622 // 9 Games currently being observed: 31, 36, 45.
623 else if (line.contains("Games currently being observed"))
624 {
625 if (line.contains("None"))
626 {
627 //emit signal_addToObservationList(0);
628 }
629 else
630 {
631 #ifdef FIXME
632 // don't work correct at IGS!!!
633 int i = line.count(',');
634 qDebug(QString("observing %1 games").arg(i+1).toLatin1());
635 // //emit signal_addToObservationList(i+1);
636 #endif //FIXME
637 }
638
639 // return IT_OTHER;
640 }
641 // 9 1 minutes were added to your opponents clock
642 else if (line.contains("minutes were added"))
643 {
644 #ifdef FIXME
645 int t = element(line, 0, " ").toInt();
646 emit signal_timeAdded(t, false);
647 #endif //FIXME
648 }
649 // 9 Your opponent has added 1 minutes to your clock.
650 else if (line.contains("opponent has added"))
651 {
652 #ifdef FIXME
653 int t = element(line, 4, " ").toInt();
654 emit signal_timeAdded(t, true);
655 #endif //FIX
656 }
657 // NNGS: 9 Game clock paused. Use "unpause" to resume.
658 else if (line.contains("Game clock paused"))
659 {
660 #ifdef FIXME
661 emit signal_timeAdded(-1, true);
662 #endif //FIX
663 }
664 // NNGS: 9 Game clock resumed.
665 else if (line.contains("Game clock resumed"))
666 {
667 #ifdef FIXME
668 emit signal_timeAdded(-1, false);
669 #endif //FIX
670 }
671 // 9 Increase frosla's time by 1 minute
672 else if (line.contains("s time by"))
673 {
674 #ifdef FIXME
675 int t = element(line, 4, " ").toInt();
676
677 if (line.contains(getUsername()))
678 emit signal_timeAdded(t, true);
679 else
680 emit signal_timeAdded(t, false);
681 #endif //FIXME
682 }
683 else if(line.contains("has typed done"))
684 {
685 /* FIXME maybe we want to add something here, like a kibitz
686 * message or something like "hit done when done", maybe not.
687 * there's also the done [1|2|.] messages, but that
688 * doesn't seem necessary */
689 }
690 // 9 Setting your . to Banana [text] (idle: 0 minutes)
691 else if (line.contains("Setting your . to"))
692 {
693 QString busy = element(line, 0, "[", "]");
694 if (!busy.isEmpty())
695 {
696 QString player = element(line, 4, " ");
697 // true = player
698 ////emit signal_talk(player, "[" + busy + "]", true);
699 }
700 }
701 /* I'm pretty sure, as with LGS and WING, we want to
702 * update the game lists here but not use 9 messages
703 * for the board
704 * Okay. WING will send 9 messages for certain observed games
705 * for point wins WITHOUT a 22 message (might be only demo
706 * games or something but...*/
707 #ifdef FIXME
708 else if (line.contains("resigns.") ||
709 line.contains("adjourned.") ||
710 //don't intefere with status
711 //line.contains(" : W ", Qt::CaseSensitive) ||
712 //line.contains(" : B ", Qt::CaseSensitive) ||
713 line.contains("forfeits on") ||
714 line.contains("lost by"))
715 {
716 GameResult * aGameResult;
717 int number = element(line, 0, " ", ":").toInt();
718 GameListing * aGame = room->getGameListing(number);
719 aGame->number = number;
720 aGame->running = false;
721 // for information
722 aGame->result = element(line, 4, " ", "}");
723
724 boarddispatch = getBoardDispatch(aGame->number);
725 /* FIXME: This shouldn't create a new board if
726 * we're not watching it.
727 * Also WING sometimes sends 9 and sometimes sends 21 perhaps
728 * depending on whether its observed or resign versus score/result
729 * we don't want to send double messages.*/
730 if(boarddispatch)
731 {
732 aGameResult = new GameResult();
733
734 if(line.contains(" : W ", Qt::CaseSensitive) ||
735 line.contains(" : B ", Qt::CaseSensitive))
736 {
737 aGameResult->result = GameResult::SCORE;
738
739 int posw, posb;
740 posw = aGame->result.indexOf("W ");
741 posb = aGame->result.indexOf("B ");
742 bool wfirst = posw < posb;
743 float sc1, sc2;
744 sc1 = aGame->result.mid(posw+1, posb-posw-2).toFloat();
745 //sc2 = aGame->result.right(aGame->result.length()-posb-1).toFloat();
746 /* FIXME This may be right for IGS and wrong for WING */
747 //sc2 = aGame->result.mid(posb +1, posb + 6).toFloat();
748 sc2 = element(aGame->result, 4, " ").toFloat();
749 qDebug("sc1: %f sc2: %f\n", sc1, sc2);
750 if(sc1 > sc2)
751 {
752 aGameResult->winner_score = sc1;
753 aGameResult->loser_score = sc2;
754 }
755 else
756 {
757 aGameResult->winner_score = sc2;
758 aGameResult->loser_score = sc1;
759
760 }
761
762 if (!wfirst)
763 {
764 int h = posw;
765 posw = posb;
766 posb = h;
767 if(sc2 > sc1)
768
769 aGameResult->winner_color = stoneWhite;
770 else
771 aGameResult->winner_color = stoneBlack;
772 }
773 else
774 {
775 if(sc1 > sc2)
776 aGameResult->winner_color = stoneWhite;
777 else
778 aGameResult->winner_color = stoneBlack;
779 }
780
781
782 }
783 else if(line.contains("forfeits on time"))
784 {
785 aGameResult->result = GameResult::TIME;
786 if(line.contains("Black"))
787 {
788 aGameResult->winner_color = stoneWhite;
789 }
790 else
791 {
792 aGameResult->winner_color = stoneBlack;
793 }
794 }
795 else if(line.contains("resign", Qt::CaseInsensitive))
796 {
797 aGameResult->result = GameResult::RESIGN;
798 if(line.contains("Black"))
799 {
800 aGameResult->winner_color = stoneWhite;
801 }
802 else
803 {
804 aGameResult->winner_color = stoneBlack;
805 }
806 }
807 else if(line.contains("lost by"))
808 {
809 aGameResult->result = GameResult::SCORE;
810 if(line.contains("Black"))
811 {
812 aGameResult->winner_color = stoneWhite;
813 }
814 else
815 {
816 aGameResult->winner_color = stoneBlack;
817
818 }
819
820 }
821 }
822
823
824 if (aGame->result.isEmpty())
825 aGame->result = "-";
826 else if (aGame->result.indexOf(":") != -1)
827 aGame->result.remove(0,2);
828
829 if(boarddispatch)
830 {
831 qDebug("Receiving result!!!");
832 boarddispatch->recvResult(aGameResult);
833 /* is kibitzing this here what we want?*/
834 /* FIXME... should be a shortened list, not the full
835 * result msg kibitz */
836 boarddispatch->recvKibitz("", line);
837 }
838 room->recvGameListing(aGame);
839 delete aGame;
840 return;
841 }
842 #endif //FIXME
843 // NNGS: 9 Messages: (after 'message' cmd)
844 // 8 File
845 // <msg>
846 // 8 File
847 // 9 Please type "erase" to erase your messages after reading
848 else if (line.contains("Messages:"))
849 {
850 // parse like IGS cmd nr 14: Messages
851 memory = 14;
852 }
853 // IGS: 9 You have a message. To read your messages, type: message
854 // NNGS: 9 You have 1 messages. Type "messages" to display them
855 else if (line.contains("You have") && line.contains("messages"))
856 {
857 // remove cmd nr
858 line = line.trimmed();
859 line = line.remove(0, 2);
860 getConsoleDispatch()->recvText(line.toLatin1().constData());
861 return;
862 }
863 // 9 Observing game 2 (chmeng vs. myao) :
864 // 9 shanghai 9k* henry 15k
865 // 9 Found 2 observers.
866 else if (line.contains("Observing game ", Qt::CaseSensitive))
867 {
868 // right now: only need for observers of teaching game
869 // game number
870 bool ok;
871 memory = element(line, 2, " ").toInt(&ok);
872 if (ok)
873 {
874 memory_str = "observe";
875 // FIXME
876 //emit signal_clearObservers(memory);
877 BoardDispatch * boarddispatch = getIfBoardDispatch(memory);
878 if(boarddispatch)
879 {
880 GameData * g = boarddispatch->getGameData();
881 g->white_name = element(line, 0, "(", " ");
882 g->black_name = element(line, 4, " ", ")");
883 boarddispatch->gameDataChanged();
884 }
885 return;
886 }
887 }
888 else if (!memory_str.isEmpty() && memory_str == "observe" && line.contains("."))
889 {
890 // QString cnt = element(line, 1, " ");
891 // //emit signal_kibitz(memory, "00", "");
892
893 memory = 0;
894 memory_str = QString();
895
896 return;
897 }
898 else if (!memory_str.isEmpty() && memory_str == "observe")
899 {
900 QString name = element(line, 0, " ");
901 QString rank;
902 boarddispatch = getBoardDispatch(memory);
903 if(!boarddispatch)
904 {
905 qDebug("No boarddispatch for observer list\n");
906 return;
907 }
908
909 for (int i = 1; ! name.isEmpty(); i++)
910 {
911 rank = element(line, i, " ");
912 fixRankString(&rank);
913 // send as kibitz from "0"
914 PlayerListing * p = getPlayerListingNeverFail(name);
915 boarddispatch->recvObserver(p, true);
916 name = element(line, ++i , " ");
917 }
918
919 return;
920 }
921 else if(line.contains("Found") && line.contains("observers"))
922 {
923 memory = 0;
924 memory_str = QString();
925 }
926 else if (line.contains("****") && line.contains("Players"))
927 return;
928
929 // 9 qGoDev has resigned the game.
930 else if (line.contains("has resigned the game"))
931 {
932 /* If this is our game, there's a 21 message maybe later that
933 * should be handling it FIXME */
934 boarddispatch = getIfBoardDispatch(memory);
935 if(!boarddispatch)
936 {
937 qDebug("No board dispatch for \"resigned the game\"\n");
938 return;
939 }
940 #ifdef FIXME
941 aGame->running = false;
942 #endif //FIXME
943
944 GameResult * aGameResult = new GameResult();
945 aGameResult->loser_name = element(line, 0, " ");
946 aGameResult->result = GameResult::RESIGN;
947 /* We need to set unknown/none color here */
948 boarddispatch->recvResult(aGameResult);
949 return;
950
951 }
952 else if (line.contains("has run out of time"))
953 {
954 boarddispatch = getBoardDispatch(protocol_save_int);
955 if(!boarddispatch)
956 {
957 qDebug("No board dispatch for \"has run out of time\"\n");
958 return;
959 }
960 #ifdef FIXME
961 aGame->running = false;
962 #endif //FIXME
963
964 GameResult * aGameResult = new GameResult();
965 aGameResult->loser_name = element(line, 0, " ");
966 aGameResult->result = GameResult::TIME;
967 /* We need to set unknown/none color here */
968 boarddispatch->recvResult(aGameResult);
969 return;
970
971 }
972
973
974
975 //9 Player: yfh2
976 //9 Game: go (1)
977 //9 Language: default
978 //9 Rating: 6k* 23
979 //9 Rated Games: 21
980 //9 Rank: 8k 21
981 //9 Wins: 13
982 //9 Losses: 16
983 //9 Idle Time: (On server) 0s
984 //9 Address: [email protected]
985 //9 Country: France
986 //9 Reg date: Tue Nov 18 04:01:05 2003
987 //9 Info: yfh2
988 //9 Defaults (help defs): time 0, size 0, byo-yomi time 0, byo-yomi stones 0
989 //9 Verbose Bell Quiet Shout Automail Open Looking Client Kibitz Chatter
990 //9 Off On On On On Off Off On On On
991
992 else if (line.contains("Player:"))
993 {
994 QString name = element(line, 1, " ");
995 statsPlayer = getPlayerListingNeverFail(name);
996 #ifdef FIXME
997 /* So this would have cleared the structure, but
998 * we're just creating a new empty object later.
999 * One HUGE FIXME question is what we might be
1000 * overriding on the other side. Maybe we need
1001 * a bit vector specifying what has updated when
1002 * the message is passed. So the notion before
1003 * was that the structure was held until
1004 * completely filled and then passed up*/
1005
1006 statsPlayer->extInfo = "";
1007 statsPlayer->won = "";
1008 statsPlayer->lost = "";
1009 statsPlayer->country = "";
1010 statsPlayer->nmatch_settings = "";
1011 statsPlayer->rank = "";
1012 statsPlayer->info = "";
1013 statsPlayer->address = "";
1014 statsPlayer->play_str = "";
1015 statsPlayer->obs_str = "";
1016 statsPlayer->idle = "";
1017 statsPlayer->rated = "";
1018 // not sure it is the best way : above code seem to make use of "signal"
1019 // but we don't need this apparently for handling stats
1020 #endif //FIXME
1021 protocol_save_string = "STATS";
1022 return;
1023 }
1024
1025 else if (line.contains("Address:"))
1026 {
1027 statsPlayer->email_address = element(line, 1, " ");
1028 return;
1029 }
1030
1031 else if (line.contains("Last Access"))
1032 {
1033 statsPlayer->idletime = element(line, 4, " ")+ " " + element(line, 5, " ")+" " + element(line, 6, " ");
1034 statsPlayer->seconds_idle = idleTimeToSeconds(statsPlayer->idletime);
1035 return;
1036 }
1037
1038 else if (line.contains("Rating:"))
1039 {
1040 statsPlayer->rank = element(line, 1, " ");
1041 fixRankString(&(statsPlayer->rank));
1042 statsPlayer->rank_score = rankToScore(statsPlayer->rank);
1043 return;
1044 }
1045
1046 else if (line.contains("Wins:"))
1047 {
1048 statsPlayer->wins = element(line, 1, " ").toInt();
1049 return;
1050 }
1051
1052 else if (line.contains("Losses:"))
1053 {
1054 statsPlayer->losses = element(line, 1, " ").toInt();
1055 return;
1056 }
1057
1058 else if ((line.contains("Country:"))||(line.contains("From:"))) //IGS || LGS
1059 {
1060 statsPlayer->country = element(line, 0, " ","EOL");
1061 return;
1062 }
1063 else if (line.contains("Experience on WING")) //WING
1064 {
1065 // FIXME Take out the initial "9" somehow
1066 statsPlayer->extInfo = line;
1067 return;
1068 }
1069 else if (line.contains("User Level:")) //LGS
1070 {
1071 statsPlayer->extInfo = line;
1072 return;
1073 }
1074
1075
1076 else if ((line.contains("Info:"))&& !(line.contains("Rank Info:")))
1077 {
1078 #ifdef FIXME
1079 if (! statsPlayer->info.isEmpty())
1080 statsPlayer->info.append("\n");
1081 statsPlayer->info.append(line);
1082 //emit signal_statsPlayer(statsPlayer);
1083 #endif //FIXME
1084 return;
1085 }
1086 else if (line.contains("(playing game")) //WING
1087 {
1088 //FIXME this is missing!!
1089 statsPlayer->playing = element(line, 1, " ",":").toInt();
1090 return;
1091 }
1092 else if (line.contains("Games Rated:")) //WING syntax
1093 {
1094 statsPlayer->rated_games = element(line, 1, " ").toInt();
1095 statsPlayer->wins = element(line, 0, "("," ").toInt();
1096 statsPlayer->losses = element(line, 6, " ").toInt();
1097 return;
1098 }
1099 else if (line.contains("Idle Time:"))
1100 {
1101 statsPlayer->idletime = element(line, 2, " ");
1102 statsPlayer->seconds_idle = idleTimeToSeconds(statsPlayer->idletime);
1103 return;
1104 }
1105
1106 else if (line.contains("Last Access"))
1107 {
1108 statsPlayer->idletime = "not on";
1109 return;
1110 }
1111
1112 else if(line.contains(" Yes ") || line.contains(" No ")) //may not be unique
1113 {
1114 if(statsPlayer)
1115 {
1116 qDebug("talk name: %s", statsPlayer->name.toLatin1().constData());
1117 Talk * talk = getDefaultRoom()->getTalk(statsPlayer);
1118 if(talk)
1119 talk->updatePlayerListing();
1120 statsPlayer = 0;
1121 }
1122 }
1123
1124 //9 ROOM 01: FREE GAME ROOM;PSMNYACF;自由対局室;19;1;10;1,19,60,600,30,25,10,60,0
1125 //9 ROOM 10: SLOW GAME ROOM;PSMNYACF;スロー対局室;19;1;20
1126 //9 ROOM 92: PANDA OPEN ROOM;X;月例大会;19;10;15
1127 else if (! line.left(5).compare("ROOM "))
1128 {
1129
1130 //emit signal_room(element(line,0," ",";"),(element(line, 1,";")=="X") || (element(line, 1,";")=="P"));
1131 return;
1132 }
1133 else if(line.contains("File"))
1134 return;
1135
1136 if (protocol_save_string != "STATS")
1137 getConsoleDispatch()->recvText(line.toLatin1().constData());
1138 }
1139
1140 void WingConnection::handle_kibitz(QString line)
1141 {
1142 static QString memory_str;
1143 static int memory = 0;
1144 line = line.remove(0, 2).trimmed();
1145 BoardDispatch * boarddispatch;
1146 qDebug("kibitz: %s", line.toLatin1().constData());
1147 if (line.contains("Kibitz"))
1148 {
1149 // who is kibitzer
1150 memory_str = element(line, 0, " ", ":");
1151 // game number
1152 memory = element(line, 1, "[", "]").toInt();
1153 }
1154 else
1155 {
1156 if (memory_str.isEmpty())
1157 // something went wrong...
1158 return;
1159 if(line.contains("Starting observation"))
1160 {
1161 QString name = element(memory_str, 0 , " ");
1162 QString rank = element(memory_str, 0, "[", "]");
1163 fixRankString(&rank);
1164 qDebug("%s %s joining", name.toLatin1().constData(), rank.toLatin1().constData());
1165 // send as kibitz from "0"
1166
1167 boarddispatch = getBoardDispatch(memory);
1168 if(!boarddispatch)
1169 {
1170 qDebug("No board dispatch for this game!");
1171 return;
1172 }
1173 PlayerListing * p = getPlayerListingNeverFail(name);
1174 boarddispatch->recvObserver(p, true);
1175 memory = 0;
1176 memory_str.clear();
1177 return;
1178 }
1179 else if(line.contains("has entered byo-yomi again") ||
1180 line.contains("is entering byo-yomi"))
1181 {
1182 qDebug("byo-yomi message");
1183 memory = 0;
1184 memory_str.clear();
1185 // just ignore though we could FIXME use it to
1186 // reset the time
1187 // yeah, this needs to trigger byo yomi entry
1188 return;
1189 }
1190 else if(memory_str.contains("SgfComment"))
1191 {
1192 if(line.contains("dead @"))
1193 {
1194 boarddispatch = getBoardDispatch(memory);
1195 if(!boarddispatch)
1196 {
1197 qDebug("No board dispatch for this game!");
1198 return;
1199 }
1200 boarddispatch->recvEnterScoreMode();
1201 /* FIXME What if we're already in score mode,
1202 * what about the double pass signifying this??? */
1203 MoveRecord * aMove = new MoveRecord();
1204 aMove->flags = MoveRecord::REMOVE;
1205 QString point = element(line, 5, " ", "EOL");
1206 qDebug("Removal point: %s", point.toLatin1().constData());
1207
1208 aMove->x = (int)(point.toLatin1().at(0));
1209 aMove->x -= 'A';
1210 point.remove(0,1);
1211 aMove->y = element(point, 0, " ").toInt();
1212 GameListing * l = getDefaultRoom()->getGameListing(memory);
1213 if(l->board_size > 9)
1214 {
1215 if(aMove->x < 9) //no I on IGS
1216 aMove->x++;
1217 aMove->y = 20 - aMove->y;
1218 }
1219 boarddispatch->recvMove(aMove);
1220 delete aMove;
1221 }
1222 memory = 0;
1223 memory_str.clear();
1224 return;
1225 }
1226 /* FIXME: Kibitz has this ugly little "->" which we might
1227 * want to cut off */
1228 //emit signal_kibitz(memory, memory_str, line);
1229 if(!memory_str.contains(getUsername()))
1230 {
1231 boarddispatch = getBoardDispatch(memory);
1232 if(boarddispatch)
1233 boarddispatch->recvKibitz(memory_str, line);
1234 }
1235 memory = 0;
1236 memory_str.clear();
1237 }
1238 }