🐛 Fixed highscore functionality

Had to fix a couple of reads and writes, as well as accessing the data in the highscore QMap
main
Isabell Pflug 2 years ago
parent af00be318f
commit 38d801c604

@ -96,6 +96,8 @@ void MainWindow::bubbleBoxes(std::set<std::vector<int>> *connected) {
QMap<QPair<QString, QDateTime>, int> MainWindow::newEntry() { QMap<QPair<QString, QDateTime>, int> MainWindow::newEntry() {
QDialog entryDial; QDialog entryDial;
entryDial.setPalette(MainWindow::palette());
QLabel *label1 = new QLabel("New highscore!"); QLabel *label1 = new QLabel("New highscore!");
QLabel *label2 = new QLabel("Enter your name:"); QLabel *label2 = new QLabel("Enter your name:");
QLabel *label3 = new QLabel("Score:"); QLabel *label3 = new QLabel("Score:");
@ -106,12 +108,13 @@ QMap<QPair<QString, QDateTime>, int> MainWindow::newEntry() {
lineEdit->setText(name); lineEdit->setText(name);
QString confirmed_name; QString confirmed_name;
QPushButton *button = new QPushButton("Ok"); QPushButton *button = new QPushButton("Ok");
QLCDNumber *lcdNumber = new QLCDNumber; QLCDNumber *lcdNumber = new QLCDNumber();
lcdNumber->display(ui->lcdScore->intValue()); lcdNumber->display(ui->lcdScore->intValue());
// Verbinden des Button-Klicks mit einem Slot // Verbinden des Button-Klicks mit einem Slot
entryDial.connect(button, &QPushButton::clicked, &entryDial, [&]() { entryDial.connect(button, &QPushButton::clicked, &entryDial, [&]() {
confirmed_name = lineEdit->text(); confirmed_name = lineEdit->text();
return;
}); });
// Erstellen des Layouts // Erstellen des Layouts
@ -127,6 +130,7 @@ QMap<QPair<QString, QDateTime>, int> MainWindow::newEntry() {
sub_layout2->addWidget(lcdNumber); sub_layout2->addWidget(lcdNumber);
layout->addWidget(button, Qt::AlignRight); layout->addWidget(button, Qt::AlignRight);
entryDial.setLayout(layout); entryDial.setLayout(layout);
entryDial.exec(); entryDial.exec();
QMap<QPair<QString, QDateTime>, int> new_entry; QMap<QPair<QString, QDateTime>, int> new_entry;
@ -138,18 +142,20 @@ QMap<QPair<QString, QDateTime>, int> MainWindow::newEntry() {
} }
void MainWindow::processHighscore() { void MainWindow::processHighscore() {
QList<int> scores = highscore.values();
std::sort(scores.begin(), scores.end());
if (ui->lcdScore->intValue() <= *(scores.end()))
return;
QMap<QPair<QString, QDateTime>, int> entry = newEntry(); QMap<QPair<QString, QDateTime>, int> entry = newEntry();
auto smallest = highscore.keyValueBegin(); if(!highscore.empty() && highscore.size() == 10) {
for (auto map_entry = highscore.keyValueBegin(); map_entry != highscore.keyValueEnd(); map_entry++) { QList<int> scores = highscore.values();
if (map_entry->second < smallest->second) std::sort(scores.begin(), scores.end());
smallest = map_entry; 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); highscore.insert(entry);
QDir appdata = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); QDir appdata = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
@ -166,7 +172,7 @@ void MainWindow::processHighscore() {
QTextStream hs(&highscoreCSV); QTextStream hs(&highscoreCSV);
for (auto key = highscore.keyValueBegin(); key != highscore.keyValueEnd(); key++){ 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(); highscoreCSV.close();
@ -206,20 +212,31 @@ void MainWindow::showHighscore_triggered() {
} }
QDialog highscoreDial; QDialog highscoreDial;
highscoreDial.setPalette(MainWindow::palette());
QVBoxLayout *layout = new QVBoxLayout; QVBoxLayout *layout = new QVBoxLayout;
for (auto entry = highscore.keyValueBegin(); entry != highscore.keyValueEnd(); entry++) { QLabel *title = new QLabel("<h1> Highscores </h1> <br>");
title->setAlignment(Qt::AlignCenter);
//title->set
layout->addWidget(title);
QList<QPair<QString, QDateTime>> 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; QHBoxLayout *entry_layout = new QHBoxLayout;
QLabel *entry_name = new QLabel(entry->first.first); QLabel *entry_name = new QLabel(QString::fromStdString(std::to_string(i)) + ") <b>" + entry.key().first + "</b>");
QLCDNumber *entry_score = new QLCDNumber(entry->second); QLCDNumber *entry_score = new QLCDNumber();
QLabel *entry_datetime = new QLabel(entry->first.second.toString(Qt::DateFormat::TextDate)); 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_name);
entry_layout->addWidget(entry_score); entry_layout->addWidget(entry_score);
entry_layout->addWidget(entry_datetime); entry_layout->addWidget(entry_datetime);
layout->addLayout(entry_layout); layout->addLayout(entry_layout);
i++;
} }
highscoreDial.setLayout(layout); highscoreDial.setLayout(layout);
highscoreDial.show(); highscoreDial.exec();
} }
// slots // slots

Loading…
Cancel
Save