-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkakomimasu.cpp
More file actions
242 lines (201 loc) · 6.58 KB
/
kakomimasu.cpp
File metadata and controls
242 lines (201 loc) · 6.58 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "kakomimasu.h"
const int DIR[8][2] = {
{-1, -1},
{0, -1},
{1, -1},
{-1, 0},
{1, 0},
{-1, 1},
{0, 1},
{1, 1},
};
string host = "https://kakomimasu.website/api";
void setHost(string s) {
host = s;
}
static size_t callbackWrite(char *ptr, size_t size, size_t nmemb, string *stream) {
int dataLength = size * nmemb;
stream->append(ptr, dataLength);
return dataLength;
}
string curlGet(string req, string token = "") {
// cout << "GET " << req << endl;
const int sz = 102400;
char buf[sz];
char cmdline[sz];
snprintf(cmdline, sz, "curl -s -H 'Authorization: %s' %s%s", token.c_str(), host.c_str(), req.c_str());
#ifdef _MSC_VER
FILE *fp = _popen(cmdline, "r");
#else
FILE *fp = popen(cmdline, "r");
#endif
fgets(buf, sz, fp);
string res(buf);
// cout << res << endl;
return res;
}
string curlPost(string req, string post_data, string bearer = "") {
// cout << "POST " << req << endl;
const int sz = 102400;
char buf[sz];
char cmdline[sz];
#ifdef _WIN64
for (int i = 0; i < post_data.size(); ++i) {
if (post_data[i] == '"') {
post_data.insert(i, "\\");
i++;
}
}
snprintf(cmdline, sz, "curl -s -X POST -H \"Authorization:Bearer %s\" -H \"Content-Type: application/json\" -d %s \"%s%s\"", bearer.c_str(), post_data.c_str(), host.c_str(), req.c_str());
#else
snprintf(cmdline, sz, "curl -s -X POST -H \"Authorization:Bearer %s\" -H \"Content-Type: application/json\" -d '%s' \"%s%s\"", bearer.c_str(), post_data.c_str(), host.c_str(), req.c_str());
#endif
#ifdef _MSC_VER
FILE *fp = _popen(cmdline, "r");
#else
FILE *fp = popen(cmdline, "r");
#endif
fgets(buf, sz, fp);
string res(buf);
// cout << res << endl;
return res;
}
string userShow(string identifier) {
string res = curlGet("/users/show/" + identifier);
cout << res << endl;
return res;
}
random_device seed_gen;
mt19937 engine(seed_gen());
int rnd(int n) {
return engine() % n;
}
KakomimasuClient::KakomimasuClient(string bearer) {
m_bearer = bearer;
}
bool KakomimasuClient::getGameInfo() {
string res = curlGet("/match/" + m_game_id);
picojson::value val;
picojson::parse(val, res);
picojson::object obj = val.get<picojson::object>();
m_gameInfo = obj;
if (!m_gameInfo["board"].is<picojson::null>()) {
m_board = m_gameInfo["board"].get<picojson::object>();
}
return m_gameInfo["gaming"].get<bool>();
}
void KakomimasuClient::waitMatching() {
picojson::object send_obj;
string res = curlPost("/match", picojson::value(send_obj).serialize(), m_bearer);
picojson::value val;
picojson::parse(val, res);
picojson::object obj = val.get<picojson::object>();
m_game_id = obj["gameId"].get<string>();
m_player_no = obj["index"].get<double>();
while (!getGameInfo()) {
this_thread::sleep_for(chrono::milliseconds(500));
}
cout << "maching!" << endl;
}
int KakomimasuClient::getWidth() {
return (int)m_board["width"].get<double>();
};
int KakomimasuClient::getHeight() {
return (int)m_board["height"].get<double>();
};
int KakomimasuClient::getAgentCount() {
return (int)m_board["nAgent"].get<double>();
};
int KakomimasuClient::getPlayerNumber() {
return m_player_no;
};
int KakomimasuClient::getNextTurnUnixTime() {
return (int)m_gameInfo["nextTurnUnixTime"].get<double>();
};
int KakomimasuClient::getTurn() {
return (int)m_gameInfo["turn"].get<double>();
};
int KakomimasuClient::getTotalTurn() {
return (int)m_gameInfo["totalTurn"].get<double>();
};
vector<vector<int>> KakomimasuClient::getPoints() {
int height = getHeight();
int width = getWidth();
vector<vector<int>> res(height, vector<int>(width));
picojson::array ary = m_board["points"].get<picojson::array>();
int i = 0, j = 0;
for (auto &val : ary) {
res[i][j] = val.get<double>();
j++;
if (j == width) j = 0, i++;
}
return res;
}
vector<vector<Tile>> KakomimasuClient::getFiled() {
int height = getHeight();
int width = getWidth();
vector<vector<Tile>> res(height, vector<Tile>(width));
picojson::array ary = m_board["points"].get<picojson::array>();
int i = 0, j = 0;
for (auto &val : ary) {
res[i][j].point = val.get<double>();
j++;
if (j == width) j = 0, i++;
}
i = j = 0;
ary = m_gameInfo["tiled"].get<picojson::array>();
for (auto &val : ary) {
int type, pid;
sscanf(val.serialize().c_str(), "[%d,%d]", &type, &pid);
res[i][j].type = type;
res[i][j].pid = pid;
j++;
if (j == width) j = 0, i++;
}
return res;
}
vector<Agent> KakomimasuClient::getAgent() {
vector<Agent> res;
picojson::array player = m_gameInfo["players"].get<picojson::array>();
picojson::object obj = player[m_player_no].get<picojson::object>();
picojson::array ary = obj["agents"].get<picojson::array>();
for (int i = 0; i < ary.size(); i++) {
picojson::object pos = ary[i].get<picojson::object>();
res.push_back({(int)pos["x"].get<double>(), (int)pos["y"].get<double>()});
}
return res;
};
vector<Agent> KakomimasuClient::getEnemyAgent() {
vector<Agent> res;
picojson::array player = m_gameInfo["players"].get<picojson::array>();
picojson::object obj = player[1 - m_player_no].get<picojson::object>();
picojson::array ary = obj["agents"].get<picojson::array>();
for (int i = 0; i < ary.size(); i++) {
picojson::object pos = ary[i].get<picojson::object>();
res.push_back({(int)pos["x"].get<double>(), (int)pos["y"].get<double>()});
}
return res;
};
void KakomimasuClient::waitNextTurn() {
int next_time = getNextTurnUnixTime();
while (time(NULL) < next_time) {
this_thread::sleep_for(chrono::milliseconds(100));
}
}
void KakomimasuClient::setAction(vector<Action> action) {
picojson::array arr;
for (const auto &[agentId, type, x, y] : action) {
picojson::object obj;
obj.emplace(make_pair("agentId", picojson::value((double)agentId)));
obj.emplace(make_pair("type", type));
obj.emplace(make_pair("x", picojson::value((double)x)));
obj.emplace(make_pair("y", picojson::value((double)y)));
arr.push_back(picojson::value(obj));
}
picojson::object send_obj;
send_obj.emplace(make_pair("actions", arr));
send_obj.emplace(make_pair("index", (double)m_player_no));
picojson::value val(send_obj);
string post_data = val.serialize();
cout << curlPost("/match/" + m_game_id + "/action", post_data, m_bearer) << endl;
};