-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin.cpp
259 lines (226 loc) · 7.74 KB
/
admin.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include <vector>
#include "streamZ.h"
#include "admin.h"
using namespace std;
badDateComp::badDateComp(const string& reason){
this->reason = reason;
}
string badDateComp::what() const {
return reason;
}
NoActiveStreams::NoActiveStreams(const string& reason) {
this->reason = reason;
}
string NoActiveStreams::what() const {
return reason;
}
Admin::Admin(const std::string& name, StreamZ* site) {
this-> name = name;
this->site = site;
}
int Admin::getNumStreams() const {
return site->streams.size();
}
string Admin::getName() const {
return name;
}
void Admin::setName(const std::string& name) {
this->name = name;
}
int Admin::getNumCreatedStreams(Date& from, const Date& to) const { // from pastStreams aswell
if (from > to) throw badDateComp("ERROR: Start date is after end date");
int cnt = 0;
Date d;
vector<Stream*>::const_iterator it;
for (it = site->streams.begin();it != site->streams.end(); it++){
d = (*it)->getStartDate();
if (d >= from && d <= to)
cnt ++;
}
Date dMap;
map<unsigned int, PastStream*>::const_iterator itMap;
for (itMap = site->pastStreams.begin();itMap != site->pastStreams.end(); itMap++){
dMap = itMap->second->StartDate;
if (d >= from && d <= to)
cnt ++;
}
return cnt;
}
float Admin::getAvgViews() const {
if (site->streams.empty()) throw NoActiveStreams("There are currently no active streams");
int sum = 0;
float avg;
vector<Stream *>::const_iterator it;
for (it = site->streams.begin(); it != site->streams.end(); it++){
sum += (*it)->getNumViewers();
}
avg = (float) sum/getNumStreams();
return avg;
}
float Admin::getAvgViews(Date& from, const Date& to) const { // paststreams aswell
if (from > to) throw badDateComp("ERROR: Start date is after end date");
int sum = 0;
int count = 0;
float avg;
Date d;
vector<Stream *>::const_iterator it;
for (it = site->streams.begin(); it != site->streams.end(); it++){
d = (*it)->getStartDate();
if (d >= from && d <= to) {
sum += (*it)->getNumViewers();
count++;
}
}
Date dMap;
map<unsigned int, PastStream*>::const_iterator itMap;
for (itMap = site->pastStreams.begin();itMap != site->pastStreams.end(); itMap++){
dMap = itMap->second->StartDate;
if (d >= from && d <= to) {
sum += itMap->second->noViewers;
count++;
}
}
avg = (float) sum/count;
return avg;
}
int Admin::getNumPublicStreams() const {
return site->publicStreams.size();
}
int Admin::getNumPublicStreams(Date& from, const Date& to) const {
if (from > to) throw badDateComp("ERROR: Start date is after end date");
int cnt = 0;
Date d;
vector<PublicStream *>::const_iterator it;
for (it = site->publicStreams.begin(); it != site->publicStreams.end(); it++){
d = (*it)->getStartDate();
if (d >= from && d <= to)
cnt++;
}
return cnt;
}
int Admin::getNumPrivateStreams() const {
return site->privateStreams.size();
}
int Admin::getNumPrivateStreams(Date& from, const Date& to) const {
if (from > to) throw badDateComp("ERROR: Start date is after end date");
int cnt = 0;
Date d;
vector<PrivateStream *>::const_iterator it;
for (it = site->privateStreams.begin(); it != site->privateStreams.end(); it++){
d = (*it)->getStartDate();
if (d >= from && d <= to)
cnt++;
}
return cnt;
}
string Admin::getPreferredLanguage(std::vector<Stream*>& streams) const {
if (streams.empty()) throw NoActiveStreams("There are currently no active streams");
int cnt = 0, max_cnt = 0, max_pos = 0;
string li, lj;
for (int i=0;i<streams.size();i++){
li = streams[i]->getLanguage();
for (int j=i+1;j<streams.size();j++){
lj = streams[j]->getLanguage();
if (li == lj)
cnt++;
}
if (cnt > max_cnt){
max_cnt = cnt;
max_pos = i;
}
}
return streams[max_pos]->getLanguage();
}
string Admin::getPreferredStreamType() const {
if (site->streams.empty()) throw NoActiveStreams("There are currently no active streams");
if (site->privateStreams.size() > site->publicStreams.size()) return "Private Streams are the preferred type of stream";
else if (site->privateStreams.size() < site->publicStreams.size()) return "Public Streams are the preferred type of stream";
else return "There is no preference in the type of stream";
}
Streamer * Admin::getMostViewedStreamer() const {
if (site->streams.empty()) throw NoActiveStreams("There are currently no active streams");
Streamer* best;
int maxViews = 0;
for (std::unordered_map<std::string, unsigned int>::const_iterator it = site->streamersNickID.begin(); it != site->streamersNickID.end(); ++it){
unsigned int id = it->second;
Streamer* streamer = (Streamer*) site->users.at(id);
int views;
if (streamer->inAStream())
views = streamer->getNumViewers();
else views = -1;
if (views > maxViews){
maxViews = views;
best = streamer;
}
}
return best;
}
void Admin::listDonations() const {
BSTItrIn<Donation> it(site->donations);
if (it.isAtEnd()) cout << "There are no donations to show" << endl;
while (!it.isAtEnd()){
it.retrieve().showDonation();
it.advance();
}
}
void Admin::listDonations(int rate1, int rate2) const {
if (rate1 > rate2) throw badDateComp("The lower bound can't be higher than the upper bound!");
BSTItrIn<Donation> it(site->donations);
bool found = false;
while (!it.isAtEnd()){
if (it.retrieve().getRating() >= rate1 && it.retrieve().getRating() <= rate2) {
it.retrieve().showDonation();
found = true;
}
it.advance();
}
if (!found) cout << "No donations match the criteria" << endl;
}
void Admin::listTopDonations() const { // top 10
BSTItrIn<Donation> it(site->donations);
if (it.isAtEnd()) cout << "There are no donations to show" << endl;
for (int i = 0; i < 10; ++i){
if (it.isAtEnd()) break;
it.retrieve().showDonation();
it.advance();
}
}
void Admin::searchStreamers() const{
while (true){
string name;
cout << "Insert the name of the streamer you want to search ('exit' to leave)" << endl;
getline(cin, name);
while (true){
if (cin.fail() || cin.eof()){
cin.clear();
cout << "Insert a valid name" << endl;
getline(cin, name);
continue;
}
if (name == ""){
cout << "Insert a valid name" << endl;
getline(cin, name);
continue;
}
break;
}
if (name == "exit") return;
transform(name.begin(), name.end(), name.begin(), ::toupper);
unordered_map<string, unsigned int>::iterator it = site->streamersNickID.begin();
bool found = false;
for(; it != site->streamersNickID.end(); ++it){
Streamer* s = (Streamer*) site->users.at(it->second);
string streamName = s->getNick();
transform(streamName.begin(), streamName.end(), streamName.begin(), ::toupper);
if(streamName == name){ // check string::npos on hover
cout << "This streamer exists ";
if (s->getActivity()) cout << "and is active" << endl;
else cout << "but isn't active" << endl;
found = true;
break;
}
}
if (!found) cout << "That streamer does not exist" << endl;
}
}