Skip to content

Commit

Permalink
more map file server work
Browse files Browse the repository at this point in the history
  • Loading branch information
Robosturm committed Nov 3, 2023
1 parent b1ff542 commit 3cec981
Show file tree
Hide file tree
Showing 25 changed files with 628 additions and 28 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ set(${PROJECT_NAME}_SRCS
network/automatchmaker.h network/automatchmaker.cpp
network/networkgame.cpp network/networkgame.h
network/matchmakingcoordinator.h network/matchmakingcoordinator.cpp
network/mapfileserver.h network/mapfileserver.cpp
network/JsonKeys.h

# game
Expand Down
25 changes: 24 additions & 1 deletion coreengine/globalutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

static constexpr double DOUBLE_EPSILON = 0.5;
static constexpr float FLOAT_EPSILON = 0.5f;
static constexpr qint32 HEXBASE = 16;
static constexpr qint32 MAGIC_HEADER_SIZE = 18;
static const quint8 MAP_MAGIC_DATA[MAGIC_HEADER_SIZE] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const QByteArray GlobalUtils::MAP_MAGIC(reinterpret_cast<const char*>(MAP_MAGIC_DATA), MAGIC_HEADER_SIZE);

spGlobalUtils GlobalUtils::m_pInstace;

GlobalUtils::GlobalUtils()
Expand All @@ -41,7 +48,23 @@ QString GlobalUtils::getByteArrayString(const QByteArray & bytes)
QString data;
for (qint32 i = 0; i < bytes.size(); i++)
{
data += "0x" + QString::number(bytes[i], 16)+ " ";
if (i > 0)
{
data += ",";
}
data += QString::number(bytes[i], HEXBASE);
}
return data;
}

QByteArray GlobalUtils::getStringByteArray(const QString & bytes)
{
QByteArray data;
QStringList items = bytes.split(",");
for (auto & item : items)
{
bool ok;
data += item.toInt(&ok, HEXBASE);
}
return data;
}
Expand Down
4 changes: 4 additions & 0 deletions coreengine/globalutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QObject>
#include <QJsonArray>
#include <QByteArray>
#include <QDir>
#include <QRandomGenerator>

Expand All @@ -18,6 +19,8 @@ class GlobalUtils final : public QObject
{
Q_OBJECT
public:
static const QByteArray MAP_MAGIC;

virtual ~GlobalUtils() = default;
static GlobalUtils* getInstance()
{
Expand All @@ -32,6 +35,7 @@ class GlobalUtils final : public QObject
static QFileInfoList getInfoList(const QString & folder, const QStringList & list = QStringList());
static QUrl getUrlForFile(const QString & file);
static QString getByteArrayString(const QByteArray & bytes);
static QByteArray getStringByteArray(const QString & bytes);
static QVector<qint32> calcWidths(const QVector<qint32> & maxWidths, const QVector<float> & distribution, qint32 totalWidth);
template<typename TData>
static bool contains(const std::vector<TData> & vec, const TData & data)
Expand Down
4 changes: 2 additions & 2 deletions coreengine/mainapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,13 @@ void Mainapp::doMapshot(BaseGamemenu* pMenu)
}
}

