4 * Created on: 2012-03-19
8 #include "CPCommandParser.h"
9 #include "CPEventListener.h"
21 const string
CPCommandParser::CN_CONNECT
= "connect";
22 const string
CPCommandParser::CN_BESTMOVE
= "bestmove";
23 const string
CPCommandParser::CN_ERROR
= "error";
24 const string
CPCommandParser::CN_CONCEDE
= "concede";
25 const string
CPCommandParser::CN_INFO
= "info";
26 const string
CPCommandParser::CN_INIT
= "init";
27 const string
CPCommandParser::CN_STARTGAME
= "startgame";
28 const string
CPCommandParser::CN_MOVE
= "move";
29 const string
CPCommandParser::CN_FEEDBACK
= "feedback";
30 const string
CPCommandParser::CN_SETOPTION
= "setoption";
31 const string
CPCommandParser::CN_GETMOVE
= "getmove";
32 const string
CPCommandParser::CN_FORCEMOVE
= "forcemove";
33 const string
CPCommandParser::CN_STOP
= "stop";
34 const string
CPCommandParser::CN_ENDGAME
= "endgame";
35 const string
CPCommandParser::CN_QUIT
= "quit";
37 const string
CPCommandParser::UNKNOWN_LABEL
= "unknown";
38 const string
CPCommandParser::WHITE_LABEL
= "white";
39 const string
CPCommandParser::BLACK_LABEL
= "black";
40 const string
CPCommandParser::ANALYST_LABEL
= "analyst";
41 const string
CPCommandParser::DRAW_LABEL
= "draw";
42 const string
CPCommandParser::INFINITY_LABEL
= "inf";
44 CPCommandParser::CPCommandParser(istream
& is
, CPEventListener
& l
):inputStream(is
),
45 commandStream(stringstream::in
| stringstream::out
), listener(l
) {
49 string
CPCommandParser::nextCommand() {
51 getline(inputStream
, commandLine
);
52 // cout << "nextCommand: " << commandLine << endl;
56 void CPCommandParser::parse(string commandLine
) {
57 // cout << "parse: " << commandLine << endl;
59 commandStream
.clear();
60 commandStream
.str(commandLine
);
61 // cout << "parse: " << commandStream.str() << endl;
62 commandStream
>> commandType
;
63 // cout << "parse: " << commandType << endl;
64 getCommand(commandType
);
67 string
CPCommandParser::pullNextArg() {
69 if (!commandStream
.eof())
76 string
CPCommandParser::pullPosition() {
81 string
CPCommandParser::pullMove() {
86 CPColor
CPCommandParser::colorFromString(string colorString
) {
87 if (colorString
== WHITE_LABEL
) return CP_COLOR_WHITE
;
88 else if (colorString
== BLACK_LABEL
) return CP_COLOR_BLACK
;
92 CPRole
CPCommandParser::roleFromString(string roleString
) {
93 if (roleString
== WHITE_LABEL
) return CP_ROLE_WHITE
;
94 else if (roleString
== BLACK_LABEL
) return CP_ROLE_BLACK
;
95 else if (roleString
== ANALYST_LABEL
) return CP_ROLE_ANALYST
;
99 CPResult
CPCommandParser::resultFromString(string resultString
) {
100 if (resultString
== WHITE_LABEL
) return CP_RESULT_WHITE
;
101 else if (resultString
== BLACK_LABEL
) return CP_RESULT_BLACK
;
102 else if (resultString
== DRAW_LABEL
) return CP_RESULT_DRAW
;
106 pair
<int, int> CPCommandParser::timesFromString(string times
) {
107 size_t found
= times
.find(":");
108 if (found
== string::npos
)
110 int firstTime
= timeFromString(times
.substr(0, found
));
111 int secondTime
= timeFromString(times
.substr(found
+ 1));
112 pair
<int, int> result
= make_pair(firstTime
, secondTime
);
116 int CPCommandParser::timeFromString(string timeString
) {
117 if (timeString
== INFINITY_LABEL
) return -1;
119 int result
= atoi(timeString
.c_str());
120 if (result
== 0 && timeString
!= "0") throw 1;
125 void CPCommandParser::getCommand(string
& commandType
) {
126 if (commandType
== CN_CONNECT
) commandConnect();
127 else if (commandType
== CN_BESTMOVE
) commandBestMove();
128 else if (commandType
== CN_ERROR
) commandError();
129 else if (commandType
== CN_CONCEDE
) commandConcede();
130 else if (commandType
== CN_INFO
) commandInfo();
131 else if (commandType
== CN_INIT
) commandInit();
132 else if (commandType
== CN_STARTGAME
) commandStartGame();
133 else if (commandType
== CN_MOVE
) commandMove();
134 else if (commandType
== CN_FEEDBACK
) commandFeedback();
135 else if (commandType
== CN_SETOPTION
) commandSetOption();
136 else if (commandType
== CN_GETMOVE
) commandGetMove();
137 else if (commandType
== CN_FORCEMOVE
) commandForceMove();
138 else if (commandType
== CN_STOP
) commandStop();
139 else if (commandType
== CN_ENDGAME
) commandEndGame();
140 else if (commandType
== CN_QUIT
) commandQuit();
141 else commandUnknown();
144 void CPCommandParser::commandUnknown() {
148 void CPCommandParser::commandConnect() {
149 // cout << "connect" << endl;
151 name
= pullNextArg();
152 listener
.connect(name
);
153 // cout << "connect" << endl;
156 void CPCommandParser::commandBestMove() {
158 move
= pullNextArg();
159 listener
.bestMove(move
);
162 void CPCommandParser::commandError() {
163 string errorMsg
= pullNextArg();
164 listener
.error(errorMsg
);
167 void CPCommandParser::commandConcede() {
171 void CPCommandParser::commandInfo() {
172 map
<string
, string
> options
;
175 string name
= pullNextArg();
176 string value
= pullNextArg();
177 options
.insert(make_pair
<string
, string
>(name
, value
));
182 listener
.info(options
);
185 void CPCommandParser::commandInit() {
189 void CPCommandParser::commandStartGame() {
190 string position
= pullPosition();
191 string role
= pullNextArg();
192 string time
= pullNextArg();
194 pair
<int, int> times
= timesFromString(time
);
195 int gameTime
= times
.first
;
196 int bonusTime
= times
.second
;
198 listener
.startGame(position
, roleFromString(role
), gameTime
, bonusTime
);
201 void CPCommandParser::commandMove() {
202 string color
= pullNextArg();
203 string move
= pullMove();
204 listener
.move(colorFromString(color
), move
);
207 void CPCommandParser::commandFeedback() {
208 string turn
= pullNextArg();
210 if (turn
== "on") on
= true;
211 else if (turn
== "off") on
= false;
213 listener
.feedback(on
);
216 void CPCommandParser::commandSetOption() {
217 map
<string
, string
> options
;
220 string name
= pullNextArg();
221 string value
= pullNextArg();
222 options
.insert(make_pair
<string
, string
>(name
, value
));
227 listener
.setOption(options
);
230 void CPCommandParser::commandGetMove() {
231 string color
= pullNextArg();
232 string moveTime
= pullNextArg();
233 string gameTime
= pullNextArg();
235 int mTime
= timeFromString(moveTime
);
236 int gTime
= timeFromString(gameTime
);
238 listener
.getMove(colorFromString(color
), mTime
, gTime
);
241 void CPCommandParser::commandForceMove() {
242 listener
.forceMove();
245 void CPCommandParser::commandStop() {
249 void CPCommandParser::commandEndGame() {
250 string result
= pullNextArg();
251 listener
.endGame(resultFromString(result
));
254 void CPCommandParser::commandQuit() {