added racelistgenerator + changed title of windows with drivers and car statistics

This commit is contained in:
2018-12-11 14:59:00 +01:00
parent ddb632c4fd
commit 5d31497f1c
7 changed files with 284 additions and 13 deletions

View File

@@ -1,5 +1,8 @@
#include "windowssettings.h"
#include "racelistgenerator.h"
#include "ui_windowssettings.h"
#include <QStringListModel>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <stdio.h>
#include <string>
@@ -29,6 +32,11 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent)
QObject::connect(this->ui->lEMinRundenzeit, SIGNAL(textChanged(QString)),
this, SLOT(repaintMinCurLapTime()));
QObject::connect(this->ui->pBcreateListClose, SIGNAL(clicked()), this,
SLOT(createRaceListAndClose()));
QObject::connect(this->ui->pBCancel, SIGNAL(clicked()), this,
SLOT(closeWindow()));
// update minimal lap time on changeing minimal sector time
// QObject::connect(this->ui->lEMinTimeSec1,
// SIGNAL(textChanged(QString)),
@@ -88,13 +96,62 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent)
statement =
"SELECT id, minimumroundtime FROM rennen order by id DESC limit 1";
res = this->db->getData(statement, 2);
this->rennId = res[0][0].toInt();
this->rennId = res.at(0).at(0).toInt();
this->ui->lEMinRundenzeitAktRennen->setText(res.at(0).at(1));
// setup Rennliste
// setup drivers
this->ui->lVDrivers->setSelectionMode(
QAbstractItemView::ExtendedSelection);
statement = "select name, id from fahrer";
driversList = this->db->getData2(statement, 2);
driversModel = new QStringListModel();
QStringList qDriversList;
std::for_each(
driversList.begin(), driversList.end(),
[&qDriversList](vector<QString> i) { qDriversList << i.at(0); });
driversModel->setStringList(qDriversList);
this->ui->lVDrivers->setModel(driversModel);
// setup cars
this->ui->lVCars->setSelectionMode(
QAbstractItemView::ExtendedSelection);
QStringList carNames;
std::stringstream ss;
try {
statement = "select id from autos";
this->carIds = this->db->getData2(statement, 1);
for (vector<QString> carId : carIds) {
ss << "select AutoKonfiguration.name from AutoKonfiguration "
"where id_auto like "
<< carId.at(0).toStdString()
<< " order by seit DESC limit 1";
carNames << this->db->getData2(ss.str(), 1).at(0).at(0);
ss.str(std::string());
}
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "cars could not be read from database" << endl;
}
carModel = new QStringListModel();
carModel->setStringList(carNames);
this->ui->lVCars->setModel(carModel);
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "missing database :(" << endl;
}
}
void WindowsSettings::closeWindow() {
this->close();
}
void WindowsSettings::repaintMinLapTime() {
int minlapTime = 0;
minlapTime += this->ui->lEMinTimeSec1->text().toInt();
@@ -179,7 +236,7 @@ WindowsSettings::~WindowsSettings() {
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
string WindowsSettings::currentDateTime() {
time_t now = time(0);
time_t now = time(nullptr);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
@@ -189,3 +246,71 @@ string WindowsSettings::currentDateTime() {
return buf;
}
void WindowsSettings::createRaceListAndClose() {
vector<int> selectedCarIds;
foreach (const QModelIndex & index,
this->ui->lVCars->selectionModel()->selectedIndexes()) {
selectedCarIds.push_back(
this->carIds.at(static_cast<unsigned long>(index.row()))
.at(0)
.toInt());
}
vector<int> selectedDriverIds;
foreach (const QModelIndex & index,
this->ui->lVDrivers->selectionModel()->selectedIndexes()) {
selectedDriverIds.push_back(
this->driversList.at(static_cast<unsigned long>(index.row()))
.at(1)
.toInt());
}
RaceListGenerator raceListGeneator(selectedDriverIds, selectedCarIds);
vector<vector<int>> generatedList;
try {
// structure: shellcar, shelldriver, deacar, deadriver
generatedList = raceListGeneator.getList();
string statement = "select mindestrundendauer from renndauer";
string minRoundTime =
this->db->getData2(statement, 1).at(0).at(0).toStdString();
// calc current time
time_t now = time(nullptr);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
// cout << buf << endl;
string curTime(buf);
std::stringstream ss;
ss << "insert into Rennen (date, minimumRoundtime) values ('" << curTime
<< "', " << minRoundTime << ")";
this->db->setData(ss.str());
// get id of current race
statement = "select id from Rennen order by id DESC limit 1";
string raceId =
this->db->getData2(statement, 1).at(0).at(0).toStdString();
for (auto zeile : generatedList) {
ss.str(std::string());
ss << "insert into aktRennen (autoShellId, fahrerShellId, "
"autoDeaId, fahrerDeaId, id_rennen) values ("
<< zeile.at(0) << ", " << zeile.at(1) << ", " << zeile.at(2)
<< ", " << zeile.at(3) << ", " << raceId << ")";
this->db->setData(ss.str());
}
} catch (std::exception & e) {
Q_UNUSED(e);
}
this->close();
// test
// std::for_each(selectedCarIds.begin(), selectedCarIds.end(),
// [](int id) { cout << id << " " << endl; });
// test
// std::for_each(selectedDriverIds.begin(), selectedDriverIds.end(),
// [](int id) { cout << id << " " << endl; });
}