Remove dead code.
[tagua/yd.git] / src / icsconnection.cpp
blobbfe412afd5b41b0666fb8fea8146536953dca533
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "icsconnection.h"
12 #include <QRegExp>
13 #include <QStringList>
15 #include <KDebug>
17 #include <core/state.h>
18 #include <core/statefactory.h>
20 #include "components.h"
21 #include "icsconnection.h"
22 #include "poolinfo.h"
23 #include "positioninfo.h"
24 #include "player.h"
25 #include "gameinfo.h"
26 #include "pgnparser.h"
27 #include "icslistener.h"
28 #include "variants.h"
30 using namespace boost;
32 QRegExp ICSConnection::pressReturn("^Press return to enter the server as \"\\S+\":");
34 // example: Creating: azsxdc (++++) Hispanico (1684) unrated crazyhouse 3 0
35 QRegExp ICSConnection::creating("^Creating: (\\S+)\\s+\\((\\S*)\\)\\s+(\\S+)\\s+\\((\\S*)\\)"
36 "\\s+(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s*.*$");
38 // example: {Game 149 (azsxdc vs. Hispanico) Creating unrated crazyhouse match.}
39 // example: {Game 149 (azsxdc vs. Hispanico) Game aborted on move 1} *
40 QRegExp ICSConnection::game("^\\{Game\\s+(\\d+)\\s+\\((\\S+)\\s+vs\\.\\s+(\\S+)\\)\\s+"
41 "(\\S+.*)\\}(?:\\s+(\\S+.*)|\\s*)$");
42 QRegExp ICSConnection::unexamine("^You\\s+are\\s+no\\s+longer\\s+examining\\s+game\\s+(\\d+)");
43 QRegExp ICSConnection::unobserve("^Removing\\s+game\\s+(\\d+)\\s+from\\s+observation\\s+list\\s*");
45 // example: 124 848 shanmark 1155 damopn [ br 2 12] 2:04 - 2:08 (39-39) W: 3
46 //QRegExp ICSConnection::gameInfo("^(\\d+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+"
47 // "\\[\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+\\]");
49 // 1 2 3 4 5 6 7 8 9
50 // example: Game 124: shanmark ( 848) damopn (1155) rated blitz 2 12
51 // Game 170: AstralVision (2167) vladx (2197) rated standard 15 0
52 QRegExp ICSConnection::observed_game("^Game\\s+(\\d+):\\s+(\\S+)\\s+\\(\\s*(\\S+)\\s*\\)\\s+"
53 "(\\S+)\\s+\\(\\s*(\\S+)\\s*\\)\\s+"
54 "(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)");
56 QRegExp ICSConnection::login("^login: $");
57 QRegExp ICSConnection::password("^password: $");
58 QRegExp ICSConnection::fics("^\\S+% ");
59 QRegExp ICSConnection::beep("^\a");
61 //ex: Movelist for game 95:
62 QRegExp ICSConnection::move_list_start("^Movelist\\s+for\\s+game\\s+(\\d+):");
63 //ex: npssnt (1586) vs. mikkk (1487) --- Sun Sep 3, 18:16 PDT 2006
64 QRegExp ICSConnection::move_list_players("^(\\S+)\\s+\\((\\S*)\\)\\s+vs.\\s+(\\S+)\\s+\\((\\S*)\\)\\s*.*");
65 //ex: Unrated blitz match, initial time: 3 minutes, increment: 0 seconds.
66 QRegExp ICSConnection::move_list_game("^(\\S+)\\s+(\\S+)\\s+.*initial\\s+time:\\s*(\\d+)"
67 "\\s+.*increment:\\s*(\\d+)\\s*.*");
68 //ex: Move npssnt mikkk
69 QRegExp ICSConnection::move_list_ignore1("^Move\\s+(\\S+)\\s+(\\S+)");
70 //ex: ---- ---------------- ----------------
71 QRegExp ICSConnection::move_list_ignore2("^( +|-+)+");
72 QRegExp ICSConnection::move_list_terminator("^\\s*$");
74 QRegExp ICSConnection::goForward("^Game (\\d+): (\\S+) goes forward (\\d+) moves?\\.");
75 QRegExp ICSConnection::goBack("^Game (\\d+): (\\S+) backs up (\\d+) moves?\\.");
78 ICSConnection::ICSConnection()
79 : incomingGameInfo(0)
80 , m_move_list_game_info(NULL)
81 , m_move_list_position_info(NULL)
82 , m_move_list_pool_info(NULL) {
83 state = Normal;
84 connect(this, SIGNAL(receivedLine(QString, int)), this, SLOT(process(QString)));
85 connect(this, SIGNAL(receivedText(QString, int)), this, SLOT(processPartialLine(QString)));
88 bool ICSConnection::test(const QRegExp& pattern, const QString& str) {
89 if (pattern.indexIn(str, m_processed_offset, QRegExp::CaretAtOffset) >= 0) {
90 m_processed_offset = pattern.matchedLength();
91 return true;
93 else return false;
96 void ICSConnection::processPartialLine(QString str) {
97 // kDebug() << "processing (partial) " << str;
98 if (test(fics, str)) {
99 // kDebug() << "matched prompt";
100 prompt();
102 else if (test(beep, str)) {
103 notification();
105 else if (test(pressReturn, str)) {
106 registeredNickname();
108 else if (test(login, str)) {
109 loginPrompt();
111 else if (test(password, str)) {
112 passwordPrompt();
114 else {
115 // no match, but it could be a partial command
116 m_processed_offset = 0;
120 void ICSConnection::process(QString str) {
121 switch(state) {
122 case Normal:
123 if (test(creating, str)) {
124 delete incomingGameInfo;
125 incomingGameInfo = new GameInfo(creating, 1);
127 else if(test(observed_game, str)) {
128 if(incomingGameInfo)
129 delete incomingGameInfo;
130 incomingGameInfo = new GameInfo(Player(observed_game.cap(2), observed_game.cap(3).toInt()),
131 Player(observed_game.cap(4), observed_game.cap(5).toInt()),
132 observed_game.cap(6), observed_game.cap(7),
133 observed_game.cap(8).toInt(), observed_game.cap(9).toInt() );
134 int number = observed_game.cap(1).toInt();
135 incomingGameInfo->setGameNumber(number);
136 m_games[number] = ICSGameData(-1, incomingGameInfo->type());
137 //kDebug() << "ok, obs " << number << " of type " << incomingGameInfo->type();
139 else if (test(game, str)) {
140 //kDebug() << "matched game. incomingGameInfo = " << incomingGameInfo;
141 if(game.cap(4).startsWith("Creating") || game.cap(4).startsWith("Continuing") ) {
142 if (!incomingGameInfo) {
143 //this should really never happen, but anyway...
144 QStringList info = game.cap(4).split(' ');
145 if(info.size() >= 3)
146 incomingGameInfo = new GameInfo(Player(game.cap(2), 0),
147 Player(game.cap(3), 0),
148 info[1], info[2], 0, 0 );
150 if (incomingGameInfo) {
151 int number = game.cap(1).toInt();
152 incomingGameInfo->setGameNumber(number);
153 m_games.insert(std::make_pair(number, ICSGameData(-1, incomingGameInfo->type())));
156 else {
157 if (!incomingGameInfo) {
158 int number = game.cap(1).toInt();
159 //kDebug() << "matching game " << number << " end";
160 m_games.erase(number);
161 QString what = game.cap(4);
162 QString result = game.cap(5);
163 endingGame(what, result);
167 else if (test(unexamine, str)) {
168 //kDebug() << "matching examined game end";
169 int gameNumber = unexamine.cap(1).toInt();
170 m_games.erase(gameNumber);
171 endingExaminedGame(gameNumber);
173 else if (test(unobserve, str)) {
174 int gameNumber = unobserve.cap(1).toInt();
175 m_games.erase(gameNumber);
176 endingObservedGame(gameNumber);
178 else if (test(move_list_start, str)) {
179 //kDebug() << "entering move list state";
180 m_move_list_game_num = move_list_start.cap(1).toInt();
181 state = MoveListHeader;
183 else {
184 PositionInfo positionInfo;
185 bool game_start = positionInfo.load(m_games, str);
186 if (positionInfo.valid) {
187 int gameNumber = positionInfo.gameNumber;
188 GameList::const_iterator game_it = m_games.find(gameNumber);
189 Q_ASSERT(game_it != m_games.end());
191 bool incoming = incomingGameInfo &&
192 incomingGameInfo->gameNumber() == gameNumber;
194 if (game_start || incoming) {
195 // delete extraneous game info
196 if (incomingGameInfo &&
197 incomingGameInfo->gameNumber() != gameNumber) {
198 int n = incomingGameInfo->gameNumber();
199 if (n != -1)
200 m_games.erase(n);
201 delete incomingGameInfo;
202 incomingGameInfo = 0;
205 // no info on this game
206 if (!incomingGameInfo) {
207 kWarning() << "unexpected style 12 for game" << gameNumber;
208 incomingGameInfo = new GameInfo(Player(positionInfo.whitePlayer, 0),
209 Player(positionInfo.blackPlayer, 0),
210 "rated", "", 0, 0);
213 switch (positionInfo.relation) {
214 case PositionInfo::NotMyMove:
215 case PositionInfo::MyMove:
216 //kDebug() << "creating game";
217 creatingGame(incomingGameInfo, positionInfo);
218 break;
219 case PositionInfo::Examining:
220 //kDebug() << "creating examination";
221 creatingExaminedGame(incomingGameInfo, positionInfo);
222 break;
223 case PositionInfo::ObservingPlayed:
224 case PositionInfo::ObservingExamined:
225 //kDebug() << "creating obs " << gameNumber << " " << incomingGameInfo->type();
226 creatingObservedGame(incomingGameInfo, positionInfo);
227 break;
228 default:
229 // unknown relation: ignoring
230 break;
233 delete incomingGameInfo;
234 incomingGameInfo = 0;
237 if (shared_ptr<ICSListener> listener = m_games[positionInfo.gameNumber].listener.lock())
238 listener->notifyStyle12(positionInfo, game_start);
240 if (positionInfo.relation == PositionInfo::MyMove) {
241 notification();
244 else {
245 PoolInfo pool_info(m_games, str);
246 if (pool_info.m_valid) {
247 // BROKEN
248 if (pool_info.m_added_piece != Piece()) {
249 if (shared_ptr<ICSListener> listener = m_games[pool_info.m_game_num].listener.lock())
250 listener->notifyPool(pool_info);
255 break;
257 case MoveListHeader:
258 if (test(move_list_players, str)){
259 //kDebug() << "move list players: " << str;
260 m_move_list_players = move_list_players.capturedTexts();
262 else if (test(move_list_game, str)){
263 //kDebug() << "move list game: " << str;
264 if (m_move_list_game_info)
265 delete m_move_list_game_info;
267 if (m_move_list_players.size()>=5)
268 m_move_list_game_info = new GameInfo(
269 Player(m_move_list_players[1], m_move_list_players[2].toInt()),
270 Player(m_move_list_players[3], m_move_list_players[4].toInt()),
271 move_list_game.cap(1).toLower(), move_list_game.cap(2),
272 move_list_game.cap(3).toInt(), move_list_game.cap(4).toInt()
274 else
275 m_move_list_game_info = new GameInfo( Player("unknown",0), Player("unknown",0),
276 move_list_game.cap(1).toLower(), move_list_game.cap(2),
277 move_list_game.cap(3).toInt(), move_list_game.cap(4).toInt()
279 m_move_list_game_info->setGameNumber(m_move_list_game_num);
281 //NOTE: here is where an unknown variant will be "upgraded" to the correct variant
282 m_games[m_move_list_game_num].setType(move_list_game.cap(2));
284 else if (test(move_list_terminator, str)) {
285 //kDebug() << "move list ign3: " << str;
287 else if (test(move_list_ignore1, str)){
288 //kDebug() << "move list ign1: " << str;
290 else if (test(move_list_ignore2, str)) {
291 //kDebug() << "move list ign2: " << str;
292 state = MoveListMoves;
294 else {
295 PositionInfo pi;
296 pi.load(m_games, str);
297 if (pi.valid)
298 m_move_list_position_info = new PositionInfo(pi);
299 else {
300 PoolInfo pooli(m_games, str);
301 if(pooli.m_valid)
302 m_move_list_pool_info = new PoolInfo(pooli);
305 break;
306 case MoveListMoves:
307 if (test(move_list_terminator, str)){
308 if (shared_ptr<ICSListener> listener = m_games[m_move_list_game_num].listener.lock()) {
309 StatePtr p;
310 if (m_move_list_position_info)
311 p = m_move_list_position_info->position;
312 else {
313 std::map<int, ICSGameData>::const_iterator gi = m_games.find(m_move_list_game_num);
314 if (gi == m_games.end()) {
315 kError() << "Received move list for unknown game " << m_move_list_game_num;
317 else {
318 Components* components = gi->second.components;
319 p = StatePtr(components->createState());
320 p->setup();
324 if (p) {
325 if (m_move_list_pool_info) {
326 //BROKEN
327 //p->setPool(m_move_list_pool_info->m_pool);
330 PGN pgn(m_move_list);
331 if (!pgn.valid())
332 kDebug() << "parse error on move list";
333 else
334 listener->notifyMoveList(m_move_list_game_num, p, pgn);
338 if (m_move_list_game_info)
339 delete m_move_list_game_info;
340 if (m_move_list_position_info)
341 delete m_move_list_position_info;
342 if (m_move_list_pool_info)
343 delete m_move_list_pool_info;
344 m_move_list_game_info = NULL;
345 m_move_list_position_info = NULL;
346 m_move_list_pool_info = NULL;
347 m_move_list = QString();
348 state = Normal;
350 else
351 m_move_list += str;
352 break;
355 m_processed_offset = 0;
358 void ICSConnection::setListener(int gameNumber, const weak_ptr<ICSListener>& listener) {
359 m_games[gameNumber].listener = listener;
362 void ICSConnection::startup() {
363 sendText("alias $ @");
364 sendText("iset startpos 1");
365 sendText("iset ms 1");
366 sendText("iset lock 1");
367 sendText("set interface Tagua-0.10 (http://www.tagua-project.org)");
368 sendText("set style 12");