-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathstatistics.cpp
More file actions
145 lines (115 loc) · 4.17 KB
/
statistics.cpp
File metadata and controls
145 lines (115 loc) · 4.17 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "statistics.hpp"
#include "color.hpp"
#include "scores-graphics.hpp"
#include "scores.hpp"
#include "statistics-graphics.hpp"
#include <algorithm>
#include <array>
#include <fstream>
#include <sstream>
namespace Statistics {
namespace {
// Helper function to receive player name input
std::string receive_input_player_name(std::istream &is) {
std::string name;
is >> name;
// Validate input
if (is.fail()) {
throw std::runtime_error("Invalid player name input.");
}
return name;
}
// Generate total game stats from input data
total_game_stats_t generateStatsFromInputData(std::istream &is) {
total_game_stats_t stats;
is >> stats;
// Validate input
if (is.fail()) {
throw std::runtime_error("Invalid statistics input data.");
}
return stats;
}
// Save statistics data to the output stream
bool generateFilefromStatsData(std::ostream &os, const total_game_stats_t &stats) {
os << stats;
return os.good(); // Check if the write was successful
}
// Save end game statistics to a file
bool saveToFileEndGameStatistics(const std::string &filename, const total_game_stats_t &s) {
std::ofstream filedata(filename);
if (!filedata) {
throw std::runtime_error("Could not open file for saving statistics.");
}
return generateFilefromStatsData(filedata, s);
}
// Create final score display data
Scoreboard::Graphics::finalscore_display_data_t make_finalscore_display_data(const Scoreboard::Score &finalscore) {
return std::make_tuple(
std::to_string(finalscore.score),
std::to_string(finalscore.largestTile),
std::to_string(finalscore.moveCount),
secondsFormat(finalscore.duration)
);
}
} // namespace
// Load statistics from a file
load_stats_status_t loadFromFileStatistics(const std::string &filename) {
std::ifstream statistics(filename);
if (!statistics) {
return load_stats_status_t{false, total_game_stats_t{}};
}
total_game_stats_t stats = generateStatsFromInputData(statistics);
return load_stats_status_t{true, stats};
}
// Load the best score from game statistics
ull load_game_best_score() {
total_game_stats_t stats;
bool stats_file_loaded{};
ull tempscore{0};
std::tie(stats_file_loaded, stats) = loadFromFileStatistics("../data/statistics.txt");
if (stats_file_loaded) {
tempscore = stats.bestScore;
}
return tempscore;
}
// Save end game statistics
void saveEndGameStats(const Scoreboard::Score &finalscore) {
total_game_stats_t stats;
std::tie(std::ignore, stats) = loadFromFileStatistics("../data/statistics.txt");
stats.bestScore = std::max(stats.bestScore, finalscore.score);
stats.gameCount++;
stats.winCount += finalscore.win ? 1 : 0;
stats.totalMoveCount += finalscore.moveCount;
stats.totalDuration += finalscore.duration;
saveToFileEndGameStatistics("../data/statistics.txt", stats);
}
// Create final score and end game data file
void CreateFinalScoreAndEndGameDataFile(std::ostream &os, std::istream &is, Scoreboard::Score finalscore) {
const auto finalscore_display_data = make_finalscore_display_data(finalscore);
DrawAlways(os, DataSuppliment(finalscore_display_data, Scoreboard::Graphics::EndGameStatisticsPrompt));
DrawAlways(os, Graphics::AskForPlayerNamePrompt);
finalscore.name = receive_input_player_name(is);
Scoreboard::saveScore(finalscore);
saveEndGameStats(finalscore);
DrawAlways(os, Graphics::MessageScoreSavedPrompt);
}
} // namespace Statistics
using namespace Statistics;
// Overload operator to read total game stats
std::istream &operator>>(std::istream &is, total_game_stats_t &s) {
is >> s.bestScore >> s.gameCount >> s.winCount >> s.totalMoveCount >> s.totalDuration;
// Validate stream state after reading
if (is.fail()) {
throw std::runtime_error("Failed to read total game stats.");
}
return is;
}
// Overload operator to write total game stats
std::ostream &operator<<(std::ostream &os, const total_game_stats_t &s) {
os << s.bestScore << "\n"
<< s.gameCount << "\n"
<< s.winCount << "\n"
<< s.totalMoveCount << "\n"
<< s.totalDuration;
return os;
}