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

@@ -5,6 +5,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <memory> #include <memory>
#include <iostream>
namespace Yc { namespace Yc {
namespace Lib { namespace Lib {

View File

@@ -8,10 +8,11 @@
"connectstring": "tsschulz.de:1521/yourpart" "connectstring": "tsschulz.de:1521/yourpart"
}, },
"room-types": { "room-types": {
0: "Standard", "0": "Standard",
1: "Dice possible", "1": "Dice possible",
2: "Poker possible", "2": "Poker possible",
4: "Room will work with rounds" "4": "Room will work with rounds",
"8": "protected"
}, },
"rooms": [ "rooms": [
{ {
@@ -28,6 +29,5 @@
"type": 5, "type": 5,
"roundlength": 20 "roundlength": 20
} }
]
],
} }

View File

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

1
room.h
View File

@@ -71,7 +71,6 @@ namespace Yc {
time_t _lastRoundEnd; time_t _lastRoundEnd;
int _roundLength; int _roundLength;
std::vector<std::pair<User *, int>> _diceValues; std::vector<std::pair<User *, int>> _diceValues;
std::mutex mutexQueue;
void _handleDice(); void _handleDice();
void _startDiceRound(); void _startDiceRound();
void _endDiceRound(); void _endDiceRound();

View File

@@ -91,6 +91,7 @@ namespace Yc {
userMsg["to"] = newRoom; userMsg["to"] = newRoom;
for (auto &room: _rooms) { for (auto &room: _rooms) {
if (room->userIsInRoom(user->name())) { if (room->userIsInRoom(user->name())) {
room->removeUser(user);
Json::Value msg = Json::objectValue; Json::Value msg = Json::objectValue;
msg["tr"] = "room_change_to"; msg["tr"] = "room_change_to";
msg["to"] = newRoom; msg["to"] = newRoom;

View File

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

2
user.h
View File

@@ -37,6 +37,7 @@ namespace Yc {
void checkerTask(); void checkerTask();
void stop(); void stop();
std::string color() const; std::string color() const;
void setParent(Room *parent);
private: private:
Room *_parent; Room *_parent;
std::string _name; std::string _name;
@@ -50,6 +51,7 @@ namespace Yc {
void handleMessage(std::string message); void handleMessage(std::string message);
void doDice(); void doDice();
void changeRoom(std::string newRoom, std::string password); void changeRoom(std::string newRoom, std::string password);
void checkString(std::string message);
}; };
} // namespace Lib } // namespace Lib