void Mainapp::saveMapAsImage(Minimap* pMinimap, QImage img)
void Mainapp::saveMapAsImage(Minimap* pMinimap, QImage * img)
{
if (!m_shuttingDown && !m_noUi)
{
if (isMainThread())
{
GamemapImageSaver::saveMapAsImage(pMinimap, img);
GamemapImageSaver::saveMapAsImage(pMinimap, *img);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions coreengine/mainapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public slots:
* @param pMinimap
* @param img
*/
void saveMapAsImage(Minimap* pMinimap, QImage img);
void saveMapAsImage(Minimap* pMinimap, QImage * img);
signals:
void sigKeyDown(oxygine::KeyEvent event);
void sigKeyUp(oxygine::KeyEvent event);
Expand All @@ -300,7 +300,7 @@ public slots:
void sigNextStartUpStep(Mainapp::StartupPhase step);
void sigCreateLineEdit();
void sigDoMapshot(BaseGamemenu* pMenu);
void sigSaveMapAsImage(Minimap* pMinimap, QImage img);
void sigSaveMapAsImage(Minimap* pMinimap, QImage * img);
protected:
virtual void keyPressEvent(QKeyEvent *event) override;
virtual void keyReleaseEvent(QKeyEvent *event) override;
Expand Down
11 changes: 11 additions & 0 deletions coreengine/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ Settings::Settings()
Interpreter::setCppOwnerShip(this);
}

bool Settings::getAllowMapUpload() const
{
return m_allowMapUpload;
}

void Settings::setAllowMapUpload(bool newAllowMapUpload)
{
m_allowMapUpload = newAllowMapUpload;
}

bool Settings::getCreateAiTrainingData() const
{
return m_createAiTrainingData;
Expand Down Expand Up @@ -1289,6 +1299,7 @@ void Settings::setup()
MemoryManagement::create<Value<quint16>>("Network", "SlaveServerPort", &m_slaveServerPort, 9003, 0, std::numeric_limits<quint16>::max()),
MemoryManagement::create<Value<quint16>>("Network", "ServerPort", &m_ServerPort, 9002, 0, std::numeric_limits<quint16>::max()),
MemoryManagement::create<Value<bool>>("Network", "Server", &m_Server, false, false, true),
MemoryManagement::create<Value<bool>>("Network", "AllowMapUpload", &m_allowMapUpload, true, false, true),
MemoryManagement::create<Value<QString>>("Network", "ServerListenAdress", &m_serverListenAdress, "", "", ""),
MemoryManagement::create<Value<QString>>("Network", "ServerSecondaryListenAdress", &m_serverSecondaryListenAdress, "", "", ""),
MemoryManagement::create<Value<QString>>("Network", "SlaveListenAdress", &m_slaveListenAdress, "", "", ""),
Expand Down
4 changes: 4 additions & 0 deletions coreengine/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ class Settings final : public QObject
*/
Q_INVOKABLE void setCreateAiTrainingData(bool newCreateAiTrainingData);

bool getAllowMapUpload() const;
void setAllowMapUpload(bool newAllowMapUpload);

private:
friend class MemoryManagement;
explicit Settings();
Expand Down Expand Up @@ -883,6 +886,7 @@ class Settings final : public QObject
QString m_Username;
QString m_slaveServerName;
QString m_serverPassword;
bool m_allowMapUpload{true};

bool m_Server{false};
quint16 m_ServerPort{9002};
Expand Down
24 changes: 23 additions & 1 deletion game/gamemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ void GameMap::serializeObject(QDataStream& pStream, bool forHash) const
pStream << getVersion();
if (!forHash)
{
pStream << GlobalUtils::MAP_MAGIC;
pStream << m_headerInfo.m_mapName;
pStream << m_headerInfo.m_mapAuthor;
pStream << m_headerInfo.m_mapDescription;
Expand Down Expand Up @@ -1629,9 +1630,27 @@ oxygine::spActor GameMap::getMarkedFieldsLayer() const
return m_markedFieldsLayer;
}

bool GameMap::validMap(const MapHeaderInfo & headerInfo)
{
return headerInfo.m_mapMagic == GlobalUtils::MAP_MAGIC;
}

bool GameMap::validMap() const
{
return validMap(m_headerInfo);
}

void GameMap::readMapHeader(QDataStream& pStream, MapHeaderInfo & headerInfo)
{
pStream >> headerInfo.m_Version;
if (headerInfo.m_Version > 15)
{
pStream >> headerInfo.m_mapMagic;
if (!validMap(headerInfo))
{
return;
}
}
if (headerInfo.m_Version > 1)
{
pStream >> headerInfo.m_mapName;
Expand Down Expand Up @@ -1667,7 +1686,10 @@ void GameMap::deserializer(QDataStream& pStream, bool fast)
spLoadingScreen pLoadingScreen = LoadingScreen::getInstance();
// restore map header
readMapHeader(pStream, m_headerInfo);

if (validMap(m_headerInfo))
{
return;
}
CONSOLE_PRINT("Loading map " + m_headerInfo.m_mapName + " Fast =" + (fast ? "true" : "false"), GameConsole::eDEBUG);
qint32 mapSize = m_headerInfo.m_width * m_headerInfo.m_heigth;
setSize(m_headerInfo.m_width * GameMap::getImageSize(), m_headerInfo.m_heigth * GameMap::getImageSize());
Expand Down
14 changes: 13 additions & 1 deletion game/gamemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class GameMap : public QObject, public FileSerializable, public oxygine::Actor,
qint32 m_heigth{0};
qint32 m_playerCount{0};
qint32 m_uniqueIdCounter{0};
QByteArray m_mapMagic;
mutable GameEnums::MapFilterFlags m_mapFlags{GameEnums::MapFilterFlags_None};
};
/**
Expand Down Expand Up @@ -167,6 +168,17 @@ class GameMap : public QObject, public FileSerializable, public oxygine::Actor,
* @param headerInfo
*/
static void readMapHeader(QDataStream& pStream, MapHeaderInfo & headerInfo);
/**
* @brief validMap
* @param headerInfo
* @return
*/
static bool validMap(const MapHeaderInfo & headerInfo);
/**
* @brief validMap
* @return
*/
bool validMap() const;
/**
* @brief readMapName
* @param pStream
Expand All @@ -179,7 +191,7 @@ class GameMap : public QObject, public FileSerializable, public oxygine::Actor,
*/
inline virtual qint32 getVersion() const override
{
return 15;
return 16;
}
/**
* @brief clearMap
Expand Down
54 changes: 51 additions & 3 deletions mapsupport/mapfilter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mapsupport/mapfilter.h"
#include "network/JsonKeys.h"

bool MapFilter::matches(GameMap::MapHeaderInfo & info) const
{
Expand All @@ -14,10 +15,57 @@ bool MapFilter::matches(GameMap::MapHeaderInfo & info) const
matches = matches && m_players.matches(info.m_playerCount);
matches = matches && m_width.matches(info.m_width);
matches = matches && m_height.matches(info.m_heigth);
matches = matches && matchesMapFlags(info.m_mapFlags);
matches = matches && matchesMapFlags(m_filter, info.m_mapFlags);
return matches;
}

QJsonObject MapFilter::toJson() const
{
QJsonObject obj;
obj.insert(JsonKeys::JSONKEY_MAPNAME, m_mapName);
obj.insert(JsonKeys::JSONKEY_MAPAUTHOR, m_mapAuthor);
obj.insert(JsonKeys::JSONKEY_MINPLAYERS, m_players.minValue);
obj.insert(JsonKeys::JSONKEY_MAXPLAYERS, m_players.maxValue);
obj.insert(JsonKeys::JSONKEY_MINWIDTH, m_width.minValue);
obj.insert(JsonKeys::JSONKEY_MAXWIDTH, m_width.maxValue);
obj.insert(JsonKeys::JSONKEY_MINHEIGHT, m_height.minValue);
obj.insert(JsonKeys::JSONKEY_MAXHEIGHT, m_height.maxValue);
QJsonArray filters;
for (const auto & filter : m_filter)
{
QJsonObject filterObj;
filterObj.insert(JsonKeys::JSONKEY_FLAGFILTERFLAGS, filter.flag);
filterObj.insert(JsonKeys::JSONKEY_FLAGFILTERISACTIVE, filter.isActive);
filterObj.insert(JsonKeys::JSONKEY_FLAGFILTERISOPTIONAL, filter.isOptional);
filters.append(filterObj);
}
obj.insert(JsonKeys::JSONKEY_FLAGFILTER, filters);
return obj;
}

void MapFilter::fromJson(const QJsonObject & object)
{
m_mapName = object.value(JsonKeys::JSONKEY_MAPNAME).toString();
m_mapAuthor = object.value(JsonKeys::JSONKEY_MAPAUTHOR).toString();
m_players.minValue = object.value(JsonKeys::JSONKEY_MINPLAYERS).toInt();
m_players.maxValue = object.value(JsonKeys::JSONKEY_MAXPLAYERS).toInt();
m_width.minValue = object.value(JsonKeys::JSONKEY_MINWIDTH).toInt();
m_width.maxValue = object.value(JsonKeys::JSONKEY_MAXWIDTH).toInt();
m_height.minValue = object.value(JsonKeys::JSONKEY_MINHEIGHT).toInt();
m_height.maxValue = object.value(JsonKeys::JSONKEY_MAXHEIGHT).toInt();
QJsonArray filters = object.value(JsonKeys::JSONKEY_FLAGFILTER).toArray();
m_filter.clear();
for (const auto & filterObj : filters)
{
auto filterInfo = filterObj.toObject();
FlagFilter filter;
filter.flag = static_cast<GameEnums::MapFilterFlags>(filterInfo.value(JsonKeys::JSONKEY_FLAGFILTERFLAGS).toInteger());
filter.isActive = filterInfo.value(JsonKeys::JSONKEY_FLAGFILTERISACTIVE).toBool();
filter.isOptional = filterInfo.value(JsonKeys::JSONKEY_FLAGFILTERISOPTIONAL).toBool();
m_filter.append(filter);
}
}

void MapFilter::addToFilter(GameEnums::MapFilterFlags flag, bool active, bool isOptional)
{
bool found = false;
Expand Down Expand Up @@ -120,12 +168,12 @@ bool MapFilter::FlagFilter::matches(GameEnums::MapFilterFlags flags) const
return (flags & flag) > 0;
}

bool MapFilter::matchesMapFlags(GameEnums::MapFilterFlags flags) const
bool MapFilter::matchesMapFlags(const QVector<FlagFilter> & filters, GameEnums::MapFilterFlags flags)
{
bool matches = true;
bool hasOptional = false;
qint32 count = 0;
for (auto & filter : m_filter)
for (auto & filter : filters)
{
if (filter.isActive)
{
Expand Down
7 changes: 5 additions & 2 deletions mapsupport/mapfilter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <QJsonObject>

#include "game/GameEnums.h"
#include "game/gamemap.h"

Expand All @@ -22,6 +24,8 @@ class MapFilter final
public:
explicit MapFilter() = default;
virtual ~MapFilter() = default;
QJsonObject toJson() const;
void fromJson(const QJsonObject & object);

void setFlagActive(GameEnums::MapFilterFlags flag, bool isOptional);
bool getFlagActive(GameEnums::MapFilterFlags flag);
Expand Down Expand Up @@ -53,8 +57,7 @@ class MapFilter final
QString getMapName() const;
void setMapName(const QString &newMapName);

private:
bool matchesMapFlags(GameEnums::MapFilterFlags flags) const;
static bool matchesMapFlags(const QVector<FlagFilter> & filters, GameEnums::MapFilterFlags flags);
private:

QVector<FlagFilter> m_filter;
Expand Down
Loading

0 comments on commit 3cec981

Please sign in to comment.