🐛 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() {
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<QPair<QString, QDateTime>, 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<QPair<QString, QDateTime>, int> MainWindow::newEntry() {
sub_layout2->addWidget(lcdNumber);
layout->addWidget(button, Qt::AlignRight);
entryDial.setLayout(layout);
entryDial.exec();
QMap<QPair<QString, QDateTime>, int> new_entry;
@ -138,18 +142,20 @@ QMap<QPair<QString, QDateTime>, int> MainWindow::newEntry() {
}
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();
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<int> 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("<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;
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)) + ") <b>" + entry.key().first + "</b>");
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

Loading…
Cancel
Save