Added some functionality

This commit is contained in:
Torsten Schulz
2017-07-28 20:14:51 +02:00
parent b9d2b3e347
commit 984f39479f
7 changed files with 46 additions and 20 deletions

View File

@@ -26,7 +26,6 @@ namespace Yc {
}
User::~User() {
delete thread;
_parent->addMessage(User::system, std::string("leaved_chat"), _name, _color);
}
@@ -73,7 +72,6 @@ namespace Yc {
tv.tv_sec = 0;
tv.tv_usec = 500;
int selectResult = select(_socket + 1, &readSd, NULL, NULL, &tv);
std::cout << selectResult << "/" << FD_ISSET(_socket, &readSd) << std::endl;
if (selectResult == 1 && FD_ISSET(_socket, &readSd) == 1) {
_parent->removeUser(_token);
_stop = true;
@@ -84,7 +82,6 @@ namespace Yc {
if (msg == "") {
continue;
}
std::cout << msg << std::endl;
handleMessage(msg);
}
}
@@ -97,6 +94,10 @@ namespace Yc {
return _color;
}
void User::setParent(Room *parent) {
_parent = parent;
}
void User::send(std::string out) {
Base::send(_socket, out);
}
@@ -107,8 +108,12 @@ namespace Yc {
void User::handleMessage(std::string message) {
Json::Value jsonTree = getJsonTree(message);
if (jsonTree["token"].asString() != _token) {
std::cout << "token wrong:" << jsonTree["token"].asString() << "/" << _token << std::endl;
return;
}
if (jsonTree["type"].asString() == "message") {
_parent->addMessage(User::message, jsonTree["message"].asString(), _name, _color);
checkString(jsonTree["message"].asString());
} else if (jsonTree["type"].asString() == "dice") {
doDice();
} else if (jsonTree["type"].asString() == "scream") {
@@ -120,6 +125,23 @@ namespace Yc {
}
}
void User::checkString(std::string message) {
if (message.substr(0, 6) == "/join ") {
message = message.substr(6);
if (message.find(" ") == std::string::npos) {
changeRoom(message, "");
} else {
std::string room = message.substr(0, message.find(" "));
std::string password = message.substr(message.find(" ") + 1);
changeRoom(room, password);
}
} else if (message == "/dice") {
doDice();
} else {
_parent->addMessage(User::message, message, _name, _color);
}
}
void User::doDice() {
switch (_parent->addDice(this, (rand() % 6) + 1)) {
case 1: