-
Notifications
You must be signed in to change notification settings - Fork 0
/
savegame.cpp
98 lines (81 loc) · 1.79 KB
/
savegame.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "savegame.h"
#include <QString>
#include <QFile>
#include <QJsonDocument>
#include <iostream>
using namespace std;
Savegame::Savegame() :
my_score(0),
my_name("Oh Good. For a moment there, I thought we were in trouble."),
my_lines(0),
my_spm(0)
{}
Savegame::Savegame(const QString &name, int score):
my_name(name),
my_score(score)
{}
QString Savegame::name() const
{
return my_name;
}
void Savegame::setName(const QString &name)
{
my_name = name;
}
int Savegame::score() const
{
return my_score;
}
void Savegame::setScore(int score)
{
my_score = score;
}
void Savegame::setLines(int lines)
{
my_lines = lines;
}
int Savegame::lines() const
{
return my_lines;
}
void Savegame::setSpm(int spm)
{
my_spm = spm;
}
float Savegame::spm() const
{
return my_spm;
}
void Savegame::read(const QJsonObject &json)
{
my_name = json["name"].toString();
my_score = json["score"].toDouble();
my_lines = json["lines"].toDouble();
my_spm = json["lines"].toDouble();
}
void Savegame::write(QJsonObject &json) const
{
time_t result = time(nullptr);
json["name"] = my_name;
json["score"] = my_score;
json["lines"] = my_lines;
json["spm"] = my_spm;
json["timestamp"] = asctime(std::localtime(&result));
}
bool Savegame::storeGame(QJsonObject &json) const
{
QFile saveFile(QStringLiteral("save_tetris.json"));
bool a = saveFile.open(QIODevice::WriteOnly);
if (!a)
{
return false;
}
QJsonDocument saveDoc(json);
saveFile.write(saveDoc.toJson());
return true;
}
QByteArray Savegame::datastream(QJsonObject &json) const
{
QJsonDocument Game_Data_doc (json);
return Game_Data_doc.toJson();
}