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:
Torsten Schulz (local)
2025-08-11 16:07:15 +02:00
parent 6ecdbda9de
commit 864d86aa09
28 changed files with 693 additions and 343 deletions

42
src/object/room.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include "room.h"
namespace Yc
{
namespace Object
{
Room::Room(const Json::Value &roomJson)
{
_id = roomJson.isMember("id") && !roomJson["id"].isNull() ? roomJson["id"].asInt() : 0;
_title = roomJson.isMember("name") && !roomJson["name"].isNull() ? roomJson["name"].asString() : "";
_owner_id = roomJson.isMember("owner_id") && !roomJson["owner_id"].isNull() ? roomJson["owner_id"].asInt() : 0;
_is_public = roomJson.isMember("is_public") && !roomJson["is_public"].isNull() ? roomJson["is_public"].asBool() : true;
_gender_restriction_id = roomJson.isMember("gender_restriction_id") && !roomJson["gender_restriction_id"].isNull() ? roomJson["gender_restriction_id"].asInt() : 0;
_min_age = roomJson.isMember("min_age") && !roomJson["min_age"].isNull() ? roomJson["min_age"].asInt() : 0;
_max_age = roomJson.isMember("max_age") && !roomJson["max_age"].isNull() ? roomJson["max_age"].asInt() : 0;
_password_hash = roomJson.isMember("password") && !roomJson["password"].isNull() ? roomJson["password"].asString() : "";
_friends_of_owner_only = roomJson.isMember("friends_of_owner_only") && !roomJson["friends_of_owner_only"].isNull() ? roomJson["friends_of_owner_only"].asBool() : false;
_required_user_right_id = roomJson.isMember("required_user_right_id") && !roomJson["required_user_right_id"].isNull() ? roomJson["required_user_right_id"].asInt() : 0;
_created_at = roomJson.isMember("created_at") && !roomJson["created_at"].isNull() ? roomJson["created_at"].asString() : "";
_updated_at = roomJson.isMember("updated_at") && !roomJson["updated_at"].isNull() ? roomJson["updated_at"].asString() : "";
_room_type_id = roomJson.isMember("type") && !roomJson["type"].isNull() ? roomJson["type"].asInt() : 0;
_room_type_tr = roomJson.isMember("room_type") && !roomJson["room_type"].isNull() ? roomJson["room_type"].asString() : "";
}
int Room::id() const { return _id; }
const std::string &Room::title() const { return _title; }
int Room::owner_id() const { return _owner_id; }
bool Room::is_public() const { return _is_public; }
int Room::gender_restriction_id() const { return _gender_restriction_id; }
int Room::min_age() const { return _min_age; }
int Room::max_age() const { return _max_age; }
const std::string &Room::password_hash() const { return _password_hash; }
bool Room::friends_of_owner_only() const { return _friends_of_owner_only; }
int Room::required_user_right_id() const { return _required_user_right_id; }
const std::string &Room::created_at() const { return _created_at; }
const std::string &Room::updated_at() const { return _updated_at; }
int Room::room_type_id() const { return _room_type_id; }
const std::string &Room::room_type_tr() const { return _room_type_tr; }
} // namespace Object
} // namespace Yc

48
src/object/room.h Normal file
View File

@@ -0,0 +1,48 @@
#pragma once
#include <string>
#include <json/value.h>
namespace Yc
{
namespace Object
{
class Room
{
public:
Room(const Json::Value &roomJson);
int id() const;
const std::string &title() const;
int owner_id() const;
bool is_public() const;
int gender_restriction_id() const;
int min_age() const;
int max_age() const;
const std::string &password_hash() const;
bool friends_of_owner_only() const;
int required_user_right_id() const;
const std::string &created_at() const;
const std::string &updated_at() const;
int room_type_id() const;
const std::string &room_type_tr() const;
private:
int _id = 0;
std::string _title;
int _owner_id = 0;
bool _is_public = true;
int _gender_restriction_id = 0;
int _min_age = 0;
int _max_age = 0;
std::string _password_hash;
bool _friends_of_owner_only = false;
int _required_user_right_id = 0;
std::string _created_at;
std::string _updated_at;
int _room_type_id = 0;
std::string _room_type_tr;
};
} // namespace Object
} // namespace Yc

37
src/object/user.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "user.h"
namespace Yc
{
namespace Object
{
User::User() = default;
User::User(const Json::Value& userJson) {
_id = userJson.isMember("id") && !userJson["id"].isNull() ? userJson["id"].asInt() : 0;
_falukant_user_id = userJson.isMember("falukant_user_id") && !userJson["falukant_user_id"].isNull() ? userJson["falukant_user_id"].asInt() : 0;
_display_name = userJson.isMember("display_name") && !userJson["display_name"].isNull() ? userJson["display_name"].asString() : "";
_color = userJson.isMember("color") && !userJson["color"].isNull() ? userJson["color"].asString() : "#000000";
_show_gender = userJson.isMember("show_gender") && !userJson["show_gender"].isNull() ? userJson["show_gender"].asBool() : true;
_show_age = userJson.isMember("show_age") && !userJson["show_age"].isNull() ? userJson["show_age"].asBool() : true;
_created_at = userJson.isMember("created_at") && !userJson["created_at"].isNull() ? userJson["created_at"].asString() : "";
_updated_at = userJson.isMember("updated_at") && !userJson["updated_at"].isNull() ? userJson["updated_at"].asString() : "";
if (userJson.isMember("rights") && userJson["rights"].isArray()) {
for (const auto& r : userJson["rights"]) {
_rights.push_back(r.asString());
}
}
}
int User::id() const { return _id; }
int User::falukant_user_id() const { return _falukant_user_id; }
const std::string& User::display_name() const { return _display_name; }
const std::string& User::color() const { return _color; }
bool User::show_gender() const { return _show_gender; }
bool User::show_age() const { return _show_age; }
const std::string& User::created_at() const { return _created_at; }
const std::string& User::updated_at() const { return _updated_at; }
const std::vector<std::string>& User::rights() const { return _rights; }
} // namespace Object
} // namespace Yc

39
src/object/user.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include <string>
#include <vector>
#include <json/value.h>
namespace Yc
{
namespace Object
{
class User {
public:
User();
User(const Json::Value& userJson);
int id() const;
int falukant_user_id() const;
const std::string& display_name() const;
const std::string& color() const;
bool show_gender() const;
bool show_age() const;
const std::string& created_at() const;
const std::string& updated_at() const;
const std::vector<std::string>& rights() const;
private:
int _id = 0;
int _falukant_user_id = 0;
std::string _display_name;
std::string _color = "#000000";
bool _show_gender = true;
bool _show_age = true;
std::string _created_at;
std::string _updated_at;
std::vector<std::string> _rights;
};
} // namespace Object
} // namespace Yc