-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamejolt.lua
More file actions
107 lines (90 loc) · 3.35 KB
/
gamejolt.lua
File metadata and controls
107 lines (90 loc) · 3.35 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
GameJolt = function (game_id, private_key)
local username, user_token
local http = require("socket.http")
local ltn12 = require("ltn12")
local md5 = require("vendor/md5")
local base_url = "http://gamejolt.com/api/game/v1"
function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
local http_request = function ( args )
local resp, r = {}, {}
if args.endpoint then
local params = ""
if args.method == nil or args.method == "GET" then
-- prepare query parameters like http://xyz.com?q=23&a=2
if args.params then
for i, v in pairs(args.params) do
params = params .. i .. "=" .. v .. "&"
end
end
end
params = string.sub(params, 1, -2)
local url = ""
if params then url = base_url .. args.endpoint .. "?" .. params else url = base_url .. args.endpoint end
local signature = md5.sumhexa(url .. private_key)
url = url .. "&signature=" .. signature
client, code, headers, status = http.request{url=url, sink=ltn12.sink.table(resp),
method=args.method or "GET", headers=args.headers, source=args.source,
step=args.step, proxy=args.proxy, redirect=args.redirect, create=args.create }
r['code'], r['headers'], r['status'], r['response'] = code, headers, status, resp
else
error("endpoint is missing")
end
return r
end
local authenticate = function ()
print("AUTHENTICATING WITH GAMEJOLT")
local result
local response = http_request({
endpoint = "/users/auth/",
params = {
game_id = game_id,
username = username,
user_token = user_token
}
}).response
local response_data = unpack(response)
if response_data ~= nil then
result = string.find(unpack(response), 'success:"true"') -- TODO ha ha ha, until I find a JSON parser I like
if result then
print(" YEAH, YOU'RE GOOD")
else
print(" NOPE NO CAN DO")
end
else
print(" FAILED TO DO THAT OK")
end
return result
end
local add_score = function (score, sort)
if not authenticate() then return end
print("UPLOADING SCORE...")
return http_request({
endpoint = "/scores/add/",
params = {
game_id = game_id,
username = username,
user_token = user_token,
score = urlencode(score),
sort = sort
}
})
end
local connect_user = function (name, token)
username = name
user_token = token
end
return {
http_request = http_request,
connect_user = connect_user,
add_score = add_score
}
end
return GameJolt