-
Notifications
You must be signed in to change notification settings - Fork 3
/
utilities.d
280 lines (253 loc) · 5.6 KB
/
utilities.d
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Author: Ivan Kazmenko ([email protected])
module utilities;
import std.algorithm;
import std.conv;
import std.datetime;
import std.digest.sha;
import std.format;
import std.json;
import std.net.curl;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
import prospectorsc_abi;
import transaction;
auto getWithData (Conn) (string url, string [string] data, Conn conn)
{
return get (url ~ "?" ~ data.byKeyValue.map !(line =>
line.key ~ "=" ~ line.value).join ("&"), conn);
}
string maybeStr () (const auto ref JSONValue value)
{
if (value.isNull)
{
return "";
}
return value.str;
}
bool isResource (int id)
{
return 1 <= id && id <= 6 || id == 31 || id == 52;
}
bool isTool (int id)
{
return (17 <= id && id <= 24) || (32 <= id && id <= 39);
}
auto parseBinary (T) (ref ubyte [] buffer)
{
// debug {writeln (T.stringof);}
static if (is (Unqual !(T) == E [], E))
{
size_t len = 0;
size_t shift = 0;
while (true)
{
auto cur = parseBinary !(byte) (buffer);
// len = (len << 7) | (cur & 127);
len |= (cur & 127) << shift;
if (!(cur & 128))
{
break;
}
shift += 7;
}
// debug {writeln ("length = ", len);}
E [] res;
res.reserve (len);
foreach (i; 0..len)
{
res ~= parseBinary !(E) (buffer);
}
return res;
}
else static if (is (T == struct))
{
T res;
alias fieldNames = FieldNameTuple !(T);
alias fieldTypes = FieldTypeTuple !(T);
static foreach (i; 0..fieldNames.length)
{
mixin ("res." ~ fieldNames[i]) =
parseBinary !(fieldTypes[i]) (buffer);
}
return res;
}
else
{
enum len = T.sizeof;
T res = *(cast (T *) (buffer.ptr));
buffer = buffer[len..$];
return res;
}
}
alias hexStringToBinary = str => str.chunks (2).map !(value =>
to !(ubyte) (value, 16)).array;
alias ItemPlan = Tuple !(long, q{id}, string, q{name}, int, q{weight});
ItemPlan [] itemList;
short [string] itemIdByName;
void prepare ()
{
itemList = [ItemPlan.init] ~
File ("items.txt").byLineCopy.map !(split)
.map !(t => ItemPlan (t[0].to !(long), t[1], t[2].to !(int)))
.array;
foreach (ref item; itemList)
{
itemIdByName[item.name] = item.id.to !(short);
}
}
immutable int items = 75;
immutable int [] codeList;
immutable bool [int] codeBreaks;
shared static this ()
{
codeList = (iota (1, 7).array ~ 31 ~ 52 ~
iota (7, 17).array ~
iota (40, 50).array ~ 51 ~
iota (17, 25).array ~
iota (32, 40).array ~ 53 ~
iota (25, 31).array ~ 50 ~ iota (54, 56).array).idup;
codeBreaks = [52: true, 16: true, 51: true,
24: true, 53: true, 55: true];
}
struct Coord
{
int row;
int col;
this (long id)
{
row = cast (short) (id & 0xFFFF);
col = cast (short) (id >> 16);
}
static string numString (int value)
{
immutable int base = 10;
string res;
if (value < 0)
{
res ~= "-";
value = -value;
}
res ~= cast (char) (value / base + '0');
res ~= cast (char) (value % base + '0');
return res;
}
string toString () const
{
// as in the game: first column, then row
return numString (col) ~ "/" ~ numString (row);
}
}
int allowedSeconds;
long nowUnix;
shared static this ()
{
nowUnix = Clock.currTime (UTC ()).toUnixTime ();
try
{
auto f = File ("utilities_config.txt", "rt");
allowedSeconds = f.readln.strip.to !(int);
}
catch (Exception e)
{
allowedSeconds = 86400;
}
}
shared static this ()
{
try
{
auto f = File ("error.txt", "rt");
}
catch (Exception e)
{
return;
}
throw new Exception ("error.txt is present");
}
void updateLogGeneric (alias doSpecific)
(string endPoint, string queryForm, string query)
{
string dfuseToken;
try
{
dfuseToken = File ("../dfuse.token").readln.strip;
}
catch (Exception e)
{
dfuseToken = "";
}
auto sha256 = query.sha256Of.format !("%(%02x%)");
immutable string cursorFileName = sha256 ~ ".cursor";
string wideCursor;
try
{
wideCursor = File (cursorFileName).readln.strip;
}
catch (Exception e)
{
wideCursor = "";
}
auto connection = HTTP ();
// connection.verbose (true);
connection.addRequestHeader ("content-type", "text/plain");
stderr.writeln ("dfuse: ", dfuseToken);
if (dfuseToken != "")
{
connection.addRequestHeader ("Authorization",
"Bearer " ~ dfuseToken);
}
auto logFile = File (sha256 ~ ".log", "ab");
while (true)
{
auto filledQuery = format (queryForm, query, wideCursor);
writeln ("updating ", query, ", cursor = ", wideCursor);
debug {writeln (filledQuery);}
auto raw = post (endPoint, filledQuery, connection);
debug {writeln (raw);}
auto cur = raw.parseJSON["data"]["searchTransactionsForward"];
auto newCursor = cur["cursor"].maybeStr;
if (newCursor == "")
{
writeln (query, " update complete");
break;
}
auto oldCursor = wideCursor;
wideCursor = newCursor;
string [] res;
foreach (const ref result; cur["results"].array)
{
auto curCursor = result["cursor"].maybeStr;
if (result["trace"]["receipt"]["status"].maybeStr !=
"EXECUTED")
{
assert (false);
}
auto ts1 = result["trace"]["block"]["timestamp"]
.maybeStr;
auto ts2 = SysTime.fromISOExtString (ts1, UTC ());
auto ts3 = ts2.toSimpleString;
auto timestamp = ts3[0..20];
auto curUnix = ts2.toUnixTime ();
if (nowUnix - curUnix > allowedSeconds)
{
auto f = File ("error.txt", "wt");
f.writeln (nowUnix);
f.writeln (curUnix);
f.writeln (oldCursor);
f.writeln (curCursor);
throw new Exception ("error.txt generated");
}
doSpecific (res, result["trace"],
timestamp, curCursor);
}
foreach (const ref line; res)
{
logFile.writeln (line);
logFile.flush ();
}
File (cursorFileName, "wb").writeln (wideCursor);
}
}