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 <sys/socket.h>
#include <memory>
#include <iostream>
namespace Yc {
namespace Lib {

View File

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

View File

@@ -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();

1
room.h
View File

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

View File

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

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:

2
user.h
View File

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