forked from MarkHenryC/Stroople
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighscores.lua
305 lines (249 loc) · 6.64 KB
/
highscores.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
-- Mark H Carolan
--
-- Stroople project
-- © 2010 Mark H Carolan, Gregory S Hooper
module(..., package.seeall)
require "storage"
require "globals"
require "languages"
require "db"
local maxEntries = 8
if globals.usingOpenFeint then
require "openfeint"
end
--
-- IDs for setting individual high scores at OF
--
local levelIDs =
{
522144, 522154, 522164, 522174
}
local scoreTable = {}
function init()
-- highScore table is key, value, so not sorted
-- move to local table for sorted display
--
local scores = globals.getHighScores()
-- Returns key, value pairs: score, player - not sorted
for k, v in pairs(scores) do
scoreTable[#scoreTable + 1] = {tonumber(k), v}
db.print("restoring", k, v)
end
table.sort(scoreTable,
function(a, b)
return a[1] > b[1]
end
)
end
local function setOFHighScore(amount)
if globals.usingOpenFeint then
if system.getInfo("environment") ~= "simulator" then
openfeint.setHighScore
{
leaderboardID=levelIDs[globals.getLevelID()],
score=amount
}
end
end
end
function newScoreboard(hsScore)
local sb = display.newGroup()
local spacing = 32
local endPos = 10 -- default if no high scores
for i = 1, #scoreTable do
local entry = scoreTable[i] -- score, player
local text1 = string.format("%000d", tonumber(entry[1]))
local t1 = display.newText(text1, 0, 0, nil, 24)
local t2 = display.newText(entry[2], 0, 0, nil, 24)
t1:setTextColor(255, 100, 100)
t2:setTextColor(200, 200, 200)
sb:insert(t1)
sb:insert(t2)
t1:setReferencePoint(display.TopRightReferencePoint)
t2:setReferencePoint(display.TopLeftReferencePoint)
t1.y = i * spacing
t2.y = i * spacing
t1.x = globals.W2 - 20
t2.x = globals.W2 +20
endPos = i
end
if hsScore then
local num = string.format("%000d", hsScore)
local tick = display.newImage("tick.png")
local cross = display.newImage("cross.png")
tick:setReferencePoint(display.CenterReferencePoint)
cross:setReferencePoint(display.CenterReferencePoint)
sb:insert(tick)
sb:insert(cross)
tick.xScale = 0.25
tick.yScale = 0.25
cross.xScale = 0.25
cross.yScale = 0.25
local divs = globals.W / 12
tick.x = divs * 2.5
tick.y = (endPos + 2) * spacing
cross.x = divs * 6
cross.y = (endPos + 2) * spacing
local hits, misses = score.getHitsAndMisses()
local hitText = display.newText(hits, 0, 0, nil, 24)
hitText:setTextColor(160, 160, 255)
hitText.y = (endPos + 2) * spacing
hitText.x = divs
local missText = display.newText(misses, 0, 0, nil, 24)
missText:setTextColor(160, 160, 255)
missText.y = (endPos + 2) * spacing
missText.x = divs * 4.5
sb:insert(hitText)
sb:insert(missText)
local arrow = display.newImage("yellow_arrow_small.png")
arrow.xScale = 0.5
arrow.yScale = 0.5
arrow.y = (endPos + 2) * spacing
arrow.x = divs * 8
yourScore = display.newText(num, 0, 0, nil, 24)
yourScore:setTextColor(160, 160, 255)
yourScore.y = (endPos + 2) * spacing
yourScore.x = divs * 10
yourScore:setReferencePoint(display.TopCenterReferencePoint)
sb:insert(yourScore)
end
return sb
end
function isNameHighScorer(name)
db.print("isNameHighScorer", name, "#scoreTable", #scoreTable)
for i = 1, #scoreTable do
local n = scoreTable[i][2]
db.print("Comparing", name, "with", n)
if name == n then
return true
end
end
db.print("isNameHighScorer returning false")
return false
end
function deleteEntry(name)
for i = 1, #scoreTable do
if name == scoreTable[i][2] then
table.remove(scoreTable, i)
globals.updateHighScores(scoreTable)
return true
end
end
return false
end
function isHighScore(gameTime, amount)
local isTop = false
local isIn = false
local rVal = "not in high scores"
local rank = -1
db.print("isHighScore()", amount)
if amount > 0 then
if #scoreTable == 0 then
db.print("First entry");
return true, true, "First entry in hi-scores", 1, amount
end
for i = 1, #scoreTable do
db.print("comparing to:", scoreTable[i][1], scoreTable[i][2])
if amount > scoreTable[i][1] then
if i <= maxEntries then
rVal = "In high scores"
db.print(rVal)
isIn = true
if i == 1 then
rVal = "Top score"
db.print(rVal)
isTop = true
end
rank = i
break
end
end
end
if #scoreTable < maxEntries and not isIn then -- append since there are empty slots
rVal = "early entry in hi-scores"
db.print(rVal)
rank = #scoreTable+1
isIn = true
end
else
isTop, isIn, rVal = false, false, "no score"
end
db.print("isTop", isTop, "isIn", isIn, "message", rVal, "rank", rank)
return isTop, isIn, rVal, rank, gameTime
end
function submitName(amount, name)
local isTop = false
local isIn = false
local rVal = "not in high scores"
if #scoreTable == 0 then
db.print("submitName:", amount, name)
table.insert(scoreTable, { amount, name })
globals.updateHighScores(scoreTable)
return true, true, "First entry in hi-scores"
elseif #scoreTable < maxEntries then
rVal = "Early entry in hi-scores"
end
for i = 1, #scoreTable do
db.print(name, amount, scoreTable[i][1])
if amount > scoreTable[i][1] then
if i <= maxEntries then
isIn = true
if i == 1 then
isTop = true
end
table.insert(scoreTable, i, { amount, name })
break
end
end
end
if #scoreTable < maxEntries and not isIn then -- append since there are empty slots
table.insert(scoreTable, { amount, name })
isIn = true
end
--
-- Trim back to maxEntries items
--
for i = #scoreTable, (maxEntries+1), -1 do
table.remove(scoreTable)
end
db.print("scoreTable length", #scoreTable)
if isIn then globals.updateHighScores(scoreTable) end
return isTop, isIn, rVal
end
function submit(amount)
local isTop = false
local isIn = false
local rVal
if #scoreTable == 0 then
table.insert(scoreTable, { amount, globals.getCurrentPlayer() })
globals.updateHighScores(scoreTable)
return true, true, "First entry in hi-scores"
elseif #scoreTable < maxEntries then
rVal = "Early entry in hi-scores"
end
for i = 1, #scoreTable do
if amount > scoreTable[i][1] then
if i <= maxEntries then
isIn = true
if i == 1 then
isTop = true
end
table.insert(scoreTable, i, { amount, globals.getCurrentPlayer() })
break
end
end
end
if #scoreTable < maxEntries and not isIn then -- append since there are empty slots
table.insert(scoreTable, { amount, globals.getCurrentPlayer() })
isIn = true
end
--
-- Trim back to maxEntries items
--
for i = #scoreTable, (maxEntries+1), -1 do
table.remove(scoreTable)
end
db.print("scoreTable length", #scoreTable)
if isIn then globals.updateHighScores(scoreTable) end
return isTop, isIn, rVal
end