Added some functionality
This commit is contained in:
19
room.cpp
19
room.cpp
@@ -7,6 +7,7 @@
|
||||
#include <time.h>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <thread>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
@@ -25,7 +26,6 @@ namespace Yc {
|
||||
_type = (RoomType)roomParams["type"].asInt();
|
||||
_roundLength = roomParams["roundlength"].asInt();
|
||||
_lastRoundEnd = std::time(NULL);
|
||||
mutexQueue.unlock();
|
||||
thread = new std::thread(&Room::run, this);
|
||||
}
|
||||
|
||||
@@ -35,22 +35,20 @@ namespace Yc {
|
||||
|
||||
void Room::run() {
|
||||
while (!_stop) {
|
||||
mutexQueue.lock();
|
||||
if (_msgQueue.size() > 0 && !_blocked) {
|
||||
_blocked = true;
|
||||
while (_msgQueue.size() > 0) {
|
||||
Message message = _msgQueue.front();
|
||||
_msgQueue.pop();
|
||||
for (auto &user: _users) {
|
||||
user->sendMsg(message.type, message.messageTr, message.userName, message.color);
|
||||
}
|
||||
_msgQueue.pop();
|
||||
}
|
||||
_blocked = false;
|
||||
}
|
||||
if ((_type & dice) == dice) {
|
||||
_handleDice();
|
||||
}
|
||||
mutexQueue.unlock();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
}
|
||||
@@ -70,6 +68,7 @@ namespace Yc {
|
||||
bool Room::addUser(User *user, std::string password) {
|
||||
if (password == _password) {
|
||||
_users.push_back(user);
|
||||
user->setParent(this);
|
||||
_initRound();
|
||||
return true;
|
||||
}
|
||||
@@ -114,7 +113,7 @@ namespace Yc {
|
||||
}
|
||||
|
||||
void Room::addMessage(User::MsgType type, const char *messageText, std::string userName, std::string color) {
|
||||
addMessage(type, messageText, userName, color);
|
||||
addMessage(type, (std::string)messageText, userName, color);
|
||||
}
|
||||
|
||||
void Room::addMessage(User::MsgType type, std::string messageText, std::string userName, std::string color) {
|
||||
@@ -131,9 +130,11 @@ namespace Yc {
|
||||
}
|
||||
|
||||
void Room::addUserWhenQueueEmpty(User *user) {
|
||||
mutexQueue.lock();
|
||||
while (_msgQueue.size() > 0) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
_users.push_back(user);
|
||||
mutexQueue.unlock();
|
||||
user->setParent(this);
|
||||
}
|
||||
|
||||
bool Room::userToNewRoom(User *user, std::string newRoom, std::string password) {
|
||||
@@ -190,12 +191,12 @@ namespace Yc {
|
||||
|
||||
void Room::_handleDice() {
|
||||
if (((_type & rounds) == rounds)) {
|
||||
if ((_users.size() < 2 && _roundRunning) || (!_roundRunning && _roundStart + _roundLength >= time(NULL))) {
|
||||
if (_roundRunning && (_users.size() < 2 || _roundStart + _roundLength >= time(NULL))) {
|
||||
_lastRoundEnd = time(NULL);
|
||||
_roundRunning = false;
|
||||
addMessage(User::system, "round_ends");
|
||||
_showDiceRoundResults();
|
||||
} else if (!_roundRunning && _lastRoundEnd <= time(NULL) - 15) {
|
||||
} else if (!_roundRunning && _lastRoundEnd <= time(NULL) - 15 && _users.size() >= 2) {
|
||||
_roundStart = time(NULL);
|
||||
_roundRunning = true;
|
||||
_diceValues.clear();
|
||||
|
||||
Reference in New Issue
Block a user