Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
187 lines (150 loc) · 7.64 KB
/
Copy pathmain.cpp
File metadata and controls
187 lines (150 loc) · 7.64 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "GameFinder.h"
#include "GameLibrary.h"
#include "CLI11.hpp"
#include "Printer.h"
#include "color.h"
void print_demo_message(std::string message)
{
std::cout << std::endl << RED << "[DEMO]\t" << message << RESET << std::endl;
}
void api_demo()
{
// Create a game finder
print_demo_message("Creating a backlog organizer");
GameFinder fin = GameFinder("games.txt");
// Create a library and specify the path to file where the games will be saved
print_demo_message("Creating a game library");
GameLibrary lib = GameLibrary("user_games.txt");
// Print the whole library
print_demo_message("Printing newly loaded library");
Printer::print(lib);
// Add some games to the library
print_demo_message("Adding some games to the library");
lib.add_game(fin.find_game("Celeste"));
lib.add_game(fin.find_game("Rocket League"));
lib.add_game(fin.find_game("Factorio"));
lib.add_game(fin.find_game("Car Parking 2"));
lib.add_game(fin.find_game("//TODO: today"));
lib.add_game(fin.find_game("Nonexistent Game")); // adding a nonexistent game
lib.add_game(fin.find_game("Cyberpunk 2077"), 50); // adding a game with user rating
lib.add_game(fin.find_game("Terraria"), "hail the moon lord"); // adding a game with user note
// Remove a game from the library
print_demo_message("Removing a game from the library");
lib.remove_game(fin.get_game_id(fin.search_name("Factorio")));
// Print the whole library
print_demo_message("Printing the library after adding and removing games");
Printer::print(lib);
// Sort library by average rating
print_demo_message("Sorting the library by average rating");
lib.sort_games_by_rating();
Printer::print_simple(lib);
// Sort library by money/hour ratio
print_demo_message("Sorting the library by money/hour ratio");
lib.sort_games_by_ratio();
Printer::print_simple(lib);
// Save the library to a file
print_demo_message("Saving the library to a file");
lib.save_games("modified_user_games.txt");
}
void create_library(std::string user_games_file)
{
std::ofstream file(user_games_file);
file << "{}";
file.close();
GameLibrary lib = GameLibrary(user_games_file);
lib.save_games(user_games_file);
}
void add(std::string game_name, std::string games_file, std::string user_games_file, int user_rating, std::string user_note)
{
GameFinder fin = GameFinder(games_file);
GameLibrary lib = GameLibrary(user_games_file);
lib.add_game(fin.find_game(game_name), user_rating, user_note);
lib.save_games(user_games_file);
}
void remove(std::string game_name, std::string games_file, std::string user_games_file)
{
GameFinder fin = GameFinder(games_file);
GameLibrary lib = GameLibrary(user_games_file);
lib.remove_game(fin.get_game_id(fin.search_name(game_name)));
lib.save_games(user_games_file);
}
void update(std::string game_name, std::string games_file, std::string user_games_file, int user_rating, std::string user_note)
{
GameFinder fin = GameFinder(games_file);
GameLibrary lib = GameLibrary(user_games_file);
lib.update_game(fin.get_game_id(fin.search_name(game_name)), user_rating, user_note);
lib.save_games(user_games_file);
}
void print(std::string user_games_file)
{
GameLibrary lib = GameLibrary(user_games_file);
Printer::print(lib);
}
void print_simple(std::string user_games_file)
{
GameLibrary lib = GameLibrary(user_games_file);
Printer::print_simple(lib);
}
void sort(std::string user_games_file, CLI::Option* rating, CLI::Option* ratio)
{
GameLibrary lib = GameLibrary(user_games_file);
if (*rating)
lib.sort_games_by_rating();
else if (*ratio)
lib.sort_games_by_ratio();
lib.save_games(user_games_file);
}
int main(int argc, char** argv)
{
std::string help_header = R""""(
_____ __ __
/ ___/__ ___ _ ___ / // /__ ___ ____
/ (_ / _ `/ ' \/ -_) / _ / -_) _ `/ _ \
\___/\_,_/_/_/_/\__/ /_//_/\__/\_,_/ .__/
/_/
)"""";
CLI::App app{help_header};
auto demo = app.add_subcommand("demo", "Run the demo");
demo->callback(api_demo);
std::string user_games_file = "user_games.txt";
std::string games_file = "games.txt";
std::string game_name;
std::string user_note;
int user_rating = -1;
auto sub_create = app.add_subcommand("create", "Create a new library");
sub_create->add_option("path", user_games_file, "Path to file with user games")->required();
sub_create->callback([&user_games_file]() { create_library(user_games_file); });
auto sub_add = app.add_subcommand("add", "Add a game to the library");
sub_add->add_option("path", user_games_file, "Path to file with user games")->check(CLI::ExistingFile)->required();
sub_add->add_option("game", game_name, "Name of the game to add")->required();
sub_add->add_option("-g,--games", games_file, "Path to file with games")->check(CLI::ExistingFile);
sub_add->add_option("-r,--rating", user_rating, "User rating")->check(CLI::Range(0, 100));
sub_add->add_option("-n,--note", user_note, "User note");
sub_add->callback([&game_name, &games_file, &user_games_file, &user_rating, &user_note]() { add(game_name, games_file, user_games_file, user_rating, user_note); });
auto sub_rem = app.add_subcommand("remove", "Remove a game from the library");
sub_rem->add_option("path", user_games_file, "Path to file with user games")->check(CLI::ExistingFile)->required();
sub_rem->add_option("game", game_name, "Name of the game to remove")->required();
sub_rem->add_option("-g,--games", games_file, "Path to file with games")->check(CLI::ExistingFile);
sub_rem->callback([&game_name, &games_file, &user_games_file]() { remove(game_name, games_file, user_games_file); });
auto sub_upd = app.add_subcommand("update", "Update a game in the library");
sub_upd->add_option("path", user_games_file, "Path to file with user games")->check(CLI::ExistingFile)->required();
sub_upd->add_option("game", game_name, "Name of the game to update")->required();
sub_upd->add_option("-g,--games", games_file, "Path to file with games")->check(CLI::ExistingFile);
sub_upd->add_option("-r,--rating", user_rating, "User rating")->check(CLI::Range(0, 100));
sub_upd->add_option("-n,--note", user_note, "User note");
sub_upd->callback([&game_name, &games_file, &user_games_file, &user_rating, &user_note]() { update(game_name, games_file, user_games_file, user_rating, user_note); });
auto sub_print = app.add_subcommand("print", "List all games in the library");
sub_print->add_option("path", user_games_file, "Path to file with user games")->check(CLI::ExistingFile)->required();
sub_print->callback([&user_games_file]() { print(user_games_file); });
auto sub_print_simple = app.add_subcommand("print-simple", "List all games in the library in a simple table");
sub_print_simple->add_option("path", user_games_file, "Path to file with user games")->check(CLI::ExistingFile)->required();
sub_print_simple->callback([&user_games_file]() { print_simple(user_games_file); });
auto sub_sort = app.add_subcommand("sort", "Sort the library");
sub_sort->add_option("path", user_games_file, "Path to file with user games")->check(CLI::ExistingFile)->required();
auto rating = sub_sort->add_flag("-r,--rating", "Sort by average rating");
auto ratio = sub_sort->add_flag("-m,--money-per-hour", "Sort by money/hour ratio");
sub_sort->callback([&user_games_file, &rating, &ratio]() { sort(user_games_file, rating, ratio); });
app.require_subcommand(1);
CLI11_PARSE(app, argc, argv);
return 0;
}