Fix clazy-writing-to-temporary issues

TODO still:
- Evidence
- Judgelog
This commit is contained in:
Cerapter 2021-05-02 18:32:39 +02:00
parent 68b4174a40
commit a42281c2c7
10 changed files with 112 additions and 196 deletions

View File

@ -237,13 +237,6 @@ class AreaData : public QObject {
void spectatable();
/**
* @brief invite
* @param f_clientId
* @return True if the client was successfully invited. False if they were already in the list of invited people.
*/
bool invite(int f_clientId);
int playerCount() const;
QList<QTimer *> timers() const;
@ -254,14 +247,27 @@ class AreaData : public QObject {
QList<int> charactersTaken() const;
bool changeCharacter(int f_from = -1, int f_to = -1);
QList<Evidence> evidence() const;
void swapEvidence(int f_eviId1, int f_eviId2);
Status status() const;
bool changeStatus(const QString& f_newStatus_r);
QList<int> invited() const;
/**
* @brief invite
* @param f_clientId
* @return True if the client was successfully invited. False if they were already in the list of invited people.
*/
bool invite(int f_clientId);
bool uninvite(int f_clientId);
QString background() const;
bool shownameAllowed() const;
@ -288,7 +294,9 @@ class AreaData : public QObject {
EvidenceMod eviMod() const;
QMap<QString, QString> notecards() const;
bool addNotecard(const QString& f_owner_r, const QString& f_notecard_r);
QStringList getNotecards();
TestimonyRecording testimonyRecording() const;
@ -298,7 +306,7 @@ class AreaData : public QObject {
void clearTestimony();
QVector<QStringList> testimony() const;
const QVector<QStringList>& testimony() const;
int statement() const;
@ -306,6 +314,8 @@ class AreaData : public QObject {
void addStatement(int f_position, const QStringList& f_newStatement);
void replaceStatement(int f_position, const QStringList& f_newStatement);
void removeStatement(int f_statementNumber);
std::pair<QStringList, TestimonyProgress> advanceTestimony(bool f_forward = true);

View File

@ -159,22 +159,14 @@ bool AOClient::changeCharacter(int char_id)
return false;
}
if (current_char != "") {
area->charactersTaken().removeAll(server->getCharID(current_char));
}
bool l_successfulChange = area->changeCharacter(server->getCharID(current_char), char_id);
if (char_id >= 0) {
current_char = "";
if (l_successfulChange) {
QString char_selected = server->characters[char_id];
bool taken = area->charactersTaken().contains(char_id);
if (taken || char_selected == "")
return false;
area->charactersTaken().append(char_id);
current_char = char_selected;
}
else {
current_char = "";
}
pos = "";

View File

@ -172,6 +172,16 @@ bool AreaData::invite(int f_clientId)
return true;
}
bool AreaData::uninvite(int f_clientId)
{
if (m_invited.contains(f_clientId)) {
return false;
}
m_invited.removeAll(f_clientId);
return true;
}
int AreaData::playerCount() const
{
return m_playerCount;
@ -197,11 +207,39 @@ QList<int> AreaData::charactersTaken() const
return m_charactersTaken;
}
bool AreaData::changeCharacter(int f_from, int f_to)
{
if (f_from != -1) {
m_charactersTaken.removeAll(f_from);
}
if (m_charactersTaken.contains(f_to)) {
return false;
}
if (f_to != -1) {
m_charactersTaken.append(f_to);
return true;
}
return false;
}
QList<AreaData::Evidence> AreaData::evidence() const
{
return m_evidence;
}
void AreaData::swapEvidence(int f_eviId1, int f_eviId2)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
//swapItemsAt does not exist in Qt older than 5.13
m_evidence.swap(f_eviId1, f_eviId2);
#else
m_evidence.swapItemsAt(f_eviId1, f_eviId2);
#endif
}
AreaData::Status AreaData::status() const
{
return m_status;
@ -324,6 +362,11 @@ void AreaData::addStatement(int f_position, const QStringList &f_newStatement)
m_testimony.insert(f_position, f_newStatement);
}
void AreaData::replaceStatement(int f_position, const QStringList &f_newStatement)
{
m_testimony.replace(f_position, f_newStatement);
}
void AreaData::removeStatement(int f_statementNumber)
{
m_testimony.remove(f_statementNumber);
@ -351,7 +394,7 @@ QStringList AreaData::jumpToStatement(int f_statementNr)
return m_testimony.at(m_statement);
}
QVector<QStringList> AreaData::testimony() const
const QVector<QStringList>& AreaData::testimony() const
{
return m_testimony;
}
@ -361,16 +404,38 @@ AreaData::TestimonyRecording AreaData::testimonyRecording() const
return m_testimonyRecording;
}
QMap<QString, QString> AreaData::notecards() const
{
return m_notecards;
}
AreaData::EvidenceMod AreaData::eviMod() const
{
return m_eviMod;
}
bool AreaData::addNotecard(const QString &f_owner_r, const QString &f_notecard_r)
{
m_notecards[f_owner_r] = f_notecard_r;
if (f_notecard_r.isNull()) {
m_notecards.remove(f_owner_r);
return false;
}
return true;
}
QStringList AreaData::getNotecards()
{
QMapIterator<QString, QString> l_noteIter(m_notecards);
QStringList l_notecards;
while (l_noteIter.hasNext()) {
l_noteIter.next();
l_notecards << l_noteIter.key() << ": " << l_noteIter.value() << "\n";
}
m_notecards.clear();
return l_notecards;
}
QString AreaData::musicPlayerBy() const
{
return m_musicPlayerBy;

View File

@ -133,11 +133,10 @@ void AOClient::cmdUnInvite(int argc, QStringList argv)
sendServerMessage("You cannot uninvite a CM!");
return;
}
else if (!area->invited().contains(uninvited_id)) {
else if (!area->uninvite(uninvited_id)) {
sendServerMessage("That ID is not on the invite list.");
return;
}
area->invited().removeAll(uninvited_id);
sendServerMessage("You uninvited ID " + argv[0]);
}
@ -152,7 +151,7 @@ void AOClient::cmdLock(int argc, QStringList argv)
area->lock();
for (AOClient* client : server->clients) {
if (client->current_area == current_area && client->joined) {
area->invited().append(client->id);
area->invite(client->id);
}
}
arup(ARUPType::LOCKED, true);
@ -169,7 +168,7 @@ void AOClient::cmdSpectatable(int argc, QStringList argv)
area->spectatable();
for (AOClient* client : server->clients) {
if (client->current_area == current_area && client->joined) {
area->invited().append(client->id);
area->invite(client->id);
}
}
arup(ARUPType::LOCKED, true);

View File

@ -85,12 +85,7 @@ void AOClient::cmdEvidence_Swap(int argc, QStringList argv)
return;
}
if ((ev_id2 <= ev_size) && (ev_id1 <= ev_size)) {
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
//swapItemsAt does not exist in Qt older than 5.13
area->evidence.swap(ev_id1, ev_id2);
#else
area->evidence().swapItemsAt(ev_id1, ev_id2);
#endif
area->swapEvidence(ev_id1, ev_id2);
sendEvidenceList(area);
sendServerMessage("The evidence " + QString::number(ev_id1) + " and " + QString::number(ev_id2) + " have been swapped.");
}
@ -256,7 +251,7 @@ void AOClient::cmdLoadTestimony(int argc, QStringList argv)
if (testimony_lines <= server->maximum_statements) {
QString line = in.readLine();
QStringList packet = line.split("#");
area->testimony().append(packet);
area->addStatement(area->testimony().size(), packet);
testimony_lines = testimony_lines + 1;
}
else {

View File

@ -103,7 +103,7 @@ QString AOClient::getAreaTimer(int area_idx, int timer_idx)
if (timer_idx == 0)
timer = server->timer;
else if (timer_idx > 0 && timer_idx <= 4)
timer = area->timers()[timer_idx - 1];
timer = area->timers().at(timer_idx - 1);
else
return "Invalid timer ID.";

View File

@ -83,7 +83,7 @@ void AOClient::cmdTimer(int argc, QStringList argv)
requested_timer = server->timer;
}
else
requested_timer = area->timers()[timer_id - 1];
requested_timer = area->timers().at(timer_id - 1);
AOPacket show_timer("TI", {QString::number(timer_id), "2"});
AOPacket hide_timer("TI", {QString::number(timer_id), "3"});
@ -130,38 +130,33 @@ void AOClient::cmdTimer(int argc, QStringList argv)
void AOClient::cmdNoteCard(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
if (area->notecards().keys().contains(current_char))
area->notecards().remove(current_char);
QString notecard = argv.join(" ");
area->notecards()[current_char] = notecard;
area->addNotecard(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->notecards().keys().contains(current_char)) {
area->notecards().remove(current_char);
if (!area->addNotecard(current_char, QString())) {
sendServerMessageArea(current_char + " erased their note card.");
}
else
sendServerMessage("You do not have a note card.");
}
void AOClient::cmdNoteCardReveal(int argc, QStringList argv)
{
AreaData* area = server->areas[current_area];
if (area->notecards().isEmpty()) {
const QStringList l_notecards = area->getNotecards();
if (l_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->notecards().begin(); i != area->notecards().end(); ++i)
message << i.key() + ": " + i.value();
sendServerMessageArea(message.join("\n"));
area->notecards().clear();
QString message("Note cards have been revealed.\n");
message.append(l_notecards.join("\n") + "\n");
sendServerMessageArea(message);
}
void AOClient::cmd8Ball(int argc, QStringList argv)

View File

@ -58,7 +58,7 @@ QStringList AOClient::updateStatement(QStringList packet)
sendServerMessage("Unable to update an empty statement. Please use /addtestimony.");
else {
packet[14] = "1";
area->testimony().replace(c_statement, packet);
area->replaceStatement(c_statement, packet);
sendServerMessage("Updated current statement.");
return area->testimony()[c_statement];
}

View File

@ -1,67 +0,0 @@
//////////////////////////////////////////////////////////////////////////////////////
// akashi - a server for Attorney Online 2 //
// Copyright (C) 2020 scatterflower //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// 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/>. //
//////////////////////////////////////////////////////////////////////////////////////
#ifndef DISCORD_H
#define DISCORD_H
#include <QtNetwork>
#include <QCoreApplication>
#include "server.h"
class Server;
class Discord : public QObject {
Q_OBJECT
public:
/**
* @brief Creates an instance of the Discord class.
*
* @param p_server A pointer to the Server instance Discord is constructed by.
* @param parent Qt-based parent, passed along to inherited constructor from QObject.
*/
Discord(Server* p_server, QObject* parent = nullptr)
: QObject(parent), server(p_server) {
};
public slots:
/**
* @brief Sends a modcall to a discord webhook.
*
* @param name The character or OOC name of the client who sent the modcall.
* @param area The area name of the area the modcall was sent from.
* @param reason The reason the client specified for the modcall.
* @param current_area The index of the area the modcall is made.
*/
void postModcallWebhook(QString name, QString reason, int current_area);
/**
* @brief Sends the reply to the POST request sent by Discord::postModcallWebhook.
*/
void onFinish(QNetworkReply *reply);
private:
/**
* @brief A pointer to the Server.
*/
Server* server;
};
#endif // DISCORD_H

View File

@ -1,73 +0,0 @@
//////////////////////////////////////////////////////////////////////////////////////
// akashi - a server for Attorney Online 2 //
// Copyright (C) 2020 scatterflower //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// 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 "include/discord.h"
void Discord::postModcallWebhook(QString name, QString reason, int current_area)
{
if (!QUrl (server->webhook_url).isValid()) {
qWarning() << "Invalid webhook url!";
return;
}
QNetworkRequest request(QUrl (server->webhook_url));
QNetworkAccessManager* nam = new QNetworkAccessManager();
connect(nam, &QNetworkAccessManager::finished,
this, &Discord::onFinish);
// This is the kind of garbage Qt makes me write.
// I am so tired. Qt has broken me.
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonObject json;
QJsonArray jsonArray;
QJsonObject jsonObject {
{"color", "13312842"},
{"title", name + " filed a modcall in " + server->areas[current_area]->name},
{"description", reason}
};
jsonArray.append(jsonObject);
json["embeds"] = jsonArray;
nam->post(request, QJsonDocument(json).toJson());
if (server->webhook_sendfile) {
QHttpMultiPart* construct = new QHttpMultiPart();
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + construct->boundary());
//This cost me two days of my life. Thanks Qt and Discord. You have broken me.
QHttpPart file;
file.setRawHeader(QByteArray("Content-Disposition"), QByteArray("form-data; name=\"file\"; filename=\"log.txt\""));
file.setRawHeader(QByteArray("Content-Type"), QByteArray("plain/text"));
QQueue<QString> buffer = server->areas[current_area]->logger->getBuffer(); // I feel no shame for doing this
QString log;
while (!buffer.isEmpty()) {
log.append(buffer.dequeue() + "\n");
}
file.setBody(log.toUtf8());
construct->append(file);
nam->post(request, construct);
}
}
void Discord::onFinish(QNetworkReply *reply)
{
QByteArray data = reply->readAll();
QString str_reply = data;
qDebug() << str_reply;
}