Added dice, added check for room access
This commit is contained in:
101
room.cpp
101
room.cpp
@@ -4,17 +4,27 @@
|
||||
#include <server.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
Room::Room(Server *parent, std::string name, std::string password, std::vector<std::string> allowedUsers) :
|
||||
Room::Room(Server *parent, Json::Value roomParams) :
|
||||
_parent(parent),
|
||||
_name(name),
|
||||
_password(password),
|
||||
_allowedUsers(allowedUsers),
|
||||
_blocked(false),
|
||||
_stop(false) {
|
||||
_stop(false),
|
||||
_roundRunning(false) {
|
||||
_name = roomParams["name"].asString();
|
||||
_password = roomParams["password"].asString();
|
||||
std::vector<std::string> allowedUsers;
|
||||
for (auto &user: roomParams["allowed"]) {
|
||||
_allowedUsers.push_back(user.asString());
|
||||
}
|
||||
_type = (RoomType)roomParams["type"].asInt();
|
||||
_roundLength = roomParams["roundlength"].asInt();
|
||||
_lastRoundEnd = std::time(NULL);
|
||||
thread = new std::thread(&Room::run, this);
|
||||
}
|
||||
|
||||
@@ -36,6 +46,9 @@ namespace Yc {
|
||||
_blocked = false;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
if ((_type & dice) == dice) {
|
||||
_handleDice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,12 +60,14 @@ namespace Yc {
|
||||
_users.push_back(newUser);
|
||||
newUser->sendMsg(User::roomList, _parent->jsonRoomList(), "", "");
|
||||
addMessage(User::system, "room_entered", newUser->name(), newUser->color());
|
||||
_initRound();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Room::addUser(User *user, std::string password) {
|
||||
if (password == _password) {
|
||||
_users.push_back(user);
|
||||
_initRound();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -80,6 +95,10 @@ namespace Yc {
|
||||
_stop = true;
|
||||
}
|
||||
|
||||
void Room::addMessage(User::MsgType type, const char *messageText, std::string userName, std::string color) {
|
||||
addMessage(type, messageText, userName, color);
|
||||
}
|
||||
|
||||
void Room::addMessage(User::MsgType type, std::string messageText, std::string userName, std::string color)
|
||||
{
|
||||
Message message;
|
||||
@@ -90,6 +109,78 @@ namespace Yc {
|
||||
_msgQueue.push(message);
|
||||
}
|
||||
|
||||
void Room::addMessage(User::MsgType type, Json::Value messageText, std::string userName, std::string color) {
|
||||
addMessage(type, getJsonString(messageText), userName, color);
|
||||
}
|
||||
|
||||
Room::RoomType Room::type() {
|
||||
return _type;
|
||||
}
|
||||
|
||||
bool Room::isType(Room::RoomType type) {
|
||||
return ((_type & type) == type);
|
||||
}
|
||||
|
||||
bool Room::canDice() {
|
||||
return (_roundRunning || (_type & rounds) == rounds) && (_type & dice) == dice;
|
||||
}
|
||||
|
||||
unsigned int Room::addDice(User *user, int diceValue) {
|
||||
if (!canDice()) {
|
||||
return 1;
|
||||
}
|
||||
for (auto &listUser: _diceValues) {
|
||||
if (listUser.first == user) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
_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) {
|
||||
return (_allowedUsers.size() == 0 || _password == "" || _password == password || std::find(_allowedUsers.begin(), _allowedUsers.end(), userName) != _allowedUsers.end());
|
||||
}
|
||||
|
||||
void Room::_handleDice() {
|
||||
if (((_type & rounds) == rounds)) {
|
||||
if ((_users.size() < 2 && _roundRunning) || (!_roundRunning && _roundStart + _roundLength >= time(NULL))) {
|
||||
_lastRoundEnd = time(NULL);
|
||||
_roundRunning = false;
|
||||
addMessage(User::system, "round_ends");
|
||||
_showDiceRoundResults();
|
||||
} else if (!_roundRunning && _lastRoundEnd <= time(NULL) - 15) {
|
||||
_roundStart = time(NULL);
|
||||
_roundRunning = true;
|
||||
_diceValues.clear();
|
||||
addMessage(User::system, "next_round_starts_now");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Room::_initRound() {
|
||||
if (_users.size() == 2) {
|
||||
_lastRoundEnd = time(NULL);
|
||||
addMessage(User::system, "next_round_starts_soon");
|
||||
}
|
||||
}
|
||||
|
||||
void Room::_showDiceRoundResults() {
|
||||
std::sort(_diceValues.begin(), _diceValues.end(), [=](std::pair<User*, int> val1, std::pair<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["value"] = user.second;
|
||||
userList.append(entry);
|
||||
}
|
||||
addMessage(User::result, userList);
|
||||
}
|
||||
|
||||
std::string Room::name() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user