-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gm48_leaderboards_library.gml
294 lines (215 loc) · 7.87 KB
/
gm48_leaderboards_library.gml
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* --------------------------------
Library of scripts to utilize gm48.net Leaderboards in GameMaker Studio 2 v2.3.0 or newer
@see https://github.com/tehwave/gm48.net-leaderboards-gms2
This library requires the gm48.net OAuth2 for GameMaker Studio 2 library to function.
@see https://github.com/tehwave/gm48.net-oauth2-gms2
-------------------------------- */
function gm48_leaderboards_init(callback)
{
// Verify that OAuth2 library exists.
if (! asset_get_index("gm48_oauth2_init") && ! debug_mode) {
show_error("gm48.net-leaderboards-gms2: OAuth2 Library is required.", true);
}
// Prepare global variables.
gm48_leaderboards_globals();
if (! is_undefined(callback)) {
global.gm48_leaderboards_callback = argument0;
}
// Prepare macros.
gm48_leaderboards_macros();
// All ready.
gm48_debug("Leaderboards functionality initialized.");
}
function gm48_leaderboards_macros()
{
#macro GM48_LEADERBOARDS_USERAGENT "gamemaker:" + game_display_name + ":" + GM_version
#macro GM48_LEADERBOARDS_API_URL "https://gm48.net/api/v4/"
}
function gm48_leaderboards_globals()
{
global.gm48_leaderboards_callback = -1;
global.gm48_leaderboards_requests = ds_map_create();
if (! variable_global_exists("gm48_game_api_token")) {
global.gm48_game_api_token = -1;
}
}
/* --------------------------------
API
-------------------------------- */
function gm48_leaderboards_add_score(leaderboardId, scoreToSubmit, meta, callback)
{
if (! gm48_isset_oauth2_access_token()) {
show_error("gm48.net-leaderboards-gms2: OAuth2 Access token is required.", true);
}
if (! gm48_isset_game_api_token()) {
show_error("gm48.net-leaderboards-gms2: Game API Token is required.", true);
}
// Put together request.
var url = GM48_LEADERBOARDS_API_URL + "leaderboards/" + string(leaderboardId) + "/scores";
var headers = ds_map_create();
headers[? "User-Agent"] = GM48_LEADERBOARDS_USERAGENT;
headers[? "Authorization"] = "Bearer " + gm48_get_oauth2_access_token();
headers[? "Content-Type"] = "application/x-www-form-urlencoded";
headers[? "Accept"] = "application/json";
headers[? "Game-Token"] = gm48_get_game_api_token();
// Set the score as the value. When it's a double, we use json_stringify to preserve
// data precision and allow better comparison between scores.
var body = "value=" + string(scoreToSubmit);
if (is_real(scoreToSubmit) && (floor(scoreToSubmit) != scoreToSubmit)) {
body = "value=" + json_stringify(scoreToSubmit);
}
// Third argument is "meta" which allows you to send additional data along with the score.
// This includes support for both ds_map and structs.
if (! is_undefined(meta)) {
if (is_struct(meta)) {
body += "&meta=" + json_stringify(meta);
} else if (is_real(meta) && ds_exists(meta, ds_type_map)) {
body += "&meta=" + string(json_encode(meta));
// Free memory.
ds_map_destroy(meta);
} else {
show_error("gm48.net-leaderboards-gms2: Meta data is wrong data type. It must be a struct or ds_map.", false);
}
}
// Send request.
var requestId = http_request(url, "POST", headers, body);
// Save request.
var request = ds_map_create();
request[? "url"] = url;
request[? "headers"] = headers;
request[? "body"] = body;
request[? "method"] = "POST";
if (! is_undefined(callback)) {
request[? "callback"] = callback;
}
gm48_add_leaderboards_request(requestId, request);
// Log it.
gm48_debug("Request sent to Leaderboards API", url, body);
// Free memory.
ds_map_destroy(headers);
return requestId;
}
function gm48_leaderboards_get_my_scores(leaderboardId, callback)
{
if (! gm48_isset_oauth2_access_token()) {
show_error("gm48.net-leaderboards-gms2: OAuth2 Access token is required.", true);
}
if (! gm48_isset_game_api_token()) {
show_error("gm48.net-leaderboards-gms2: Game API Token is required.", true);
}
// Put together request.
var url = GM48_LEADERBOARDS_API_URL + "leaderboards/" + string(leaderboardId) + "/me";
var headers = ds_map_create();
headers[? "User-Agent"] = GM48_LEADERBOARDS_USERAGENT;
headers[? "Authorization"] = "Bearer " + gm48_get_oauth2_access_token();
headers[? "Accept"] = "application/json";
headers[? "Game-Token"] = gm48_get_game_api_token();
// Send request.
var requestId = http_request(url, "GET", headers, "");
// Save request.
var request = ds_map_create();
request[? "url"] = url;
request[? "headers"] = headers;
request[? "body"] = undefined;
request[? "method"] = "GET";
if (! is_undefined(callback)) {
request[? "callback"] = argument[1];
}
gm48_add_leaderboards_request(requestId, request);
// Log it.
gm48_debug("Request sent to Leaderboards API", url);
// Free memory.
ds_map_destroy(headers);
return requestId;
}
function gm48_leaderboards_get_all_scores(leaderboardId, callback)
{
if (! gm48_isset_oauth2_access_token()) {
show_error("gm48.net-leaderboards-gms2: OAuth2 Access token is required.", true);
}
if (! gm48_isset_game_api_token()) {
show_error("gm48.net-leaderboards-gms2: Game API Token is required.", true);
}
// Put together request.
var url = GM48_LEADERBOARDS_API_URL + "leaderboards/" + string(leaderboardId);
var headers = ds_map_create();
headers[? "User-Agent"] = GM48_LEADERBOARDS_USERAGENT;
headers[? "Authorization"] = "Bearer " + gm48_get_oauth2_access_token();
headers[? "Accept"] = "application/json";
headers[? "Game-Token"] = gm48_get_game_api_token();
// Send request.
var requestId = http_request(url, "GET", headers, "");
// Save request.
var request = ds_map_create();
request[? "url"] = url;
request[? "headers"] = headers;
request[? "body"] = undefined;
request[? "method"] = "GET";
if (! is_undefined(callback)) {
request[? "callback"] = argument[1];
}
gm48_add_leaderboards_request(requestId, request);
// Log it.
gm48_debug("Request sent to Leaderboards API", url);
// Free memory.
ds_map_destroy(headers);
return requestId;
}
/* --------------------------------
Async
-------------------------------- */
function gm48_leaderboards_http()
{
// Validate that the request is one of ours.
var requestId = async_load[? "id"];
if (! ds_exists(global.gm48_leaderboards_requests, ds_type_map)) {
show_error("gm48.net-leaderboards-gms2: Requests ds_map doesn't exist.", true);
}
if (ds_map_size(global.gm48_leaderboards_requests) == 0) {
gm48_debug("HTTP request is not of Leaderboards variant.", requestId);
return;
}
var request = gm48_get_leaderboards_request(requestId);
if (is_undefined(request)) {
gm48_debug("HTTP request is not of Leaderboards variant.", requestId);
return;
}
// Retrieve our remaining data.
var httpStatusCode = async_load[? "http_status"];
var status = async_load[? "status"];
var result = async_load[? "result"];
// Validate response.
if (status < 0) {
gm48_debug("Something went wrong with request.", httpStatusCode, status, result);
return;
}
if (status = 1) {
gm48_debug("Content is being downloaded.", httpStatusCode, status, result);
return;
}
if (httpStatusCode < 200 || httpStatusCode >= 300) {
gm48_debug("Non-successful response.", httpStatusCode, status, result);
return;
}
// Process result.
var decodedResult = json_parse(result);
gm48_debug("Successful response received.", httpStatusCode, result, decodedResult);
// Execute callbacks.
if (! is_undefined(request[? "callback"]) && script_exists(request[? "callback"])) {
script_execute(request[? "callback"], decodedResult, requestId);
}
if (script_exists(global.gm48_leaderboards_callback)) {
script_execute(global.gm48_leaderboards_callback, decodedResult, requestId);
}
}
/* --------------------------------
Local helper scripts.
-------------------------------- */
function gm48_add_leaderboards_request(requestId, request)
{
return ds_map_add(global.gm48_leaderboards_requests, requestId, request);
}
function gm48_get_leaderboards_request(requestId)
{
return ds_map_find_value(global.gm48_leaderboards_requests, requestId);
}