Fix deserialization of shogi promotions.
[tagua/yd.git] / src / icsconnection.cpp
blob6d3566f81f782c704aeb3fbfd160395c339fe789
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 <iostream>
12 #include <QRegExp>
13 #include <QStringList>
15 #include "icsconnection.h"
16 #include "poolinfo.h"
17 #include "positioninfo.h"
18 #include "player.h"
19 #include "gameinfo.h"
20 #include "pgnparser.h"
21 #include "icslistener.h"
22 #include "variants.h"
24 using namespace boost;
26 QRegExp ICSConnection::pressReturn("^Press return to enter the server as \"\\S+\":");
28 // example: Creating: azsxdc (++++) Hispanico (1684) unrated crazyhouse 3 0
29 QRegExp ICSConnection::creating("^Creating: (\\S+)\\s+\\((\\S*)\\)\\s+(\\S+)\\s+\\((\\S*)\\)"
30 "\\s+(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s*.*$");
32 // example: {Game 149 (azsxdc vs. Hispanico) Creating unrated crazyhouse match.}
33 // example: {Game 149 (azsxdc vs. Hispanico) Game aborted on move 1} *
34 QRegExp ICSConnection::game("^\\{Game\\s+(\\d+)\\s+\\((\\S+)\\s+vs\\.\\s+(\\S+)\\)\\s+"
35 "(\\S+.*)\\}(?:\\s+(\\S+.*)|\\s*)$");
36 QRegExp ICSConnection::unexamine("^You\\s+are\\s+no\\s+longer\\s+examining\\s+game\\s+(\\d+)");
37 QRegExp ICSConnection::unobserve("^Removing\\s+game\\s+(\\d+)\\s+from\\s+observation\\s+list\\s*");
39 // example: 124 848 shanmark 1155 damopn [ br 2 12] 2:04 - 2:08 (39-39) W: 3
40 //QRegExp ICSConnection::gameInfo("^(\\d+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+"
41 // "\\[\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+\\]");
43 // 1 2 3 4 5 6 7 8 9
44 // example: Game 124: shanmark ( 848) damopn (1155) rated blitz 2 12
45 // Game 170: AstralVision (2167) vladx (2197) rated standard 15 0
46 QRegExp ICSConnection::observed_game("^Game\\s+(\\d+):\\s+(\\S+)\\s+\\(\\s*(\\S+)\\s*\\)\\s+"
47 "(\\S+)\\s+\\(\\s*(\\S+)\\s*\\)\\s+"
48 "(\\S+)\\s+(\\S+)\\s+(\\d+)\\s+(\\d+)");
50 QRegExp ICSConnection::login("^login: $");
51 QRegExp ICSConnection::password("^password: $");
52 QRegExp ICSConnection::fics("^\\S+% ");
53 QRegExp ICSConnection::beep("^\a");
55 //ex: Movelist for game 95:
56 QRegExp ICSConnection::move_list_start("^Movelist\\s+for\\s+game\\s+(\\d+):");
57 //ex: npssnt (1586) vs. mikkk (1487) --- Sun Sep 3, 18:16 PDT 2006
58 QRegExp ICSConnection::move_list_players("^(\\S+)\\s+\\((\\S*)\\)\\s+vs.\\s+(\\S+)\\s+\\((\\S*)\\)\\s*.*");
59 //ex: Unrated blitz match, initial time: 3 minutes, increment: 0 seconds.
60 QRegExp ICSConnection::move_list_game("^(\\S+)\\s+(\\S+)\\s+.*initial\\s+time:\\s*(\\d+)"
61 "\\s+.*increment:\\s*(\\d+)\\s*.*");
62 //ex: Move npssnt mikkk
63 QRegExp ICSConnection::move_list_ignore1("^Move\\s+(\\S+)\\s+(\\S+)");
64 //ex: ---- ---------------- ----------------
65 QRegExp ICSConnection::move_list_ignore2("^( +|-+)+");
66 QRegExp ICSConnection::move_list_terminator("^\\s*$");
68 QRegExp ICSConnection::goForward("^Game (\\d+): (\\S+) goes forward (\\d+) moves?\\.");
69 QRegExp ICSConnection::goBack("^Game (\\d+): (\\S+) backs up (\\d+) moves?\\.");
72 ICSConnection::ICSConnection()
73 : incomingGameInfo(0)
74 , m_move_list_game_info(NULL)
75 , m_move_list_position_info(NULL)
76 , m_move_list_pool_info(NULL) {
77 state = Normal;
78 connect(this, SIGNAL(receivedLine(QString, int)), this, SLOT(process(QString)));
79 connect(this, SIGNAL(receivedText(QString, int)), this, SLOT(processPartialLine(QString)));
82 bool ICSConnection::test(const QRegExp& pattern, const QString& str) {
83 if (pattern.indexIn(str, m_processed_offset, QRegExp::CaretAtOffset) >= 0) {
84 m_processed_offset = pattern.matchedLength();
85 return true;
87 else return false;
90 void ICSConnection::processPartialLine(QString str) {
91 // std::cout << "processing (partial) " << str << std::endl;
92 if (test(fics, str)) {
93 // std::cout << "matched prompt" << std::endl;
94 prompt();
96 else if (test(beep, str)) {
97 notification();
99 else if (test(pressReturn, str)) {
100 registeredNickname();
102 else if (test(login, str)) {
103 loginPrompt();
105 else if (test(password, str)) {
106 passwordPrompt();
108 else {
109 // no match, but it could be a partial command
110 m_processed_offset = 0;
114 void ICSConnection::process(QString str) {
115 switch(state) {
116 case Normal:
117 if (test(creating, str)) {
118 delete incomingGameInfo;
119 incomingGameInfo = new GameInfo(creating, 1);
121 else if(test(observed_game, str)) {
122 if(incomingGameInfo)
123 delete incomingGameInfo;
124 incomingGameInfo = new GameInfo(Player(observed_game.cap(2), observed_game.cap(3).toInt()),
125 Player(observed_game.cap(4), observed_game.cap(5).toInt()),
126 observed_game.cap(6), observed_game.cap(7),
127 observed_game.cap(8).toInt(), observed_game.cap(9).toInt() );
128 int number = observed_game.cap(1).toInt();
129 incomingGameInfo->setGameNumber(number);
130 m_games[number] = ICSGameData(-1, incomingGameInfo->type());
131 //std::cout << "ok, obs " << number << " of type " << incomingGameInfo->type() << std::endl;
133 else if (test(game, str)) {
134 //std::cout << "matched game. incomingGameInfo = " << incomingGameInfo << std::endl;
135 if(game.cap(4).startsWith("Creating") || game.cap(4).startsWith("Continuing") ) {
136 if (!incomingGameInfo) {
137 //this should really never happen, but anyway...
138 QStringList info = game.cap(4).split(' ');
139 if(info.size() >= 3)
140 incomingGameInfo = new GameInfo(Player(game.cap(2), 0),
141 Player(game.cap(3), 0),
142 info[1], info[2], 0, 0 );
144 if (incomingGameInfo) {
145 int number = game.cap(1).toInt();
146 incomingGameInfo->setGameNumber(number);
147 m_games.insert(std::make_pair(number, ICSGameData(-1, incomingGameInfo->type())));
150 else {
151 if (!incomingGameInfo) {
152 int number = game.cap(1).toInt();
153 //std::cout << "matching game " << number << " end" << std::endl;
154 m_games.erase(number);
155 QString what = game.cap(4);
156 QString result = game.cap(5);
157 endingGame(what, result);
161 else if (test(unexamine, str)) {
162 //std::cout << "matching examined game end" << std::endl;
163 int gameNumber = unexamine.cap(1).toInt();
164 m_games.erase(gameNumber);
165 endingExaminedGame(gameNumber);
167 else if (test(unobserve, str)) {
168 int gameNumber = unobserve.cap(1).toInt();
169 m_games.erase(gameNumber);
170 endingObservedGame(gameNumber);
172 else if (test(move_list_start, str)) {
173 //std::cout << "entering move list state" << std::endl;
174 m_move_list_game_num = move_list_start.cap(1).toInt();
175 state = MoveListHeader;
177 else {
178 PositionInfo positionInfo(m_games, str);
179 if (positionInfo.valid) {
180 // if this is the first style12 for this game, notify game creation
181 int gameNumber = positionInfo.gameNumber;
182 bool game_start = ( !m_games.count(gameNumber)
183 || (incomingGameInfo &&
184 (incomingGameInfo->gameNumber() == gameNumber
185 || incomingGameInfo->gameNumber()==-1) ) );
187 if (game_start) {
188 //std::cout << "notifying game creation" << std::endl;
189 if(!m_games.count(gameNumber))
190 m_games[gameNumber] = ICSGameData(-1, "unknown"); //???
192 if (incomingGameInfo && (incomingGameInfo->gameNumber() != -1)
193 && (incomingGameInfo->gameNumber() != gameNumber) ) {
194 m_games.erase(incomingGameInfo->gameNumber());
195 delete incomingGameInfo;
196 incomingGameInfo = NULL; // discard game info
199 if (!incomingGameInfo) {
200 std::cout << "warning, got unexpected style12!!! " << gameNumber << std::endl;
201 incomingGameInfo = new GameInfo(Player(positionInfo.whitePlayer, 0),
202 Player(positionInfo.blackPlayer, 0),
203 "rated", "unknown", 0, 0 );
206 switch (positionInfo.relation) {
207 case PositionInfo::NotMyMove:
208 case PositionInfo::MyMove:
209 //std::cout << "creating game" << std::endl;
210 creatingGame(incomingGameInfo, positionInfo);
211 break;
212 case PositionInfo::Examining:
213 //std::cout << "creating examination" << std::endl;
214 creatingExaminedGame(incomingGameInfo, positionInfo);
215 break;
216 case PositionInfo::ObservingPlayed:
217 case PositionInfo::ObservingExamined:
218 //std::cout << "creating obs " << gameNumber << " " << incomingGameInfo->type() << std::endl;
219 creatingObservedGame(incomingGameInfo, positionInfo);
220 break;
221 default:
222 // unknown relation: ignoring
223 break;
225 delete incomingGameInfo;
226 incomingGameInfo = NULL;
229 m_games[positionInfo.gameNumber].index = positionInfo.index();
230 if (shared_ptr<ICSListener> listener = m_games[positionInfo.gameNumber].listener.lock())
231 listener->notifyStyle12(positionInfo, game_start);
232 // time(positionInfo.whiteTime, positionInfo.blackTime);
233 if (positionInfo.relation == PositionInfo::MyMove) {
234 notification();
237 else {
238 PoolInfo pool_info(m_games, str);
239 if (pool_info.m_valid) {
240 // BROKEN
241 if (!pool_info.m_added_piece) {
242 if (shared_ptr<ICSListener> listener = m_games[pool_info.m_game_num].listener.lock())
243 listener->notifyPool(pool_info);
248 break;
250 case MoveListHeader:
251 if (test(move_list_players, str)){
252 //std::cout << "move list players: " << str << std::endl;
253 m_move_list_players = move_list_players.capturedTexts();
255 else if (test(move_list_game, str)){
256 //std::cout << "move list game: " << str << std::endl;
257 if (m_move_list_game_info)
258 delete m_move_list_game_info;
260 if (m_move_list_players.size()>=5)
261 m_move_list_game_info = new GameInfo(
262 Player(m_move_list_players[1], m_move_list_players[2].toInt()),
263 Player(m_move_list_players[3], m_move_list_players[4].toInt()),
264 move_list_game.cap(1).toLower(), move_list_game.cap(2),
265 move_list_game.cap(3).toInt(), move_list_game.cap(4).toInt()
267 else
268 m_move_list_game_info = new GameInfo( Player("unknown",0), Player("unknown",0),
269 move_list_game.cap(1).toLower(), move_list_game.cap(2),
270 move_list_game.cap(3).toInt(), move_list_game.cap(4).toInt()
272 m_move_list_game_info->setGameNumber(m_move_list_game_num);
274 //NOTE: here is where an unknown variant will be "upgraded" to the correct variant
275 m_games[m_move_list_game_num].setType(move_list_game.cap(2));
277 else if (test(move_list_terminator, str)) {
278 //std::cout << "move list ign3: " << str << std::endl;
280 else if (test(move_list_ignore1, str)){
281 //std::cout << "move list ign1: " << str << std::endl;
283 else if (test(move_list_ignore2, str)) {
284 //std::cout << "move list ign2: " << str << std::endl;
285 state = MoveListMoves;
287 else {
288 PositionInfo pi(m_games, str);
289 if (pi.valid)
290 m_move_list_position_info = new PositionInfo(pi);
291 else {
292 PoolInfo pooli(m_games, str);
293 if(pooli.m_valid)
294 m_move_list_pool_info = new PoolInfo(pooli);
297 break;
298 case MoveListMoves:
299 if (test(move_list_terminator, str)){
300 if (shared_ptr<ICSListener> listener = m_games[m_move_list_game_num].listener.lock()) {
301 AbstractPosition::Ptr p;
302 if (m_move_list_position_info)
303 p = m_move_list_position_info->position;
304 else {
305 std::map<int, ICSGameData>::const_iterator gi = m_games.find(m_move_list_game_num);
306 if (gi == m_games.end()) {
307 ERROR("BUG: Received move list for unknown game " << m_move_list_game_num);
309 else {
310 VariantPtr variant = gi->second.variant;
311 p = variant->createPosition();
312 p->setup();
316 if (p) {
317 if (m_move_list_pool_info) {
318 //BROKEN
319 //p->setPool(m_move_list_pool_info->m_pool);
322 PGN pgn(m_move_list);
323 if (!pgn.valid())
324 std::cout << "parse error on move list" << std::endl;
325 else
326 listener->notifyMoveList(m_move_list_game_num, p, pgn);
330 if (m_move_list_game_info)
331 delete m_move_list_game_info;
332 if (m_move_list_position_info)
333 delete m_move_list_position_info;
334 if (m_move_list_pool_info)
335 delete m_move_list_pool_info;
336 m_move_list_game_info = NULL;
337 m_move_list_position_info = NULL;
338 m_move_list_pool_info = NULL;
339 m_move_list = QString();
340 state = Normal;
342 else
343 m_move_list += str;
344 break;
347 m_processed_offset = 0;
350 void ICSConnection::setListener(int gameNumber, const weak_ptr<ICSListener>& listener) {
351 m_games[gameNumber].listener = listener;
354 void ICSConnection::startup() {
355 sendText("alias $ @");
356 sendText("iset startpos 1");
357 sendText("iset ms 1");
358 sendText("iset lock 1");
359 sendText("set interface Tagua-0.10 (http://www.tagua-project.org)");
360 sendText("set style 12");