Refactor project structure: replace User class with ChatUser, integrate Database class, and update CMake configuration for new files
This commit is contained in:
13
server.cpp
13
server.cpp
@@ -15,8 +15,9 @@
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
|
||||
Server::Server(std::shared_ptr<Yc::Lib::Config> config) :
|
||||
Server::Server(std::shared_ptr<Yc::Lib::Config> config, std::shared_ptr<Yc::Lib::Database> database) :
|
||||
_config(std::move(config)),
|
||||
_database(std::move(database)),
|
||||
_stop(false) {
|
||||
struct sockaddr_in serverAddr;
|
||||
int opt = true;
|
||||
@@ -82,7 +83,7 @@ namespace Yc {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Server::changeRoom(std::shared_ptr<User> user, std::string newRoom, std::string password) {
|
||||
bool Server::changeRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password) {
|
||||
if (!roomAllowed(newRoom, user->name(), password)) {
|
||||
return false;
|
||||
}
|
||||
@@ -96,16 +97,16 @@ namespace Yc {
|
||||
msg["tr"] = "room_change_to";
|
||||
msg["to"] = newRoom;
|
||||
userMsg["from"] = room->name();
|
||||
room->addMessage(User::system, msg, user->name(), user->color());
|
||||
room->addMessage(ChatUser::system, msg, user->name(), user->color());
|
||||
}
|
||||
}
|
||||
user->sendMsg(User::system, userMsg, "", "");
|
||||
user->sendMsg(ChatUser::system, userMsg, "", "");
|
||||
for (auto &room: _rooms) {
|
||||
if (room->name() == newRoom) {
|
||||
Json::Value msg = Json::objectValue;
|
||||
msg["tr"] = "room_change_to";
|
||||
msg["from"] = userMsg["from"];
|
||||
room->addMessage(User::system, msg, user->name(), user->color());
|
||||
room->addMessage(ChatUser::system, msg, user->name(), user->color());
|
||||
room->addUserWhenQueueEmpty(user);
|
||||
}
|
||||
}
|
||||
@@ -155,7 +156,7 @@ namespace Yc {
|
||||
void Server::initUser(int userSocket, Json::Value data) {
|
||||
if (userExists(data["name"].asString())) {
|
||||
Json::Value errorJson;
|
||||
errorJson["type"] = User::error;
|
||||
errorJson["type"] = ChatUser::error;
|
||||
errorJson["message"] = "loggedin";
|
||||
send(userSocket, errorJson);
|
||||
close(userSocket);
|
||||
|
||||
Reference in New Issue
Block a user