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.
13 #include <QStringList>
15 #include "icsconnection.h"
17 #include "positioninfo.h"
20 #include "pgnparser.h"
21 #include "icslistener.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+\\]");
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()
74 , m_move_list_game_info(NULL
)
75 , m_move_list_position_info(NULL
)
76 , m_move_list_pool_info(NULL
) {
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();
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;
96 else if (test(beep
, str
)) {
99 else if (test(pressReturn
, str
)) {
100 registeredNickname();
102 else if (test(login
, str
)) {
105 else if (test(password
, str
)) {
109 // no match, but it could be a partial command
110 m_processed_offset
= 0;
114 void ICSConnection::process(QString str
) {
117 if (test(creating
, str
)) {
118 delete incomingGameInfo
;
119 incomingGameInfo
= new GameInfo(creating
, 1);
121 else if(test(observed_game
, str
)) {
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(' ');
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())));
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
;
178 PositionInfo positionInfo
;
179 bool game_start
= positionInfo
.load(m_games
, str
);
180 if (positionInfo
.valid
) {
181 int gameNumber
= positionInfo
.gameNumber
;
182 GameList::const_iterator game_it
= m_games
.find(gameNumber
);
183 Q_ASSERT(game_it
!= m_games
.end());
185 bool incoming
= incomingGameInfo
&&
186 incomingGameInfo
->gameNumber() == gameNumber
;
188 if (game_start
|| incoming
) {
189 // delete extraneous game info
190 if (incomingGameInfo
&&
191 incomingGameInfo
->gameNumber() != gameNumber
) {
192 int n
= incomingGameInfo
->gameNumber();
195 delete incomingGameInfo
;
196 incomingGameInfo
= 0;
199 // no info on this game
200 if (!incomingGameInfo
) {
201 WARNING("unexpected style 12 for game " << gameNumber
);
202 incomingGameInfo
= new GameInfo(Player(positionInfo
.whitePlayer
, 0),
203 Player(positionInfo
.blackPlayer
, 0),
207 switch (positionInfo
.relation
) {
208 case PositionInfo::NotMyMove
:
209 case PositionInfo::MyMove
:
210 //std::cout << "creating game" << std::endl;
211 creatingGame(incomingGameInfo
, positionInfo
);
213 case PositionInfo::Examining
:
214 //std::cout << "creating examination" << std::endl;
215 creatingExaminedGame(incomingGameInfo
, positionInfo
);
217 case PositionInfo::ObservingPlayed
:
218 case PositionInfo::ObservingExamined
:
219 //std::cout << "creating obs " << gameNumber << " " << incomingGameInfo->type() << std::endl;
220 creatingObservedGame(incomingGameInfo
, positionInfo
);
223 // unknown relation: ignoring
227 delete incomingGameInfo
;
228 incomingGameInfo
= 0;
231 if (shared_ptr
<ICSListener
> listener
= m_games
[positionInfo
.gameNumber
].listener
.lock())
232 listener
->notifyStyle12(positionInfo
, game_start
);
234 if (positionInfo
.relation
== PositionInfo::MyMove
) {
239 PoolInfo
pool_info(m_games
, str
);
240 if (pool_info
.m_valid
) {
242 if (!pool_info
.m_added_piece
) {
243 if (shared_ptr
<ICSListener
> listener
= m_games
[pool_info
.m_game_num
].listener
.lock())
244 listener
->notifyPool(pool_info
);
252 if (test(move_list_players
, str
)){
253 //std::cout << "move list players: " << str << std::endl;
254 m_move_list_players
= move_list_players
.capturedTexts();
256 else if (test(move_list_game
, str
)){
257 //std::cout << "move list game: " << str << std::endl;
258 if (m_move_list_game_info
)
259 delete m_move_list_game_info
;
261 if (m_move_list_players
.size()>=5)
262 m_move_list_game_info
= new GameInfo(
263 Player(m_move_list_players
[1], m_move_list_players
[2].toInt()),
264 Player(m_move_list_players
[3], m_move_list_players
[4].toInt()),
265 move_list_game
.cap(1).toLower(), move_list_game
.cap(2),
266 move_list_game
.cap(3).toInt(), move_list_game
.cap(4).toInt()
269 m_move_list_game_info
= new GameInfo( Player("unknown",0), Player("unknown",0),
270 move_list_game
.cap(1).toLower(), move_list_game
.cap(2),
271 move_list_game
.cap(3).toInt(), move_list_game
.cap(4).toInt()
273 m_move_list_game_info
->setGameNumber(m_move_list_game_num
);
275 //NOTE: here is where an unknown variant will be "upgraded" to the correct variant
276 m_games
[m_move_list_game_num
].setType(move_list_game
.cap(2));
278 else if (test(move_list_terminator
, str
)) {
279 //std::cout << "move list ign3: " << str << std::endl;
281 else if (test(move_list_ignore1
, str
)){
282 //std::cout << "move list ign1: " << str << std::endl;
284 else if (test(move_list_ignore2
, str
)) {
285 //std::cout << "move list ign2: " << str << std::endl;
286 state
= MoveListMoves
;
290 pi
.load(m_games
, str
);
292 m_move_list_position_info
= new PositionInfo(pi
);
294 PoolInfo
pooli(m_games
, str
);
296 m_move_list_pool_info
= new PoolInfo(pooli
);
301 if (test(move_list_terminator
, str
)){
302 if (shared_ptr
<ICSListener
> listener
= m_games
[m_move_list_game_num
].listener
.lock()) {
303 AbstractPosition::Ptr p
;
304 if (m_move_list_position_info
)
305 p
= m_move_list_position_info
->position
;
307 std::map
<int, ICSGameData
>::const_iterator gi
= m_games
.find(m_move_list_game_num
);
308 if (gi
== m_games
.end()) {
309 ERROR("BUG: Received move list for unknown game " << m_move_list_game_num
);
312 VariantPtr variant
= gi
->second
.variant
;
313 p
= variant
->createPosition();
319 if (m_move_list_pool_info
) {
321 //p->setPool(m_move_list_pool_info->m_pool);
324 PGN
pgn(m_move_list
);
326 std::cout
<< "parse error on move list" << std::endl
;
328 listener
->notifyMoveList(m_move_list_game_num
, p
, pgn
);
332 if (m_move_list_game_info
)
333 delete m_move_list_game_info
;
334 if (m_move_list_position_info
)
335 delete m_move_list_position_info
;
336 if (m_move_list_pool_info
)
337 delete m_move_list_pool_info
;
338 m_move_list_game_info
= NULL
;
339 m_move_list_position_info
= NULL
;
340 m_move_list_pool_info
= NULL
;
341 m_move_list
= QString();
349 m_processed_offset
= 0;
352 void ICSConnection::setListener(int gameNumber
, const weak_ptr
<ICSListener
>& listener
) {
353 m_games
[gameNumber
].listener
= listener
;
356 void ICSConnection::startup() {
357 sendText("alias $ @");
358 sendText("iset startpos 1");
359 sendText("iset ms 1");
360 sendText("iset lock 1");
361 sendText("set interface Tagua-0.10 (http://www.tagua-project.org)");
362 sendText("set style 12");