Tree2
[siwg.git] / espy / src / tree / CPCommandParser.cc
blob923d6667209bd4bdad8465b24fc1b84ea9730fc6
1 /*
2 * CPCommandParser.cc
4 * Created on: 2012-03-19
5 * Author: elshize
6 */
8 #include "CPCommandParser.h"
9 #include "CPEventListener.h"
10 #include "CPTypes.h"
11 #include <istream>
12 #include <sstream>
13 #include <iostream>
14 #include <string>
15 #include <map>
16 #include <cstdlib>
17 #include <utility>
19 using namespace std;
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() {
50 string commandLine;
51 getline(inputStream, commandLine);
52 // cout << "nextCommand: " << commandLine << endl;
53 return commandLine;
56 void CPCommandParser::parse(string commandLine) {
57 // cout << "parse: " << commandLine << endl;
58 string commandType;
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() {
68 string res;
69 if (!commandStream.eof())
70 commandStream >> res;
71 else
72 throw 1;
73 return res;
76 string CPCommandParser::pullPosition() {
77 // TODO:
78 return pullNextArg();
81 string CPCommandParser::pullMove() {
82 // TODO:
83 return pullNextArg();
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;
89 else throw 1;
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;
96 else throw 1;
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;
103 else throw 1;
106 pair<int, int> CPCommandParser::timesFromString(string times) {
107 size_t found = times.find(":");
108 if (found == string::npos)
109 throw 1;
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);
113 return result;
116 int CPCommandParser::timeFromString(string timeString) {
117 if (timeString == INFINITY_LABEL) return -1;
118 else {
119 int result = atoi(timeString.c_str());
120 if (result == 0 && timeString != "0") throw 1;
121 return result;
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() {
145 throw 2;
148 void CPCommandParser::commandConnect() {
149 // cout << "connect" << endl;
150 string name;
151 name = pullNextArg();
152 listener.connect(name);
153 // cout << "connect" << endl;
156 void CPCommandParser::commandBestMove() {
157 string move;
158 move = pullNextArg();
159 listener.bestMove(move);
162 void CPCommandParser::commandError() {
163 string errorMsg = pullNextArg();
164 listener.error(errorMsg);
167 void CPCommandParser::commandConcede() {
168 listener.concede();
171 void CPCommandParser::commandInfo() {
172 map<string, string> options;
173 while (true) {
174 try {
175 string name = pullNextArg();
176 string value = pullNextArg();
177 options.insert(make_pair<string, string>(name, value));
178 } catch (int code) {
179 break;
182 listener.info(options);
185 void CPCommandParser::commandInit() {
186 listener.init();
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();
209 bool on;
210 if (turn == "on") on = true;
211 else if (turn == "off") on = false;
212 else throw 1;
213 listener.feedback(on);
216 void CPCommandParser::commandSetOption() {
217 map<string, string> options;
218 while (true) {
219 try {
220 string name = pullNextArg();
221 string value = pullNextArg();
222 options.insert(make_pair<string, string>(name, value));
223 } catch (int code) {
224 break;
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() {
246 listener.stop();
249 void CPCommandParser::commandEndGame() {
250 string result = pullNextArg();
251 listener.endGame(resultFromString(result));
254 void CPCommandParser::commandQuit() {
255 listener.quit();