-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.hpp
More file actions
60 lines (51 loc) · 1.41 KB
/
Copy pathstats.hpp
File metadata and controls
60 lines (51 loc) · 1.41 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
#pragma once
#include <string>
#include <unordered_map>
// structures for keeping track of and storing level statistics
struct Stats {
unsigned time = 0; // ticks
unsigned jumps = 0;
unsigned double_jumps = 0;
int deaths = 0;
int restarts = 0;
constexpr unsigned total_jumps() const {
return jumps + double_jumps;
}
constexpr unsigned total_respawns() const {
return deaths + restarts;
}
struct Stats operator+(const Stats &other) const {
return {
time + other.time,
jumps + other.jumps,
double_jumps + other.double_jumps,
deaths + other.deaths,
restarts + other.restarts,
};
}
struct Stats &operator+=(const Stats &other) {
time += other.time;
jumps += other.jumps;
double_jumps += other.double_jumps;
deaths += other.deaths;
restarts += other.restarts;
return *this;
}
bool better_than(const Stats &other) const {
if (time != other.time) return time < other.time;
if (total_respawns() != other.total_respawns()) return total_respawns() < other.total_respawns();
if (total_jumps() != other.total_jumps()) return total_jumps() < other.total_jumps();
return false;
}
};
class PBFile {
std::unordered_map<std::string, Stats> pbs{};
static PBFile load(std::istream &inp);
void save(std::ostream &out) const;
public:
static PBFile load();
void save() const;
bool has_pb(std::string key) const;
const Stats *get(std::string key) const;
void set(std::string key, Stats val);
};