Skip to content

Commit

Permalink
add a MVC pattern and upgrade database
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Feb 9, 2024
1 parent f90a7dd commit f5b75bb
Show file tree
Hide file tree
Showing 32 changed files with 4,459 additions and 1,108 deletions.
21 changes: 12 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ add_subdirectory(libs/quazip)

set(SOURCES
src/main.cpp
src/network.cpp
src/network.h
src/worker.h
src/controller.cpp
src/controller.h
src/fileop.h
src/fileop.cpp
src/TombRaiderLinuxLauncher.cpp
src/Network.h
src/Network.cpp
src/Controller.h
src/Controller.cpp
src/FileManager.h
src/FileManager.cpp
src/Model.h
src/Model.cpp
src/Data.h
src/Data.cpp
src/TombRaiderLinuxLauncher.h
src/TombRaiderLinuxLauncher.cpp
src/TombRaiderLinuxLauncher.ui
src/resources.qrc
)

add_executable(${PROJECT_NAME} ${SOURCES})
add_executable(${PROJECT_NAME} ${SOURCES} )

target_link_libraries(${PROJECT_NAME}
Qt5::Core
Expand Down
Binary file modified doc/TombRaiderLinuxLauncher.pdf
Binary file not shown.
100 changes: 100 additions & 0 deletions src/Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "Controller.h"
#include <QDebug>

Controller::Controller(QObject *parent) : QObject(parent), workerRunning(false)
{
workerThread = new QThread();

// Move the Controller to a separate thread
this->moveToThread(workerThread);

// Connect signals and slots for thread management
connect(workerThread, &QThread::started, this, &Controller::doWork);
connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
connect(this, &Controller::workFinished, this, &Controller::onWorkerThreadFinished);
}

Controller::~Controller()
{
// Cleanup: Ensure the worker thread is stopped and cleaned up
stopWorkerThread();
}

void Controller::startWorkerThread()
{
if (!workerRunning)
{
workerThread->start();
workerRunning = true;
}
}

void Controller::stopWorkerThread()
{
if (workerRunning)
{
workerThread->quit();
workerThread->wait();
workerRunning = false;
}
}

int Controller::checkGameDirectory(int id)
{
return model.checkGameDirectory(id);
}

bool Controller::setupCamp(const QString& level, const QString& game)
{
return model.setDirectory(level,game);
}

bool Controller::setupOg(int id)
{
return model.setUpOg(id);
}

void Controller::getList(QVector<ListItemData>& list)
{
return model.getList(list);
}

const InfoData Controller::getInfo(int id)
{
return model.getInfo(id);
}

const QString Controller::getWalkthrough(int id)
{
return model.getWalkthrough(id);
}

bool Controller::link(int id)
{
return model.setLink(id);
}

bool Controller::link(const QString& levelDir)
{
return false;
}

int Controller::getItemState(int id)
{
return model.getItemState(id);
}

void Controller::doWork()
{
// Your worker functionality goes here
qDebug() << "Worker is doing some work...";

// Emit a signal to indicate the work is finished
emit workFinished();
}

void Controller::onWorkerThreadFinished()
{
qDebug() << "Worker thread finished.";
// Handle worker thread finished event
}
50 changes: 50 additions & 0 deletions src/Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QObject>
#include <QThread>
#include "Model.h"

/**
* The controller activate UI thread work or light instant work on the model
*
*/
class Controller : public QObject
{
Q_OBJECT
public:
static Controller& getInstance()
{
static Controller instance;
return instance;
}
void startWorkerThread();
void stopWorkerThread();
bool setupCamp(const QString& level, const QString& game);
bool setupOg(int id);
void getList(QVector<ListItemData>& list);
bool link(int id);
bool link(const QString& levelDir);
const InfoData getInfo(int id);
const QString getWalkthrough(int id);
int getItemState(int id);
int checkGameDirectory(int id);

signals:
void workFinished(); // Declare the signal

private slots:
void doWork(); // Worker functionality
void onWorkerThreadFinished();

private:
QThread* workerThread;
bool workerRunning;

Model& model = Model::getInstance();

Controller(QObject *parent = nullptr);
~Controller();
Q_DISABLE_COPY(Controller)
};

#endif // CONTROLLER_H
182 changes: 182 additions & 0 deletions src/Data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#include "Data.h"

std::array<QVector<QString>, 2> Data::getFileList(const int id, bool trleList)
{
std::array<QVector<QString>, 2> list;
QSqlQuery query;
if (trleList)
{
query.prepare("SELECT Files.path, Files.md5sum "
"FROM Files "
"JOIN LevelFileList ON Files.FileID = LevelFileList.fileID "
"JOIN Level ON LevelFileList.levelID = Level.LevelID "
"WHERE Level.LevelID = :id");
}
else
{
query.prepare("SELECT Files.path, Files.md5sum "
"FROM Files "
"JOIN GameFileList ON Files.FileID = GameFileList.fileID "
"JOIN Game ON GameFileList.gameID = Game.GameID "
"WHERE Game.GameID = :id");
}
query.bindValue(":id", id);
if (query.exec()) {
while (query.next()) {
list[0] << query.value("Files.path").toString();
list[1] << query.value("Files.md5sum").toString();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return list;
}


QVector<ListItemData> Data::getListItems()
{
QVector<ListItemData> items;
QSqlQuery index;
int rowCount = 0;
index.prepare("SELECT COUNT(*) FROM Level");

if (!index.exec()) {
// Handle the error if needed
qDebug() << "Error executing query:" << index.lastError().text();
return items;
}

// Move to the first (and only) result row
if (index.next()) {
// Retrieve the count value (assuming it's in the first column, index 0)
rowCount = index.value(0).toInt();

// Now, 'rowCount' contains the count of rows in the 'Level' table
qDebug() << "Number of rows in 'Level' table:" << rowCount;
} else {
// Handle the case where no rows are returned
qDebug() << "No rows returned from the query";
}
for(int i=0; i<rowCount;i++)
{
QSqlQuery query;
query.prepare("SELECT Level.*, Info.*, Picture.* "
"FROM Level "
"JOIN Info ON Level.infoID = Info.InfoID "
"JOIN Screens ON Level.LevelID = Screens.levelID "
"JOIN Picture ON Screens.pictureID = Picture.PictureID "
"WHERE Level.LevelID = :id "
"GROUP BY Level.LevelID "
"ORDER BY MIN(Picture.PictureID) ASC");
query.bindValue(":id", i+1); // Set the ID autoincrament starts at 1

// Execute the query
if (query.exec()) {
// Iterate over the result set
while (query.next()) {
items.append(ListItemData(
query.value("Info.title").toString(),
query.value("Info.author").toString(),
query.value("Info.release").toString(),
query.value("Info.difficulty").toString(),
query.value("Info.duration").toString(),
query.value("Info.type").toString(),
query.value("Info.class").toString(),
query.value("Picture.data").toByteArray()));
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
}
return items;
}

InfoData Data::getInfo(const int id)
{
QVector<QByteArray> imageList;
QSqlQuery query;
query.prepare("SELECT Level.body, Picture.data "
"FROM Level "
"JOIN Screens ON Level.LevelID = Screens.levelID "
"JOIN Picture ON Screens.pictureID = Picture.PictureID "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);

if (query.exec())
{
if (query.next())
{
QString body = query.value("body").toString();
while (query.next())
{
imageList.push_back(query.value("Picture.data").toByteArray());
}
return InfoData(body, imageList);
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return InfoData();
}

QString Data::getWalkthrough(const int id)
{
QSqlQuery query;
query.prepare("SELECT Level.walkthrough "
"FROM Level "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);
if (query.exec())
{
if (query.next()) {
QString body = query.value("Level.walkthrough").toString();
return body;
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return "";
}

ZipData Data::getDownload(const int id)
{
QSqlQuery query;
query.prepare("SELECT Zip.name, Zip.size, Zip.md5sum, Zip.url "
"FROM Level "
"JOIN Zip ON Level.zipID = Zip.ZipID "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);

if (query.exec())
{
if (query.next()) {
return ZipData(
query.value("Zip.name").toString(),
query.value("Zip.size").toFloat(),
query.value("Zip.md5sum").toString(),
query.value("Zip.url").toString());
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return ZipData();
}

int Data::getType(const int id)
{
QSqlQuery query;
query.prepare("SELECT Info.type "
"FROM Level "
"JOIN Info ON Level.infoID = Info.InfoID "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);
if (query.exec())
{
if (query.next()) {
return query.value("Info.type").toInt();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return 0;
}
Loading

0 comments on commit f5b75bb

Please sign in to comment.