More work on AreaData rework

This commit is contained in:
Cerapter 2021-05-01 17:45:59 +02:00
parent 85cc9af73a
commit 08fd5896ba
10 changed files with 172 additions and 74 deletions

View File

@ -1825,11 +1825,6 @@ class AOClient : public QObject {
*/
QStringList updateStatement(QStringList packet);
/**
* @brief Called when area enum is set to PLAYBACK. Sends the IC-Message stored at the current statement.
* @return IC-Message stored in the QVector.
*/
QStringList playTestimony();
///@}
/**

View File

@ -182,6 +182,17 @@ class AreaData : public QObject {
/// Exposes the metadata of the TestimonyRecording enum.
Q_ENUM(TestimonyRecording);
enum class TestimonyProgress {
OK,
LOOPED,
STAYED_AT_FIRST,
};
enum class Side {
DEFENCE,
PROSECUTOR,
};
/**
* @brief A client in the area has left the area.
*
@ -209,6 +220,8 @@ class AreaData : public QObject {
bool blankpostingAllowed() const;
void toggleBlankposting();
bool isProtected() const;
LockStatus lockStatus() const;
@ -222,6 +235,8 @@ class AreaData : public QObject {
int playerCount() const;
void changePlayerCount(bool f_increase);
QList<QTimer *> timers() const;
QString name() const;
@ -244,6 +259,8 @@ class AreaData : public QObject {
bool iniswapAllowed() const;
void toggleIniswap();
bool bgLocked() const;
QString document() const;
@ -252,29 +269,47 @@ class AreaData : public QObject {
int proHP() const;
void changeHP(AreaData::Side f_side, int f_newHP);
QString currentMusic() const;
QString musicPlayerBy() const;
Logger *logger() const;
EvidenceMod eviMod() const;
QMap<QString, QString> notecards() const;
TestimonyRecording testimonyRecording() const;
void setTestimonyRecording(const TestimonyRecording &testimonyRecording);
void clearTestimony();
QVector<QStringList> testimony() const;
int statement() const;
void recordStatement(const QStringList& f_newStatement);
void addStatement(int f_position, const QStringList& f_newStatement);
std::pair<QStringList, TestimonyProgress> advanceTestimony(bool f_forward = true);
QStringList jumpToStatement(int f_statementNr);
QStringList judgelog() const;
QStringList lastICMessage() const;
void updateLastICMessage(const QStringList& f_lastMessage);
bool forceImmediate() const;
bool toggleMusic() const;
void toggleImmediate();
bool isMusicAllowed() const;
void toggleMusic();
private:
/**

View File

@ -236,7 +236,7 @@ void AOClient::arup(ARUPType type, bool broadcast)
break;
}
case ARUPType::LOCKED: {
QString lock_status = QVariant::fromValue(area->locked).toString();
QString lock_status = QVariant::fromValue(area->locked()).toString();
arup_data.append(lock_status);
break;
}

View File

@ -15,6 +15,9 @@
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include "include/area_data.h"
AreaData::AreaData(QString p_name, int p_index) :
@ -114,6 +117,11 @@ bool AreaData::blankpostingAllowed() const
return m_blankpostingAllowed;
}
void AreaData::toggleBlankposting()
{
m_blankpostingAllowed = !m_blankpostingAllowed;
}
bool AreaData::isProtected() const
{
return m_isProtected;
@ -139,6 +147,11 @@ int AreaData::playerCount() const
return m_playerCount;
}
void AreaData::changePlayerCount(bool f_increase)
{
f_increase ? m_playerCount++: m_playerCount--;
}
QList<QTimer *> AreaData::timers() const
{
return m_timers;
@ -179,21 +192,48 @@ AreaData::LockStatus AreaData::locked() const
return m_locked;
}
bool AreaData::toggleMusic() const
bool AreaData::isMusicAllowed() const
{
return m_toggleMusic;
}
void AreaData::toggleMusic()
{
m_toggleMusic = !m_toggleMusic;
}
void AreaData::setTestimonyRecording(const TestimonyRecording &testimonyRecording)
{
m_testimonyRecording = testimonyRecording;
}
void AreaData::clearTestimony()
{
m_testimonyRecording = AreaData::TestimonyRecording::STOPPED;
m_statement = -1;
m_testimony.clear();
}
bool AreaData::forceImmediate() const
{
return m_forceImmediate;
}
void AreaData::toggleImmediate()
{
m_forceImmediate = !m_forceImmediate;
}
QStringList AreaData::lastICMessage() const
{
return m_lastICMessage;
}
void AreaData::updateLastICMessage(const QStringList &f_lastMessage)
{
m_lastICMessage = f_lastMessage;
}
QStringList AreaData::judgelog() const
{
return m_judgelog;
@ -204,6 +244,38 @@ int AreaData::statement() const
return m_statement;
}
void AreaData::recordStatement(const QStringList &f_newStatement)
{
++m_statement;
m_testimony.append(f_newStatement);
}
void AreaData::addStatement(int f_position, const QStringList &f_newStatement)
{
m_testimony.insert(f_position, f_newStatement);
}
std::pair<QStringList, AreaData::TestimonyProgress> AreaData::advanceTestimony(bool f_forward)
{
f_forward ? m_statement++: m_statement--;
if (m_statement > m_testimony.size() - 1) {
return {m_testimony.at(m_statement), TestimonyProgress::LOOPED};
}
if (m_statement <= 0) {
return {m_testimony.at(m_statement), TestimonyProgress::STAYED_AT_FIRST};
}
else {
return {m_testimony.at(m_statement), TestimonyProgress::OK};
}
}
QStringList AreaData::jumpToStatement(int f_statementNr)
{
m_statement = f_statementNr;
return m_testimony.at(m_statement);
}
QVector<QStringList> AreaData::testimony() const
{
return m_testimony;
@ -224,11 +296,6 @@ AreaData::EvidenceMod AreaData::eviMod() const
return m_eviMod;
}
Logger *AreaData::logger() const
{
return m_logger;
}
QString AreaData::musicPlayerBy() const
{
return m_musicPlayerBy;
@ -244,6 +311,15 @@ int AreaData::proHP() const
return m_proHP;
}
void AreaData::changeHP(AreaData::Side f_side, int f_newHP)
{
if (f_side == Side::DEFENCE) {
m_defHP = std::min(std::max(0, f_newHP), 10);
} else if(f_side == Side::PROSECUTOR) {
m_proHP = std::min(std::max(0, f_newHP), 10);
}
}
int AreaData::defHP() const
{
return m_defHP;
@ -264,6 +340,11 @@ bool AreaData::iniswapAllowed() const
return m_iniswapAllowed;
}
void AreaData::toggleIniswap()
{
m_iniswapAllowed = !m_iniswapAllowed;
}
bool AreaData::shownameAllowed() const
{
return m_shownameAllowed;

View File

@ -333,7 +333,7 @@ void AOClient::cmdAllowBlankposting(int argc, QStringList argv)
{
QString sender_name = ooc_name;
AreaData* area = server->areas[current_area];
area->blankpostingAllowed() = !area->blankpostingAllowed();
area->toggleBlankposting();
if (area->blankpostingAllowed() == false) {
sendServerMessageArea(sender_name + " has set blankposting in the area to forbidden.");
}
@ -391,7 +391,7 @@ void AOClient::cmdReload(int argc, QStringList argv)
void AOClient::cmdForceImmediate(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
area->forceImmediate() = !area->forceImmediate();
area->toggleImmediate();
QString state = area->forceImmediate() ? "on." : "off.";
sendServerMessage("Forced immediate text processing in this area is now " + state);
}
@ -399,7 +399,7 @@ void AOClient::cmdForceImmediate(int argc, QStringList argv)
void AOClient::cmdAllowIniswap(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
area->iniswapAllowed() = !area->iniswapAllowed();
area->toggleIniswap();
QString state = area->iniswapAllowed() ? "allowed." : "disallowed.";
sendServerMessage("Iniswapping in this area is now " + state);
}

View File

@ -86,7 +86,7 @@ void AOClient::cmdUnBlockDj(int argc, QStringList argv)
void AOClient::cmdToggleMusic(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
area->toggleMusic() = !area->toggleMusic();
QString state = area->toggleMusic() ? "allowed." : "disallowed.";
area->toggleMusic();
QString state = area->isMusicAllowed() ? "allowed." : "disallowed.";
sendServerMessage("Music in this area is now " + state);
}

View File

@ -83,7 +83,7 @@ void AOClient::cmdTimer(int argc, QStringList argv)
requested_timer = server->timer;
}
else
requested_timer = area->timers()s[timer_id - 1];
requested_timer = area->timers()[timer_id - 1];
AOPacket show_timer("TI", {QString::number(timer_id), "2"});
AOPacket hide_timer("TI", {QString::number(timer_id), "3"});
@ -130,18 +130,18 @@ void AOClient::cmdTimer(int argc, QStringList argv)
void AOClient::cmdNoteCard(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
if (area->m_notecards.keys().contains(current_char))
area->m_notecards.remove(current_char);
if (area->notecards().keys().contains(current_char))
area->notecards().remove(current_char);
QString notecard = argv.join(" ");
area->m_notecards[current_char] = notecard;
area->notecards()[current_char] = notecard;
sendServerMessageArea(current_char + " wrote a note card.");
}
void AOClient::cmdNoteCardClear(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
if (area->m_notecards.keys().contains(current_char)) {
area->m_notecards.remove(current_char);
if (area->notecards().keys().contains(current_char)) {
area->notecards().remove(current_char);
sendServerMessageArea(current_char + " erased their note card.");
}
else
@ -151,17 +151,17 @@ void AOClient::cmdNoteCardClear(int argc, QStringList argv)
void AOClient::cmdNoteCardReveal(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
if (area->m_notecards.isEmpty()) {
if (area->notecards().isEmpty()) {
sendServerMessage("There are no cards to reveal in this area.");
return;
}
QStringList message;
message << "Note cards have been revealed.";
QMap<QString, QString>::iterator i;
for (i = area->m_notecards.begin(); i != area->m_notecards.end(); ++i)
for (i = area->notecards().begin(); i != area->notecards().end(); ++i)
message << i.key() + ": " + i.value();
sendServerMessageArea(message.join("\n"));
area->m_notecards.clear();
area->notecards().clear();
}
void AOClient::cmd8Ball(int argc, QStringList argv)

View File

@ -96,7 +96,7 @@ void AOClient::pktLoadingDone(AreaData* area, int argc, QStringList argv, AOPack
}
server->player_count++;
area->playerCount()++;
area->changePlayerCount(true);
joined = true;
server->updateCharsTaken(area);
@ -166,8 +166,7 @@ void AOClient::pktIcChat(AreaData* area, int argc, QStringList argv, AOPacket pa
area->logger()->logIC(this, &validated_packet);
server->broadcast(validated_packet, current_area);
area->lastICMessage().clear();
area->lastICMessage().append(validated_packet.contents);
area->updateLastICMessage(validated_packet.contents);
}
void AOClient::pktOocChat(AreaData* area, int argc, QStringList argv, AOPacket packet)
@ -230,7 +229,7 @@ void AOClient::pktChangeMusic(AreaData* area, int argc, QStringList argv, AOPack
sendServerMessage("You are blocked from changing the music.");
return;
}
if (!area->toggleMusic() && !checkAuth(ACLFlags.value("CM"))) {
if (!area->isMusicAllowed() && !checkAuth(ACLFlags.value("CM"))) {
sendServerMessage("Music is disabled in this area.");
return;
}
@ -280,14 +279,18 @@ void AOClient::pktHpBar(AreaData* area, int argc, QStringList argv, AOPacket pac
sendServerMessage("You are blocked from using the judge controls.");
return;
}
int l_newValue = argv.at(1).toInt();
if (argv[0] == "1") {
area->defHP() = std::min(std::max(0, argv[1].toInt()), 10);
area->changeHP(AreaData::Side::DEFENCE, l_newValue);
}
else if (argv[0] == "2") {
area->proHP() = std::min(std::max(0, argv[1].toInt()), 10);
area->changeHP(AreaData::Side::PROSECUTOR, l_newValue);
}
server->broadcast(AOPacket("HP", {"1", QString::number(area->defHP())}), area->index());
server->broadcast(AOPacket("HP", {"2", QString::number(area->proHP())}), area->index());
updateJudgeLog(area, this, "updated the penalties");
}
@ -614,7 +617,7 @@ AOPacket AOClient::validateIcPacket(AOPacket packet)
if (incoming_args.length() > 15) {
// showname
QString incoming_showname = dezalgo(incoming_args[15].toString().trimmed());
if (!(incoming_showname == current_char || incoming_showname.isEmpty()) && !area->m_shownameAllowed) {
if (!(incoming_showname == current_char || incoming_showname.isEmpty()) && !area->shownameAllowed()) {
sendServerMessage("Shownames are not allowed in this area!");
return invalid;
}
@ -745,23 +748,31 @@ AOPacket AOClient::validateIcPacket(AOPacket packet)
args = updateStatement(args);
}
else if (area->testimonyRecording() == AreaData::TestimonyRecording::PLAYBACK) {
AreaData::TestimonyProgress l_progress;
if (args[4] == ">") {
pos = "wit";
area->statement() = area->statement() + 1;
args = playTestimony();
std::make_pair(args, l_progress) = area->advanceTestimony();
if (l_progress == AreaData::TestimonyProgress::LOOPED) {
sendServerMessageArea("Last statement reached. Looping to first statement.");
}
}
if (args[4] == "<") {
pos = "wit";
area->statement() = area->statement() - 1;
args = playTestimony();
std::make_pair(args, l_progress) = area->advanceTestimony(false);
if (l_progress == AreaData::TestimonyProgress::LOOPED) {
sendServerMessage("First statement reached.");
}
}
QString decoded_message = decodeMessage(args[4]); //Get rid of that pesky encoding first.
QRegularExpression jump("(?<arrow>>)(?<int>[0,1,2,3,4,5,6,7,8,9]+)");
QRegularExpressionMatch match = jump.match(decoded_message);
if (match.hasMatch()) {
pos = "wit";
area->statement() = match.captured("int").toInt();
args= playTestimony();
args = area->jumpToStatement(match.captured("int").toInt());
}
}

View File

@ -26,7 +26,7 @@ Server::Server(int p_port, int p_ws_port, QObject* parent) :
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(clientConnected()));
timer = new Qtimers();
timer = new QTimer();
db_manager = new DBManager();
}

View File

@ -25,13 +25,12 @@ void AOClient::addStatement(QStringList packet)
int c_statement = area->statement();
if (c_statement >= -1) {
if (area->testimonyRecording() == AreaData::TestimonyRecording::RECORDING) {
if (c_statement <= server->maximum_statements()s) {
if (c_statement <= server->maximum_statements) {
if (c_statement == -1)
packet[14] = "3";
else
packet[14] = "1";
area->statement() = c_statement + 1;
area->testimony().append(packet);
area->recordStatement(packet);
return;
}
else {
@ -40,12 +39,12 @@ void AOClient::addStatement(QStringList packet)
}
else if (area->testimonyRecording() == AreaData::TestimonyRecording::ADD) {
packet[14] = "1";
area->testimony().insert(c_statement,packet);
area->testimonyRecording() = AreaData::TestimonyRecording::PLAYBACK;
area->addStatement(c_statement, packet);
area->setTestimonyRecording(AreaData::TestimonyRecording::PLAYBACK);
}
else {
sendServerMessage("Unable to add more statements. The maximum amount of statements has been reached.");
area->testimonyRecording() = AreaData::TestimonyRecording::PLAYBACK;
area->setTestimonyRecording(AreaData::TestimonyRecording::PLAYBACK);
}
}
}
@ -54,7 +53,7 @@ QStringList AOClient::updateStatement(QStringList packet)
{
AreaData* area = server->areas[current_area];
int c_statement = area->statement();
area->testimonyRecording() = AreaData::TestimonyRecording::PLAYBACK;
area->setTestimonyRecording(AreaData::TestimonyRecording::PLAYBACK);
if (c_statement <= 0 || area->testimony()[c_statement].empty())
sendServerMessage("Unable to update an empty statement. Please use /addtestimony.");
else {
@ -69,28 +68,5 @@ QStringList AOClient::updateStatement(QStringList packet)
void AOClient::clearTestimony()
{
AreaData* area = server->areas[current_area];
area->testimonyRecording() = AreaData::TestimonyRecording::STOPPED;
area->statement() = -1;
area->testimony().clear(); //!< Empty out the QVector
area->testimony().squeeze(); //!< Release memory. Good idea? God knows, I do not.
area->clearTestimony();
}
QStringList AOClient::playTestimony()
{
AreaData* area = server->areas[current_area];
int c_statement = area->statement();
if (c_statement > area->testimony().size() - 1) {
sendServerMessageArea("Last statement reached. Looping to first statement.");
area->statement() = 1;
return area->testimony()[area->statement()];
}
if (c_statement <= 0) {
sendServerMessage("First statement reached.");
area->statement() = 1;
return area->testimony()[area->statement() = 1];
}
else {
return area->testimony()[c_statement];
}
}