345 lines
12 KiB
C++
Executable File
345 lines
12 KiB
C++
Executable File
#include "user.h"
|
|
|
|
#include <tools.h>
|
|
#include <json/json.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include "room.h"
|
|
#include <iostream>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <sstream>
|
|
|
|
namespace Yc {
|
|
namespace Lib {
|
|
|
|
User::User(std::shared_ptr<Room> parent, std::string name, std::string color, int socket) :
|
|
_parent(std::move(parent)),
|
|
_name(std::move(name)),
|
|
_color(std::move(color)),
|
|
_socket(socket),
|
|
_stop(false) {
|
|
_token = Yc::Lib::Tools::generateRandomString(32);
|
|
sendMsg(token, _token, "", "");
|
|
thread = std::make_unique<std::thread>(&User::checkerTask, this);
|
|
}
|
|
|
|
User::~User() {
|
|
_parent->addMessage(User::system, std::string("leaved_chat"), _name, _color);
|
|
}
|
|
|
|
std::string User::name() const {
|
|
return _name;
|
|
}
|
|
|
|
bool User::validateToken(std::string token) {
|
|
return (token == _token);
|
|
}
|
|
|
|
bool User::isUser(std::shared_ptr<User> toValidate) {
|
|
return (toValidate.get() == this);
|
|
}
|
|
|
|
void User::sendMsg(MsgType type, const char *message, std::string userName, std::string color) {
|
|
sendMsg(type, std::string(message), userName, color);
|
|
}
|
|
|
|
void User::sendMsg(MsgType type, std::string message , std::string userName, std::string color) {
|
|
Json::Value sendMessage;
|
|
sendMessage["type"] = type;
|
|
sendMessage["message"] = message;
|
|
sendMessage["userName"] = userName;
|
|
sendMessage["color"] = color;
|
|
send(sendMessage);
|
|
}
|
|
|
|
void User::sendMsg(User::MsgType type, Json::Value message, std::string userName, std::string color) {
|
|
Json::Value sendMessage;
|
|
sendMessage["type"] = type;
|
|
sendMessage["message"] = message;
|
|
sendMessage["userName"] = userName;
|
|
sendMessage["color"] = color;
|
|
send(sendMessage);
|
|
}
|
|
|
|
void User::checkerTask() {
|
|
while (!_stop) {
|
|
fd_set readSd;
|
|
FD_ZERO(&readSd);
|
|
FD_SET(_socket, &readSd);
|
|
timeval tv;
|
|
tv.tv_sec = 0;
|
|
tv.tv_usec = 500;
|
|
int selectResult = select(_socket + 1, &readSd, NULL, NULL, &tv);
|
|
if (selectResult == 1 && FD_ISSET(_socket, &readSd) == 1) {
|
|
_parent->removeUser(_token);
|
|
_stop = true;
|
|
delete this;
|
|
return;
|
|
}
|
|
std::string msg = readSocket(_socket);
|
|
if (msg == "") {
|
|
continue;
|
|
}
|
|
handleMessage(msg);
|
|
}
|
|
}
|
|
|
|
void User::stop() {
|
|
_stop = true;
|
|
}
|
|
|
|
std::string User::color() const {
|
|
return _color;
|
|
}
|
|
|
|
void User::setParent(std::shared_ptr<Room> parent) {
|
|
_parent = std::move(parent);
|
|
}
|
|
|
|
void User::send(std::string out) {
|
|
Base::send(_socket, out);
|
|
}
|
|
|
|
void User::send(Json::Value out) {
|
|
Base::send(_socket, out);
|
|
}
|
|
|
|
void User::handleMessage(std::string message) {
|
|
Json::Value jsonTree = getJsonTree(message);
|
|
if (jsonTree["token"].asString() != _token) {
|
|
return;
|
|
}
|
|
if (jsonTree["type"].asString() == "message") {
|
|
checkString(jsonTree["message"].asString());
|
|
} else if (jsonTree["type"].asString() == "dice") {
|
|
doDice();
|
|
} else if (jsonTree["type"].asString() == "scream") {
|
|
_parent->addMessage(User::scream, jsonTree["message"].asString(), _name, _color);
|
|
} else if (jsonTree["type"].asString() == "do") {
|
|
_parent->addMessage(User::dosomething, jsonTree["message"].asString(), _name, _color);
|
|
} else if (jsonTree["type"].asString() == "join") {
|
|
changeRoom(jsonTree["newroom"].asString(), jsonTree["password"].asString());
|
|
} else if (jsonTree["type"].asString() == "userlist") {
|
|
sendUserList();
|
|
}
|
|
}
|
|
|
|
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::sendUserList() {
|
|
Json::Value userList = _parent->userList();
|
|
Json::Value msg = Json::objectValue;
|
|
msg["userlist"] = userList;
|
|
sendMsg(userListe, msg, "", "");
|
|
}
|
|
|
|
void User::doDice() {
|
|
switch (_parent->addDice(shared_from_this(), (rand() % 6) + 1)) {
|
|
case 1:
|
|
sendMsg(system, "dice_not_possible", "", "");
|
|
break;
|
|
case 2:
|
|
sendMsg(system, "dice_allready_done", "", "");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void User::changeRoom(std::string newRoom, std::string password) {
|
|
if (!_parent->userToNewRoom(shared_from_this(), newRoom, password)) {
|
|
sendMsg(User::system, "room_not_possible", "", "");
|
|
}
|
|
}
|
|
} // namespace Lib
|
|
} // namespace Yc
|
|
#include "user.h"
|
|
|
|
#include <tools.h>
|
|
#include <json/json.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include "room.h"
|
|
#include <iostream>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <sstream>
|
|
|
|
namespace Yc {
|
|
namespace Lib {
|
|
|
|
User::User(std::shared_ptr<Room> parent, std::string name, std::string color, int socket) :
|
|
_parent(std::move(parent)),
|
|
_name(std::move(name)),
|
|
_color(std::move(color)),
|
|
_socket(socket),
|
|
_stop(false) {
|
|
_token = Yc::Lib::Tools::generateRandomString(32);
|
|
sendMsg(token, _token, "", "");
|
|
thread = std::make_unique<std::thread>(&User::checkerTask, this);
|
|
}
|
|
|
|
User::~User() {
|
|
_parent->addMessage(User::system, std::string("leaved_chat"), _name, _color);
|
|
}
|
|
|
|
std::string User::name() const {
|
|
return _name;
|
|
}
|
|
|
|
bool User::validateToken(std::string token) {
|
|
return (token == _token);
|
|
}
|
|
|
|
bool User::isUser(std::shared_ptr<User> toValidate) {
|
|
return (toValidate.get() == this);
|
|
}
|
|
|
|
void User::sendMsg(MsgType type, const char *message, std::string userName, std::string color) {
|
|
sendMsg(type, std::string(message), userName, color);
|
|
}
|
|
|
|
void User::sendMsg(MsgType type, std::string message , std::string userName, std::string color) {
|
|
Json::Value sendMessage;
|
|
sendMessage["type"] = type;
|
|
sendMessage["message"] = message;
|
|
sendMessage["userName"] = userName;
|
|
sendMessage["color"] = color;
|
|
send(sendMessage);
|
|
}
|
|
|
|
void User::sendMsg(User::MsgType type, Json::Value message, std::string userName, std::string color) {
|
|
Json::Value sendMessage;
|
|
sendMessage["type"] = type;
|
|
sendMessage["message"] = message;
|
|
sendMessage["userName"] = userName;
|
|
sendMessage["color"] = color;
|
|
send(sendMessage);
|
|
}
|
|
|
|
void User::checkerTask() {
|
|
while (!_stop) {
|
|
fd_set readSd;
|
|
FD_ZERO(&readSd);
|
|
FD_SET(_socket, &readSd);
|
|
timeval tv;
|
|
tv.tv_sec = 0;
|
|
tv.tv_usec = 500;
|
|
int selectResult = select(_socket + 1, &readSd, NULL, NULL, &tv);
|
|
if (selectResult == 1 && FD_ISSET(_socket, &readSd) == 1) {
|
|
_parent->removeUser(_token);
|
|
_stop = true;
|
|
delete this;
|
|
return;
|
|
}
|
|
std::string msg = readSocket(_socket);
|
|
if (msg == "") {
|
|
continue;
|
|
}
|
|
handleMessage(msg);
|
|
}
|
|
}
|
|
|
|
void User::stop() {
|
|
_stop = true;
|
|
}
|
|
|
|
std::string User::color() const {
|
|
return _color;
|
|
}
|
|
|
|
void User::setParent(std::shared_ptr<Room> parent) {
|
|
_parent = std::move(parent);
|
|
}
|
|
|
|
void User::send(std::string out) {
|
|
Base::send(_socket, out);
|
|
}
|
|
|
|
void User::send(Json::Value out) {
|
|
Base::send(_socket, out);
|
|
}
|
|
|
|
void User::handleMessage(std::string message) {
|
|
Json::Value jsonTree = getJsonTree(message);
|
|
if (jsonTree["token"].asString() != _token) {
|
|
return;
|
|
}
|
|
if (jsonTree["type"].asString() == "message") {
|
|
checkString(jsonTree["message"].asString());
|
|
} else if (jsonTree["type"].asString() == "dice") {
|
|
doDice();
|
|
} else if (jsonTree["type"].asString() == "scream") {
|
|
_parent->addMessage(User::scream, jsonTree["message"].asString(), _name, _color);
|
|
} else if (jsonTree["type"].asString() == "do") {
|
|
_parent->addMessage(User::dosomething, jsonTree["message"].asString(), _name, _color);
|
|
} else if (jsonTree["type"].asString() == "join") {
|
|
changeRoom(jsonTree["newroom"].asString(), jsonTree["password"].asString());
|
|
} else if (jsonTree["type"].asString() == "userlist") {
|
|
sendUserList();
|
|
}
|
|
}
|
|
|
|
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::sendUserList() {
|
|
Json::Value userList = _parent->userList();
|
|
Json::Value msg = Json::objectValue;
|
|
msg["userlist"] = userList;
|
|
sendMsg(userListe, msg, "", "");
|
|
}
|
|
|
|
void User::doDice() {
|
|
switch (_parent->addDice(shared_from_this(), (rand() % 6) + 1)) {
|
|
case 1:
|
|
sendMsg(system, "dice_not_possible", "", "");
|
|
break;
|
|
case 2:
|
|
sendMsg(system, "dice_allready_done", "", "");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void User::changeRoom(std::string newRoom, std::string password) {
|
|
if (!_parent->userToNewRoom(shared_from_this(), newRoom, password)) {
|
|
sendMsg(User::system, "room_not_possible", "", "");
|
|
}
|
|
}
|
|
} // namespace Lib
|
|
} // namespace Yc
|