-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighscore.hpp
92 lines (76 loc) · 1.8 KB
/
highscore.hpp
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
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include "global.hpp"
int key = HELP_STR.size() + 30;
std::string enc(std::string inpString)
{
// Define XOR key
// Any character value will work
// calculate length of input string
int len = inpString.size();
// perform XOR operation of key
// with every caracter in string
for (int i = 0; i < len; i++)
{
inpString[i] = inpString[i] ^ key;
}
return inpString;
}
bool update_highscore(int points, std::string hipath = "./highscores") {
std::ofstream filew;
std::ifstream filer;
int hspoints;
bool newfile = false;
filer.open(hipath, std::ios::out);
bool hs = false; //This variable represents if the given points results in a new highscore or not
if (!filer.is_open()) {
filew.open(hipath);
filew << enc("0");
hspoints = 0;
newfile = true;
filew.close();
}
if (!newfile) {
std::string output = "";
std::string line;
while (std::getline(filer, line)) {
hspoints = std::stoi(enc(line));
}
}
filer.close();
if (hspoints > points) {
hs = false;
} else {
filew.open(hipath);
std::string newpoints = std::to_string(points);
filew << enc(newpoints);
filew.close();
hs = true;
}
return hs;
}
int get_highscore(std::string hipath = "./highscores") {
std::ofstream filew;
std::ifstream filer;
int hspoints;
bool newfile = false;
filer.open(hipath, std::ios::out);
if (!filer.is_open()) {
filew.open(hipath);
filew << enc("0");
hspoints = 0;
newfile = true;
filew.close();
}
if (!newfile) {
std::string output = "";
std::string line;
while (std::getline(filer, line)) {
hspoints = stoi(enc(line));
}
}
filer.close();
return hspoints;
}