-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDataSave.cpp
More file actions
96 lines (83 loc) · 1.88 KB
/
DataSave.cpp
File metadata and controls
96 lines (83 loc) · 1.88 KB
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
#include "DataSave.h"
// 0 Correct
// 1 Level file not found
// 2 Player file not found
int LoadAndDecryptFile(playerArray & v) {
int error = 0;
playerData aux;
std::string s;
std::ifstream f;
std::istringstream o;
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_append_path_component(path, "Resources");
al_append_path_component(path, "Players");
al_change_directory(al_path_cstr(path, '/'));
al_destroy_path(path);
f.open(PLAYER_INFO);
if (!f.is_open()) {
error = 2;
}
else
{
getline(f, s);
while (!f.eof())
{
DecryptVigenere(s);
o.str(s);
o >> aux.name;
o >> s; //To parse correctly the text
aux.score.SetScore(stoi(s));
v.push_back(aux);
o.clear();
getline(f, s);
}
f.close();
}
return error;
}
void DecryptVigenere(std::string & d) {
std::vector <int> v;
int keySize = ENCRYPTION_KEY.size();
for (int i = 0; i < (int)d.size(); ++i)
{
v.push_back(ENCRYPTION_KEY[i % keySize] % RANGE);
}
for (int i = 0; i < (int)d.size(); ++i)
{
d[i] = d[i] - v[i];
if (d[i] < MIN)
{
d[i] = MAX - (MIN - d[i]);
}
}
}
void SaveAndEncrypt(playerArray const& v) {
std::string aux;
std::ofstream file;
std::vector <int> u;
int p;
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_append_path_component(path, "Resources");
al_append_path_component(path, "Players");
al_change_directory(al_path_cstr(path, '/'));
al_destroy_path(path);
file.open(PLAYER_INFO);
for (int i = 0; i < (int)v.size() && i < 10; ++i) //Only the first 10 are relevant
{
aux = v[i].name + " " + std::to_string(v[i].score.ReturnScore());
p = 0;
for (int j = 0; j < (int)aux.size(); ++j)
{
u.push_back(ENCRYPTION_KEY[p] % RANGE);
aux[j] = aux[j] + u[j];
if (aux[j] > MAX)
{
aux[j] = MIN + (aux[j] - MAX);
}
p = (p + 1) % ENCRYPTION_KEY.size();
}
u.clear();
file << aux << '\n';
}
file.close();
}