-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuction.cpp
More file actions
103 lines (71 loc) · 2.31 KB
/
Copy pathAuction.cpp
File metadata and controls
103 lines (71 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
#include "Auction.h"
Auction::~Auction() {
}
void Auction::AddBanner(const Banner& banner) {
this->_SelectedBanners.emplace_back(banner);
}
void Auction::SetFiltersFunctions(std::vector<std::function<bool(const Banner&)>> filterFunction) {
this->filterFunctions = filterFunction;
}
std::vector<Banner> Auction::GetBanners() const {
return this->_SelectedBanners;
}
bool Auction::CheckCountry(std::vector<std::string> bannersCuntryList, std::string& country) {
if (std::find(bannersCuntryList.begin(), bannersCuntryList.end(), country) != bannersCuntryList.end()) {
return true;
}
return false;
}
bool Auction::checkFilters(const Banner& banner) {
for (auto filter : filterFunctions) {
if (!filter(banner)) {
return false;
}
}
return true;
}
bool Auction::CheckBannerId(const unsigned int& id) {
if (std::find(_UniqueAdvIds.begin(), _UniqueAdvIds.end(), id) == _UniqueAdvIds.end()) {
this->_UniqueAdvIds.emplace_back(id);
return true;
}
return false;
}
std::vector<Banner> Auction::Auction::runAuction(const std::vector<Banner> &banners, int slots, std::string country,
std::vector<std::function<bool(const Banner &)>> filterFunctions) {
SetFiltersFunctions(filterFunctions);
int slotsCounter = 0;
std::vector<Banner> sortedBanners = banners;
std::sort(sortedBanners.begin(), sortedBanners.end(), [&](Banner &b1, Banner& b2) {
return b1.GetPrice() > b2.GetPrice();});
for (const Banner& banner : sortedBanners) {
//Default FilterPaass state
bool FilterPassed = true;
//Check for advertising slots
if (slotsCounter < slots) {
std::vector<std::string> BannerCountries = banner.GetCountryList();
//Check Filters
FilterPassed = checkFilters(banner);
//Check Country
if (!BannerCountries.empty()) {
if (CheckCountry(banner.GetCountryList(), country)) {
//Check Id
if (CheckBannerId(banner.GetAdvertisingCompanyID()) && FilterPassed) {
AddBanner(banner);
slotsCounter++;
}
}
}
else {
//If countries is empty (Any country)
if (FilterPassed) {
if (CheckBannerId(banner.GetAdvertisingCompanyID())) {
AddBanner(banner);
slotsCounter++;
}
}
}
}
}
return this->GetBanners();
}