-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppMode.cpp
More file actions
106 lines (101 loc) · 2.31 KB
/
AppMode.cpp
File metadata and controls
106 lines (101 loc) · 2.31 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
// /////////////////////////////////////////////////////////
//
// File: clerdle/AppMode.cpp
// Author: Michael Foster
// Date: 2022.04.29
//
// This class processes command line arguments to determine
// the desired application mode.
//
// /////////////////////////////////////////////////////////
#include "AppMode.h"
//----------------//
#include <vector>
#include <string>
#include "StatsCollection.h"
#include "Puzzle.h"
#include "UX.h"
AppMode::AppMode(const std::vector<std::string> &args) : test_{0}, generate_{0}, stats_{0}, help_{0}, playerName_{}, quit_{0}
{
if (args.size() && args.at(0)[0] == '-')
{
switch (args.at(0)[1])
{
case 't':
this->test_ = true;
if (int(args.size()) > 1)
this->playerName_ = args.at(1);
break;
case 'g':
if (int(args.size()) > 1)
{
try
{
this->generate_ = std::stoi(args.at(1));
}
catch (const std::invalid_argument &)
{
this->generate_ = -1;
}
catch (const std::out_of_range &)
{
this->generate_ = -1;
}
}
else
this->generate_ = -1;
this->quit_ = true;
break;
case 's':
this->stats_ = true;
if (int(args.size()) > 1)
this->playerName_ = args.at(1);
break;
case 'h':
this->help_ = true;
break;
}
}
else
{
if (args.size())
this->playerName_ = args.at(0);
}
}
bool AppMode::executeSimpleModes(Stats &stats)
{
if (this->isHelp())
{
this->quit_ = true;
UX::printHelp();
}
if (this->isGenerate())
{ // print a number of example puzzles
this->quit_ = true;
if (this->generate_ == -1)
{
UX::errorNoGenNumber();
this->generate_ = 0;
}
for (int i = 0; i < this->generate_; i++)
UX::prints(Puzzle().getAnswer(), '(', i + 1, ')');
}
else if (this->isStats())
{ // print user stats
this->quit_ = true;
if (this->playerName_.length())
{
std::vector<int> pstats{stats.getPlayerStats(this->playerName_, false)};
if (!pstats.size())
UX::errorUserNotFound();
else
UX::printHistogram(this->playerName_, pstats);
}
else
{
for (const auto &p : stats.getAllPlayerStats())
UX::printHistogram(p.first, p.second);
}
}
return this->quit_;
}