-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cpp
More file actions
222 lines (195 loc) · 6.34 KB
/
Copy pathCache.cpp
File metadata and controls
222 lines (195 loc) · 6.34 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
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
#include "Cache.h"
#include "Deck.h"
#include <sw/redis++/redis++.h>
#include "include/rapidjson/document.h"
#include "include/rapidjson/reader.h"
#include <iostream>
#include <vector>
#include "Game.h"
#include <chrono>
#include "include/Color.h"
#include "Eval.h"
#include "Guesstimate.h"
#include <regex>
#include <thread>
using namespace sw::redis;
Cache::Cache(std::string dsn, bool verbose){
_verbose = verbose;
_dsn = dsn;
}
bool Cache::test(std::string dsn){
try {
if(_verbose) {
std::cout << "trying cache...\n";
}
auto redis = Redis(dsn);
redis.set("key", "val");
auto val = redis.get("key");
if (val && _verbose) {
std::cout << *val << std::endl;
}
if(*val == "val"){
redis.del("key");
return true;
}
}
catch (const Error &e) {
if(_verbose){
std::cout << "cant connect to redis\n";
}
}
return false;
}
void Cache::threadSingleEval(std::string channel){
auto redis = Redis(_dsn);
auto sub = redis.subscriber();
sub.on_pmessage([this](std::string pattern, std::string channel, std::string msg) {
std::vector<std::string> temp = split(channel,"\\_+");
// std::string prefix = temp[0];
std::string cmd = temp[2];
int num_threads = 4;
int nounce = 0;
if(temp.size() >= 4){
num_threads = stoi(temp[3]);
}
if(temp.size() >= 5){
nounce = stoi(temp[4]);
}
if(cmd == "guess"){
// guess cards first (one time only / no threading for it needed)
Guesstimate guess(msg);
std::string missingCards = guess.findMissingCards();
std::cout << "missing cards : " << missingCards << std::endl;
}
if(cmd == "eval"){
// spawn threads
std::thread t[num_threads];
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(&Cache::call_from_thread,this, i,msg,nounce);
}
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
}
});
sub.psubscribe(channel);
while (true) {
try {
sub.consume();
} catch (const Error &err) {
std::cout << "unable to consume";
}
}
}
void Cache::call_from_thread(int tid,std::string gameData,int nounce) {
std::cout << "Launched by thread " << tid << " nounce is " << nounce << std::endl;
auto publisher = Redis(_dsn);
std::vector<std::vector<std::vector<float>>> evalResults;
std::string playsString = "";
std::string channelName = "eval_result_";
channelName.append(std::to_string(nounce));
Eval ev(gameData,true);
evalResults = ev.run(2000,2,0,playsString);
std::string stringToPHP = playsString;
stringToPHP.append("#");
stringToPHP.append(serialize(evalResults));
publisher.publish(channelName,stringToPHP);
}
void Cache::listen(std::string channel,std::string evalMode){
auto redis = Redis(_dsn);
auto sub = redis.subscriber();
sub.on_pmessage([this,evalMode](std::string pattern, std::string channel, std::string msg) {
std::string playsString = "";
Eval ev(msg,true);
playsString = "";
std::vector<std::vector<std::vector<float>>> evalResults;
if (evalMode == "1"){
evalResults = ev.run(1000,0,1,playsString);
}
else if (evalMode == "2"){
evalResults = ev.run(1000,0,2,playsString);
}
else if (evalMode == "3"){
evalResults = ev.run(1000,0,3,playsString);
}
else if (evalMode == "4"){
evalResults = ev.run(1000,1,0,playsString);
}
else if (evalMode == "5"){
evalResults = ev.run(2000,2,0,playsString);
}
else if (evalMode == "6"){
evalResults = ev.run(5000,6,0,playsString);
}
else {
Guesstimate guess(msg);
std::string missingCards = guess.findMissingCards();
// publish missing cards
auto publisher = Redis(_dsn);
std::string channelToPublish = "lumen_database_eval_results_";
channelToPublish.append(evalMode);
publisher.publish(channelToPublish,missingCards);
std::cout << "channel: " << channelToPublish << " => " << missingCards << std::endl;
}
if (evalMode != "10"){
std::string stringToPHP = playsString;
stringToPHP.append("#");
stringToPHP.append(serialize(evalResults));
auto publisher = Redis(_dsn);
std::string channelToPublish = "lumen_database_eval_results_";
channelToPublish.append(evalMode);
publisher.publish(channelToPublish,stringToPHP);
std::cout << stringToPHP << std::endl;
}
});
sub.psubscribe(channel);
while (true) {
try {
sub.consume();
} catch (const Error &err) {
std::cout << "unable to consume";
}
}
}
std::vector<int> Cache::parseIntVector(const rapidjson::Value& a){
std::vector<int> vec;
assert(a.IsArray());
for (rapidjson::SizeType i = 0; i < a.Size(); i++){
vec.push_back(a[i].GetInt());
}
return vec;
}
std::string Cache::serialize(std::vector<std::vector<std::vector<float>>> vec){
std::string str = "";
for (size_t i = 0; i < vec.size() ; i++){
for (size_t j = 0; j < vec[i].size() ; j++){
str.append(resultToString(vec[i][j]));
if(j == 0){
str.append("-");
}
}
if(i != vec.size() -1){
str.append("|");
}
}
return str;
}
std::string Cache::resultToString(std::vector<float> result){
std::string str;
str.append(std::to_string(result[0]));
str.append(",");
str.append(std::to_string(result[1]));
str.append(",");
str.append(std::to_string(result[2]));
str.append(",");
str.append(std::to_string(result[3]));
return str;
}
std::vector<std::string> Cache::split(const string& input, const string& regex) {
// passing -1 as the submatch index parameter performs splitting
std::regex re(regex);
std::sregex_token_iterator
first{input.begin(), input.end(), re, -1},
last;
return {first, last};
}