-
Notifications
You must be signed in to change notification settings - Fork 2
/
warehouse.inc
252 lines (202 loc) · 6.5 KB
/
warehouse.inc
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
// Provides helper functions for working with the API via Pawn.
#include <a_samp>
#include <errors>
#include <requests>
#include <map>
// OnWarehouseIndex is called after WarehouseIndex finishes
forward OnWarehouseIndex(bool:success, message[], Error:error, Node:result);
// OnWarehousePlayerCreate is called after WarehousePlayerCreate finishes
forward OnWarehousePlayerCreate(playerid, bool:success, message[], Error:error, Node:result);
// OnWarehousePlayerGet is called after WarehousePlayerGetBy(Name|ID) finishes
forward OnWarehousePlayerGet(playerid, bool:success, message[], Error:error, Node:result);
// OnWarehousePlayerUpdate is called after WarehousePlayerUpdate finishes
forward OnWarehousePlayerUpdate(playerid, bool:success, message[], Error:error, Node:result);
// ParseStatus extracts success and message from Warehouse success responses:
//
// {
// "result": {...},
// "success": false,
// "message": "error message"
// }
//
stock Error:ParseStatus(Node:node, &bool:success, &Node:result, message[], len = sizeof message) {
new ret;
ret = JsonGetBool(node, "success", success);
if(ret) {
return Error(1, "failed to access key 'success' in status node");
}
ret = JsonGetString(node, "message", message, len);
// if(ret) {
// err("failed to access key 'message' in status node");
// return ret;
// }
ret = JsonGetObject(node, "result", result);
if(ret) {
result = Node:-1;
}
return NoError();
}
// -
// Interface: /
// -
forward _warehouse_onIndex(Request:id, E_HTTP_STATUS:status, Node:node);
stock Error:WarehouseIndex(RequestsClient:client) {
new Request:id = RequestJSON(
client,
"/",
HTTP_METHOD_GET,
"_warehouse_onIndex"
);
if(!IsValidRequest(id)) {
return Error(1, "failed to send playerCreate request");
}
return NoError();
}
public _warehouse_onIndex(Request:id, E_HTTP_STATUS:status, Node:node) {
new bool:success;
new Node:result;
new message[128];
new Error:e;
if(status == HTTP_STATUS_OK) {
e = ParseStatus(node, success, result, message);
if(IsError(e)) {
e = Error(1, "failed to parse status");
}
} else {
e = Error(1, "status was not OK");
}
if(message[0] == 0) {
message[0] = '\1';
}
JsonToggleGC(result, false);
CallLocalFunction("OnWarehouseIndex", "dsdd", success, message, _:e, _:result);
JsonToggleGC(result, true);
return;
}
// -
// Interface: /store/playerCreate
// -
static Map:playerCreatePlayerID;
forward _warehouse_onPlayerCreate(Request:id, E_HTTP_STATUS:status, Node:node);
stock Error:WarehousePlayerCreate(RequestsClient:client, playerid, Node:node) {
new Request:id = RequestJSON(
client,
"/store/playerCreate",
HTTP_METHOD_POST,
"_warehouse_onPlayerCreate",
node
);
if(!IsValidRequest(id)) {
return Error(1, "failed to send playerCreate request");
}
MAP_insert_val_val(playerCreatePlayerID, _:id, playerid);
return NoError();
}
public _warehouse_onPlayerCreate(Request:id, E_HTTP_STATUS:status, Node:node) {
new playerid = MAP_get_val_val(playerCreatePlayerID, _:id);
MAP_remove_val(playerCreatePlayerID, _:id);
new bool:success;
new Node:result;
new message[128];
new Error:e;
if(status == HTTP_STATUS_OK) {
e = ParseStatus(node, success, result, message);
if(IsError(e)) {
e = Error(1, "failed to parse status");
}
} else {
e = Error(1, "status was not OK");
}
if(message[0] == 0) {
message[0] = '\1';
}
JsonToggleGC(result, false);
CallLocalFunction("OnWarehousePlayerCreate", "ddsdd", playerid, success, message, _:e, _:result);
JsonToggleGC(result, true);
return;
}
// -
// Interface: /store/playerGet
// -
static Map:playerGetPlayerID;
forward _warehouse_onPlayerGet(Request:id, E_HTTP_STATUS:status, Node:node);
stock Error:WarehousePlayerGetByName(RequestsClient:client, playerid, name[]) {
new path[128];
format(path, sizeof path, "/store/playerGet?name=%s", name);
new Request:id = RequestJSON(
client,
path,
HTTP_METHOD_GET,
"_warehouse_onPlayerGet"
);
if(!IsValidRequest(id)) {
return Error(1, "failed to send playerGet request");
}
MAP_insert_val_val(playerGetPlayerID, _:id, playerid);
return NoError();
}
public _warehouse_onPlayerGet(Request:id, E_HTTP_STATUS:status, Node:node) {
new playerid = MAP_get_val_val(playerGetPlayerID, _:id);
MAP_remove_val(playerGetPlayerID, _:id);
new bool:success;
new Node:result;
new message[128];
new Error:e;
if(status == HTTP_STATUS_OK) {
e = ParseStatus(node, success, result, message);
if(IsError(e)) {
e = Error(1, "failed to parse status");
}
} else {
e = Error(1, "status was not OK");
}
if(message[0] == 0) {
message[0] = '\1';
}
JsonToggleGC(result, false);
CallLocalFunction("OnWarehousePlayerGet", "ddsdd", playerid, success, message, _:e, _:result);
JsonToggleGC(result, true);
return;
}
// -
// Interface: /store/playerUpdate
// -
static Map:playerUpdatePlayerID;
forward _warehouse_onPlayerUpdate(Request:id, E_HTTP_STATUS:status, Node:node);
stock Error:WarehousePlayerUpdate(RequestsClient:client, playerid, Node:node) {
new Request:id = RequestJSON(
client,
"/store/playerUpdate",
HTTP_METHOD_PATCH,
"_warehouse_onPlayerUpdate",
node
);
if(!IsValidRequest(id)) {
return Error(1, "failed to send playerUpdate request");
}
// Same map strategy as with the loading data process
MAP_insert_val_val(playerUpdatePlayerID, _:id, playerid);
return NoError();
}
public _warehouse_onPlayerUpdate(Request:id, E_HTTP_STATUS:status, Node:node) {
new playerid = MAP_get_val_val(playerGetPlayerID, _:id);
MAP_remove_val(playerGetPlayerID, _:id);
new bool:success;
new Node:result;
new message[128];
new Error:e;
if(status == HTTP_STATUS_OK) {
e = ParseStatus(node, success, result, message);
if(IsError(e)) {
e = Error(1, "failed to parse status");
}
} else {
e = Error(1, "status was not OK");
}
if(message[0] == 0) {
message[0] = '\1';
}
JsonToggleGC(result, false);
CallLocalFunction("OnWarehousePlayerUpdate", "ddsdd", playerid, success, message, _:e, _:result);
JsonToggleGC(result, true);
}