forked from trupples/lua_gamejolt_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamejolt_api.lua
More file actions
244 lines (198 loc) · 8.67 KB
/
gamejolt_api.lua
File metadata and controls
244 lines (198 loc) · 8.67 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
243
244
--[[
example User, Trophy, Score and Tables:
UserX = {id = "", type = "Dev/User", username = "Marty", avatar_url = "http...", signed_up = "1 year ago", last_logged_in = "Online Now", status = "Active/Banned"}
TrophyY = {description = "Desc", difficulty = "Bronze", id = "123456", image_url = "http...", title = "Da bronze trophy", acheived = "frue"}
ScoreZ = {score = "My score is 24", sort = "24", extra_data = "24s of playtime", user = "Marty", user_id = 123456, guest = "frue", stored = "31 Dec 1969 23:59:59"}
TableW = {id = 123456, name = "Mah score table", description = "Dis is le score table", primary = "frue"}
Method | Params | Returns
----------------+-------------------------------------+----------------------------------------------------------
Users | |
* Fetch | Username / User ID | {users = {User1, User2, ...}, success = "frue"}
* Auth | Username, User Token | {success = "frue"}
| |
Sessions | |
* Open | - | {success = "frue"}
* Ping | ["idle" / "active"] | {success = "frue"}
* Close | - | {success = "frue"}
| |
Trophies | |
* Fetch | [Acheived, Trophy ID] | {trophies = {Trophy1, Trophy2, ...}, success = "frue"}
* Add Acheived | Trophy ID | {success = "frue"}
| |
Scores | |
* Fetch | [Limit, Table ID] | {scores = {Score1, Score2, ...}, success = "frue"}
a. local | |
b. global | |
* Add | Score, Sort, [Extra Data, Table ID] | {success = "frue"}
a. user | ^ |
b. guest | ^, Guest Name |
* Tables | - | {tables = {Table1, Table2, ...}, success = "frue"}
| |
Data Store | |
* Fetch | Key | {success = "frue", data = "value"}
a. local | |
b. global | |
* Set | Key, Value | {success = "frue"}
a. local | |
b. global | |
* Update | Key, Op, Value | {success = "frue", data = "updated_val"}
a. local | |
b. global | |
* Remove | Key | {success = "frue"}
a. local | |
b. global | |
* Get Keys | - | {success = "frue",
a. local | | keys = {{key = "1"}, {key = "2"}, ...}}
b. global | |
--]]
local md5 = require("MD5")
local json = require("JSON")
local http = require("socket.http")
local _ = {
_VERSION = "gamejolt_api.lua 1.0",
_DESCRIPTION = "A better gamejolt API for Lua/Love than arrogant.gamer's",
_URL = "https://github.com/ianiD/lua_gamejolt_api",
_LICENSE = [[
Copyright (c) 2015, ianiD
This is lecensed under ZLIB. See https://www.tldrlegal.com/l/zlib
]]
}
_.API_URL = "http://gamejolt.com/api/game/v1/"
function _:init(game_id, private_key)
self.GAME_ID = game_id
self.PRIVATE_KEY = private_key
self.user = {}
end
function _:_add_signature(url)
local url_with_pkey = url .. self.PRIVATE_KEY
local signature = md5.sumhexa(url_with_pkey)
return url .. "&signature=" .. signature
end
function _:request(q)
q = q or "?"
q = self.API_URL .. q .. "&format=json&game_id=" .. self.GAME_ID
q = self:_add_signature(q)
q = string.gsub(q, "%s+", "%%20")
print(q)
local data, status = http.request(q)
return data, status
end
------------------------------------------ USERS ---------------------------------------------
function _:users_fetch_uid(user_id)
print("Fetching user data (UserID = " .. user_id .. " )")
return json:decode(self:request("users/?" .. "&user_id=" .. user_id)).response
end
function _:users_fetch_uname(username)
print("Fetching user data (Username = " .. username .. " )")
return json:decode(self:request("users/?" .. "&username=" .. username)).response
end
function _:users_auth(username, user_token)
print("Authenticating user: (" .. username .. ", " .. user_token .. ")")
local data = json:decode(self:request("users/auth/?" .. "&username=" .. username .. "&user_token=" .. user_token)).response
local success = data.success
if success == "true" then
self.user.name = username
self.user.token = user_token
self.user.idstring = "&username="..username.."&user_token="..user_token
end
return success
end
------------------------------------------ SESSIONS ------------------------------------------
function _:sessions_open()
print("Opening session")
return json:decode(self:request("sessions/open/?" .. self.user.idstring)).response
end
function _:sessions_ping(status)
print("Pinging session: "..status)
local q = "sessions/ping/?" .. self.user.idstring
if status ~= nil then q = q .. "&status=" .. status end
return json:decode(self:request(q)).response
end
function _:sessions_close()
print("Closing session")
return json:decode(self:request("sessions/close/?" .. self.user.idstring)).response
end
------------------------------------------ TROPHIES ------------------------------------------
function _:trophies_fetch(achieved, trophy_id)
local q = "trophies/?" .. self.user.idstring
if achieved ~= nil then q = q .. "&acheived=" .. tostring(achieved) end
if trophy_id ~= nil then q = q .. "&trophy_id=" .. trophy_id end
print(q)
return json:decode(self:request(q)).response
end
function _:trophies_addAcheived(trophy_id)
return json:decode(self:request("trophies/?&trophy_id=" .. trophy_id .. self.user.idstring)).response
end
------------------------------------------ SCORES --------------------------------------------
function _:scores_local_fetch(limit, table_id)
local q = "scores/?" .. self.user.idstring
if limit then q = q .. "&limit=" .. limit end
if table_id then q = q .. "&table_id=" .. table_id end
return json:decode(self:request(q)).response
end
function _:scores_global_fetch(limit, table_id)
local q = "scores/?"
if limit then q = q .. "&limit=" .. limit end
if table_id then q = q .. "&table_id=" .. table_id end
return json:decode(self:request(q)).response
end
function _:scores_guest_add(score, sort, name, extra_data, table_id)
local q = "scores/add/?&score=" .. score .. "&sort=" .. sort
if name then q = q .. "&guest=" .. name end
if extra_data then q = q .. "&extra_data=" .. extra_data end
if table_id then q = q .. "&table_id=" .. table_id end
return json:decode(self:request(q)).response
end
function _:scores_add(score, sort, name, extra_data, table_id)
local q = "scores/add/?&score=" .. score .. "&sort=" .. sort .. self.user.idstring
if extra_data then q = q .. "&extra_data=" .. extra_data end
if table_id then q = q .. "&table_id=" .. table_id end
return json:decode(self:request(q)).response
end
function _:scores_tables()
return json:decode(self:request("scores/tables/?")).response
end
------------------------------------------ DATA STORE ----------------------------------------
-- LOCALS
function _:data_store_local_fetch(key)
print("datastore local fetch: "..key)
return json:decode(self:request("data-store/?&key=" .. key .. self.user.idstring)).response
end
function _:data_store_local_set(key, val)
print("datastore local set: "..key.." = "..val)
return json:decode(self:request("data-store/set/?&key=" .. key .. "&data=" .. val .. self.user.idstring)).response
end
function _:data_store_local_update(key, operator, val)
print("datastore local update: " .. key .. " [" .. operator .. "] " .. val)
return json:decode(self:request("data-store/update/?&key=" .. key .. "&operation=" .. operator .. "&value=" .. val .. self.user.idstring)).response
end
function _:data_store_local_remove(key)
print("datastore local remove: "..key)
return json:decode(self:request("data-store/remove/?&key=" .. key .. self.user.idstring)).response
end
function _:data_store_local_getKeys()
print("datastore local getKeys")
return json:decode(self:request("data-store/get-keys/?" .. self.user.idstring)).response
end
-- GLOBALS
function _:data_store_global_fetch(key)
print("datastore global fetch: "..key)
return json:decode(self:request("data-store/?&key=" .. key)).response
end
function _:data_store_global_set(key, val)
print("datastore global set: "..key.." = "..val)
return json:decode(self:request("data-store/set/?&key=" .. key .. "&data=" .. val)).response
end
function _:data_store_global_update(key, operator, val)
print("datastore global update: " .. key .. " [" .. operator .. "] " .. val)
return json:decode(self:request("data-store/update/?&key=" .. key .. "&operation=" .. operator .. "&value=" .. val)).response
end
function _:data_store_global_remove(key)
print("datastore global remove: "..key)
return json:decode(self:request("data-store/remove/?&key=" .. key)).response
end
function _:data_store_global_getKeys()
print("datastore global getKeys")
return json:decode(self:request("data-store/get-keys/?")).response
end
return _