-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscore.cpp
64 lines (49 loc) · 2.07 KB
/
highscore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "highscore.h"
Highscore::Highscore(QVector<QVariant> model)
{
this->id = model[0].toInt();
this->score = model[1].toInt();
this->username = model[2].toString();
this->idLevel = model[3].toInt();
this->token = model[4].toString();
this->created_at = model[5].toString();
this->table = this->getTable();
}
Highscore::Highscore(DatabaseAnswer<Highscore>* response)
{
*this = *response->getObject();
}
DatabaseAnswer<Highscore>* Highscore::create(int score, int idLevel, QString username, Database* db)
{
QDateTime created_at = QDateTime::currentDateTime();
QString tokenString = QString::number(score) + QString::number(idLevel) + username + created_at.toString();
QString token = QString(QCryptographicHash::hash(tokenString.toLocal8Bit(), QCryptographicHash::Md5).toHex());
DatabaseAnswer<Highscore>* response = db->execModel<Highscore>(Highscore::generateSaveModel(score, idLevel, username, token));
if(response->hasError()){
Error(500, "QueryError", response->errorToText());
} else {
response = db->find<Highscore>(Highscore::generateFindModel(token, Highscore::getTable()));
}
return response;
}
QString Highscore::getTable()
{
return "tblHighscores";
}
QString Highscore::generateFindModel(QString token, QString table)
{
return "SELECT * FROM " + table + " WHERE token = '" + token + "'";
}
QString Highscore::generateFindIdModel(int id, QString table)
{
return "SELECT * FROM " + table + " WHERE id = " + QString::number(id);
}
QString Highscore::generateFindAllModel(int idLevel, QString table)
{
return "SELECT * FROM " + table + " WHERE idLevel = " + QString::number(idLevel) + " ORDER BY score DESC";
}
QString Highscore::generateSaveModel(int score, int idLevel, QString username, QString token)
{
QDate created_at = QDate::currentDate();
return "INSERT INTO " + Highscore::getTable() + " (token, score, idLevel, username, created_at) VALUES ('" + token + "', '" + QString::number(score) + "', " + QString::number(idLevel) + ", '" + username + "', '" + created_at.toString() + "')";
}