Final AI tweaks.
[NALCG.git] / src / client / remoteplayer.cpp
blobffa10717014c451ea410d3e3a7d38aba8a7446ff
1 #include "remoteplayer.h"
2 #include "network.h"
3 #include "../middleman.h"
4 #include "../logic/chessboard.h"
6 #include <fstream>
7 #include <sstream>
9 RemotePlayer::RemotePlayer()
10 : mNetwork(Network::createNewNetwork())
11 , mMiddleman(0)
12 , mDisabled(true)
13 , mConnected(false)
14 , mThread(0)
15 , mCollectUsers(false)
19 RemotePlayer::~RemotePlayer()
21 disconnect();
22 delete mNetwork;
25 void RemotePlayer::init(Middleman* middleman)
27 mMiddleman = middleman;
28 //~ mDisabled = !parseConfigFile();
29 mDisabled = true;
33 bool RemotePlayer::connect()
35 if (mDisabled)
37 log("Re-parsing configs begin");
38 mDisabled = !parseConfigFile();
39 log("Re-parsing configs end");
42 log("Connect begin");
43 if (!mDisabled
44 && mNetwork->connect(mNInfo.address.c_str(), mNInfo.port.c_str()))
46 log("Connect successful");
47 mNetwork->startBuffering();
49 mConnected = true;
50 mThread = new boost::thread(boost::bind(
51 &RemotePlayer::handleIncomingMessages, this));
53 // Send nickname.
54 log("Nickname send begin");
55 mNetwork->send(mNInfo.nick);
56 log("Nickname send end");
58 return true;
60 log("Connect failed");
61 mDisabled = true;
62 return false;
65 void RemotePlayer::disconnect()
67 log("Disconnect begin");
68 mConnected = false;
69 if (mThread)
71 mThread->join();
72 delete mThread;
74 log("Disconnect end");
77 void RemotePlayer::move(int fromX, int fromY, int toX, int toY, unsigned int promoteTo)
79 if (!mDisabled)
81 log("Move begin");
82 std::ostringstream message;
83 message << "TPE_3M ";
84 message << fromX << " " << fromY << " " << toX << " " << toY << " " << promoteTo;
85 mNetwork->sendln(message.str());
86 log("Move end");
91 void RemotePlayer::sendUndo(unsigned int steps)
93 log("Undo begin");
94 std::ostringstream message;
95 message << "TPE_3U ";
96 message << steps;
97 mNetwork->sendln(message.str());
98 log("Undo end[" + message.str() + "]");
101 void RemotePlayer::setControl(bool white, bool black)
103 // This holds no meaning in network play.
104 // Or at least so it seems.
105 // In reality, we are going to use this method to define who controls
106 // what and where.
109 void RemotePlayer::sendChallenge(const std::string& name)
111 log("Challenge send begin[" + name + "]");
112 mNetwork->send("MSG_B" + name);
113 log("Challenge send end");
116 void RemotePlayer::respondToChallenge(bool accept)
118 log("Response begin");
119 if (accept)
121 mNetwork->sendln("MSG_C");
122 log("Response true");
124 else
126 mNetwork->sendln("MSG_D");
127 log("Response false");
131 void RemotePlayer::handleIncomingMessages()
133 while (mConnected)
135 while (mNetwork->hasLines())
137 std::string line = mNetwork->popLine();
139 if (line != "MSG_A")
141 log("Received[" + line + "]");
144 if (line.substr(0, 6) == "TPE_3M")
146 log("Receive move begin");
147 std::stringstream move;
148 move << line.substr(7);
149 std::size_t fromX, fromY, toX, toY, promoteTo;
150 move >> fromX;
151 move >> fromY;
152 move >> toX;
153 move >> toY;
154 move >> promoteTo;
156 std::ostringstream response;
157 response << "TPE_3R ";
158 response << mMiddleman->move(fromX, fromY, toX, toY, promoteTo);
159 mNetwork->sendln(response.str());
160 log("Receive move end");
162 else if (line.substr(0, 6) == "TPE_3U")
164 log("Receive undo begin");
165 std::stringstream undo;
166 undo << line.substr(7);
167 unsigned int steps;
168 undo >> steps;
169 mMiddleman->undo(steps);
170 log("Receive undo end");
172 else if (line.substr(0, 5) == "MSG_E")
174 log("Receive challenge begin[" + line.substr(5) + "]");
175 mMiddleman->promptChallenge(line.substr(5));
176 log("Receive challenge end");
178 else if (line == "MSG_U")
180 log("Receive userlist begin");
181 mCollectUsers = true;
182 mUsers.clear();
184 else if (line == "MSG_UEND")
186 mCollectUsers = false;
187 mMiddleman->updateUsers(mUsers);
188 log("Receive userlist end");
190 else if (mCollectUsers)
192 log("Add user[" + line + "]");
193 mUsers.push_back(line);
196 boost::this_thread::sleep(boost::posix_time::millisec(10));
200 void RemotePlayer::log(const std::string& message)
202 std::cout << "@Remote: " << message << std::endl;
206 bool RemotePlayer::parseConfigFile()
208 log("parsing begin");
209 std::ifstream inputFile("../data/remote.cfg");
210 if (!inputFile.is_open())
212 log("parsing end, Fail(couldn't open config file)");
213 return false;
216 std::string line;
217 while (std::getline(inputFile, line))
219 removeInvalidChars(line);
220 std::size_t separator;
222 if (!line.empty()
223 && (separator = line.find('=')) != std::string::npos)
225 std::string key = line.substr(0, separator);
226 std::string value = line.substr(separator + 1);
228 if (!updateNetworkInfo(key, value))
230 log("parsing end, Fail(wrong key)");
231 return false;
236 log("parsing end, success");
237 return true;
241 bool RemotePlayer::updateNetworkInfo(const std::string& key,
242 const std::string& value)
244 if (key == "name")
246 mNInfo.nick = value;
247 return true;
249 if (key == "address")
251 mNInfo.address = value;
252 return true;
254 if (key == "port")
256 std::stringstream ss(value);
257 unsigned int num;
258 if ((ss >> num).fail())
260 return false;
263 mNInfo.port = value;
264 return true;
267 return false;
270 void RemotePlayer::removeInvalidChars(std::string& line)
272 std::size_t found;
273 while ((found = line.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
274 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789[]=-_#.")) != std::string::npos) {
275 line.erase(found, 1);