-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscores.lua
More file actions
222 lines (166 loc) · 5.22 KB
/
scores.lua
File metadata and controls
222 lines (166 loc) · 5.22 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
--when creating a class
local scores = {}
scores.json = require("json")
scores.pfClient = require("plugin.playfab.client")
scores.doLogin = false
scores.getRemoteOK = nil
scores.getRemoteFail = nil
scores.updateRemoteOK = nil
scores.updateRemoteFail = nil
scores.lastScore = 0
scores.scoresTable = {}
--api wrapper for underlying resources
scores.filePath = system.pathForFile("scores.json",system.DocumentsDirectory)
scores.PlayFabClientApi = scores.pfClient.PlayFabClientApi
scores.PlayFabClientApi.settings.titleId = "4F8B4"
function scores:setGetRemoteOK(cb)
scores.getRemoteOK = cb
end
function scores:setGetRemoteFail(cb)
scores.getRemoteFail = cb
end
function scores:setUpdateRemoteOK(cb)
scores.updateRemoteOK = cb
end
function scores:setUpdateRemoteFail(cb)
scores.updateRemoteFail = cb
end
function scores:updateLocal(score)
scores.loadLocal()
table.insert( scores.scoresTable, score )
scores.saveLocal()
end
function scores:updateRemote(score)
local function errorHandler(error)
if scores.updateRemoteFail
then
scores.doLogin = false
scores.updateRemoteFail(error)
end
end
local function succesHandler(sc)
if scores.updateRemoteOK
then
scores.updateRemoteOK(sc)
end
end
local function succesLoginHandler(result)
print("Login Successful: " .. result.PlayFabId)
scores.updateRemote(scores.lastScore)
end
if(scores.doLogin == false)
then
scores.lastScore = score
scores.doLogin = true
local loginRequest =
{
-- https://api.playfab.com/Documentation/Client/method/LoginWithCustomID
CustomId = system.getInfo("deviceID"),
CreateAccount = true
}
scores.PlayFabClientApi.LoginWithCustomID(loginRequest,function(result) succesLoginHandler(result) end,
function(error) errorHandler(error) end)
return
end
scores.doLogin = false
score = scores.lastScore
scores.lastScore = 0
local request =
{
Statistics =
{
{ StatisticName = "HighScore", Value = score }
}
}
scores.PlayFabClientApi.UpdatePlayerStatistics(request, function(result) succesHandler(score) end,
function(error) errorHandler(error) end)
end
function scores:loadLocal()
local file = io.open( scores.filePath, "r" )
if file then
local contents = file:read( "*a" )
io.close( file )
scores.scoresTable = scores.json.decode( contents )
end
if ( scores.scoresTable == nil or #scores.scoresTable == 0 ) then
scores.scoresTable = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
end
end
function scores:loadRemote()
local function errorHandler(error)
print("Login Failed: " .. error.errorMessage)
if (scores.getRemoteFail)
then scores.doLogin = false
scores.getRemoteFail(error)
end
end
if(scores.doLogin == false)
then
scores.doLogin = true
local loginRequest =
{
-- https://api.playfab.com/Documentation/Client/method/LoginWithCustomID
CustomId = system.getInfo("deviceID"),
CreateAccount = true
}
scores.PlayFabClientApi.LoginWithCustomID(loginRequest,function(result) print("Login Successful: " .. result.PlayFabId) scores.loadRemote() end,
function(error) errorHandler(error) end)
return
end
scores.doLogin = false
scores.scoresTable = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
local request =
{
StatisticName = "HighScore",
StartPosition = 0,
MaxResultsCount = 10
}
local function successFunction(result)
-- print("Leaderboard pulled")
local rows = #result.Leaderboard
for i = 1, rows, 1
do
local res = result.Leaderboard[i]
table.insert(scores.scoresTable,res.StatValue)
end
local function compare( a, b )
return a > b
end
table.sort( scores.scoresTable, compare )
for i = #scores.scoresTable, 11, -1 do
table.remove( scores.scoresTable, i )
end
if (scores.getRemoteOK)
then
scores.getRemoteOK()
end
end
local function errorHandler(err)
if (scores.getRemoteFail)
then scores.getRemoteFail(err)
end
end
scores.PlayFabClientApi.GetLeaderboard(request, successFunction,
function(err) errorHandler(err) end)
end
function scores:getLocal()
return scores.scoresTable
end
function scores:saveLocal()
local function compare( a, b )
return a > b
end
table.sort( scores.scoresTable, compare )
for i = #scores.scoresTable, 11, -1 do
table.remove( scores.scoresTable, i )
end
local file = io.open( scores.filePath, "w" )
if file then
file:write( scores.json.encode( scores.scoresTable ) )
io.close( file )
end
end
function scores:getRemote()
return scores.scoresTable
end
return scores