Skip to content

Commit

Permalink
feat: added chat command for rolling dice
Browse files Browse the repository at this point in the history
  • Loading branch information
Ro_bat authored and Ro_bat committed Sep 18, 2023
1 parent 849acae commit 60450f1
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
2 changes: 1 addition & 1 deletion OmegaRPG/OmegaRPG.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(../version.pri)

RC_FILE = OmegaRPG.rc
QT += widgets network multimedia xml opengl quickwidgets svg
CONFIG += c++14
CONFIG += c++17
CONFIG += static
INCLUDEPATH += ..
# INCLUDEPATH += /usr/include/openssl-1.0/
Expand Down
2 changes: 2 additions & 0 deletions core/client/core-client.pri
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SOURCES += \
$$PWD/serversource/metaserversource.cpp \
$$PWD/clientcore.cpp \
$$PWD/networkclient.cpp \
$$PWD/util/chatcommand.cpp \
$$PWD/util/distancemeasure.cpp \
$$PWD/util/sslsettings.cpp \
$$PWD/util/textformat.cpp \
Expand Down Expand Up @@ -58,6 +59,7 @@ HEADERS += \
$$PWD/serversource/metaserversource.h \
$$PWD/clientcore.h \
$$PWD/networkclient.h \
$$PWD/util/chatcommand.h \
$$PWD/util/distancemeasure.h \
$$PWD/util/sslsettings.h \
$$PWD/util/textformat.h \
Expand Down
46 changes: 46 additions & 0 deletions core/client/util/chatcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "chatcommand.h"

const QString ChatCommand::sCommand = "command";
const QString ChatCommand::sArguments = "arguments";
const QRegularExpression ChatCommand::sParserString =
QRegularExpression("^"
"\\/(?<"+sCommand+">[a-zA-Z0-9_]+)([\\s\\t](?<"+sArguments+">.*))?"
"$");

ChatCommand::ChatCommand()
{
mType = ChatCommandType::INVALID;
}

ChatCommand::ChatCommand(ChatCommandType type, const QString& commandString, const QString& argumentString) {
mType = type;
mCommandString = commandString;
mArgumentString = argumentString;
}

std::optional<ChatCommand> ChatCommand::parse(const QString& chatMessage) {
const QRegularExpressionMatch match = sParserString.match(chatMessage);

if(match.hasMatch()) {
ChatCommandType type = ChatCommandType::INVALID;
QString commandString = match.captured(sCommand);
if(commandString == "roll" || commandString == "dice" || commandString == "diceroll" || commandString == "r") {
type = ChatCommandType::DICEROLL;
}
return ChatCommand(type, commandString, match.captured(sArguments));
} else {
return std::optional<ChatCommand>();
}
}

ChatCommandType ChatCommand::type() const {
return mType;
}

const QString& ChatCommand::commandString() const {
return mCommandString;
}

const QString& ChatCommand::argumentString() const {
return mArgumentString;
}
31 changes: 31 additions & 0 deletions core/client/util/chatcommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef CHATCOMMAND_H
#define CHATCOMMAND_H

#include<QString>
#include<optional>
#include<QRegularExpression>

enum class ChatCommandType {
DICEROLL,
INVALID
};

class ChatCommand
{
static const QString sCommand;
static const QString sArguments;
static const QRegularExpression sParserString;
ChatCommandType mType;
QString mCommandString;
QString mArgumentString;
public:
ChatCommand();
ChatCommand(ChatCommandType type, const QString& commandString, const QString& argumentString);
static std::optional<ChatCommand> parse(const QString& chatMessage);

ChatCommandType type() const;
const QString& commandString() const;
const QString& argumentString() const;
};

#endif // CHATCOMMAND_H
20 changes: 18 additions & 2 deletions gui/client/widgets/chatareawidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "widgets/squarepushbutton.h"
#include <QtGlobal>
#include "core/client/util/dice.h"
#include "core/client/util/diceroll.h"
#include "core/client/util/chatcommand.h"

QString ChatAreaWidget::getTo()
{
Expand Down Expand Up @@ -392,8 +394,22 @@ void ChatAreaWidget::updateAlias()

void ChatAreaWidget::chatPrivate(QString text)
{
if(!text.isEmpty())
emit chat(text,getTo());
if(!text.isEmpty()) {
if (auto chatCommand = ChatCommand::parse(text)) {
if(chatCommand->type() == ChatCommandType::DICEROLL) {
DiceRoll roll(chatCommand->argumentString());
if(roll.valid()) {
emit chat(roll.roll(), getTo());
} else {
printStatusMessage("Invalid roll format invalid.");
}
} else {
printStatusMessage("Unknown command: " + chatCommand->commandString());
}
} else {
emit chat(text, getTo());
}
}
}

void ChatAreaWidget::macroKey(int index)
Expand Down
4 changes: 2 additions & 2 deletions version.pri
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
win32:VERSION = 0.5.4.0 # major.minor.patch.build
else:VERSION = 0.5.4 # major.minor.patch
win32:VERSION = 0.5.5.0 # major.minor.patch.build
else:VERSION = 0.5.5 # major.minor.patch
DEFINES += APP_VERSION="\\\"$$VERSION\\\""
DEFINES += PROTOCOL_VERSION="\\\"0.2.0\\\""

0 comments on commit 60450f1

Please sign in to comment.