-
Notifications
You must be signed in to change notification settings - Fork 85
/
bidder.hpp
116 lines (102 loc) · 4.36 KB
/
bidder.hpp
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
/*
* File: bidder.hpp
* Author: Vladimir Venediktov
* Author: Arseny Bushev
* Copyright (c) 2016-2018 Venediktes Gruppe, LLC
*
* Created on January 8, 2018, 2:42 PM
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#ifndef VANILLA_BIDDER_HPP
#define VANILLA_BIDDER_HPP
#include <memory>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include "user_info.hpp"
namespace vanilla {
template <typename Request>
struct request_extractor {
static const Request& request(const Request &request) {
return request;
}
};
template <>
struct request_extractor<VanillaRequest> {
static auto request(const VanillaRequest &r) -> decltype(r.request()) {
return r.request();
}
};
template<typename DSL, typename Selector>
class Bidder {
using BidRequest = typename DSL::deserialized_type;
using BidResponse = typename DSL::serialized_type;
public:
Bidder(Selector selector) : selector{std::move(selector)}, uuid_generator{}
{}
template <typename Request , typename Tuple, std::size_t... Idx, typename ...Info>
const BidResponse& bid(const Request &vanilla_request, Tuple&& tuple, std::index_sequence<Idx...>, Info && ...) {
response.clear();
auto request = request_extractor<Request>::request(vanilla_request);
for (auto &imp : request.imp) {
buildImpResponse(request, imp, std::get<Idx>(std::forward<Tuple>(tuple))...);
}
return response;
}
template <typename Request , typename ...Keys>
const BidResponse& bid(const Request &vanilla_request, Keys&&... keys) {
return this->bid(vanilla_request, std::make_tuple(std::forward<Keys>(keys)...),
std::make_index_sequence<sizeof...(keys)>()
);
}
private:
template<typename Impression>
void addCurrency(const BidRequest& request, const Impression& imp) {
if (request.cur.size()) {
response.cur = request.cur[0];
} else if (imp.bidfloorcur.length()) {
response.cur = imp.bidfloorcur; // Just return back
}
}
template<typename Impression, typename Ad>
void addBid(const BidRequest& request, const Impression& imp, Ad && ad) {
if (response.seatbid.size() == 0) {
response.seatbid.emplace_back();
}
typename std::remove_reference<decltype(BidResponse().seatbid[0].bid[0])>::type bid;
boost::uuids::uuid bidid = uuid_generator();
bid.id = boost::uuids::to_string(bidid); // TODO check documentation
// Is it the same as response.bidid?
bid.impid = imp.id;
bid.price = ad.auth_bid_micros ? *ad.auth_bid_micros / 1000000.0 : ad.max_bid_micros / 1000000.0 ; // Not micros?
bid.w = ad.width;
bid.h = ad.height;
bid.adm = ad.code;
bid.adid = std::to_string(ad.ad_id); //for T = std::string , for T = string_view must use thread_local storage
response.seatbid.back().bid.emplace_back(std::move(bid));
}
template<typename Impression, typename Arg, typename ...ChainedFuncs>
void buildImpResponse(const BidRequest& request, const Impression& imp, Arg && arg, ChainedFuncs... chained) {
if (auto ad = selector.select(request, imp, std::forward<Arg>(arg), chained... )) {
boost::uuids::uuid bidid = uuid_generator();
response.bidid = boost::uuids::to_string(bidid);
addCurrency(request, imp);
addBid(request, imp, *ad);
}
}
Selector selector;
boost::uuids::random_generator uuid_generator;
BidResponse response;
};
}
#endif /* VANILLA_BIDDER_HPP */