-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseLib.lua
336 lines (314 loc) · 10.9 KB
/
ParseLib.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
-- ParseLib V1.1.1
-- https://github.com/lsouchet/gideros-parse-rest
--Copyright (c) 2015, Lucas Souchet (https://github.com/lsouchet)
--All rights reserved.
--
--Redistribution and use in source and binary forms, with or without
--modification, are permitted provided that the following conditions are met:
--
--1. Redistributions of source code must retain the above copyright notice, this
-- list of conditions and the following disclaimer.
--2. Redistributions in binary form must reproduce the above copyright notice,
-- this list of conditions and the following disclaimer in the documentation
-- and/or other materials provided with the distribution.
--
--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
--ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
--WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
--DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
--ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
--(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
--LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
--ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
--(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--
--The views and conclusions contained in the software and documentation are those
--of the authors and should not be interpreted as representing official policies,
--either expressed or implied, of the FreeBSD Project.
ParseLib = Core.class()
ParseLibDebug = false
function ParseLib:init(appId, apiKey, scoreTable, playerTable)
self.version = "1.1.1"
if scoreTable then
self.scoreClass = scoreTable
else
self.scoreClass = "Score"
end
if playerTable then
self.playerClass = playerTable
else
self.playerClass = "Player"
end
self.baseUrl = "https://api.parse.com/1/classes/"
self.urls = {score = self.baseUrl..self.scoreClass, player = self.baseUrl..self.playerClass}
self.headerAppId = {name = "X-Parse-Application-Id", key = appId}
self.headerApiKey = {name = "X-Parse-REST-API-Key", key = apiKey}
self.headerJson = {name = "Content-Type", key = "application/json"}
self:resetUserData()
end
function debug(source, message)
if ParseLibDebug then
print(source..": ", message)
end
end
function ParseLib:resetUserData()
self.userObjectId = nil
self.currentFacebookId = nil
end
function ParseLib:getPostHeader()
return {[self.headerAppId["name"]] = self.headerAppId["key"],
[self.headerApiKey["name"]] = self.headerApiKey["key"],
[self.headerJson["name"]] = self.headerJson["key"]}
end
function ParseLib:getGETHeader()
return {[self.headerAppId["name"]] = self.headerAppId["key"],
[self.headerApiKey["name"]] = self.headerApiKey["key"]}
end
onLoginComplete = function(request, event)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
local user = json.decode(event.data)
if user.results == nil then
if request.userRequest and callback then
callback(false, nil, params)
end
return
end
if #user.results < 1 then
if (request.self.createPlayer == nil) then
if callback then
callback(false, nil, params)
else
debug("ParseLib.onLoginComplete", "Unable to create player")
end
return
end
request.self.createPlayer(request.self, request.userRequest["FacebookId"],
request.userRequest["Callback"])
return
end
if user.results[1].objectId == nil then
if callback then
callback(false, nil, params)
else
debug("ParseLib.onLoginComplete","No object id")
end
return
end
if user.results[1].facebookId == nil then
if callback then
callback(false, nil, params)
else
debug("ParseLib.onLoginComplete","No object id")
end
return
end
request.self.userObjectId = user.results[1].objectId
request.self.currentFacebookId = user.results[1].facebookId
if callback then
callback(true, user.results[1], params)
end
end
onLoginError = function(request)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
debug("ParseLib.onLoginError","Unable to connect")
if callback then
callback(false, nil, params)
end
end
-- Get user id from facebook ID. Callback(bool, user id)
function ParseLib:login(facebookId, callback, params)
self:resetUserData()
local headers = self:getGETHeader()
local body = '?where={"facebookId":"'..facebookId..'"}'
local loader = UrlLoader.new(self.urls["player"]..body, UrlLoader.GET, headers, '')
local request = {}
request.userRequest = {}
request.userRequest["Callback"] = callback
request.userRequest["Param"] = params
request.userRequest["FacebookId"] = facebookId
request.self = self
loader:addEventListener(Event.COMPLETE, onLoginComplete, request)
loader:addEventListener(Event.ERROR, onLoginError, request)
end
onCreateComplete = function(request, event)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
local user = json.decode(event.data)
if user.createdAt == nil or user.objectId == nil then
if callback then
callback(false, nil, params)
else
debug("ParseLib.onCreateComplete", event.data)
end
return
end
request.self.userObjectId = user.objectId
request.self.currentFacebookId = request.userRequest["FacebookId"]
if callback then
callback(true, user, params)
end
end
onCreateError = function(request)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
if (callback) then
callback(false, nil, params)
else
debug("ParseLib.onCreateError","")
end
end
-- Add player to database with username and password.
-- Callback(bool, user) is called when query ends.
function ParseLib:createPlayer(facebookId, callback, params)
self:resetUserData()
local headers = self:getPostHeader()
local body = '{"facebookId":"'..facebookId..'"}'
local loader = UrlLoader.new(self.urls["player"], UrlLoader.POST, headers, body)
local request = {}
request.userRequest = {}
request.userRequest["Callback"] = callback
request.userRequest["Param"] = params
request.userRequest["FacebookId"] = facebookId
request.self = self
loader:addEventListener(Event.COMPLETE, onCreateComplete, request)
loader:addEventListener(Event.ERROR, onCreateError, request)
end
onGetScoreComplete = function(request, event) -- tab = {self,player}
--print("ParseLib.onGetScoreComplete", event.data)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
local scores = json.decode(event.data)
debug("ParseLib.onGetScoreComplete",event.data)
if scores == nil or scores.results == nil or #scores.results < 1 or
scores.results[1].score == nil then
if callback then
callback(true, {}, params)
return
end
end
if callback then
callback(true, scores.results, params)
end
end
onGetScoreError = function(request)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
debug("ParseLib.onGetScoreError","Unable to connect")
if callback then
callback(false, {}, params)
end
end
-- get scores for current user at level "level". callback(bool, scores)
function ParseLib:getScore(level, facebookId, callback, params)
local headers = {[self.headerAppId["name"]] = self.headerAppId["key"],
[self.headerApiKey["name"]] = self.headerApiKey["key"]}
local body = '?limit=10000&order='
if level then
body = body..'score'
else
body = body..'level'
end
body = body..'&include=owner&where={"owner":{"$inQuery":{"where":{"facebookId":'
if facebookId == nil then
if self.currentFacebookId == nil or self.userObjectId == nil then
debug("ParseLib.getScore", "not logged in")
if callback then
callback(false, params)
end
return
end
body = body..'"'..self.currentFacebookId..'"'
elseif #facebookId == 0 then
debug("ParseLib.getScore", "no facebook id provided")
callback(false, params)
return
elseif #facebookId < 2 then
body = body..'"'..facebookId[1]..'"'
else
body = body..'{"$in":["'..facebookId[1]..'"'
for i = 2, table.getn(facebookId), 1 do
body = body..',"'..facebookId[i]..'"'
end
body = body..']}'
end
body = body..'},"className":"'..self.playerClass..'"}}'
if level then
body = body..',"level":'..level
end
body = body.."}"
local loader = UrlLoader.new(self.urls["score"]..body, UrlLoader.GET, headers, "")
local request = {}
request.userRequest = {}
request.userRequest["Callback"] = callback
request.userRequest["Param"] = params
request.userRequest["GetScore"] = facebookId
request.self = self
loader:addEventListener(Event.COMPLETE, onGetScoreComplete, request)
loader:addEventListener(Event.ERROR, onGetScoreError, request)
end
function ParseLib:addScore(levelScorePair, callback, params)
if (self.currentFacebookId == nil or self.userObjectId == nil) then
debug("ParseLib.addScore", "not logged in")
if callback then
callback(false, params)
end
return
end
self.currentAddScore = levelScorePair
for k, v in ipairs(levelScorePair) do
self:handleScore(v.level, v.score, callback, params)
end
end
onHandleScoreComplete = function(request, event)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
local result = json.decode(event.data)
debug("onHandleScoreComplete",event.data)
local level = result.result.level
local score = result.result.score
local callCallback = true
if request.self.currentAddScore then
callCallback = false
for k, v in ipairs(request.self.currentAddScore) do
if v.level == level then
table.remove(request.self.currentAddScore, k)
break
end
end
if #request.self.currentAddScore < 1 then
callCallback = true
end
end
if callback and callCallback == true then
callback(true, params)
else
debug("ParseLib.onHandleScoreComplete", "level: "..level..", score: "..score)
end
end
onHandleScoreError = function(request)
local callback = request.userRequest["Callback"]
local params = request.userRequest["Param"]
if callback then
callback(false, params)
else
debug("ParseLib.onHandleScoreError", "Unable to connect")
end
end
function ParseLib:handleScore(level, score, callback, params)
local headers = self:getPostHeader()
local body = '{"table":{"score":"'..self.scoreClass..'","player":"'..self.playerClass..'"},"data":{"objectId":"'..self.userObjectId..'","level":'..level..',"score":'..score..'}}'
debug("ParseLib.handleScore", "request: "..body)
local loader = UrlLoader.new("https://api.parse.com/1/functions/handleScore", UrlLoader.POST, headers, body)
local request = {}
request.userRequest = {}
request.userRequest["Callback"] = callback
request.userRequest["Param"] = params
request.userRequest["Level"] = level
request.userRequest["Score"] = score
request.self = self
loader:addEventListener(Event.COMPLETE, onHandleScoreComplete, request)
loader:addEventListener(Event.ERROR, onHandleScoreError, request)
end