From 38d801c6048ae1a70918b60a55c49a36ae1b05bc Mon Sep 17 00:00:00 2001 From: Isabell Pflug Date: Mon, 29 May 2023 17:58:03 +0200 Subject: [PATCH] :bug: Fixed highscore functionality Had to fix a couple of reads and writes, as well as accessing the data in the highscore QMap --- ThinkPink/mainwindow.cpp | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/ThinkPink/mainwindow.cpp b/ThinkPink/mainwindow.cpp index 68b461b..f29db76 100644 --- a/ThinkPink/mainwindow.cpp +++ b/ThinkPink/mainwindow.cpp @@ -96,6 +96,8 @@ void MainWindow::bubbleBoxes(std::set> *connected) { QMap, int> MainWindow::newEntry() { QDialog entryDial; + entryDial.setPalette(MainWindow::palette()); + QLabel *label1 = new QLabel("New highscore!"); QLabel *label2 = new QLabel("Enter your name:"); QLabel *label3 = new QLabel("Score:"); @@ -106,12 +108,13 @@ QMap, int> MainWindow::newEntry() { lineEdit->setText(name); QString confirmed_name; QPushButton *button = new QPushButton("Ok"); - QLCDNumber *lcdNumber = new QLCDNumber; + QLCDNumber *lcdNumber = new QLCDNumber(); lcdNumber->display(ui->lcdScore->intValue()); // Verbinden des Button-Klicks mit einem Slot entryDial.connect(button, &QPushButton::clicked, &entryDial, [&]() { confirmed_name = lineEdit->text(); + return; }); // Erstellen des Layouts @@ -127,6 +130,7 @@ QMap, int> MainWindow::newEntry() { sub_layout2->addWidget(lcdNumber); layout->addWidget(button, Qt::AlignRight); entryDial.setLayout(layout); + entryDial.exec(); QMap, int> new_entry; @@ -138,18 +142,20 @@ QMap, int> MainWindow::newEntry() { } void MainWindow::processHighscore() { - QList scores = highscore.values(); - std::sort(scores.begin(), scores.end()); - if (ui->lcdScore->intValue() <= *(scores.end())) - return; - QMap, int> entry = newEntry(); - auto smallest = highscore.keyValueBegin(); - for (auto map_entry = highscore.keyValueBegin(); map_entry != highscore.keyValueEnd(); map_entry++) { - if (map_entry->second < smallest->second) - smallest = map_entry; + if(!highscore.empty() && highscore.size() == 10) { + QList scores = highscore.values(); + std::sort(scores.begin(), scores.end()); + if (ui->lcdScore->intValue() <= *(scores.end())) + return; + + auto smallest = highscore.keyValueBegin(); + for (auto map_entry = highscore.keyValueBegin(); map_entry != highscore.keyValueEnd(); map_entry++) { + if (map_entry->second < smallest->second) + smallest = map_entry; + } + highscore.remove(smallest->first); } - highscore.remove(smallest->first); highscore.insert(entry); QDir appdata = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); @@ -166,7 +172,7 @@ void MainWindow::processHighscore() { QTextStream hs(&highscoreCSV); for (auto key = highscore.keyValueBegin(); key != highscore.keyValueEnd(); key++){ - hs << key->first.first << ";" << key->first.second.toString(Qt::DateFormat::ISODate) << ";" << key->second; + hs << key->first.first << ";" << key->first.second.toString(Qt::DateFormat::ISODate) << ";" << key->second << "\n"; } highscoreCSV.close(); @@ -206,20 +212,31 @@ void MainWindow::showHighscore_triggered() { } QDialog highscoreDial; + highscoreDial.setPalette(MainWindow::palette()); QVBoxLayout *layout = new QVBoxLayout; - for (auto entry = highscore.keyValueBegin(); entry != highscore.keyValueEnd(); entry++) { + QLabel *title = new QLabel("

Highscores


"); + title->setAlignment(Qt::AlignCenter); + //title->set + layout->addWidget(title); + QList> sorted_keys = highscore.keys(); + std::sort(sorted_keys.begin(), sorted_keys.end()); + int i = 1; + for (auto key = sorted_keys.end()-1; key != sorted_keys.begin()-1; key--) { + auto entry = highscore.find(*key); QHBoxLayout *entry_layout = new QHBoxLayout; - QLabel *entry_name = new QLabel(entry->first.first); - QLCDNumber *entry_score = new QLCDNumber(entry->second); - QLabel *entry_datetime = new QLabel(entry->first.second.toString(Qt::DateFormat::TextDate)); + QLabel *entry_name = new QLabel(QString::fromStdString(std::to_string(i)) + ") " + entry.key().first + ""); + QLCDNumber *entry_score = new QLCDNumber(); + entry_score->display(entry.value()); + QLabel *entry_datetime = new QLabel(entry.key().second.toString(Qt::DateFormat::TextDate)); entry_layout->addWidget(entry_name); entry_layout->addWidget(entry_score); entry_layout->addWidget(entry_datetime); layout->addLayout(entry_layout); + i++; } highscoreDial.setLayout(layout); - highscoreDial.show(); + highscoreDial.exec(); } // slots