Refactor chat system: Introduce ChatRoom and ChatUser classes
- Created ChatRoom class to manage chat room functionalities, including user management, message handling, and game mechanics. - Developed ChatUser class to represent individual users, handling user-specific actions and interactions within chat rooms. - Implemented a Config class for loading configuration settings from a JSON file. - Established a Server class to manage connections, handle requests, and facilitate communication between users and chat rooms. - Introduced a Database class for database interactions, utilizing PostgreSQL for user and room data management. - Added utility functions in the Base class for JSON handling and socket communication. - Created Object classes for Room and User to encapsulate their properties and behaviors. - Updated main function to initialize server and load chat rooms from configuration.
This commit is contained in:
53
src/lib/base.cpp
Executable file
53
src/lib/base.cpp
Executable file
@@ -0,0 +1,53 @@
|
||||
#include "base.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
std::string Base::getJsonString(Json::Value json) {
|
||||
std::string outString;
|
||||
std::stringstream outStream;
|
||||
outStream << json;
|
||||
return outStream.str();
|
||||
}
|
||||
|
||||
void Base::send(int socket, std::string out) {
|
||||
write(socket, out.c_str(), out.length());
|
||||
}
|
||||
|
||||
void Base::send(int socket, Json::Value out) {
|
||||
std::string outString = getJsonString(out);
|
||||
send(socket, outString);
|
||||
}
|
||||
|
||||
std::string Base::readSocket(int socket)
|
||||
{
|
||||
std::string msg("");
|
||||
char buffer[256];
|
||||
bzero(buffer, 256);
|
||||
while (int received = recv(socket, buffer, 255, 0) > 0) {
|
||||
msg += std::string(buffer);
|
||||
if (received < 255) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
Json::Value Base::getJsonTree(std::string msg) {
|
||||
Json::Value inputTree;
|
||||
Json::CharReaderBuilder rbuilder;
|
||||
std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
|
||||
JSONCPP_STRING inputJsonString(msg);
|
||||
reader->parse(inputJsonString.data(), inputJsonString.data() + inputJsonString.size(), &inputTree, NULL);
|
||||
return inputTree;
|
||||
}
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
21
src/lib/base.h
Executable file
21
src/lib/base.h
Executable file
@@ -0,0 +1,21 @@
|
||||
#ifndef YC_LIB_BASE_H
|
||||
#define YC_LIB_BASE_H
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
class Base {
|
||||
protected:
|
||||
std::string getJsonString(Json::Value json);
|
||||
void send(int socket, std::string out);
|
||||
void send(int socket, Json::Value out);
|
||||
std::string readSocket(int socket);
|
||||
Json::Value getJsonTree(std::string msg);
|
||||
};
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
|
||||
#endif // YC_LIB_BASE_H
|
||||
42
src/lib/database.cpp
Normal file
42
src/lib/database.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "database.h"
|
||||
#include <pqxx/pqxx>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
Database::Database(std::shared_ptr<Config> config)
|
||||
{
|
||||
// Hole Verbindungsdaten aus der Config
|
||||
std::string dbname = config->value("database", "database").asString();
|
||||
std::string user = config->value("database", "user").asString();
|
||||
std::string password = config->value("database", "password").asString();
|
||||
std::string host = config->value("database", "host").asString();
|
||||
|
||||
std::string conninfo =
|
||||
"dbname=" + dbname +
|
||||
" user=" + user +
|
||||
" password=" + password +
|
||||
" host=" + host;
|
||||
|
||||
try {
|
||||
_connection = std::make_unique<pqxx::connection>(conninfo);
|
||||
if (!_connection->is_open()) {
|
||||
throw std::runtime_error("Failed to open database connection");
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
throw std::runtime_error(std::string("Database connection error: ") + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
// Beispielmethode für eine Abfrage
|
||||
pqxx::result Database::exec(const std::string& query)
|
||||
{
|
||||
pqxx::work txn(*_connection);
|
||||
pqxx::result r = txn.exec(query);
|
||||
txn.commit();
|
||||
return r;
|
||||
}
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
23
src/lib/database.h
Normal file
23
src/lib/database.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef YC_LIB_DATABASE_H
|
||||
#define YC_LIB_DATABASE_H
|
||||
|
||||
#include <memory>
|
||||
#include <pqxx/pqxx>
|
||||
#include "../core/config.h"
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
class Database {
|
||||
public:
|
||||
Database(std::shared_ptr<Config> config);
|
||||
~Database() = default;
|
||||
pqxx::result exec(const std::string& query);
|
||||
private:
|
||||
std::unique_ptr<pqxx::connection> _connection;
|
||||
};
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yc
|
||||
|
||||
#endif // YC_LIB_DATABASE_H
|
||||
33
src/lib/tools.cpp
Executable file
33
src/lib/tools.cpp
Executable file
@@ -0,0 +1,33 @@
|
||||
#include "tools.h"
|
||||
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
Tools::Tools() {
|
||||
|
||||
}
|
||||
|
||||
std::string Tools::generateRandomString(size_t length) {
|
||||
std::string choices(
|
||||
"0123456789"
|
||||
"`~!@#$%^&*()-_=+[{]}|\\:;<,>./?"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
);
|
||||
|
||||
std::random_device random;
|
||||
std::mt19937 generator(random());
|
||||
std::uniform_int_distribution<size_t> distribution(0, choices.size());
|
||||
|
||||
std::string result(length, '0');
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
result[i] = choices[distribution(generator)];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yp
|
||||
19
src/lib/tools.h
Executable file
19
src/lib/tools.h
Executable file
@@ -0,0 +1,19 @@
|
||||
#ifndef YP_LIB_TOOLS_H
|
||||
#define YP_LIB_TOOLS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
class Tools
|
||||
{
|
||||
public:
|
||||
Tools();
|
||||
static std::string generateRandomString(size_t length = 16);
|
||||
};
|
||||
|
||||
} // namespace Lib
|
||||
} // namespace Yp
|
||||
|
||||
#endif // YP_LIB_TOOLS_H
|
||||
Reference in New Issue
Block a user