1 #include "remoteplayer.h"
3 #include "../middleman.h"
4 #include "../logic/chessboard.h"
9 RemotePlayer::RemotePlayer()
10 : mNetwork(Network::createNewNetwork())
15 , mCollectUsers(false)
19 RemotePlayer::~RemotePlayer()
25 void RemotePlayer::init(Middleman
* middleman
)
27 mMiddleman
= middleman
;
28 //~ mDisabled = !parseConfigFile();
33 bool RemotePlayer::connect()
37 log("Re-parsing configs begin");
38 mDisabled
= !parseConfigFile();
39 log("Re-parsing configs end");
44 && mNetwork
->connect(mNInfo
.address
.c_str(), mNInfo
.port
.c_str()))
46 log("Connect successful");
47 mNetwork
->startBuffering();
50 mThread
= new boost::thread(boost::bind(
51 &RemotePlayer::handleIncomingMessages
, this));
54 log("Nickname send begin");
55 mNetwork
->send(mNInfo
.nick
);
56 log("Nickname send end");
60 log("Connect failed");
65 void RemotePlayer::disconnect()
67 log("Disconnect begin");
74 log("Disconnect end");
77 void RemotePlayer::move(int fromX
, int fromY
, int toX
, int toY
, unsigned int promoteTo
)
82 std::ostringstream message
;
84 message
<< fromX
<< " " << fromY
<< " " << toX
<< " " << toY
<< " " << promoteTo
;
85 mNetwork
->sendln(message
.str());
91 void RemotePlayer::sendUndo(unsigned int steps
)
94 std::ostringstream message
;
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
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");
121 mNetwork
->sendln("MSG_C");
122 log("Response true");
126 mNetwork
->sendln("MSG_D");
127 log("Response false");
131 void RemotePlayer::handleIncomingMessages()
135 while (mNetwork
->hasLines())
137 std::string line
= mNetwork
->popLine();
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
;
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);
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;
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)");
217 while (std::getline(inputFile
, line
))
219 removeInvalidChars(line
);
220 std::size_t separator
;
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)");
236 log("parsing end, success");
241 bool RemotePlayer::updateNetworkInfo(const std::string
& key
,
242 const std::string
& value
)
249 if (key
== "address")
251 mNInfo
.address
= value
;
256 std::stringstream
ss(value
);
258 if ((ss
>> num
).fail())
270 void RemotePlayer::removeInvalidChars(std::string
& line
)
273 while ((found
= line
.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
274 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789[]=-_#.")) != std::string::npos
) {
275 line
.erase(found
, 1);