-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mod.lua
142 lines (122 loc) · 3.87 KB
/
server.mod.lua
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
local groups = {}
local module = {}
local setmetatable = setmetatable
local error = error
local wait = wait
local httpService = game:GetService'HttpService'
local postAsync = httpService.PostAsync
local getAsync = httpService.GetAsync
local jsonEncode = httpService.JSONEncode
local jsonDecode = httpService.JSONDecode
local base, key
local function encode (tab)
return jsonEncode(httpService, tab)
end
local function decode (tab)
return jsonDecode(httpService, tab)
end
local function request (url, method, suppress, data)
local body = method(httpService, url, data)
local success, response = pcall(decode, body)
local err
if success then
err = response.error
else
err = 'Response was not valid json, full body: '..body
end
if not err and suppress then
return true, response
elseif not err then
return response
elseif suppress then
return false, err
else
error(err)
end
end
local http = {
post = function (path, data, suppress)
if not data then
data = {}
end
data.key = key
return request(base..path, postAsync, suppress, encode(data))
end,
get = function (path, suppress)
return request(base..path, getAsync, suppress)
end
}
function groups.promote (group, userId)
return http.post('/promote/'..group..'/'..userId)
end
function groups.demote (group, userId)
return http.post('/demote/'..group..'/'..userId)
end
function groups.setRank (group, userId, rank)
return http.post('/setRank/'..group..'/'..userId..'/'..rank)
end
function groups.shout (group, message)
return http.post('/shout/'..group, {message = message})
end
function groups.post (group, message)
return http.post('/post/'..group, {message = message})
end
function groups.handleJoinRequest (group, username, accept)
local acceptString = accept and 'true' or 'false'
return http.post('/handleJoinRequest/'..group..'/'..username..'/'..acceptString)
end
function groups.getPlayers (group, rank, limit, online)
local job = http.post('/getPlayers/make/'..group..(rank and '/'..rank or '')..'?limit='..(limit and limit or '-2')..'&online='..(online and 'true' or 'false')).data.uid
local complete, response = false
repeat
wait(0.5)
local success
success, response = http.get('/getPlayers/retrieve/'..job, true)
if not success and response:match('$Response was not valid json') then
error(response)
elseif success then
complete = response.data.complete
end
until complete
return response
end
function module.message (userId, subject, message)
return http.post('/message/'..userId, {subject = subject, body = message})
end
function module.forumPostNew (forumId, subject, body, locked)
return http.post('/forumPost/new/'..forumId..'?locked='..(locked and 'true' or 'false'), {subject = subject, body = body})
end
function module.forumPostReply (postId, body, locked)
return http.post('/forumPost/reply/'..postId..'?locked='..(locked and 'true' or 'false'), {body = body})
end
function module.getBlurb (userId)
return http.get('/getBlurb/'..userId)
end
return function (domain, newKey, group)
local isString = (type(domain) == 'string')
if (not domain) or (not isString) or (isString and #domain <= 0) then
error('Url is required and must be a string greater than length 0')
end
isString = (type(newKey) == 'string')
if (not newKey) or (not isString) or (isString and #newKey <= 0) then
error('Key is required and must be a string greater than length 0')
end
base = 'http://'..domain
key = newKey
if group then
local isNumber = (type(group) == 'number')
if (not isNumber) or (group <= 0) then
error('If group is provided it must be a number greater than 0')
end
for name, func in next, groups do
module[name] = function (...)
return func(group, ...)
end
end
return module
end
for name, func in next, groups do
module[name] = func
end
return module
end