[Server] Add a servatrice-dev executable on the in-memory database (#7864)

This commit is contained in:
BruebachL 2026-11-10 12:16:36 +01:00 • committed by GitHub
parent a6d5dd5275
commit 15d69eb929
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 358 additions and 27 deletions

View file

@ -1,28 +1,34 @@
#include "libcockatrice/server/dev/servatrice_in_memory_database_interface.h"
Servatrice_InMemoryDatabaseInterface::Servatrice_InMemoryDatabaseInterface(QObject *parent)
: Server_DatabaseInterface(parent)
: Server_DatabaseInterface(parent), state(std::make_shared<State>())
{
}
void Servatrice_InMemoryDatabaseInterface::addUser(const ServerInfo_User &user)
{
users.insert(QString::fromStdString(user.name()), user);
state->users.insert(QString::fromStdString(user.name()), user);
}
void Servatrice_InMemoryDatabaseInterface::setAuthenticationResult(AuthenticationResult result)
{
authenticationResult = result;
state->authenticationResult = result;
}
void Servatrice_InMemoryDatabaseInterface::setActiveUserCount(int count)
{
activeUserCount = count;
state->activeUserCount = count;
}
const QList<Servatrice_InMemoryDatabaseInterface::LogEntry> &Servatrice_InMemoryDatabaseInterface::getLogEntries() const
void Servatrice_InMemoryDatabaseInterface::addRoom(const BootstrapRoomRecord &room)
{
return logEntries;
state->rooms.append(room);
}
QList<Servatrice_InMemoryDatabaseInterface::LogEntry> Servatrice_InMemoryDatabaseInterface::getLogEntries() const
{
QMutexLocker locker(&state->mutex);
return state->logEntries;
}
AuthenticationResult Servatrice_InMemoryDatabaseInterface::checkUserPassword(Server_ProtocolHandler *,
@ -33,14 +39,18 @@ AuthenticationResult Servatrice_InMemoryDatabaseInterface::checkUserPassword(Ser
int &,
bool)
{
return authenticationResult;
return state->authenticationResult;
}
ServerInfo_User Servatrice_InMemoryDatabaseInterface::getUserData(const QString &name, bool withId)
{
const auto it = users.constFind(name);
if (it == users.constEnd()) {
return {};
const auto it = state->users.constFind(name);
if (it == state->users.constEnd()) {
// The MySQL implementation always names the requested user, with the base user level.
ServerInfo_User user;
user.set_name(name.toStdString());
user.set_user_level(ServerInfo_User::IsUser);
return user;
}
ServerInfo_User user = *it;
@ -52,17 +62,31 @@ ServerInfo_User Servatrice_InMemoryDatabaseInterface::getUserData(const QString
int Servatrice_InMemoryDatabaseInterface::getNextGameId()
{
return nextGameId++;
QMutexLocker locker(&state->mutex);
return state->nextGameId++;
}
int Servatrice_InMemoryDatabaseInterface::getNextReplayId()
{
return nextReplayId++;
QMutexLocker locker(&state->mutex);
return state->nextReplayId++;
}
int Servatrice_InMemoryDatabaseInterface::getActiveUserCount(QString)
{
return activeUserCount;
return state->activeUserCount;
}
QList<Server_DatabaseInterface::BootstrapRoomRecord> Servatrice_InMemoryDatabaseInterface::getBootstrapRooms(int)
{
return state->rooms;
}
Server_DatabaseInterface *Servatrice_InMemoryDatabaseInterface::createWorkerInterface(int)
{
auto *worker = new Servatrice_InMemoryDatabaseInterface();
worker->state = state;
return worker;
}
void Servatrice_InMemoryDatabaseInterface::logMessage(const int,
@ -74,10 +98,11 @@ void Servatrice_InMemoryDatabaseInterface::logMessage(const int,
const QString &targetName,
const QString &channel)
{
logEntries.append(LogEntry{.senderName = senderName,
.message = logMessage,
.targetType = targetType,
.targetId = targetId,
.targetName = targetName,
.channel = channel});
QMutexLocker locker(&state->mutex);
state->logEntries.append(LogEntry{.senderName = senderName,
.message = logMessage,
.targetType = targetType,
.targetId = targetId,
.targetName = targetName,
.channel = channel});
}

View file

@ -5,7 +5,9 @@
#include <QHash>
#include <QList>
#include <QMutex>
#include <QString>
#include <memory>
/**
* @brief In-memory Server_DatabaseInterface for tests and the dev server.
@ -37,8 +39,10 @@ public:
void setAuthenticationResult(AuthenticationResult result);
/** @brief Sets the value getActiveUserCount() reports. */
void setActiveUserCount(int count);
/** @brief Adds a room the SQL room bootstrap returns, in insertion order. */
void addRoom(const BootstrapRoomRecord &room);
const QList<LogEntry> &getLogEntries() const;
QList<LogEntry> getLogEntries() const;
AuthenticationResult checkUserPassword(Server_ProtocolHandler * /* handler */,
const QString & /* user */,
@ -51,6 +55,8 @@ public:
int getNextGameId() override;
int getNextReplayId() override;
int getActiveUserCount(QString connectionType = QString()) override;
QList<BootstrapRoomRecord> getBootstrapRooms(int serverId) override;
Server_DatabaseInterface *createWorkerInterface(int instanceId) override;
void logMessage(const int /* senderId */,
const QString &senderName,
const QString & /* senderIp */,
@ -61,12 +67,20 @@ public:
const QString &channel = QString()) override;
private:
QHash<QString, ServerInfo_User> users;
AuthenticationResult authenticationResult = NotLoggedIn;
int activeUserCount = 0;
int nextGameId = 1;
int nextReplayId = 1;
QList<LogEntry> logEntries;
/** @brief The state every interface created by createWorkerInterface() shares. */
struct State
{
QHash<QString, ServerInfo_User> users;
AuthenticationResult authenticationResult = NotLoggedIn;
int activeUserCount = 0;
int nextGameId = 1;
int nextReplayId = 1;
QList<BootstrapRoomRecord> rooms;
QList<LogEntry> logEntries;
mutable QMutex mutex;
};
std::shared_ptr<State> state;
};
#endif // SERVATRICE_IN_MEMORY_DATABASE_INTERFACE_H