Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/aoapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ AOApplication::AOApplication(QObject *parent)
: QObject(parent)
{
net_manager = new NetworkManager(this);
debug_func = new debug_functions(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug_func is too vague as a variable name.
debug_functions is meaningless as a class name nor does it follow at all any kind of somewhat established naming convention.


discord = new AttorneyOnline::Discord();

asset_lookup_cache.reserve(2048);
Expand Down Expand Up @@ -142,10 +144,11 @@ void AOApplication::server_disconnected()
{
if (w_courtroom->isVisible())
{
call_notice(tr("Disconnected from server."));
construct_lobby();
destruct_courtroom();
// call_notice(tr("Disconnected from server."));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove rather than comment out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

debug_func->call_notice_reconnect(tr("Disconnected from server, reconnect?"));
}
construct_lobby();
destruct_courtroom();
}
Options::getInstance().setServerSubTheme(QString());
}
Expand All @@ -158,14 +161,17 @@ void AOApplication::loading_cancelled()
void AOApplication::call_settings_menu()
{
AOOptionsDialog *l_dialog = new AOOptionsDialog(this);
// force disconnect as a test
server_disconnected();

if (is_courtroom_constructed())
{
connect(l_dialog, &AOOptionsDialog::reloadThemeRequest, w_courtroom, &Courtroom::on_reload_theme_clicked);
}

if (is_lobby_constructed())
{}
l_dialog->exec();
// if (is_lobby_constructed())
// {}
// l_dialog->exec();
Comment on lines +176 to +178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remove these, this snippet is for the execution of the lobby settings when the settings button is pressed.
Currently, pressing it causes a disconnection for the purposes of testing the reconnection button.


if (is_courtroom_constructed())
{
Expand Down
2 changes: 2 additions & 0 deletions src/aoapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NetworkManager;
class Lobby;
class Courtroom;
class Options;
class debug_functions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs removed


class VPath : QString
{
Expand Down Expand Up @@ -57,6 +58,7 @@ class AOApplication : public QObject
~AOApplication();

NetworkManager *net_manager;
debug_functions *debug_func;
Lobby *w_lobby = nullptr;
Courtroom *w_courtroom = nullptr;
AttorneyOnline::Discord *discord;
Expand Down
53 changes: 53 additions & 0 deletions src/debug_functions.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "debug_functions.h"
#include "aoapplication.h"
#include "networkmanager.h"

#include <QCoreApplication>
#include <QDialogButtonBox>
Expand All @@ -9,6 +11,12 @@

#include <functional>

debug_functions::debug_functions(AOApplication *parent)
: QObject(parent)
{
ao_app = parent;
}

void call_error(QString p_message)
{
QMessageBox *msgBox = new QMessageBox;
Expand Down Expand Up @@ -58,3 +66,48 @@ void call_notice(QString p_message)

msgBox->exec();
}

void debug_functions::call_notice_reconnect(QString p_message)
{
auto *msgBox = new QMessageBox;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a member variable.


msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setText(p_message);
msgBox->setWindowTitle(QCoreApplication::translate("debug_functions", "Notice"));

msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setDefaultButton(QMessageBox::Ok);
msgBox->defaultButton()->setEnabled(false);

QPushButton *reconnectButton = msgBox->addButton(QObject::tr("Reconnect"), QMessageBox::ActionRole);

QTimer intervalTimer;
intervalTimer.setInterval(1000);

int counter = 3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a member variable.

const auto updateCounter = [msgBox, &counter] {
if (counter <= 0)
{
return;
}
msgBox->defaultButton()->setText(QString("%1 (%2)").arg(QDialogButtonBox::tr("OK")).arg(counter));
counter--;
};

QObject::connect(&intervalTimer, &QTimer::timeout, msgBox, updateCounter);
intervalTimer.start();
updateCounter();

QTimer::singleShot(3000, msgBox, [msgBox, &intervalTimer] {
msgBox->defaultButton()->setEnabled(true);
msgBox->defaultButton()->setText(QDialogButtonBox::tr("OK"));
intervalTimer.stop();
});

msgBox->exec();

if (msgBox->clickedButton() == reconnectButton)
{
ao_app->net_manager->reconnect_to_last_server();
}
}
13 changes: 13 additions & 0 deletions src/debug_functions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#pragma once

#include "aoapplication.h"

#include <QString>

void call_error(QString message);
void call_notice(QString message);

class debug_functions : public QObject
{
Q_OBJECT

public:
explicit debug_functions(AOApplication *parent);
AOApplication *ao_app;

void call_notice_reconnect(QString message);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning behind making this an object over a simple function call? I don't see a reason as to why it would be.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was made into an object because I needed to pass the parent / ao_app into the function of call_notice_reconnect. Couldn't find a way of doing that without extending the QObject from a class.

Do you know an example implementation for doing it without having to do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply add a bool prompt_choice(QString message) which either allow you to accept or cancel. That way you keep it within a singular function and can use it elsewhere within the code. Additionally, the return value will make it easy on whatever reconnecting is wanted by the user or not.

6 changes: 6 additions & 0 deletions src/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void NetworkManager::request_document(MSDocumentType document_type, const std::f

void NetworkManager::connect_to_server(ServerInfo server)
{
m_last_server = server;
disconnect_from_server();

qInfo().noquote() << QObject::tr("Connecting to %1").arg(server.toString());
Expand All @@ -167,6 +168,11 @@ void NetworkManager::disconnect_from_server()
}
}

void NetworkManager::reconnect_to_last_server()
{
connect_to_server(m_last_server);
}

void NetworkManager::ship_server_packet(AOPacket packet)
{
if (!m_connection)
Expand Down
3 changes: 3 additions & 0 deletions src/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class NetworkManager : public QObject

void connect_to_server(ServerInfo p_server);
void disconnect_from_server();
void reconnect_to_last_server();

QString get_user_agent() const;

Expand Down Expand Up @@ -60,4 +61,6 @@ private Q_SLOTS:
const int heartbeat_interval = 60 * 5 * 1000;

unsigned int s_decryptor = 5;

ServerInfo m_last_server;
};
Loading