Update CMake configuration and refactor code to use smart pointers for memory management
This commit is contained in:
55
room.cpp
55
room.cpp
@@ -12,11 +12,11 @@
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
Room::Room(Server *parent, Json::Value roomParams) :
|
||||
_parent(parent),
|
||||
_blocked(false),
|
||||
_stop(false),
|
||||
_roundRunning(false) {
|
||||
Room::Room(std::shared_ptr<Server> parent, Json::Value roomParams) :
|
||||
_parent(std::move(parent)),
|
||||
_blocked(false),
|
||||
_stop(false),
|
||||
_roundRunning(false) {
|
||||
_name = roomParams["name"].asString();
|
||||
_password = roomParams["password"].asString();
|
||||
std::vector<std::string> allowedUsers;
|
||||
@@ -26,12 +26,10 @@ namespace Yc {
|
||||
_type = (RoomType)roomParams["type"].asInt();
|
||||
_roundLength = roomParams["roundlength"].asInt();
|
||||
_lastRoundEnd = std::time(NULL);
|
||||
thread = new std::thread(&Room::run, this);
|
||||
thread = std::make_unique<std::thread>(&Room::run, this);
|
||||
}
|
||||
|
||||
Room::~Room() {
|
||||
delete thread;
|
||||
}
|
||||
Room::~Room() = default;
|
||||
|
||||
void Room::run() {
|
||||
while (!_stop) {
|
||||
@@ -57,7 +55,7 @@ namespace Yc {
|
||||
if (_password != "" && _password == _password && std::find(std::begin(_allowedUsers), std::end(_allowedUsers), _userName) == std::end(_allowedUsers)) {
|
||||
return false;
|
||||
}
|
||||
User *newUser = new User(this, _userName, color, socket);
|
||||
auto newUser = std::make_shared<User>(shared_from_this(), _userName, color, socket);
|
||||
_users.push_back(newUser);
|
||||
newUser->sendMsg(User::roomList, _parent->jsonRoomList(), "", "");
|
||||
addMessage(User::system, "room_entered", newUser->name(), newUser->color());
|
||||
@@ -65,10 +63,10 @@ namespace Yc {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Room::addUser(User *user, std::string password) {
|
||||
bool Room::addUser(std::shared_ptr<User> user, std::string password) {
|
||||
if (password == _password) {
|
||||
_users.push_back(user);
|
||||
user->setParent(this);
|
||||
user->setParent(shared_from_this());
|
||||
_initRound();
|
||||
return true;
|
||||
}
|
||||
@@ -76,7 +74,7 @@ namespace Yc {
|
||||
}
|
||||
|
||||
bool Room::userNameExists(std::string userName) {
|
||||
for (auto &user: _users) {
|
||||
for (const auto &user: _users) {
|
||||
if (user->name() == userName) {
|
||||
return true;
|
||||
}
|
||||
@@ -85,24 +83,24 @@ namespace Yc {
|
||||
}
|
||||
|
||||
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);
|
||||
for (auto it = _users.begin(); it != _users.end(); ++it) {
|
||||
if ((*it)->validateToken(_token)) {
|
||||
if (!silent) {
|
||||
addMessage(User::system, "room_exit", (*user)->name(), (*user)->color());
|
||||
addMessage(User::system, "room_exit", (*it)->name(), (*it)->color());
|
||||
}
|
||||
_users.erase(it);
|
||||
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);
|
||||
void Room::removeUser(std::shared_ptr<User> userToRemove, bool silent) {
|
||||
for (auto it = _users.begin(); it != _users.end(); ++it) {
|
||||
if (*it == userToRemove) {
|
||||
if (!silent) {
|
||||
addMessage(User::system, "room_exit", (*user)->name(), (*user)->color());
|
||||
addMessage(User::system, "room_exit", (*it)->name(), (*it)->color());
|
||||
}
|
||||
_users.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -129,15 +127,15 @@ namespace Yc {
|
||||
addMessage(type, getJsonString(messageText), userName, color);
|
||||
}
|
||||
|
||||
void Room::addUserWhenQueueEmpty(User *user) {
|
||||
void Room::addUserWhenQueueEmpty(std::shared_ptr<User> user) {
|
||||
while (_msgQueue.size() > 0) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
_users.push_back(user);
|
||||
user->setParent(this);
|
||||
user->setParent(shared_from_this());
|
||||
}
|
||||
|
||||
bool Room::userToNewRoom(User *user, std::string newRoom, std::string password) {
|
||||
bool Room::userToNewRoom(std::shared_ptr<User> user, std::string newRoom, std::string password) {
|
||||
return _parent->changeRoom(user, newRoom, password);
|
||||
}
|
||||
|
||||
@@ -171,7 +169,7 @@ namespace Yc {
|
||||
return (_roundRunning || (_type & rounds) == rounds) && (_type & dice) == dice;
|
||||
}
|
||||
|
||||
unsigned int Room::addDice(User *user, int diceValue) {
|
||||
unsigned int Room::addDice(std::shared_ptr<User> user, int diceValue) {
|
||||
if (!canDice()) {
|
||||
return 1;
|
||||
}
|
||||
@@ -183,7 +181,6 @@ namespace Yc {
|
||||
_diceValues.push_back(std::make_pair(user, diceValue));
|
||||
addMessage(User::dice, std::to_string(diceValue), user->name(), user->color());
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
bool Room::accessAllowed(std::string userName, std::string password) {
|
||||
@@ -223,13 +220,13 @@ namespace Yc {
|
||||
}
|
||||
|
||||
void Room::_showDiceRoundResults() {
|
||||
std::sort(_diceValues.begin(), _diceValues.end(), [=](std::pair<User*, int> val1, std::pair<User*, int> val2) {
|
||||
std::sort(_diceValues.begin(), _diceValues.end(), [=](const std::pair<std::shared_ptr<User>, int>& val1, const std::pair<std::shared_ptr<User>, int>& val2) {
|
||||
return (val1.second > val2.second);
|
||||
});
|
||||
Json::Value userList = Json::arrayValue;
|
||||
for (auto &user: _diceValues) {
|
||||
Json::Value entry = Json::objectValue;
|
||||
entry["name"] = user.first;
|
||||
entry["name"] = user.first->name();
|
||||
entry["value"] = user.second;
|
||||
userList.append(entry);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user