Skip to content

Commit

Permalink
Use Char.Vitals to populate the group manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nschimme committed Sep 24, 2021
1 parent b1e226a commit 1672bed
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/pandoragroup/mmapper2group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "../global/AnsiColor.h"
#include "../global/roomid.h"
#include "../parser/CommandQueue.h"
#include "../proxy/GmcpMessage.h"
#include "CGroup.h"
#include "CGroupChar.h"
#include "GroupClient.h"
Expand Down Expand Up @@ -46,6 +47,7 @@ Mmapper2Group::Mmapper2Group(QObject *const /* parent */)
{
qRegisterMetaType<CharacterPositionEnum>("CharacterPositionEnum");
qRegisterMetaType<CharacterAffectEnum>("CharacterAffectEnum");
qRegisterMetaType<GmcpMessage>("GmcpMessage");

connect(this,
&Mmapper2Group::sig_invokeStopInternal,
Expand Down Expand Up @@ -299,6 +301,16 @@ void Mmapper2Group::parseScoreInformation(const QByteArray &score)
const int moves = match.captured(5).toInt();
const int maxmoves = match.captured(6).toInt();

updateCharacterScore(hp, maxhp, mana, maxmana, moves, maxmoves);
}

void Mmapper2Group::updateCharacterScore(const int hp,
const int maxhp,
const int mana,
const int maxmana,
const int moves,
const int maxmoves)
{
const SharedGroupChar &self = getGroup()->getSelf();
if (self->hp == hp && self->maxhp == maxhp && self->mana == mana && self->maxmana == maxmana
&& self->moves == moves && self->maxmoves == maxmoves)
Expand Down Expand Up @@ -545,6 +557,30 @@ void Mmapper2Group::slot_reset()
issueLocalCharUpdate();
}

void Mmapper2Group::slot_parseGmcpInput(const GmcpMessage &msg)
{
if (!group)
return;

if (msg.isCharVitals()) {
// "Char.Vitals {\"hp\":100,\"maxhp\":100,\"mana\":100,\"maxmana\":100,\"mp\":139,\"maxmp\":139}"
QJsonDocument doc = QJsonDocument::fromJson(msg.getJson()->toQString().toUtf8());
if (!doc.isObject())
return;
const auto &obj = doc.object();

const SharedGroupChar &self = getGroup()->getSelf();
const int hp = obj.value("hp").toInt(self->hp);
const int maxhp = obj.value("maxhp").toInt(self->maxhp);
const int mana = obj.value("mana").toInt(self->mana);
const int maxmana = obj.value("maxmana").toInt(self->maxmana);
const int mp = obj.value("mp").toInt(self->moves);
const int maxmp = obj.value("maxmp").toInt(self->maxmoves);

updateCharacterScore(hp, maxhp, mana, maxmana, mp, maxmp);
}
}

void Mmapper2Group::slot_sendLog(const QString &text)
{
log(text);
Expand Down
8 changes: 8 additions & 0 deletions src/pandoragroup/mmapper2group.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "GroupManagerApi.h"
#include "mmapper2character.h"

class GmcpMessage;
class GroupAuthority;
class CGroupCommunicator;
class CGroup;
Expand Down Expand Up @@ -100,6 +101,7 @@ public slots:

void slot_setPath(CommandQueue);
void slot_reset();
void slot_parseGmcpInput(const GmcpMessage &msg);

protected slots:
// Communicator
Expand Down Expand Up @@ -130,6 +132,12 @@ protected slots:

bool init();
void issueLocalCharUpdate();
void updateCharacterScore(const int hp,
const int maxhp,
const int mana,
const int maxmana,
const int mp,
const int maxmp);

QMutex networkLock{QMutex::Recursive};
std::unique_ptr<QThread> thread;
Expand Down
5 changes: 5 additions & 0 deletions src/proxy/GmcpMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ NODISCARD static GmcpMessageTypeEnum toGmcpMessageType(const std::string &str)
return GmcpMessageTypeEnum::UNKNOWN;
}

GmcpMessage::GmcpMessage()
: name("")
, type(GmcpMessageTypeEnum::UNKNOWN)
{}

GmcpMessage::GmcpMessage(const std::string &package)
: name(package)
, type(toGmcpMessageType(package))
Expand Down
5 changes: 3 additions & 2 deletions src/proxy/GmcpMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ParseEvent;

// X(UPPER_CASE, CamelCase, "normalized name", "friendly name")
#define X_FOREACH_GMCP_MESSAGE_TYPE(X) \
X(CHAR_VITALS, CharVitals, "char.vitals", "Char.Vitals") \
X(CORE_GOODBYE, CoreGoodbye, "core.goodbye", "Core.Goodbye") \
X(CORE_HELLO, CoreHello, "core.hello", "Core.Hello") \
X(CORE_SUPPORTS_ADD, CoreSupportsAdd, "core.supports.add", "Core.Supports.Add") \
Expand All @@ -33,10 +34,9 @@ enum class NODISCARD GmcpMessageTypeEnum {
#undef X_DECL_GMCP_MESSAGE_TYPE
};

static constexpr const size_t NUM_GMCP_MESSAGES = 6u;
static constexpr const size_t NUM_GMCP_MESSAGES = 7u;
static_assert(NUM_GMCP_MESSAGES
== static_cast<int>(GmcpMessageTypeEnum::MMAPPER_COMM_GROUPTELL) + 1);
DEFINE_ENUM_COUNT(GmcpMessageTypeEnum, NUM_GMCP_MESSAGES)

namespace tags {
struct NODISCARD GmcpMessageNameTag final
Expand All @@ -60,6 +60,7 @@ class GmcpMessage final
GmcpMessageTypeEnum type = GmcpMessageTypeEnum::UNKNOWN;

public:
explicit GmcpMessage();
explicit GmcpMessage(const std::string &package);
explicit GmcpMessage(const std::string &package, const std::string &json);
explicit GmcpMessage(const GmcpMessageTypeEnum type);
Expand Down
4 changes: 4 additions & 0 deletions src/proxy/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ void Proxy::slot_start()
connect(mudTelnet, &MudTelnet::sig_sendToSocket, this, &Proxy::slot_onSendToMudSocket);
connect(mudTelnet, &MudTelnet::sig_relayEchoMode, userTelnet, &UserTelnet::slot_onRelayEchoMode);
connect(mudTelnet, &MudTelnet::sig_relayGmcp, userTelnet, &UserTelnet::slot_onGmcpToUser);
connect(mudTelnet,
&MudTelnet::sig_relayGmcp,
&m_groupManager,
&Mmapper2Group::slot_parseGmcpInput);

connect(this, &Proxy::sig_analyzeUserStream, userTelnet, &UserTelnet::slot_onAnalyzeUserStream);

Expand Down

0 comments on commit 1672bed

Please sign in to comment.