Room change, room list with flags
This commit is contained in:
52
room.cpp
52
room.cpp
@@ -25,6 +25,7 @@ namespace Yc {
|
||||
_type = (RoomType)roomParams["type"].asInt();
|
||||
_roundLength = roomParams["roundlength"].asInt();
|
||||
_lastRoundEnd = std::time(NULL);
|
||||
mutexQueue.unlock();
|
||||
thread = new std::thread(&Room::run, this);
|
||||
}
|
||||
|
||||
@@ -34,6 +35,7 @@ namespace Yc {
|
||||
|
||||
void Room::run() {
|
||||
while (!_stop) {
|
||||
mutexQueue.lock();
|
||||
if (_msgQueue.size() > 0 && !_blocked) {
|
||||
_blocked = true;
|
||||
while (_msgQueue.size() > 0) {
|
||||
@@ -45,10 +47,11 @@ namespace Yc {
|
||||
}
|
||||
_blocked = false;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
if ((_type & dice) == dice) {
|
||||
_handleDice();
|
||||
}
|
||||
mutexQueue.unlock();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,10 +85,25 @@ namespace Yc {
|
||||
return false;
|
||||
}
|
||||
|
||||
void Room::removeUser(std::string _token) {
|
||||
void Room::removeUser(std::string _token, bool silent) {
|
||||
for (std::vector<User*>::iterator user = _users.begin(); user != _users.end(); ++user) {
|
||||
if ((*user)->validateToken(_token)) {
|
||||
_users.erase(user);
|
||||
if (!silent) {
|
||||
addMessage(User::system, "room_exit", (*user)->name(), (*user)->color());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Room::removeUser(User *userToRemove, bool silent) {
|
||||
for (std::vector<User*>::iterator user = _users.begin(); user != _users.end(); ++user) {
|
||||
if (*user == userToRemove) {
|
||||
_users.erase(user);
|
||||
if (!silent) {
|
||||
addMessage(User::system, "room_exit", (*user)->name(), (*user)->color());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -99,8 +117,7 @@ namespace Yc {
|
||||
addMessage(type, messageText, userName, color);
|
||||
}
|
||||
|
||||
void Room::addMessage(User::MsgType type, std::string messageText, std::string userName, std::string color)
|
||||
{
|
||||
void Room::addMessage(User::MsgType type, std::string messageText, std::string userName, std::string color) {
|
||||
Message message;
|
||||
message.type = type;
|
||||
message.messageTr = messageText;
|
||||
@@ -113,6 +130,24 @@ namespace Yc {
|
||||
addMessage(type, getJsonString(messageText), userName, color);
|
||||
}
|
||||
|
||||
void Room::addUserWhenQueueEmpty(User *user) {
|
||||
mutexQueue.lock();
|
||||
_users.push_back(user);
|
||||
mutexQueue.unlock();
|
||||
}
|
||||
|
||||
bool Room::userToNewRoom(User *user, std::string newRoom, std::string password) {
|
||||
return _parent->changeRoom(user, newRoom, password);
|
||||
}
|
||||
|
||||
unsigned int Room::flags() {
|
||||
unsigned int value = (unsigned int)_type;
|
||||
if (_password != "") {
|
||||
value += (unsigned int)Room::password;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
Room::RoomType Room::type() {
|
||||
return _type;
|
||||
}
|
||||
@@ -144,6 +179,15 @@ namespace Yc {
|
||||
return (_allowedUsers.size() == 0 || _password == "" || _password == password || std::find(_allowedUsers.begin(), _allowedUsers.end(), userName) != _allowedUsers.end());
|
||||
}
|
||||
|
||||
bool Room::userIsInRoom(std::string userName) {
|
||||
for (auto &user: _users) {
|
||||
if (userName == user->name()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Room::_handleDice() {
|
||||
if (((_type & rounds) == rounds)) {
|
||||
if ((_users.size() < 2 && _roundRunning) || (!_roundRunning && _roundStart + _roundLength >= time(NULL))) {
|
||||
|
||||
Reference in New Issue
Block a user