Refactor: Move files in src/client/game_logic (#6070)

* move abstract_client to src/server

* move key_signals to src/utility
This commit is contained in:
RickyRister 2025-08-07 11:47:48 -03:00 committed by GitHub
parent fe7853a389
commit 3a42354efd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 32 additions and 32 deletions

View file

@ -0,0 +1,198 @@
#include "abstract_client.h"
#include "featureset.h"
#include "get_pb_extension.h"
#include "pb/commands.pb.h"
#include "pb/event_add_to_list.pb.h"
#include "pb/event_connection_closed.pb.h"
#include "pb/event_game_joined.pb.h"
#include "pb/event_list_rooms.pb.h"
#include "pb/event_notify_user.pb.h"
#include "pb/event_remove_from_list.pb.h"
#include "pb/event_replay_added.pb.h"
#include "pb/event_server_identification.pb.h"
#include "pb/event_server_message.pb.h"
#include "pb/event_server_shutdown.pb.h"
#include "pb/event_user_joined.pb.h"
#include "pb/event_user_left.pb.h"
#include "pb/event_user_message.pb.h"
#include "pb/server_message.pb.h"
#include "pending_command.h"
#include <google/protobuf/descriptor.h>
AbstractClient::AbstractClient(QObject *parent)
: QObject(parent), nextCmdId(0), status(StatusDisconnected), serverSupportsPasswordHash(false)
{
qRegisterMetaType<QVariant>("QVariant");
qRegisterMetaType<CommandContainer>("CommandContainer");
qRegisterMetaType<Response>("Response");
qRegisterMetaType<Response::ResponseCode>("Response::ResponseCode");
qRegisterMetaType<ClientStatus>("ClientStatus");
qRegisterMetaType<RoomEvent>("RoomEvent");
qRegisterMetaType<GameEventContainer>("GameEventContainer");
qRegisterMetaType<Event_ServerIdentification>("Event_ServerIdentification");
qRegisterMetaType<Event_ConnectionClosed>("Event_ConnectionClosed");
qRegisterMetaType<Event_ServerShutdown>("Event_ServerShutdown");
qRegisterMetaType<Event_AddToList>("Event_AddToList");
qRegisterMetaType<Event_RemoveFromList>("Event_RemoveFromList");
qRegisterMetaType<Event_UserJoined>("Event_UserJoined");
qRegisterMetaType<Event_UserLeft>("Event_UserLeft");
qRegisterMetaType<Event_ServerMessage>("Event_ServerMessage");
qRegisterMetaType<Event_ListRooms>("Event_ListRooms");
qRegisterMetaType<Event_GameJoined>("Event_GameJoined");
qRegisterMetaType<Event_UserMessage>("Event_UserMessage");
qRegisterMetaType<Event_NotifyUser>("Event_NotifyUser");
qRegisterMetaType<ServerInfo_User>("ServerInfo_User");
qRegisterMetaType<QList<ServerInfo_User>>("QList<ServerInfo_User>");
qRegisterMetaType<Event_ReplayAdded>("Event_ReplayAdded");
qRegisterMetaType<QList<QString>>("missingFeatures");
qRegisterMetaType<PendingCommand *>("pendingCommand");
FeatureSet features;
features.initalizeFeatureList(clientFeatures);
connect(this, &AbstractClient::sigQueuePendingCommand, this, &AbstractClient::queuePendingCommand);
}
AbstractClient::~AbstractClient()
{
}
void AbstractClient::processProtocolItem(const ServerMessage &item)
{
switch (item.message_type()) {
case ServerMessage::RESPONSE: {
const Response &response = item.response();
const int cmdId = response.cmd_id();
PendingCommand *pend = pendingCommands.value(cmdId, 0);
if (!pend)
return;
pendingCommands.remove(cmdId);
pend->processResponse(response);
pend->deleteLater();
break;
}
case ServerMessage::SESSION_EVENT: {
const SessionEvent &event = item.session_event();
switch ((SessionEvent::SessionEventType)getPbExtension(event)) {
case SessionEvent::SERVER_IDENTIFICATION:
emit serverIdentificationEventReceived(event.GetExtension(Event_ServerIdentification::ext));
break;
case SessionEvent::SERVER_MESSAGE:
emit serverMessageEventReceived(event.GetExtension(Event_ServerMessage::ext));
break;
case SessionEvent::SERVER_SHUTDOWN:
emit serverShutdownEventReceived(event.GetExtension(Event_ServerShutdown::ext));
break;
case SessionEvent::CONNECTION_CLOSED:
emit connectionClosedEventReceived(event.GetExtension(Event_ConnectionClosed::ext));
break;
case SessionEvent::USER_MESSAGE:
emit userMessageEventReceived(event.GetExtension(Event_UserMessage::ext));
break;
case SessionEvent::NOTIFY_USER:
emit notifyUserEventReceived(event.GetExtension(Event_NotifyUser::ext));
break;
case SessionEvent::LIST_ROOMS:
emit listRoomsEventReceived(event.GetExtension(Event_ListRooms::ext));
break;
case SessionEvent::ADD_TO_LIST:
emit addToListEventReceived(event.GetExtension(Event_AddToList::ext));
break;
case SessionEvent::REMOVE_FROM_LIST:
emit removeFromListEventReceived(event.GetExtension(Event_RemoveFromList::ext));
break;
case SessionEvent::USER_JOINED:
emit userJoinedEventReceived(event.GetExtension(Event_UserJoined::ext));
break;
case SessionEvent::USER_LEFT:
emit userLeftEventReceived(event.GetExtension(Event_UserLeft::ext));
break;
case SessionEvent::GAME_JOINED:
emit gameJoinedEventReceived(event.GetExtension(Event_GameJoined::ext));
break;
case SessionEvent::REPLAY_ADDED:
emit replayAddedEventReceived(event.GetExtension(Event_ReplayAdded::ext));
break;
default:
break;
}
break;
}
case ServerMessage::GAME_EVENT_CONTAINER: {
emit gameEventContainerReceived(item.game_event_container());
break;
}
case ServerMessage::ROOM_EVENT: {
emit roomEventReceived(item.room_event());
break;
}
}
}
void AbstractClient::setStatus(const ClientStatus _status)
{
QMutexLocker locker(&clientMutex);
if (_status != status) {
status = _status;
emit statusChanged(_status);
}
}
void AbstractClient::sendCommand(const CommandContainer &cont)
{
sendCommand(new PendingCommand(cont));
}
void AbstractClient::sendCommand(PendingCommand *pend)
{
pend->moveToThread(thread());
emit sigQueuePendingCommand(pend);
}
void AbstractClient::queuePendingCommand(PendingCommand *pend)
{
// This function is always called from the client thread via signal/slot.
const int cmdId = getNewCmdId();
pend->getCommandContainer().set_cmd_id(cmdId);
pendingCommands.insert(cmdId, pend);
sendCommandContainer(pend->getCommandContainer());
}
PendingCommand *AbstractClient::prepareSessionCommand(const ::google::protobuf::Message &cmd)
{
CommandContainer cont;
SessionCommand *c = cont.add_session_command();
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
return new PendingCommand(cont);
}
PendingCommand *AbstractClient::prepareRoomCommand(const ::google::protobuf::Message &cmd, int roomId)
{
CommandContainer cont;
RoomCommand *c = cont.add_room_command();
cont.set_room_id(roomId);
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
return new PendingCommand(cont);
}
PendingCommand *AbstractClient::prepareModeratorCommand(const ::google::protobuf::Message &cmd)
{
CommandContainer cont;
ModeratorCommand *c = cont.add_moderator_command();
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
return new PendingCommand(cont);
}
PendingCommand *AbstractClient::prepareAdminCommand(const ::google::protobuf::Message &cmd)
{
CommandContainer cont;
AdminCommand *c = cont.add_admin_command();
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
return new PendingCommand(cont);
}

View file

@ -0,0 +1,129 @@
#ifndef ABSTRACTCLIENT_H
#define ABSTRACTCLIENT_H
#include "pb/response.pb.h"
#include "pb/serverinfo_user.pb.h"
#include <QMutex>
#include <QObject>
#include <QVariant>
class PendingCommand;
class CommandContainer;
class RoomEvent;
class GameEventContainer;
class ServerMessage;
class Event_ServerIdentification;
class Event_AddToList;
class Event_RemoveFromList;
class Event_UserJoined;
class Event_UserLeft;
class Event_ServerMessage;
class Event_ListRooms;
class Event_GameJoined;
class Event_UserMessage;
class Event_NotifyUser;
class Event_ConnectionClosed;
class Event_ServerShutdown;
class Event_ReplayAdded;
class FeatureSet;
enum ClientStatus
{
StatusDisconnected,
StatusDisconnecting,
StatusConnecting,
StatusRegistering,
StatusActivating,
StatusLoggingIn,
StatusLoggedIn,
StatusRequestingForgotPassword,
StatusSubmitForgotPasswordReset,
StatusSubmitForgotPasswordChallenge,
StatusGettingPasswordSalt,
};
class AbstractClient : public QObject
{
Q_OBJECT
signals:
void statusChanged(ClientStatus _status);
void maxPingTime(int seconds, int maxSeconds);
// Room events
void roomEventReceived(const RoomEvent &event);
// Game events
void gameEventContainerReceived(const GameEventContainer &event);
// Session events
void serverIdentificationEventReceived(const Event_ServerIdentification &event);
void connectionClosedEventReceived(const Event_ConnectionClosed &event);
void serverShutdownEventReceived(const Event_ServerShutdown &event);
void addToListEventReceived(const Event_AddToList &event);
void removeFromListEventReceived(const Event_RemoveFromList &event);
void userJoinedEventReceived(const Event_UserJoined &event);
void userLeftEventReceived(const Event_UserLeft &event);
void serverMessageEventReceived(const Event_ServerMessage &event);
void listRoomsEventReceived(const Event_ListRooms &event);
void gameJoinedEventReceived(const Event_GameJoined &event);
void userMessageEventReceived(const Event_UserMessage &event);
void notifyUserEventReceived(const Event_NotifyUser &event);
void userInfoChanged(const ServerInfo_User &userInfo);
void buddyListReceived(const QList<ServerInfo_User> &buddyList);
void ignoreListReceived(const QList<ServerInfo_User> &ignoreList);
void replayAddedEventReceived(const Event_ReplayAdded &event);
void registerAccepted();
void registerAcceptedNeedsActivate();
void activateAccepted();
void sigQueuePendingCommand(PendingCommand *pend);
private:
int nextCmdId;
mutable QMutex clientMutex;
ClientStatus status;
private slots:
void queuePendingCommand(PendingCommand *pend);
protected slots:
void processProtocolItem(const ServerMessage &item);
protected:
QMap<int, PendingCommand *> pendingCommands;
QString userName, password, email, country, realName, token;
bool serverSupportsPasswordHash;
void setStatus(ClientStatus _status);
int getNewCmdId()
{
return nextCmdId++;
}
virtual void sendCommandContainer(const CommandContainer &cont) = 0;
public:
explicit AbstractClient(QObject *parent = nullptr);
~AbstractClient() override;
ClientStatus getStatus() const
{
QMutexLocker locker(&clientMutex);
return status;
}
void sendCommand(const CommandContainer &cont);
void sendCommand(PendingCommand *pend);
bool getServerSupportsPasswordHash() const
{
return serverSupportsPasswordHash;
}
const QString &getUserName() const
{
return userName;
}
static PendingCommand *prepareSessionCommand(const ::google::protobuf::Message &cmd);
static PendingCommand *prepareRoomCommand(const ::google::protobuf::Message &cmd, int roomId);
static PendingCommand *prepareModeratorCommand(const ::google::protobuf::Message &cmd);
static PendingCommand *prepareAdminCommand(const ::google::protobuf::Message &cmd);
QMap<QString, bool> clientFeatures;
};
#endif

View file

@ -1,7 +1,7 @@
#ifndef LOCALCLIENT_H
#define LOCALCLIENT_H
#include "../client/game_logic/abstract_client.h"
#include "abstract_client.h"
#include <QLoggingCategory>

View file

@ -1,7 +1,7 @@
#ifndef REMOTECLIENT_H
#define REMOTECLIENT_H
#include "../../client/game_logic/abstract_client.h"
#include "../abstract_client.h"
#include "pb/commands.pb.h"
#include <QLoggingCategory>

View file

@ -1,6 +1,6 @@
#include "remote_decklist_tree_widget.h"
#include "../../client/game_logic/abstract_client.h"
#include "../abstract_client.h"
#include "../pending_command.h"
#include "pb/command_deck_list.pb.h"
#include "pb/response_deck_list.pb.h"

View file

@ -1,6 +1,6 @@
#include "remote_replay_list_tree_widget.h"
#include "../../client/game_logic/abstract_client.h"
#include "../abstract_client.h"
#include "../pending_command.h"
#include "pb/command_replay_list.pb.h"
#include "pb/response_replay_list.pb.h"

View file

@ -1,10 +1,10 @@
#include "user_context_menu.h"
#include "../../client/game_logic/abstract_client.h"
#include "../../client/tabs/tab_account.h"
#include "../../client/tabs/tab_game.h"
#include "../../client/tabs/tab_supervisor.h"
#include "../../game/game_selector.h"
#include "../abstract_client.h"
#include "../chat_view/chat_view.h"
#include "../pending_command.h"
#include "pb/command_kick_from_game.pb.h"

View file

@ -1,11 +1,11 @@
#include "user_info_box.h"
#include "../../client/game_logic/abstract_client.h"
#include "../../client/get_text_with_max.h"
#include "../../client/ui/pixel_map_generator.h"
#include "../../dialogs/dlg_edit_avatar.h"
#include "../../dialogs/dlg_edit_password.h"
#include "../../dialogs/dlg_edit_user.h"
#include "../abstract_client.h"
#include "../pending_command.h"
#include "passwordhasher.h"
#include "pb/response_get_user_info.pb.h"

View file

@ -1,7 +1,7 @@
#include "user_list_manager.h"
#include "../../client/game_logic/abstract_client.h"
#include "../../client/sound_engine.h"
#include "../abstract_client.h"
#include "../pending_command.h"
#include "pb/event_add_to_list.pb.h"
#include "pb/event_remove_from_list.pb.h"

View file

@ -1,11 +1,11 @@
#include "user_list_widget.h"
#include "../../client/game_logic/abstract_client.h"
#include "../../client/tabs/tab_account.h"
#include "../../client/tabs/tab_supervisor.h"
#include "../../client/ui/pixel_map_generator.h"
#include "../../game/game_selector.h"
#include "../../server/user/user_list_manager.h"
#include "../abstract_client.h"
#include "../pending_command.h"
#include "pb/moderator_commands.pb.h"
#include "pb/response_get_games_of_user.pb.h"